Joy DOM

Custom components

Render the document's kebab-case node types with your own React components.

A document can reference custom nodes by a kebab-case type. Register a React component for each one through the components prop:

import { , type  } from "@joy-dom/react";
import type {  } from "@joy-dom/core";

function ({ , ,  }: ) {
  return (
    < ={} ={}>
      {}
    </>
  );
}

export function ({  }: { :  }) {
  return < ={} ={{ "contact-button":  }} />;
}

The component receives the resolved children, className, id, and the original node. A custom node with no matching registration throws at render time.

Styling a custom node

The renderer does not pass custom components an inline style. That prop stays undefined for custom types. Style the node from the document cascade instead:

  • Give the node a className.
  • Write the rule in the spec's style, the same way you would for a built-in.

The renderer passes the className through, and the matching rule applies inside the shadow root.

{
  "type": "contact-button",
  "props": { "id": "primary", "className": ["btn"] }
}

alt and src follow the same rule: they stay img-only and never reach a custom component. The on* handlers are passed when the node carries a matching action. Attach them to the element that should fire (an inner <input>, a click surface), or ignore them. For anything else, read node.props directly. See §7.1 Component props.

Where next

On this page