Menhir

Accessible UI components for Solid 2.0

Solid 2.0 changed effects, ownership, async data and the props helpers, so a component library written for Solid 1.x has to be migrated, and one ported from React has to be rethought. Menhir is written for Solid 2.0 from its first line: each component follows its WAI-ARIA pattern, and its API follows Solid 2.0's idioms. This page is what that means for an app that uses it.

What it needs

Menhir's peer dependencies are solid-js and @solidjs/web 2.0. Solid 1.x is not supported.

pnpm add menhir solid-js @solidjs/web

The package ships its JSX source under the solid export condition, which @solidjs/vite-plugin compiles with your app. The same components then render on the server and hydrate in the browser.

Props are read where they are used

A Solid component runs once, and its props are read where they are used, never destructured. Menhir's parts read theirs the same way, so a signal passed to checked updates the switch without running anything again. Pass the value, on(), not the signal:

import { createSignal } from "solid-js";
import { Switch } from "menhir/switch";

export function AirplaneMode() {
  const [on, setOn] = createSignal(false);
  return (
    <label>
      <Switch.Root checked={on()} onCheckedChange={setOn} class="switch">
        <Switch.Thumb class="thumb" />
      </Switch.Root>
      Airplane mode
    </label>
  );
}

Another element: the render prop

Every part that renders an element takes render, a function that receives the element's props and returns the element. In Solid, a function from props to JSX is a component, so render is the component to render the part with.

// The default element, a <button>.
<Switch.Root class="switch" />

// Another element: attributes only it has go in the function.
<Switch.Root render={(props) => <a {...props} href="/settings" />} />

// A component of your own that spreads its props onto an element.
<Dialog.Trigger render={Button} />
<Dialog.Trigger render={(props) => <Button {...props} variant="ghost" />} />

There is no as prop. Rendering a tag name passed as a prop takes Solid's dynamic, which keeps the element runtime in every app that imports the part, about 1.6 kB gzipped, whether the app ever passes a tag name or not. A render function costs a few bytes per part. The function is read once, when the part is created, as an event handler is.

Controlled and uncontrolled state

A part's state is a value, its default and a change callback: checked, defaultChecked and onCheckedChange on a Switch, and the same pattern for open and value. An empty controlled value is null, since undefined means uncontrolled. The callback receives the new value; a popup's also receives { reason, event }, such as "escape-key" or "outside-press".

In Solid 2.0, a write can be held until async work that the same update started settles. An uncontrolled part writes its own state right after the event's flush, with onFlush, so a Switch flips and a Menu closes at once, even when your callback starts a fetch or an action. A controlled part's value, and its timing, are yours.

Async data under Loading

Solid 2.0 has no createResource: a memo that returns a Promise is pending until it lands, and <Loading> shows a fallback meanwhile. Menhir's parts work inside it. A Combobox's items can come from a search, with createDeferred keeping the last items shown while the next answer is in flight, and isPending telling a screen reader that a search is running:

import { createDeferred, createSignal, For, isPending, Loading } from "solid-js";
import { Combobox } from "menhir/combobox";

export function CountryPicker() {
  const [query, setQuery] = createSignal("");
  // After its first answer, a new query keeps the last items on screen.
  const results = createDeferred(() => searchCountries(query()));
  return (
    <Combobox.Root
      onInputValueChange={(text, { reason }) =>
        setQuery(reason === "input-change" ? text : "")
      }
    >
      <Combobox.Input aria-label="Country" />
      <Combobox.Popup>
        <Combobox.List aria-label="Countries">
          <Loading>
            <For each={results()}>
              {(name) => <Combobox.Item value={name}>{name}</Combobox.Item>}
            </For>
          </Loading>
        </Combobox.List>
        <Loading fallback={<p>Loading countries…</p>}>
          <Combobox.Status>
            {(count) =>
              isPending(() => results()) ? "Searching…" : `${count} results`
            }
          </Combobox.Status>
        </Loading>
      </Combobox.Popup>
    </Combobox.Root>
  );
}

The list opens before the first answer lands, and the keys reach the items as soon as they are rendered.

Server rendering and hydration

  • Controls such as Switch and Checkbox are native buttons, so their labels, focus and keyboard work in the server HTML, before the page hydrates.
  • The ids that link one part to another, such as a trigger's aria-controls, are generated with Solid's createUniqueId, which the server render and hydration agree on. A Field's label names its control from the server HTML on.
  • A popup open by default is rendered closed on the server and opened by hydration: a <dialog open> in the HTML would not be modal.

Popups without a Portal

Dialog and Alert Dialog are native <dialog> elements; Popover, Tooltip, Menu, Select and Combobox use the popover attribute. Both open in the browser's top layer, above the rest of the page, so a popup stays where you wrote it in your component tree, with no Portal and no z-index. A closed popup stays in the document, so its content keeps its state between openings.

Small bundles

Each component is its own entry point, such as menhir/dialog, and its parts are re-exported so that a bundler drops the ones you do not render. Behaviour a component can do without, such as a popup's arrow, is a part rather than a flag, so an app that does not render it does not ship it. Every entry point's size is measured and budgeted: see Bundle size.

Next

Get started with a first component, or browse all components.