Button
Button and buttonVariants, from shadcn/ui, with the look of the kit.
Preview
Add to your site
npx docs-sitekit add buttonIt writes these files, and keeps a file you changed:
src/presentation/components/ui/button.tsx
It requires these entries of the registry, which add restores first:
Props
Built from the TypeScript types and the TSDoc comments of the component, so they cannot drift from the code.
| Key | Type | Default | Allowed values | Description |
|---|---|---|---|---|
variant | string | "default" | "default", "destructive", "ghost", "link", "outline", "secondary" | The look of the button. |
size | string | "default" | "default", "icon", "icon-lg", "icon-sm", "icon-xs", "lg", "sm", "xs" | The size of the button. The icon sizes are squares for a button that holds only an icon. |
asChild | boolean | false | true, false | Draw the child element instead of a button. It does not work with Astro children, so on an Astro page use buttonVariants on the element. |
Variants and states
Variants
Six looks for six jobs: the main action, a quiet one, an outline, a ghost, a destructive action and a link.
Sizes
Three sizes for text, a smaller one, and four for a button that holds only an icon.
As a link
In an Astro file the look of a button goes on an anchor with buttonVariants, because asChild does not work with Astro children.
Disabled
A disabled button is dimmed and takes no pointer events.
About
The button of shadcn/ui, wired to the tokens of the kit. Button draws a <button>. buttonVariants returns the classes of the same look, for an anchor or any other element.
On an Astro page, put the look on the element with buttonVariants. asChild hands the look to a child element through a React slot, and a child written in Astro is not a React element, so it does not work there.
Accessibility
- A
Buttonis a native<button>: Tab reaches it, and Enter and Space press it. An anchor withbuttonVariantsis a link, so Enter presses it and Space does not. - The focus ring is the 3 px ring of the component on top of the outline of the site, in both themes.
- The heights are 36 px (
default), 24 px (xs), 32 px (sm) and 40 px (lg). The squareiconsizes are 36 px, 24 px, 32 px and 40 px. None of them reaches the 44 px that the kit asks for on a phone, so on a phone add a taller class, for exampleclassName="min-h-11". The 24 px sizes only meet the minimum of WCAG 2.2. - A button that holds only an icon needs an
aria-label. The component cannot know the name, so it does not add one. disableddims the button to half its opacity and stops pointer events. A disabled control is exempt from the contrast rules.
Source
Last updated