Skip to content

Adapter type

Home > @rimitive/view > Adapter

Adapter type - core tree operations

Generic over: - TConfig: The tree configuration (nodes, attributes, node)

## Lifecycle Hooks

The adapter supports six symmetric lifecycle hooks across three phases:

| Phase | Before | After (on) | |---------|----------------|------------| | Create | beforeCreate | onCreate | | Attach | beforeAttach | onAttach | | Destroy | beforeDestroy | onDestroy |

Node type (element vs fragment) is determined by ref.status: - STATUS_ELEMENT (1): Element node with actual tree node - STATUS_FRAGMENT (2): Fragment node (logical container, no tree node)

### Hydration

For hydration-specific position tracking, use the HydrationAdapter extension which adds seekToPosition and skipContent methods.

Signature:

export type Adapter<TConfig extends TreeConfig> = {
createNode: <K extends keyof TConfig['nodes'] & string>(type: K, props?: Record<string, unknown>, parentContext?: ParentContext<unknown>) => TConfig['nodes'][K];
setAttribute: (node: NodeOf<TConfig>, key: string, value: unknown) => void;
appendChild: (parent: NodeOf<TConfig>, child: NodeOf<TConfig>) => void;
removeChild: (parent: NodeOf<TConfig>, child: NodeOf<TConfig>) => void;
insertBefore: (parent: NodeOf<TConfig>, child: NodeOf<TConfig>, reference: NodeOf<TConfig> | null) => void;
beforeCreate?: (type: string, props?: Record<string, unknown>) => void;
onCreate?: (ref: NodeRef<NodeOf<TConfig>>, parent: NodeOf<TConfig>) => void;
beforeAttach?: (ref: NodeRef<NodeOf<TConfig>>, parent: NodeOf<TConfig>, nextSibling: NodeOf<TConfig> | null) => void;
onAttach?: (ref: NodeRef<NodeOf<TConfig>>, parent: NodeOf<TConfig>) => void;
beforeDestroy?: (ref: NodeRef<NodeOf<TConfig>>, parent: NodeOf<TConfig>) => void;
onDestroy?: (ref: NodeRef<NodeOf<TConfig>>, parent: NodeOf<TConfig>) => void;
createShadowRoot?: (host: NodeOf<TConfig>, options: {
mode: 'open' | 'closed';
delegatesFocus?: boolean;
}) => {
container: NodeOf<TConfig>;
shadowRoot: ShadowRoot | null;
};
};

References: TreeConfig, ParentContext, NodeOf, NodeRef

import type { Adapter, TreeConfig } from '@rimitive/view/adapter';
const domAdapter: Adapter<DOMTreeConfig> = {
createNode: (type, props) => {
if (type === 'text') return document.createTextNode(props?.value as string || '');
return document.createElement(type);
},
setAttribute: (node, key, value) => {
if (key === 'textContent') node.textContent = value as string;
else (node as HTMLElement).setAttribute(key, String(value));
},
appendChild: (parent, child) => parent.appendChild(child),
removeChild: (parent, child) => parent.removeChild(child),
insertBefore: (parent, child, ref) => parent.insertBefore(child, ref),
};