Learn how to style the primitives with the styling approach you prefer.
The primitives are unstyled and don't ship any CSS. They work with Tailwind, CSS isolation, a plain stylesheet or any other way of styling, so you keep total control of your styling layer.
Style hooks
CSS classes
Parts that render an HTML element pass a class attribute on to it:
<SwitchThumb class="switch-thumb" />
To pick classes from a part's state, style its data attributes (below), or render the element yourself with
Render, whose props carry the state:
<SwitchThumb>
<Render Context="thumb">
<span @attributes="thumb.Attributes" class="@(thumb.Checked ? "checked" : "unchecked")"></span>
</Render>
</SwitchThumb>Data attributes
Parts render data attributes for their states. For example, Switch can be styled
with its [data-checked] and [data-unchecked] attributes, among others:
.switch-thumb[data-checked] {
background-color: green;
}CSS variables
Parts set CSS variables to help with styling, often with numbers for sizes and transforms. For example, the popups of
Popover can use --available-height and --anchor-width:
.popover-popup {
max-height: var(--available-height);
min-width: var(--anchor-width);
}Each primitive's API Reference lists the data attributes and CSS variables of every part.
Style attribute
Parts that render an HTML element pass a style attribute on to it as well. A part that sets styles of its
own puts yours after them, so your declarations win:
<SwitchThumb style="height: 100px" />Tailwind CSS
Put Tailwind classes on each part. Tailwind's data-* variants match the parts' data attributes, as in
data-highlighted:bg-neutral-950, and its arbitrary values read their CSS variables, as in
origin-(--transform-origin). The styled components are a complete example.
<MenuRoot>
<MenuTrigger class="flex h-8 items-center justify-center rounded-md border border-neutral-950 bg-white px-3 text-sm text-neutral-950 select-none hover:bg-neutral-100 active:bg-neutral-200 data-popup-open:bg-neutral-100 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:border-white dark:bg-neutral-950 dark:text-white dark:hover:bg-neutral-800 dark:active:bg-neutral-700 dark:data-popup-open:bg-neutral-800 dark:focus-visible:outline-white">
Song
</MenuTrigger>
<MenuPortal>
<MenuPositioner class="outline-hidden" SideOffset="8">
<MenuPopup class="origin-(--transform-origin) border border-neutral-950 bg-white py-1 text-neutral-950 outline-hidden transition data-ending-style:scale-95 data-ending-style:opacity-0 data-starting-style:scale-95 data-starting-style:opacity-0 dark:border-white dark:bg-neutral-950 dark:text-white">
<MenuItem class="flex cursor-default py-2 pr-8 pl-4 text-sm leading-4 outline-hidden select-none data-highlighted:bg-neutral-950 data-highlighted:text-white dark:data-highlighted:bg-white dark:data-highlighted:text-neutral-950">
Add to Library
</MenuItem>
<MenuItem class="flex cursor-default py-2 pr-8 pl-4 text-sm leading-4 outline-hidden select-none data-highlighted:bg-neutral-950 data-highlighted:text-white dark:data-highlighted:bg-white dark:data-highlighted:text-neutral-950">
Add to Playlist
</MenuItem>
</MenuPopup>
</MenuPositioner>
</MenuPortal>
</MenuRoot>CSS
Put a class on each part and style it in a stylesheet your app loads, such as app.css. Give the
classes names that won't clash with other components' classes:
<MenuRoot>
<MenuTrigger class="song-menu-trigger">Song</MenuTrigger>
<MenuPopup class="song-menu-popup" SideOffset="8">
<MenuItem class="song-menu-item">Add to Library</MenuItem>
<MenuItem class="song-menu-item">Add to Playlist</MenuItem>
</MenuPopup>
</MenuRoot>.song-menu-popup {
border: 1px solid currentColor;
transform-origin: var(--transform-origin);
}
.song-menu-item[data-highlighted] {
background-color: black;
color: white;
}Every example in these docs comes styled both ways: switch between CSS and Tailwind with the styling method select under the example.
CSS isolation
A component's scoped styles only reach the elements it renders itself, and a part renders its element in its own
component. Put ::deep in front of selectors for parts, under an element of your own:
<div class="song-menu">
<MenuRoot>
<MenuTrigger class="trigger">Song</MenuTrigger>
<MenuPopup class="popup" SideOffset="8">
<MenuItem class="item">Add to Library</MenuItem>
<MenuItem class="item">Add to Playlist</MenuItem>
</MenuPopup>
</MenuRoot>
</div>.song-menu ::deep .popup {
border: 1px solid currentColor;
transform-origin: var(--transform-origin);
}
.song-menu ::deep .item[data-highlighted] {
background-color: black;
color: white;
}
Popups stay where you wrote them in the DOM (they're shown in the browser's top layer rather than moved into a
portal), so ::deep reaches them too.