Skip to contents

Composition

A guide to composing Base UI components with your own Solid components.

Composing custom Solid components

Use the render prop to compose a Base UI part with your own Solid components.

For example, most triggers render a <button> by default. The code snippet below shows how to use a custom button instead.

index.tsx
// prettier-ignore
<Menu.Trigger render={(props) => <MyButton size="md" {...props} />}>
  Open menu
</Menu.Trigger>

The custom component must spread all the received props on its underlying DOM node. This includes the ref callback, so don’t omit ref from the spread.

Composing multiple components

In situations where you need to compose multiple Base UI components with custom Solid components, render props can be nested as deeply as necessary. Working with Tooltip is a common example.

index.tsx
<Dialog.Root>
  <Tooltip.Root>
    <Tooltip.Trigger
      render={(tooltipProps) => (
        <Dialog.Trigger
          {...tooltipProps}
          render={(dialogProps) => (
            <Menu.Trigger
              {...dialogProps}
              render={(menuProps) => <MyButton size="md" {...menuProps} />}
            >
              Open menu
            </Menu.Trigger>
          )}
        />
      )}
    />
    <Tooltip.Portal>...</Tooltip.Portal>
  </Tooltip.Root>
  <Dialog.Portal>...</Dialog.Portal>
</Dialog.Root>

How props are merged

When the render function renders another part, merge the part’s props with the other part’s props using mergeProps, so they end up on a single element. For example, this toolbar button renders a menu trigger:

index.tsx
<Toolbar.Button
  class="toolbar-button"
  style={{ color: 'red', padding: '4px' }}
  onClick={handleToolbarClick}
  ref={toolbarButtonRef}
  render={(props) => (
    <Menu.Trigger
      {...mergeProps(props, {
        class: 'menu-trigger',
        style: { color: 'blue' },
        onClick: handleMenuClick,
        ref: menuTriggerRef,
      })}
    />
  )}
/>

The rendered <button> gets:

  • class="menu-trigger toolbar-button": class names are combined, starting with the menu trigger’s.
  • style={{ color: 'blue', padding: '4px' }}: style objects are merged, and the menu trigger’s values win.
  • Both click handlers, starting with handleMenuClick. Calling event.preventBaseUIHandler() skips every handler that runs after it, so calling it in handleMenuClick also skips handleToolbarClick. Some parts call it internally.
  • Both refs: toolbarButtonRef and menuTriggerRef point to the same element.

For other attributes set on both, such as id or title, the menu trigger’s value wins. Component props like disabled are read by each part separately, so a value on the menu trigger doesn’t override the part’s own.

A render function doesn’t merge props automatically. Spreading props and then adding attributes in JSX keeps only the last value of each, while mergeProps combines them and keeps all the refs.

Changing the default rendered element

You can also use the render prop to override the rendered element of the component.

For example, <Accordion.Header> renders an <h3> element by default. The code snippet below shows how to render it as an <h2> element so that it fits the heading hierarchy of the page.

index.tsx
import { Accordion } from 'base-ui-solid/accordion';

export default () => (
  <Accordion.Root>
    <Accordion.Item>
      <Accordion.Header render="h2">
        <Accordion.Trigger>What is Base UI?</Accordion.Trigger>
      </Accordion.Header>
      <Accordion.Panel>Base UI is a library of unstyled Solid components.</Accordion.Panel>
    </Accordion.Item>
  </Accordion.Root>
);

Each Base UI component renders the most appropriate element by default, and in most cases, rendering a different element is recommended only on a case-by-case basis.

Render function

Use a function, component, or tag name for render. Solid cannot clone JSX elements. Read the callback props and state reactively, without destructuring them.

switch.tsx
<Switch.Thumb
  render={(props, state) =>
    // prettier-ignore
    <span {...props}>
      {state.checked ? <CheckedIcon /> : <UncheckedIcon />}
    </span>
  }
/>

Using a function gives you complete control over spreading props and also allows you to render different content based on the component’s state.

Reactivity

Base UI follows Solid’s conventions for reactive values, so you can tell from a value’s shape how to read it:

  • A single reactive value is an accessor. Call it in a reactive scope (JSX, a memo or an effect) to read the current value. For example useDirection(), Combobox.useFilteredItems() and useMediaQuery() return accessors.
  • Structured state is a reactive object. Its properties are getters that you read where you need them, like Solid props. Don’t destructure it, because destructuring reads the values once. For example useToastManager().toasts, a handle’s isOpen, the result of mergeProps, and the props and state passed to render functions.
  • Methods are plain, stable functions. You can destructure them and call them from event handlers. For example useToastManager().add, handle.open(), the actions passed to actionsRef and the contains() method of Combobox.useFilter().
  • Function children are called once, with accessors. Like Solid’s <For> and <Show>, parts that take a function child call it once and pass their values as accessors, for example the item of <Combobox.List>, the value of <Select.Value> or <Progress.Value>, and the payload of a popup root. Read them in JSX so the content updates in place.
  • Options objects are read lazily. To make an option of a hook reactive, pass a getter, like Combobox.useFilter({ get locale() { return locale() } }).
Reading reactive values
const direction = useDirection();
const toastManager = useToastManager();
const { add } = toastManager; // methods are safe to destructure

<p dir={direction()}>{toastManager.toasts.length} notifications</p>;

A function child’s body runs once and doesn’t track, so read its accessors inside the JSX it returns (or in a memo), not at the top of the function. This applies to Base UI’s function children and to Solid’s own:

  • <Show when={payload()}>{(Content) => <Content />}</Show> passes an accessor of the narrowed value (Content here). Call it in the returned JSX, because a variable assigned at the top of the callback keeps the first value.
  • <For each={items()}>{(item, index) => …}</For> keys rows by identity: item is a plain value and index is an accessor. With keyed={false}, item is an accessor and index a plain number. With a key function (keyed={(item) => item.id}), both are accessors. Call them where you read them.
Reading accessors in function children
<Popover.Root handle={handle}>
  {({ payload }) => (
    <Popover.Portal>
      <Popover.Positioner>
        <Popover.Popup>
          <Show when={payload()}>{(Content) => <Dynamic component={Content()} />}</Show>
        </Popover.Popup>
      </Popover.Positioner>
    </Popover.Portal>
  )}
</Popover.Root>;

<For each={toastManager.toasts} keyed={(toast) => toast.id}>
  {(toast) => <Toast.Root toast={toast()} />}
</For>;

Props that hand you something are callbacks, like Solid’s ref: ref, actionsRef and inputRef. Props that take one of your elements (container, anchor, initialFocus and finalFocus) take the element itself. Keep it in a signal set by a ref callback and pass the signal’s value:

Passing an element to a prop
const [input, setInput] = createSignal<HTMLInputElement | null>(null);

<Dialog.Popup initialFocus={input()}>
  <input ref={setInput} />
</Dialog.Popup>;