Button
Runs an action, shows that it's working, and stays reachable when disabled.
Variants
Use primary for the one action a view is about, and quieter variants for the rest.
Sizes
icon is square, for a button holding only an icon. Give it an aria-label.
Pending
Return a Promise from onClick and the button stays pending until it settles: it ignores
further presses, keeps focus, and tells screen readers it’s busy after a second. Style the
waiting state with the data-pending attribute, or Tailwind’s in-data-pending: variant
inside the button.
Dependencies
What it's built from and what's built from it in Mocha, read from the imports in its source.
| Uses | Textprimitive |
|---|---|
| Used by | Inputform itemToast |
| Packages | @ui-sdk/core@0.1.0-beta.20@ui-sdk/react@0.1.0-beta.20tailwind-variants@4.1.0 |
Installation
Set the project up for Mocha once first, as Getting started shows. Then add it with the CLI, or by hand.
With the CLI
Manually
Install its packages:
Add the components it uses first, listed under Dependencies.
Then copy the source:
API reference
Generated from Mocha's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.
Button
Runs an action. Return a Promise from onClick to keep it pending until the action finishes.
- sizeours
Height and padding.
iconis square, for a button holding only an icon.Type NonNullable<"sm" | "md" | "lg" | "icon" | "icon-xs">
- variantours
How much the button stands out.
Type NonNullable<"primary" | "destructive" | "secondary" | "outline" | "ghost" | "link">
- children
Content, or a function of the part's state that returns it.
Type ReactNode | ((state: ButtonState) => ReactNode)
- className
Class names, or a function of the part's state that returns them.
Type string | ((state: ButtonState) => string)
- debug
Logs every state change with its reason to the console, in development builds.
Type boolean
- disabled
Stops presses. The button is natively disabled, so it leaves the tab order and its form doesn't submit through it, unless
focusableWhenDisabledis set.Type boolean · Default false
- focusableWhenDisabled
Keeps a disabled button in the tab order with
aria-disabledinstead of the native attribute, so it can hold focus while it becomes disabled and host a tooltip that says why. Presses still do nothing.Type boolean · Default false
- onClick
Runs when the button is pressed. Return a Promise to make the button pending until it settles: it gets
aria-busy,aria-disabledanddata-pending, keeps focus, ignores presses meanwhile, so a form can't submit twice, and sayspendingAnnouncementafter a second. The event's element is whateverrenderrenders, like a link.details.sourcesays how it was pressed, for changes made after anawait.Type (event: MouseEvent<HTMLElement, MouseEvent>, details: ActionDetails) => unknown
- pendingAnnouncement
What screen readers hear once the button has been pending for a second, so a slow action doesn't pass in silence. Set it in the page's language. An empty string says nothing.
Type string · Default "Loading"
- render
Renders your own element: one that receives the part's props, or a function of the props and the state.
Type RenderProp<ButtonState>
- style
Styles, or a function of the part's state that returns them.
Type CSSProperties | ((state: ButtonState) => CSSProperties)
- type
What a native button does in a form:
"submit"submits it and"reset"resets it. Ignored when the button renders as something else.Type "button" | "submit" | "reset" · Default "button"