Sub topic 3
Composing React UIs with shadcn/ui
Assume you know React function components, TypeScript props, Tailwind CSS classes, and npm-based React projects. This chapter focuses on composing and maintaining shadcn/ui components rather than learning React basics.
Composing components and using `asChild`
Radix primitives provide behavior such as focus management and keyboard handling. The asChild prop uses Radix Slot to apply that behavior and props to your own element instead of rendering an extra wrapper.
import { Slot } from "@radix-ui/react-slot"
import { cva } from "class-variance-authority"
import { cn } from "@/lib/utils"
const styles = cva("rounded px-4 py-2", {
variants: { variant: { primary: "bg-blue-600 text-white", outline: "border" } },
defaultVariants: { variant: "primary" },
})
export function Button({
asChild = false,
className,
variant,
...props
}: React.ComponentProps<"button"> & {
asChild?: boolean
variant?: "primary" | "outline"
}) {
const Component = asChild ? Slot : "button"
return <Component className={cn(styles({ variant }), className)} {...props} />
}
// <Button asChild><a href="/settings">Settings</a></Button>The child must accept the forwarded props and ref. Avoid putting multiple children inside Slot, because it needs one element to receive them.
Customizing components you own
The CLI copies source files into your project. Edit those files to match your product. Keep public props stable, preserve accessibility attributes, and place repeated styling in cva variants. Add tests when changing interaction behavior.
Building validated forms
Use React Hook Form for field state and submission, and Zod for schema validation. The resolver connects the two. Render errors with role="alert" and connect labels to controls.
Building data tables
TanStack Table manages headless table state. You render its rows and cells with shadcn/ui Table components. This separation lets you add sorting, filtering, and pagination without changing your visual markup.
Testing keyboard and screen reader behavior
Test that users can tab to controls, open dialogs with the keyboard, close them with Escape, and see focus return to the trigger. Use accessible names and roles in queries. Test validation messages and table headers, not only CSS snapshots.
Choosing among headless libraries
Radix is a strong choice for accessible primitives and composable overlays. TanStack specializes in data-heavy state such as tables. React Aria offers broad accessibility hooks. Choose based on required behavior, styling freedom, bundle size, and team familiarity.
Keeping copied components up to date
Record the source version and compare upstream changes periodically. Apply security and accessibility fixes manually, then rerun interaction tests. Avoid replacing files blindly, because that can remove local variants and bug fixes.
Exercise
Why might <Button asChild><a href="/help">Help</a></Button> be preferable to nesting a button inside a link?
Show answer
It renders one link that receives the button's styles and forwarded props, preserving valid HTML and native link behavior.