@hyperfrontend/features/host

Host

Host-side SDK for embedding hyperfrontend features — shell factory, display modes, iframe utilities, and lifecycle.

import { createShell, DisplayMode } from '@hyperfrontend/features/host'

const shell = createShell({
  url: 'https://features.example.com/clock',
  container: '#clock-slot',
  displayMode: DisplayMode.Embedded,
})

shell.on('open', () => console.log('feature connected'))
shell.on('tick', (time) => console.log('feature said', time))

shell.open()
shell.send('set-timezone', { tz: 'UTC' })

API

ExportPurpose
createShellBuild a shell handle for a feature; returns open/close/send/on/isOpen.
DisplayModeThe four built-in modes: Embedded, Dialog, Popup, Standalone.
ShellHandleType of the handle returned by createShell.
ShellOptionsOptions accepted by createShell and per-open overrides.
ExperiencePluginOpt-in extension point for layering transitions/animations onto display modes.

The shell wraps a @hyperfrontend/nexus broker: send emits a contract action to the feature, and on subscribes to feature messages and the open/close/error lifecycle events. close disconnects the channel; destroy also releases the DOM.

API Reference

ƒ Functions

§function

createShell(options: ShellOptions): ShellHandle

Creates a host-side shell for embedding a feature.
Provisions a nexus broker and returns a handle whose open mounts the feature in the requested display mode. The contract option takes the feature's contract exactly as the feature authored it; the shell derives the host-side orientation itself, so the handle sends what the feature accepts and receives what the feature emits.

Parameters

NameTypeDescription
§options
ShellOptions
Create-time shell options, overridable per open call.

Returns

ShellHandle
A handle exposing open, close, destroy, send, on, and isOpen.

Example

Embedding a clock feature

const clock = createShell({ container: '#clock', url: 'https://clock.example.com' })
clock.open({ displayMode: DisplayMode.Dialog, dialogWidth: 530 })
clock.on('timeUpdated', (data) => console.log(data))

Interfaces

§interface

ShellHandle

Public handle returned by createShell.

Properties

§readonly isOpen:boolean
Whether the feature channel is currently open (true while connected).
§interface

ActionDescription

Description of a single action a feature can emit or accept.
Structurally compatible with nexus's channel contract action shape so the same contract can drive both messaging and the shell type generator.

Properties

§description?:string
Human-readable explanation of the action, surfaced in tooling.
§respondsWith?:string
When this action is used as a request, the type of the action in the other direction that answers it.
§schema?:object
Optional JSON-schema-like shape describing the action payload.
§type:string
Wire type string that identifies the action.
§interface

ExperiencePlugin

Opt-in extension that decorates a feature's mount lifecycle (e.g. transitions, animations).
Register plugins through ShellOptions.plugins. After each successful mount the shell calls onMount on every plugin in registration order; before each unmount it calls onUnmount one plugin at a time in reverse registration order, awaiting any returned promise, then runs the teardowns returned by onMount (also in reverse registration order) and finally removes the feature. The SDK ships no built-in plugins.

Properties

§name:string
Unique plugin name, surfaced in debug logs.
§interface

ExperiencePluginContext

Context handed to an ExperiencePlugin around a feature's mount lifecycle.

Properties

§displayMode:DisplayMode
The display mode the feature was surfaced in.
§element:HTMLElement
The in-document root the display mode mounted: the iframe for embedded, the dialog container for dialog, and null for popup and standalone, which open a separate window with no in-document element.
§interface

FeatureContract

The set of actions a feature emits to, and accepts from, its counterpart.
This is the same shape the on-disk *.contract.json files and the shell generator consume.

Properties

§accepted:ActionDescription[]
Actions this side handles from the other side.
§emitted:ActionDescription[]
Actions this side sends to the other side.
§interface

RequestOptions

Per-request settings accepted by request.

Properties

§timeoutMs?:number
Milliseconds to wait for the response before rejecting; defaults to 30000.
§interface

ShellOptions

Options accepted by the host-side FeatureContract consumer when creating or opening a shell.

Properties

§closeOnEscape?:boolean
Whether pressing Escape closes the shell; defaults to true.
§container:string | HTMLElement
Target element (or CSS selector) the embedded feature mounts into.
§contract?:FeatureContract
The feature's contract exactly as the feature authored it (emitted = what the feature sends, accepted = what the feature handles). The shell derives the host-side orientation itself — hand it the feature's contract, never a pre-swapped copy. Replaces the generic default when provided.
§dialogHeight?:number
Dialog height in pixels (dialog mode only).
§dialogOverlay?:boolean
Whether the dialog renders a dimmed backdrop; defaults to true.
§dialogWidth?:number
Dialog width in pixels (dialog mode only).
§displayMode?:DisplayMode
How the feature should be surfaced; defaults to DisplayMode.Embedded.
§embedSizing?:EmbedSizing
How an embedded feature is sized; defaults to fill (the iframe fills its container).
§name?:string
Stable identifier for the feature; seeds the broker name surfaced in debug logs.
§onUnresponsive?:UnresponsivePolicy
How the host reacts when the feature stops responding; defaults to emit.
§plugins?:unknown
Experience plugins wrapped around each mount/unmount; onMount runs in registration order, onUnmount in reverse.
§protocol?:SecurityProtocol
Security envelope to negotiate; defaults to none.
§sharedKey?:string
Pre-shared key used by the v2 protocol.
§url?:string
URL of the feature app to load.
§interface

UnresponsiveInfo

Context passed to an UnresponsivePolicy callback when a feature stops beating.

Properties

§displayMode:DisplayMode
The display mode the unresponsive feature was using.
§lastBeatAt:number
Timestamp (ms) of the last beat received, or null if none ever arrived.
§missedBeats:number
Consecutive missed beats that tripped the watchdog.

Types

§type

EmbedSizing

How the feature is laid out when the host surfaces it.
type EmbedSizing = "fill" | "content"
§type

RequestHandler

Answers one request type; may return the response value directly or a promise of it.
type RequestHandler = (data: unknown) => unknown
§type

SecurityProtocol

Union of the supported security envelope selectors.
none is the local default (opt-in security); production builds must pick v1 or v2.
type SecurityProtocol = "none" | "v1" | "v2"
§type

UnresponsivePolicy

What the host does when a feature misses too many heartbeats.
emit (the default) emits an error; unmount also tears the feature down; a callback takes over handling entirely with the UnresponsiveInfo.
type UnresponsivePolicy = "emit" | "unmount" | (info: UnresponsiveInfo) => void

Variables

§type

DisplayMode

Supported ways a host can surface an embedded feature.
All four modes are built into the SDK.