Skip to Content
CodeUI

Compose styled parts

@codeui/solid v0.3.0

Build reusable Solid components from styled parts.

Build an app-level primitive from styled parts when you need a layout that you can reuse across screens. Keep the component's props focused on what each caller supplies. Assemble the parts once inside the component. Choose ready components or parts lists the available entry points.

Build a reusable primitive from styled parts

This PickerPopover sets a shared layout for compact pickers. Its close button sits beside the title. Callers supply the trigger label, panel title, and content.

PickerPopover.tsx
import { Button } from "@codeui/solid/button";
import * as P from "@codeui/solid/popover/parts";
import { Stack } from "@codeui/solid/stack";
import type { ParentProps } from "solid-js";

type PickerPopoverProps = ParentProps<{
	triggerLabel: string;
	title: string;
	closeLabel: string;
}>;

export function PickerPopover(props: PickerPopoverProps) {
	return (
		<P.Root placement="bottom-start">
			<P.Trigger as={Button} variant="secondary">
				{props.triggerLabel}
			</P.Trigger>
			<P.Portal>
				<P.Content>
					<Stack direction="row" align="center" justify="between" gap="2">
						<P.Title>{props.title}</P.Title>
						<P.CloseButton aria-label={props.closeLabel} />
					</Stack>
					{props.children}
				</P.Content>
			</P.Portal>
		</P.Root>
	);
}

Use the primitive anywhere that needs this layout. Supply the picker itself as children.

import { RadioList, RadioListItem } from "@codeui/solid/radio-list";
import { PickerPopover } from "./PickerPopover";

<PickerPopover triggerLabel="Typography" title="Typography" closeLabel="Close typography">
	<RadioList label="Line height" defaultValue="1.5">
		<RadioListItem value="1.2" label="1.2" />
		<RadioListItem value="1.5" label="1.5" />
		<RadioListItem value="1.8" label="1.8" />
	</RadioList>
</PickerPopover>

PickerPopover owns the layout and the parts that every instance needs. The picker owns its choices and value. Kit parts keep the accessible name, keyboard dismissal, and focus return through their shared Root. Add props for capabilities callers need, such as controlled open state.

For a field primitive, keep the input, label, and validation message under one field root. For a navigation primitive, keep triggers and panels in the same tabs root and pair them by value. See TextField parts, Tabs parts, and DropdownMenu parts for complete compositions.

Build a custom control

The examples below build a Line height popover. Start with the ready component and move to styled parts if you need to change the layout inside it.

Use the ready component

compose-styled-parts/ready.tsx
import { Button, Popover, PopoverContent, PopoverTrigger } from "@codeui/solid";

export default function ComposeReady() {
	return (
		<Popover placement="bottom-start">
			<PopoverTrigger as={Button} variant="secondary">
				Line height
			</PopoverTrigger>
			<PopoverContent title="Line height" description="Space between lines of code.">
				1.5 fits most fonts.
			</PopoverContent>
		</Popover>
	);
}

Import Popover, PopoverTrigger, and PopoverContent from @codeui/solid/popover or the kit root. PopoverContent portals the panel and renders the title and the description for you. Pick this layer first. You own the content and the state. The kit owns the arrangement, the IDs, and the ARIA links.

Compose the styled parts

compose-styled-parts/parts.tsx
import { Button } from "@codeui/solid";
import * as P from "@codeui/solid/popover/parts";

export default function ComposeParts() {
	return (
		<P.Root placement="bottom-start">
			<P.Trigger as={Button} variant="secondary">
				Line height
			</P.Trigger>
			<P.Portal>
				<P.Content>
					<div style={{ display: "flex", "align-items": "center", "justify-content": "space-between" }}>
						<P.Title>Line height</P.Title>
						<P.CloseButton aria-label="Close line height" />
					</div>
					<P.Description>Space between lines of code.</P.Description>
					<div style={{ display: "flex", gap: "8px" }}>
						<Button variant="secondary" size="sm">
							1.2
						</Button>
						<Button variant="secondary" size="sm">
							1.5
						</Button>
						<Button variant="secondary" size="sm">
							1.8
						</Button>
					</div>
				</P.Content>
			</P.Portal>
		</P.Root>
	);
}

The parts come from @codeui/solid/popover/parts, or @codeui/solid/dialog/parts for a dialog. Each part wraps one Kobalte primitive, adds the kit class, and sets a data-cui attribute. Pick this layer when the ready component fixes something you need to change. Here the close button sits next to the title, which PopoverContent does not render.

You own the arrangement. Keep every part inside its Root in the Solid tree, including when you use Portal. Keep Title inside Content so the panel has an accessible name. Use Trigger as={Button} when the trigger should use the kit’s Button styling. Dialog shows a larger example, a settings dialog with a breadcrumb title and two panes.

Kobalte with kit classes

compose-styled-parts/styles.tsx
import { Popover } from "@kobalte/core/popover";
import { buttonStyles } from "@codeui/solid";
import { popoverParts, popoverStyles } from "@codeui/solid/popover/parts";

export default function ComposeStyles() {
	return (
		<Popover placement="bottom-start" gutter={6}>
			<Popover.Trigger class={buttonStyles.button()} data-variant="secondary" data-size="md">Line height</Popover.Trigger>
			<Popover.Portal>
				<Popover.Content data-cui={popoverParts.content} class={popoverStyles.content()} data-variant="bordered">
					<Popover.Title data-cui={popoverParts.title} class={popoverStyles.title()}>
						Line height
					</Popover.Title>
					<Popover.Description data-cui={popoverParts.description} class={popoverStyles.description()}>
						Kobalte renders the panel. The kit only supplies the classes.
					</Popover.Description>
				</Popover.Content>
			</Popover.Portal>
		</Popover>
	);
}

Here Kobalte's Popover renders every element, and popoverStyles from @codeui/solid/popover/parts supplies the classes. The trigger takes its class from buttonStyles.button. Pick this layer when you need a Kobalte option or element that no kit part exposes, but still want the kit look.

You own the composition and the data-cui attributes. Copy them from popoverParts or dialogParts, so CSS and tests that target a part still match. Install @kobalte/core as a direct dependency of your app before you import it.

Pick a layer

You needUse
A standard dialog, popover, or menuThe ready component
A different arrangement of the same parts, such as an extra close button or a two-pane bodyThe styled parts
A Kobalte part or option the kit does not wrap, with the kit lookKobalte with *Styles classes
A different look altogetherKobalte alone, with your own CSS

Override kit styles with plain CSS

compose-styled-parts/override.tsx
import { Button, Popover, PopoverContent, PopoverTrigger } from "@codeui/solid";
import "./override.css";

export default function ComposeOverride() {
	return (
		<Popover placement="bottom-start">
			<PopoverTrigger as={Button} variant="secondary">
				Wider picker
			</PopoverTrigger>
			<PopoverContent
				title="Font family"
				description="One class, no !important. App CSS wins over the cui layer."
				class="compose-override-content"
			>
				Fira Code, JetBrains Mono, and Cascadia Code.
			</PopoverContent>
		</Popover>
	);
}

Every kit rule sits in the cui cascade layer. Unlayered CSS in your app wins over a layered rule, whatever the stylesheet order. So one class on PopoverContent widens the panel and adds padding, with no !important and no extra specificity. The same holds for a class on a part or next to a *Styles class.

A CSS reset is the exception, because an unlayered reset also wins. Cascade layers shows how to put the reset in a layer below cui.

Style components inside a toolbar

The toolbar sets the layout and chooses the variants of the controls it renders. Each control supplies its own base styles, focus styles, and interaction states.

No unsaved changes.

compose-styled-parts/editor-toolbar.tsx
import { Button, Switch } from "@codeui/solid";
import { createSignal } from "solid-js";
import "./editor-toolbar.css";

export default function EditorToolbar() {
	const [lineNumbers, setLineNumbers] = createSignal(true);
	const [savedLineNumbers, setSavedLineNumbers] = createSignal(true);

	return (
		<div class="editor-toolbar" role="group" aria-label="Editor preferences">
			<Switch
				class="editor-toolbar__line-numbers"
				label="Show line numbers"
				checked={lineNumbers()}
				onChange={setLineNumbers}
			/>
			<Button ghost onClick={() => setLineNumbers(true)}>
				Reset
			</Button>
			<Button
				class="editor-toolbar__save"
				variant="primary"
				onClick={() => setSavedLineNumbers(lineNumbers())}
			>
				Save
			</Button>
			<p class="editor-toolbar__status" role="status">
				{lineNumbers() === savedLineNumbers() ? "No unsaved changes." : "Unsaved changes."}
			</p>
		</div>
	);
}
editor-toolbar.css
.editor-toolbar {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	gap: var(--cui-space-3);
}

.editor-toolbar__save {
	margin-inline-start: auto;
}

.editor-toolbar__line-numbers [data-cui="switch-control"] {
	margin-inline-end: var(--cui-space-1);
}

.editor-toolbar__status {
	flex-basis: 100%;
	margin: 0;
	color: var(--cui-text-hint);
	font-size: var(--cui-font-size-caption);
}

variant="primary" gives Save the kit's primary treatment. The editor-toolbar__save class pushes that instance to the end of the row. A selector such as .editor-toolbar [data-cui="button"] would also match Reset and any buttons nested inside future toolbar children.

To style a part of a ready Switch, pass classes={{ control: "line-numbers-track" }} when you can set its props. With styled parts, pass class="line-numbers-track" directly to Switch.Control. Both give the track a class you can target directly.

When the integration gives you access to the root alone, scope an Anatomy selector to that instance. The demo puts editor-toolbar__line-numbers on the Switch root and targets [data-cui="switch-control"] beneath it. The selector matches the track in both checked and unchecked states. Switch anatomy lists the part names.

For color or geometry, set a documented CSS custom property on the component or part where you want it to take effect. Set it on a shared ancestor when the entire subtree should inherit it. Change one instance shows a token override on a Button.

  • Dialog lists the dialog parts and dialogStyles.
  • Popover lists the popover parts and popoverStyles.
  • Cascade layers explains the cui layer.

On this page