This documentation is under construction. Pages can be missing or change at any time. Follow it on GitHub.

Skip to content

Base UI Components

Phem includes the components of Base UI (dialogs, menus, selects, comboboxes, tabs, toasts, fields and more), styled with Tailwind CSS v4. They live in the Phem\Ui namespace of phem/phem, so there's nothing extra to install. Each component's markup and classes are in a .phem file, and Phem's headless Stimulus controllers (phem-dialog, phem-menu, phem-combobox…) handle the browser behavior.

Browse every component with live examples, code and API reference on the Components pages. This guide covers setup and customization.

Set Up the Components#

  1. Import the theme in your input CSS. It sets the colors, radius and shadows, and registers the components as Tailwind sources, so you don't need an @source for them:

    /* assets/styles/app.css */
    @import "tailwindcss";
    @import "../../vendor/phem/phem/assets/ui/theme.css";

    With AssetMapper, the theme is also available as @phem/ui/theme.css. It's a Tailwind source (@theme, @variant), not browser CSS, so import it in your input CSS rather than linking it in a page.

  2. Enable the controllers the components use in assets/controllers.json (see Enable the controllers). Enable just the ones you need, or all of them:

    "@phem/phem": {
        "accordion": { "enabled": true, "fetch": "lazy" },
        "avatar": { "enabled": true, "fetch": "lazy" },
        "button": { "enabled": true, "fetch": "lazy" },
        "checkbox-group": { "enabled": true, "fetch": "lazy" },
        "collapsible": { "enabled": true, "fetch": "lazy" },
        "combobox": { "enabled": true, "fetch": "lazy" },
        "context-menu": { "enabled": true, "fetch": "lazy" },
        "dialog": { "enabled": true, "fetch": "lazy" },
        "drawer-provider": { "enabled": true, "fetch": "lazy" },
        "field": { "enabled": true, "fetch": "lazy" },
        "form": { "enabled": true, "fetch": "lazy" },
        "menu": { "enabled": true, "fetch": "lazy" },
        "menubar": { "enabled": true, "fetch": "lazy" },
        "navigation-menu": { "enabled": true, "fetch": "lazy" },
        "number-field": { "enabled": true, "fetch": "lazy" },
        "otp-field": { "enabled": true, "fetch": "lazy" },
        "popover": { "enabled": true, "fetch": "lazy" },
        "radio-group": { "enabled": true, "fetch": "lazy" },
        "scroll-area": { "enabled": true, "fetch": "lazy" },
        "select": { "enabled": true, "fetch": "lazy" },
        "slider": { "enabled": true, "fetch": "lazy" },
        "tabs": { "enabled": true, "fetch": "lazy" },
        "toast": { "enabled": true, "fetch": "lazy" },
        "toggle": { "enabled": true, "fetch": "lazy" },
        "toggle-group": { "enabled": true, "fetch": "lazy" },
        "toolbar": { "enabled": true, "fetch": "lazy" },
        "tooltip": { "enabled": true, "fetch": "lazy" }
    }
  3. Use the components. Base UI's X.Root is the X tag, and every other part is X<Part>:

    use Phem\Ui\{Accordion, AccordionItem};
    use function Phem\Ui\{AccordionHeader, AccordionPanel, AccordionTrigger};
    
    #[Page('/faq')]
    function FaqPage(): Node
    {
        return (
            <Document title="FAQ">
                <Accordion>
                    <AccordionItem value="what">
                        <AccordionHeader>
                            <AccordionTrigger>What is Phem?</AccordionTrigger>
                        </AccordionHeader>
                        <AccordionPanel>A library of UI server components for Symfony web apps.</AccordionPanel>
                    </AccordionItem>
                </Accordion>
            </Document>
        );
    }

vendor/bin/phemc components lists the components. vendor/bin/phemc components Dialog shows one component's imports, props and parts, and vendor/bin/phemc examples Dialog shows its examples.

The format and locale props of Meter and Progress need PHP's intl extension.

Customize#

The components take their colors, radius and popup shadow from CSS variables named like shadcn/ui's: bg-background, text-foreground, border-input, bg-primary… The defaults match the look of Base UI's Tailwind demos. There are no dark: classes in the components; the theme sets the dark values.

Change the Theme#

Set the variables in a rule after the import:

/* assets/styles/app.css */
@import "tailwindcss";
@import "../../vendor/phem/phem/assets/ui/theme.css";

:root {
    --primary: oklch(0.55 0.2 260);
    --primary-foreground: oklch(0.98 0 0);
    --ring: oklch(0.55 0.2 260);
    --radius: 0.625rem;
}

Every <Checkbox>, <Switch>, <RadioGroup> and pressed <Toggle> turns blue, and controls and popups get rounder. See the Theme Variables Reference for every variable.

Use a Shadcn Theme#

Paste a shadcn theme (or one from a shadcn theme generator) after the import. It sets the shadcn variables. A few of the theme's extra variables default to the Base UI look, so add these lines to get the shadcn look everywhere:

:root {
    --highlight: var(--accent);
    --highlight-foreground: var(--accent-foreground);
    --separator: var(--border);
    --popup-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1); /* shadow-md */
    --disabled-foreground: var(--foreground);
    --disabled-border: var(--input);
    --disabled-opacity: 0.5;
    --control-height: 2.25rem; /* h-9 */
    --drawer-overlay: var(--overlay);
    --link: var(--primary);
    --match: var(--primary);
}

Dark Mode#

The theme sets its dark values with @variant dark, so it follows your build's dark variant. Tailwind's default (the prefers-color-scheme media query) works as is. For a class-based toggle, declare a custom variant before the import:

@import "tailwindcss";
@custom-variant dark (&:where(.dark, .dark *));
@import "../../vendor/phem/phem/assets/ui/theme.css";

Warning — The variant must match the <html> element itself, because the theme sets its variables on :root. shadcn's variant, &:is(.dark *), doesn't match <html>, so the theme's own dark values (like --popup-shadow or --link) won't apply.

Add Classes#

Every part merges the caller's className into its own classes. Phem's Tailwind class merger is on by default, so your class replaces one from the same group (see Merge Tailwind classes):

<Button className="h-9 rounded-md">Save</Button>
// class="… h-9 rounded-md …": h-control and rounded-control are removed

A class only replaces one with the same variants. The joined parts of a NumberField use first:rounded-s-control last:rounded-e-control, so pass the same variants to change them:

<NumberFieldDecrement className="first:rounded-s-none" />
// class="… first:rounded-s-none …": first:rounded-s-control is removed

The merger reads the components' theme by default, so it knows rounded-control, rounded-popup and shadow-popup. If your input CSS defines its own @theme names, point the css option to it. Since it imports the components' theme, the merger reads both:

# config/packages/phem.yaml
phem:
    class_merger:
        tailwind:
            css: '%kernel.project_dir%/assets/styles/app.css'

Replace the Button#

The parts that are buttons render the Button component: DialogTrigger, DialogClose, AlertDialogTrigger, AlertDialogClose, DrawerTrigger, DrawerClose, PopoverTrigger, PopoverClose, MenuTrigger, SelectTrigger, CollapsibleTrigger, ToastAction and ToastClose. One replacement of Button changes all of them:

use Phem\Ui\Button;

#[Component(replaces: Button::class)]
function AppButton(?Html $children = null, mixed ...$rest): Node
{
    return <Button className={{ classes('h-9 px-4') }} {{ ...rest }}>{{ children }}</Button>;
}
// <DialogTrigger>, <MenuTrigger> and <SelectTrigger> now have h-9 and px-4 too.

Each part still merges its own classes on top (MenuTrigger adds padding for its caret). Triggers inside a Toolbar or Menubar are bar items: they keep the bar's look and don't render Button.

Style a Part With Its Slot#

Every part has a data-slot attribute: its name in kebab case, without a trailing Root. DialogPopup is dialog-popup, OTPFieldInput is otp-field-input, and SwitchRoot is switch, as in shadcn. A part that renders another part keeps its own name: AlertDialogPopup is alert-dialog-popup.

Use slots to style a part from your CSS or from a parent, without a className:

/* assets/styles/app.css */
[data-slot="dialog-popup"] {
    @apply w-[32rem];
}
<DialogPopup className="has-data-[slot=dialog-description]:gap-2">
    <DialogTitle>Notifications</DialogTitle>
    <DialogDescription>You are all caught up.</DialogDescription>
</DialogPopup>

Pass your own data-slot to rename a part: <Button data-slot="save">. Icons have no slot, and neither do MenuPayload, PopoverPayload, PreviewCardPayload and ComboboxChipTemplate, which render an invisible <template> and take no className. Other payloads render an element and have a slot (DialogPayload is dialog-payload).

Note — Don't style the data-phem-*-target attributes. They're the controllers' API, not a styling hook.

Names#

The components follow Base UI's anatomy. X.Root is the X tag and each part is X<Part>: Dialog, DialogTrigger, DialogPopup, DialogTitle… A root that shares its props with its parts is a class with #[Provide], so import it with use. Parts are function components, imported with use function. A part that shares props with its own children is also a class, like AccordionItem and MenuRadioGroup.

All 37 Base UI components are included, with a few differences:

  • Portal and Positioner have no tag. Popups render in the top layer (popover="manual" or a native <dialog>), and Positioner props (side, align, sideOffset…) go on the root.
  • Backdrop is the native <dialog>'s ::backdrop, styled through the popup's classes.
  • Ids come from a value (or the root's id), because the morph needs ids that come from the server render.
  • Switch's root is SwitchRoot, because switch is a reserved word in PHP.

Use Them With a Coding Agent#

phem ships the components' examples (examples/ui/, via the extra.phem.examples key of its composer.json) and this guide (extra.phem.guide) for coding agents. In an app, phemc serves them to the agent (see AI agents):

vendor/bin/phemc components                  # the families, one line each
vendor/bin/phemc components Dialog           # the props, the context and the anatomy of a component
vendor/bin/phemc examples Dialog             # the examples that use Dialog
vendor/bin/phemc search "dialog with a form" # the best components and examples for a task

Follow these rules when you write code with the components:

  • Families: a family is one file of src/Ui/ (Dialog.phem) with a root and its parts. Import the root and class parts with use, and function parts with use function, as phemc components <name> shows.
  • Anatomy: put each part inside the component whose context it needs, as the root's anatomy shows. A popup part goes inside its root, and the root takes the Positioner props.
  • Start from an example: each example is one use case from base-ui.com, with the text of its page. Copy the closest one, and keep its classes, its handle and its data attributes.
  • Check: run vendor/bin/phemc check <file> after each change.

Theme Variables Reference#

The shadcn variables:

Variable Used for
--background, --foreground a control's box and text
--popover, --popover-foreground a popup's box and text: popover, menu, select, dialog, drawer, toast
--primary, --primary-foreground a checked, pressed or filled part: checkbox, switch, radio, toggle, slider, progress, meter
--accent, --accent-foreground hover and open states
--muted, --muted-foreground a track (avatar, meter, progress, scroll area); descriptions, placeholders, disabled text
--border borders of popups and frames (accordion, fieldset), and separators in popups
--input a control's border: button, input, select, checkbox, switch
--ring the focus outline
--destructive a field's error
--radius rounded-popup is --radius, rounded-control is --radius * 0.8. The joined parts of a NumberField and a row of bordered TooltipTriggers are only rounded on their outer corners

The theme adds these, where one shadcn color has two roles in the Base UI look:

Variable Default Used for
--highlight, --highlight-foreground --primary, --primary-foreground the highlighted item of a menu, select or combobox
--separator neutral-300 the <Separator> component
--accent-active --accent with 10% of --foreground a control while it's pressed (active:)
--overlay black / 20% a dialog's backdrop
--drawer-overlay --overlay, black / 70% in dark mode a drawer's backdrop
--link blue-700, blue-500 in dark mode the hover color of a link in a toolbar
--match blue-800, blue-500 in dark mode the matched text in an item (ComboboxHighlight)
--popup-shadow an offset shadow, none in dark mode a popup's shadow (shadow-popup)
--disabled-foreground --muted-foreground the text of a disabled control or item
--disabled-border --muted-foreground the border of a disabled control
--disabled-opacity none (full opacity) the opacity of a disabled control or item
--control-height 2rem (h-8) a control's height: button, input, select, toggle, tab, menu trigger (h-control, size-control). An OTPField input is 0.5rem larger
Edit this page on GitHubLast updated
Phem
Esc
↑↓ to navigate↵ to openEsc to close