|
| 1 | +# Button |
| 2 | + |
| 3 | +Trigger an action or event, such as submitting a form, opening a dialog, or performing operations like canceling or deleting. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Built with a native `button` element. |
| 8 | + |
| 9 | +- Supports a `pending` state to indicate ongoing actions and provide feedback to assistive technologies. |
| 10 | + |
| 11 | +- Keyboard event support for `Space` and `Enter` keys. |
| 12 | + |
| 13 | +## Import |
| 14 | + |
| 15 | +```tsx |
| 16 | +import { Button } from 'qwik-primitives'; |
| 17 | +``` |
| 18 | + |
| 19 | +## Anatomy |
| 20 | + |
| 21 | +Import all parts and piece them together. |
| 22 | + |
| 23 | +```tsx |
| 24 | +import { component$ } from '@builder.io/qwik'; |
| 25 | +import { Button } from 'qwik-primitives'; |
| 26 | + |
| 27 | +const ButtonDemo = component$(() => { |
| 28 | + return ( |
| 29 | + <Button.Root> |
| 30 | + <Button.Content /> |
| 31 | + <Button.Fallback /> |
| 32 | + </Button.Root> |
| 33 | + ); |
| 34 | +}); |
| 35 | +``` |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +```tsx |
| 40 | +import { component$ } from '@builder.io/qwik'; |
| 41 | +import { Button } from 'qwik-primitives'; |
| 42 | + |
| 43 | +const ButtonDemo = component$(() => { |
| 44 | + return ( |
| 45 | + <Button.Root> |
| 46 | + <Button.Content>Save data</Button.Content> |
| 47 | + </Button.Root> |
| 48 | + ); |
| 49 | +}); |
| 50 | +``` |
| 51 | + |
| 52 | +## API Reference |
| 53 | + |
| 54 | +### Root |
| 55 | + |
| 56 | +Contains all the parts of a button. This component is based on the `button` element. |
| 57 | + |
| 58 | +| Prop | Type | Default | Description | |
| 59 | +| ---------- | --------------------------------- | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 60 | +| `as` | `FunctionComponent` | `-` | Change the default rendered element for the one passed as, merging their props and behavior. Read our [Composition](https://github.com/ZAHON/qwik-primitives/blob/main/packages/primitives/docs/composition.md) guide for more details. | |
| 61 | +| `type` | `"button" \| "submit" \| "reset"` | `"button"` | The behavior of the button when used in an HTML form. | |
| 62 | +| `disabled` | `boolean` | `-` | When `true`, prevents the user from interacting with the button. | |
| 63 | +| `pending` | `boolean` | `-` | Whether the button is in a pending state. | |
| 64 | +| `style` | `CSSProperties` | `-` | The inline style for the element. | |
| 65 | + |
| 66 | +| Data attribute | Values | |
| 67 | +| ----------------- | ------------------------------------------------------- | |
| 68 | +| `[data-scope]` | `"button"` | |
| 69 | +| `[data-part]` | `"root"` | |
| 70 | +| `[data-disabled]` | Present when the button is disabled | |
| 71 | +| `[data-pending]` | Present when the button is currently in a pending state | |
| 72 | + |
| 73 | +### Content |
| 74 | + |
| 75 | +Contains the content for the button. By default, it will render only when the button is not currently in a `pending` state, use the `forceMount` prop to always render the content. This component is based on the `span` element. |
| 76 | + |
| 77 | +| Prop | Type | Default | Description | |
| 78 | +| ------------ | ------------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 79 | +| `as` | `FunctionComponent` | `-` | Change the default rendered element for the one passed as, merging their props and behavior. Read our [Composition](https://github.com/ZAHON/qwik-primitives/blob/main/packages/primitives/docs/composition.md) guide for more details. | |
| 80 | +| `forceMount` | `boolean` | `-` | Used to force mounting when more control is needed. | |
| 81 | +| `style` | `CSSProperties` | `-` | The inline style for the element. | |
| 82 | + |
| 83 | +| Data attribute | Values | |
| 84 | +| ----------------- | ------------------------------------------------------- | |
| 85 | +| `[data-scope]` | `"button"` | |
| 86 | +| `[data-part]` | `"content"` | |
| 87 | +| `[data-disabled]` | Present when the button is disabled | |
| 88 | +| `[data-pending]` | Present when the button is currently in a pending state | |
| 89 | + |
| 90 | +### Fallback |
| 91 | + |
| 92 | +An element that renders when the button is currently in a `pending` state. For more control use the `forceMount` prop to always render the component. This component is based on the `span` element. |
| 93 | + |
| 94 | +| Prop | Type | Default | Description | |
| 95 | +| ------------ | ------------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 96 | +| `as` | `FunctionComponent` | `-` | Change the default rendered element for the one passed as, merging their props and behavior. Read our [Composition](https://github.com/ZAHON/qwik-primitives/blob/main/packages/primitives/docs/composition.md) guide for more details. | |
| 97 | +| `forceMount` | `boolean` | `-` | Used to force mounting when more control is needed. | |
| 98 | +| `style` | `CSSProperties` | `-` | The inline style for the element. | |
| 99 | + |
| 100 | +| Data attribute | Values | |
| 101 | +| ----------------- | ------------------------------------------------------- | |
| 102 | +| `[data-scope]` | `"button"` | |
| 103 | +| `[data-part]` | `"fallback"` | |
| 104 | +| `[data-disabled]` | Present when the button is disabled | |
| 105 | +| `[data-pending]` | Present when the button is currently in a pending state | |
| 106 | + |
| 107 | +## Examples |
| 108 | + |
| 109 | +### Pending |
| 110 | + |
| 111 | +A button can be put into a pending state using the `pending` prop. This is useful when an action takes a long time to complete, and you want to provide feedback to the user that the action is in progress. The pending state announces the state change to assistive technologies. |
| 112 | + |
| 113 | +```tsx |
| 114 | +import { component$, useSignal, $ } from '@builder.io/qwik'; |
| 115 | +import { Button } from 'qwik-primitives'; |
| 116 | + |
| 117 | +const ButtonDemo = component$(() => { |
| 118 | + const isPending = useSignal(false); |
| 119 | + |
| 120 | + const handleClick$ = $(() => { |
| 121 | + if (!isPending.value) { |
| 122 | + isPending.value = true; |
| 123 | + |
| 124 | + setTimeout(() => { |
| 125 | + isPending.value = false; |
| 126 | + }, 5000); |
| 127 | + } |
| 128 | + }); |
| 129 | + |
| 130 | + return ( |
| 131 | + <Button.Root pending={isPending.value} onClick$={handleClick$}> |
| 132 | + <Button.Content>Save data</Button.Content> |
| 133 | + <Button.Fallback>Saving...</Button.Fallback> |
| 134 | + </Button.Root> |
| 135 | + ); |
| 136 | +}); |
| 137 | +``` |
| 138 | + |
| 139 | +## Accessibility |
| 140 | + |
| 141 | +### Keyboard Interactions |
| 142 | + |
| 143 | +| Key | Description | |
| 144 | +| ------- | --------------------- | |
| 145 | +| `Space` | Activates the button. | |
| 146 | +| `Enter` | Activates the button. | |
0 commit comments