headless

API directive

attributesData

function

attributesData<T>(...directives): object

Returns an object with the attributes, style and class keys containing information derived from a list of directives.

  • The attributes value is a JSON representation of key/value attributes, excepted for the class and style attributes
  • The classNames value is an array of string representing the classes to be applied
  • The style value is a JSON representation of the styles to be applied
Type Parameters

T extends any[]

The type of the directives array.

Parameters

• ...directives: DirectivesAndOptParam<T, SSRHTMLElement>

List of directives to generate attributes from. Each parameter can be the directive or an array with the directive and its parameter

Returns

object

JSON object with the attributes, class and style keys.

attributes

attributes: Record<string, string>

classNames

classNames: string[]

style

style: Partial<Record<StyleKey, StyleValue>>


AttributesDirectiveProps

interface

Properties for configuring server-side rendering directives.

Properties
attributes?

optional attributes: Record<string, AttributeValue | ReadableSignal<AttributeValue>>

Attributes to be added to the provided node.

Remarks

The style attribute must be added separately.


classNames?

optional classNames: Record<string, boolean | ReadableSignal<boolean>>

Class names to be added to an HTML element.

Remarks

Key-value pairs where keys are class names and values indicate whether the class should be added (true) or removed (false).


events?

optional events: Partial<object>

Events to be attached to an HTML element.

Remarks

Key-value pairs where keys are event types and values are event handlers. xw


styles?

optional styles: Partial<Record<StyleKey, StyleValue | ReadableSignal<StyleValue>>>

Styles to be added to an HTML element.

Remarks

Key-value pairs where keys are CSS style properties and values are style values.


bindDirective

function

bindDirective<T, U>(directive, directiveArg$): Directive<void, U>

Binds the given directive to a store that provides its argument.

Type Parameters

T

The type of the directive argument.

U extends SSRHTMLElement = SSRHTMLElement

The type of the SSRHTMLElement, defaults to SSRHTMLElement.

Parameters

directive: Directive<T, U>

The directive to bind to the element.

directiveArg$: ReadableSignal<T>

The signal to subscribe to for directive updates.

Returns

Directive<void, U>

A directive that manages the lifecycle of the bound directive.

Remarks

The returned directive can be used without argument, it will ignore any argument passed to it and will call the provided directive with the content of the provided store as its argument, calling its update method when the content of the store changes.


bindDirectiveNoArg

function

bindDirectiveNoArg<T, U>(directive): Directive<void, U>

Returns a directive that ignores any argument passed to it and calls the provided directive without any argument.

Type Parameters

T

The type of the directive's argument.

U extends SSRHTMLElement = SSRHTMLElement

The type of the SSRHTMLElement, defaults to SSRHTMLElement.

Parameters

directive: Directive<T, U>

The directive to bind without arguments.

Returns

Directive<void, U>

A new directive that does not require any arguments.


browserDirective

function

browserDirective<T, U>(directive): Directive<T, SSRHTMLElement>

A higher-order directive function that conditionally applies a directive based on the environment. If running in a browser environment, it applies the given directive to the node. If not in a browser environment, it returns a no-op function.

Type Parameters

T

The type of the directive's argument.

U extends HTMLElement

The type of the HTML element the directive is applied to.

Parameters

directive: Directive<T, U>

The directive to be conditionally applied.

Returns

Directive<T, SSRHTMLElement>

  • A directive that applies the given directive in a browser environment, or a no-op in a non-browser environment.

classDirective

function

classDirective(node, args): void | object

Directive that takes as an argument a string containing CSS classes to be put on the HTML element.

Parameters

node: SSRHTMLElement

args: string

Returns

void | object


createAttributesDirective

function

createAttributesDirective<T>(propsFn): Directive<T>

Creates a directive that binds attributes, styles, class names, and events to a DOM node.

Type Parameters

T = void

The type of the arguments passed to the directive.

Parameters

propsFn

A function that takes a readable signal of type T and returns an object containing attributes, styles, class names, and events to bind to the node.

Returns

Directive<T>

A directive function that can be used to bind the specified properties to a DOM node.

The returned directive function takes a DOM node and arguments of type T, and sets up the bindings specified by the propsFn function. It returns an object with update and destroy methods:

  • update(args: T): Updates the arguments passed to the directive.
  • destroy(): Cleans up all bindings and event listeners.

createBrowserStoreArrayDirective

function

createBrowserStoreArrayDirective(): object

Returns a directive and a store. The store contains at any time the array of all the DOM elements on which the directive is currently used.

Returns

object

An object with two properties: the directive property that is the directive to use on some DOM elements, and the elements$ property that is the store containing an array of all the elements on which the directive is currently used.

directive

directive: Directive<void, SSRHTMLElement>

elements$

elements$: ReadableSignal<HTMLElement[]>

Remarks

It is the same as createStoreArrayDirective, but the returned directive is only executed in a browser environment and the type of the elements is HTMLElement instead of SSRHTMLElement.

If the directive is intended to be used on a single element element, it may be more appropriate to use createBrowserStoreDirective instead.


createBrowserStoreDirective

function

createBrowserStoreDirective(): object

Returns a directive and a store. When the directive is used on a DOM element, the store contains that DOM element. When the directive is not used, the store contains null.

Returns

object

An object with two properties: the directive property that is the directive to use on one DOM element, and the element$ property that is the store containing the element on which the directive is currently used (or null if the store is not currently used).

directive

directive: Directive<void, SSRHTMLElement>

element$

element$: ReadableSignal<null | HTMLElement>

Remarks

It is the same as createStoreDirective, but the returned directive is only executed in a browser environment and the type of the element is HTMLElement instead of SSRHTMLElement.

If the directive is used on more than one element, an error is displayed in the console and the element is ignored. If the directive is intended to be used on more than one element, please use createStoreArrayDirective instead.


createStoreArrayDirective

function

createStoreArrayDirective(): object

Returns a directive and a store. The store contains at any time the array of all the DOM elements on which the directive is currently used.

Returns

object

An object with two properties: the directive property that is the directive to use on some DOM elements, and the elements$ property that is the store containing an array of all the elements on which the directive is currently used.

directive

directive: Directive

elements$

elements$: ReadableSignal<SSRHTMLElement[]>

Remarks

It is the same as createBrowserStoreArrayDirective, but the returned directive is also executed in a server environment and the type of the elements is SSRHTMLElement instead of HTMLElement.

If the directive is intended to be used on a single element element, it may be more appropriate to use createStoreDirective instead.


createStoreDirective

function

createStoreDirective(): object

Returns a directive and a store. When the directive is used on a DOM element, the store contains that DOM element. When the directive is not used, the store contains null.

Returns

object

An object with two properties: the directive property that is the directive to use on one DOM element, and the element$ property that is the store containing the element on which the directive is currently used (or null if the store is not currently used).

directive

directive: Directive

element$

element$: ReadableSignal<null | SSRHTMLElement>

Remarks

It is the same as createBrowserStoreDirective, but the returned directive is also executed in a server environment and the type of the element is SSRHTMLElement instead of HTMLElement.

If the directive is used on more than one element, an error is displayed in the console and the element is ignored. If the directive is intended to be used on more than one element, please use createStoreArrayDirective instead.


directiveAttributes

function

directiveAttributes<T>(...directives): Record<string, string>

Combines multiple directives into a single attributes object.

This function processes an array of directives and optional parameters, extracting attributes, class names, and styles. It then combines these into a single attributes object, where class names are joined into a single string and styles are formatted as a CSS string.

Type Parameters

T extends any[]

The type of the directives and optional parameters.

Parameters

• ...directives: DirectivesAndOptParam<T, SSRHTMLElement>

The directives and optional parameters to process.

Returns

Record<string, string>

An object containing the combined attributes.


directiveSubscribe

function

directiveSubscribe(store, asyncUnsubscribe): Directive

Returns a directive that subscribes to the given store while it is used on a DOM element, and that unsubscribes from it when it is no longer used.

Parameters

store: ReadableSignal<any>

store on which there will be an active subscription while the returned directive is used.

asyncUnsubscribe: boolean = true

true if unsubscribing from the store should be done asynchronously (which is the default), and false if it should be done synchronously when the directive is destroyed

Returns

Directive

The resulting directive.


directiveUpdate

function

directiveUpdate<T>(update): Directive<T>

Returns a directive that calls the provided function with the arguments passed to the directive on initialization and each time they are updated.

Type Parameters

T

The type of the argument that the update function accepts.

Parameters

update

Function called with the directive argument when the directive is initialized and when its argument is updated.

Returns

Directive<T>

The resulting directive.


isBrowserHTMLElement

function

isBrowserHTMLElement(element): element is HTMLElement

On a browser environment, returns true if the given element is an HTMLElement. On a server environment, always returns false.

Parameters

element: SSRHTMLElement

The element to check.

Returns

element is HTMLElement

true in a browser environment if the given element is an HTMLElement, otherwise false.


mapDirectiveArg

function

mapDirectiveArg<T, U, V>(directive, fn): Directive<T, V>

Maps the argument of a directive to a new value using a provided function.

Type Parameters

T

The type of the original argument.

U

The type of the mapped argument.

V extends SSRHTMLElement = SSRHTMLElement

The type of the SSRHTMLElement, defaults to SSRHTMLElement.

Parameters

directive: Directive<U, V>

The original directive to be mapped.

fn

The function to map the original argument to the new argument.

Returns

Directive<T, V>

A new directive with the mapped argument.


mergeDirectives

function

mergeDirectives<T, U>(...args): Directive<T, U>

Merges multiple directives into a single directive that executes all of them when called.

Type Parameters

T

The type of the argument passed to the directive.

U extends SSRHTMLElement = SSRHTMLElement

The type of the SSRHTMLElement, defaults to SSRHTMLElement.

Parameters

• ...args: (Directive<T, U> | Directive<void, U>)[]

The directives to merge.

Returns

Directive<T, U>

A new directive that applies all the given directives.

The returned directive has the following lifecycle methods:

  • update(arg): Updates all merged directives with the given argument.
  • destroy(): Destroys all merged directives in reverse order.
Remarks

All directives receive the same argument upon initialization and update. Directives are created and updated in the same order as they appear in the arguments list, they are destroyed in the reverse order. All calls to the directives (to create, update and destroy them) are wrapped in a call to the batch function of tansu


multiDirective

function

multiDirective<T, U>(element, directives): object

Applies multiple directives to a given SSRHTMLElement and provides methods to update or destroy them.

Type Parameters

T extends any[]

A tuple type representing the arguments for each directive.

U extends SSRHTMLElement = SSRHTMLElement

The type of the SSRHTMLElement, defaults to SSRHTMLElement.

Parameters

element: U

The SSRHTMLElement to which the directives will be applied.

directives: DirectivesAndOptParam<T, U>

An array of directives and their optional parameters.

Returns

object

An object containing:

  • update: A function to update the directives with new parameters.
  • destroy: A function to destroy all applied directives.
destroy

destroy: () => void

Returns

void

update

update: (directives) => void

Parameters

directives: (Directive<void, U> | DirectiveAndParam<any, U>)[]

Returns

void


registrationArray

function

registrationArray<T>(): ReadableSignal<T[]> & object

Creates a registration array that allows elements to be added and removed.

Type Parameters

T

The type of elements in the array.

Returns

ReadableSignal<T[]> & object

An object that includes a readable signal of the array and a register function.

The returned object has the following properties:

  • register: A function to add an element to the array. It takes an element of type T as a parameter and returns a function to remove the element from the array.

ssrAttributes

function

ssrAttributes<T>(...directives): Record<string, string>

Generates a record of SSR (Server-Side Rendering) attributes based on the provided directives.

This function behaves differently depending on the environment:

  • In a browser environment (BROWSER is true), it returns an empty object.
  • In a non-browser environment, it delegates to the directiveAttributes function.
Type Parameters

T extends any[]

A tuple type representing the directives and optional parameters.

Parameters

• ...directives: DirectivesAndOptParam<T, SSRHTMLElement>

Returns

Record<string, string>

A record of SSR attributes.


UseDirective

class

A directive that allows the use of another directive with optional parameters.

Remarks

This directive uses a private instance of useDirectiveForHost to manage the directive and its parameter.

Type Parameters

T

The type of the parameter that can be passed to the directive.

Implements

useDirectiveForHost

function

useDirectiveForHost<T>(directive?, params?): object

A utility function to manage the lifecycle of a directive for a host element.

This function handles the creation, updating, and destruction of a directive instance associated with a host element. It ensures that the directive is called appropriately based on the platform (server or client) and manages the directive's lifecycle within the Angular injection context.

Type Parameters

T

The type of parameters that the directive accepts.

Parameters

directive?: Directive<T>

The directive to be applied to the host element.

params?: T

The parameters to be passed to the directive.

Returns

object

An object containing an update function to update the directive and its parameters.

update

update: (newDirective?, newParams?) => void

Parameters

newDirective?: Directive<T>

newParams?: T

Returns

void


UseMultiDirective

class

A directive that allows the use of multiple directives on a host element.

Type Parameters

T extends any[]

A tuple type representing the directives and their optional parameters.

Implements
Properties
useMulti

useMulti: DirectivesAndOptParam<T, SSRHTMLElement>

An input property that takes a tuple of directives and their optional parameters.