Skip to content
Esc
↑↓navigate↵open⌘Jpreview

Port Base-UI To Astro

Port Base-UI to Astro with server-rendered markup, data-slot behavior, and shadcn semantic tokens such as --background.

Port Base-UI to Astro when a React component tree must become HTML that Astro can render on the server. Base UI supplies React components. data-slot adds the same keyboard behavior, ARIA patterns, and many of the same state attributes to elements you write. gnostic-ui wraps those elements in Astro components. The installed source stays in your project. Read Credits for the design sources, and the introduction for how components, blocks, and templates fit together.

The Base UI parity page is the part-by-part reference. This page records what that comparison means for an Astro port, and how shadcn themes stay on the same semantic tokens.

What you keep

A port keeps the interaction model. Accordion, tabs, dialogs, menus, and the other controls in the status table below still open, close, and move focus with the keyboard. Popup parts still expose placement through data-side and data-align. Accordion and collapsible panels still publish their size as CSS variables such as --accordion-panel-height.

The runtime is the part that changes. You compose Astro components, or the data-slot attributes those components render. A React render prop, a className function, and a controlled open prop become markup, state attributes, and a small browser controller.

Map the React API

Base UI Astro port
Compound components such as Dialog.Popup An element with data-slot="dialog-content", usually through Dialog
render prop The element you choose. Triggers are buttons.
className or style as a function of state A selector for the attribute the controller sets, such as [data-open] or [data-state=active]
Controlled value or open A controller method such as open() or setValue(), or a <name>:set event on the root
onValueChange(value, eventDetails) An onValueChange(value) option and a bubbling <name>:change event
defaultValue and defaultOpen The same JavaScript options, or data-default-value and data-default-open. JavaScript options win.
<Portal> Popover, tooltip, hover card, select, combobox, dropdown menu, navigation menu, and drawer move the popup to document.body while it is open. Dialog and alert dialog move when you include their portal part.
keepMounted Closed content stays in the document with hidden on accordion, collapsible, tabs, dialog, and alert dialog. Tooltip, hover card, select, and navigation menu can use mountStrategy.
Item arrays and object values Items in the markup, each with a string data-value
Field, Fieldset, and Form Native label, fieldset, and constraint validation. See Field and Form.
DirectionProvider The dir attribute
Tooltip.Provider One shared warm-up window for every tooltip on the page
Unmounting the component destroy() on the controller

Port a dialog

A Base UI dialog is a tree of React parts. The Astro version is the same tree as components. Dialog renders the data-slot hooks and starts the controller:

---
import { Button } from "@/components/ui/button"
import {
	Dialog,
	DialogClose,
	DialogContent,
	DialogDescription,
	DialogFooter,
	DialogHeader,
	DialogTitle,
	DialogTrigger,
} from "@/components/ui/dialog"
---

<Dialog>
	<DialogTrigger>Open</DialogTrigger>
	<DialogContent>
		<DialogHeader>
			<DialogTitle>Notifications</DialogTitle>
			<DialogDescription>You are all caught up.</DialogDescription>
		</DialogHeader>
		<DialogFooter>
			<DialogClose variant="outline">Close</DialogClose>
			<Button>Continue</Button>
		</DialogFooter>
	</DialogContent>
</Dialog>

Those components render data-slot="dialog", dialog-trigger, dialog-portal, dialog-overlay, dialog-content, dialog-title, dialog-description, and dialog-close. Closed overlay and content include hidden, so the page shows the server HTML until the script runs.

Installed dialog source creates controllers on astro:page-load and calls destroy() on astro:before-swap. View transitions replace the bound elements, so a controller must follow that cycle. To drive one dialog from script, query the root and use the controller:

import { createDialog } from "@data-slot/dialog"

const root = document.querySelector('[data-slot="dialog"]')
const dialog = createDialog(root)

dialog.open()
root?.dispatchEvent(new CustomEvent("dialog:set", { detail: { open: false } }))
root?.addEventListener("dialog:change", (event) => {
	console.log(event.detail.open)
})

closeOnClickOutside: false replaces disablePointerDismissal. Focus moves to an element with autofocus. The full option list is on the parity page.

Style from state

Popups and panels use data-starting-style and data-ending-style, and they wait for your CSS transition or animation before they hide. Most components also set data-state, so a selector written for Radix or shadcn/ui can keep its open, closed, active, on, and off rules.

Meaning Attribute to style
Open or closed data-open, data-closed, and data-state="open" or "closed". Collapsible sets data-state only.
Trigger of an open popup aria-expanded="true". Select and combobox triggers also get data-open.
Open accordion trigger data-panel-open
Highlighted item data-highlighted. Command uses data-selected.
Selected option data-selected
Checked data-checked and data-unchecked on Switch and Radio Group
Pressed toggle data-state="on" or "off" on Toggle
Active tab data-state="active" on the Tabs trigger and panel
Disabled data-disabled or aria-disabled, depending on the component
Placement data-side and data-align

Popover, tooltip, hover card, select, combobox, and dropdown menu default to a side offset of 4 and a collision padding of 8. Popups stay inside the viewport.

Component status

Near parity means the core behavior is covered and the gaps are secondary options, parts, or names. Partial means a major feature or accessibility behavior can be missing. None means Base UI has no matching component. The linked guide is the Astro component in this library. The parity page lists every gap.

Component Guide Base UI Status Main gaps
Accordion Accordion Accordion Near parity No Header part, per-item onOpenChange, or object values
Alert dialog Alert Dialog Alert Dialog Partial Outside content stays active. No initialFocus, finalFocus, detached triggers, or nesting attributes
Carousel Carousel — None Base UI has no carousel
Collapsible Collapsible Collapsible Near parity Uses data-state. No root disabled
Combobox Combobox Combobox Partial No multiple selection, chips, object values, limit, or virtualization
Command Command — None Base UI has no command menu
Dialog Dialog Dialog Partial Outside content stays active. No non-modal modes, initialFocus, or finalFocus
Drawer Drawer Drawer Partial One snap point. No swipe area
Dropdown menu Dropdown Menu Menu Partial No submenus, hover open, or multiple radio groups
Hover card Hover Card Preview Card Near parity No arrow, safe-polygon hover, or detached triggers
Navigation menu Navigation Menu Navigation Menu Partial No vertical orientation, nested menus, or collision avoidance
Popover Popover Popover Partial No role="dialog", modal mode, hover open, arrow, or title part
Radio group Radio Group Radio Group, Radio Near parity No form prop or Field integration
Resizable Resizable — None Base UI has no resizable panels
Select Select Select Partial No multiple selection, scroll arrows, or typeahead on the closed trigger
Slider Slider Slider Partial At most two thumbs. No form name or value part
Switch Switch Switch Near parity No form prop or Field integration
Tabs Tabs Tabs Near parity Arrow keys select a tab unless activation is manual
Toggle Toggle Toggle Near parity Uses data-state="on"
Toggle group Toggle Group Toggle Group Near parity Uses data-state="on". No RTL arrow keys
Tooltip Tooltip Tooltip Near parity No provider scope, closeDelay, or safe-polygon hover

Toast follows the Sonner API in data-slot. This library uses Alert for a message that stays in the page.

Use native HTML for the rest

Base UI Astro equivalent
Autocomplete Combobox without name, or Command for a list of actions
Avatar Avatar
Button Button. aria-disabled keeps a disabled button focusable.
Checkbox and Checkbox Group Checkbox inside Field
Field, Fieldset, Form, Input Label, Field, Form, and Input
Meter Meter
Number Field Input with type="number"
Progress Progress
Scroll Area Scroll Area
Separator Separator
Toolbar Toggle Group for a row of toggles

Context menu, menubar, and OTP field have no data-slot package. Write those with native elements when a page needs them.

Shadcn styles and semantic tokens

The styles for these components follow the shadcn registry specification. A preset is a registry item. It carries the component recipe, colors, fonts, and radius. bunx --bun shadcn@latest init --preset <id-or-url> installs that item into an Astro project. Installation shows the setup command. Gnostic choices that an upstream short code cannot store use a registry preset URL from the theme generator.

Because the classes follow that spec, a theme can change values and leave the component source in place. Semantic tokens such as --background remain. The shared stylesheet maps each token into Tailwind:

--color-background: var(--background);
--color-foreground: var(--foreground);
--color-primary: var(--primary);
--color-card: var(--card);

bg-background, text-foreground, bg-primary, bg-card, border-border, and ring-ring then read the current theme. The same set includes --secondary, --muted, --accent, --popover, --destructive, --input, and their -foreground pairs. Theming lists the tokens to edit. The shadcn theming docs are the reference for the token model.

A preset writes OKLCH color values and a --radius token. Light and dark mode each set the same variable names. Installed components contain the Tailwind utilities for the preset you chose. They do not contain a second copy of the color values. Change --background in the theme file, and every surface that uses bg-background follows it.

data-state on the ported controls is what lets a shadcn selector survive the move from React to Astro. An open dialog, an active tab, and a pressed toggle keep the state hooks those recipes already use. Switch and radio group are the exception: style [data-checked] and [data-unchecked] on those two.

Continue