|
| 1 | +import { Meta, Story, Controls } from '@storybook/addon-docs/blocks'; |
| 2 | +import * as ButtonSplitStories from './ButtonSplit.stories'; |
| 3 | + |
| 4 | +<Meta of={ButtonSplitStories} /> |
| 5 | + |
| 6 | +# Button.Split |
| 7 | + |
| 8 | +A split button that groups a primary action with a dropdown trigger for secondary actions. Supports two modes: **custom** (arbitrary button children) and **strict** (declarative action list with built-in menu). |
| 9 | + |
| 10 | +## When to Use |
| 11 | + |
| 12 | +- Present a default action alongside alternative actions in a compact control |
| 13 | +- Allow users to switch between related operations (e.g., deploy targets, save modes) |
| 14 | +- Group two or more buttons visually as a single connected unit |
| 15 | + |
| 16 | +## Component |
| 17 | + |
| 18 | +<Story of={ButtonSplitStories.Default} /> |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## Properties |
| 23 | + |
| 24 | +<Controls /> |
| 25 | + |
| 26 | +### Base Properties |
| 27 | + |
| 28 | +Supports [Base properties](https://github.com/tenphi/tasty/blob/main/docs/tasty.md) |
| 29 | + |
| 30 | +### Styling Properties |
| 31 | + |
| 32 | +#### styles |
| 33 | + |
| 34 | +Customises the root wrapper element of the split button. |
| 35 | + |
| 36 | +### Style Properties |
| 37 | + |
| 38 | +These properties allow direct styling without using the `styles` prop: `width`, `height`. |
| 39 | + |
| 40 | +## Modes |
| 41 | + |
| 42 | +### Strict Mode |
| 43 | + |
| 44 | +Pass an `actions` array to automatically render an action button and a trigger button with a dropdown menu. The trigger displays a `DirectionIcon` pointing down. |
| 45 | + |
| 46 | +```jsx |
| 47 | +import { Button } from '@cube-dev/ui-kit'; |
| 48 | + |
| 49 | +const actions = [ |
| 50 | + { key: 'deploy', label: 'Deploy', icon: <IconSend /> }, |
| 51 | + { key: 'deploy-staging', label: 'Deploy to Staging', icon: <IconSend /> }, |
| 52 | + { key: 'deploy-preview', label: 'Deploy Preview', icon: <IconPlayerPlay /> }, |
| 53 | +]; |
| 54 | + |
| 55 | +<Button.Split |
| 56 | + actions={actions} |
| 57 | + defaultActionKey="deploy" |
| 58 | + type="primary" |
| 59 | + onAction={(key) => console.log('Executed:', key)} |
| 60 | + onActionChange={(key) => console.log('Switched to:', key)} |
| 61 | +/> |
| 62 | +``` |
| 63 | + |
| 64 | +<Story of={ButtonSplitStories.StrictUncontrolled} /> |
| 65 | + |
| 66 | +#### Controlled |
| 67 | + |
| 68 | +Use `actionKey` and `onActionChange` for controlled state. |
| 69 | + |
| 70 | +```jsx |
| 71 | +const [currentKey, setCurrentKey] = useState('deploy'); |
| 72 | + |
| 73 | +<Button.Split |
| 74 | + actions={actions} |
| 75 | + actionKey={currentKey} |
| 76 | + type="primary" |
| 77 | + onAction={(key) => console.log('Executed:', key)} |
| 78 | + onActionChange={setCurrentKey} |
| 79 | +/> |
| 80 | +``` |
| 81 | + |
| 82 | +<Story of={ButtonSplitStories.StrictControlled} /> |
| 83 | + |
| 84 | +### Custom Mode |
| 85 | + |
| 86 | +Pass `children` to render arbitrary buttons with split-group styling (joined radius, collapsed borders). Use `Menu.Trigger` and `Menu` for dropdown behavior. |
| 87 | + |
| 88 | +```jsx |
| 89 | +import { Button, Menu } from '@cube-dev/ui-kit'; |
| 90 | + |
| 91 | +<Button.Split> |
| 92 | + <Button type="primary" onPress={() => console.log('Save')}> |
| 93 | + Save |
| 94 | + </Button> |
| 95 | + <Menu.Trigger placement="bottom end"> |
| 96 | + <Button type="primary" aria-label="More save options" icon={true} /> |
| 97 | + <Menu onAction={(key) => console.log(key)}> |
| 98 | + <Menu.Item key="save-draft">Save as Draft</Menu.Item> |
| 99 | + <Menu.Item key="save-publish">Save & Publish</Menu.Item> |
| 100 | + </Menu> |
| 101 | + </Menu.Trigger> |
| 102 | +</Button.Split> |
| 103 | +``` |
| 104 | + |
| 105 | +<Story of={ButtonSplitStories.Custom} /> |
| 106 | + |
| 107 | +### Multiple Buttons |
| 108 | + |
| 109 | +More than two buttons are supported. Middle buttons have no border radius. |
| 110 | + |
| 111 | +```jsx |
| 112 | +<Button.Split> |
| 113 | + <Button type="outline" icon={<IconPlus />}>Add</Button> |
| 114 | + <Button type="outline" icon={<IconCopy />}>Copy</Button> |
| 115 | + <Button type="outline" icon={<IconDownload />}>Export</Button> |
| 116 | +</Button.Split> |
| 117 | +``` |
| 118 | + |
| 119 | +<Story of={ButtonSplitStories.ThreeButtons} /> |
| 120 | + |
| 121 | +## Examples |
| 122 | + |
| 123 | +### Variants |
| 124 | + |
| 125 | +Different types and themes can be applied in strict mode. |
| 126 | + |
| 127 | +<Story of={ButtonSplitStories.Variants} /> |
| 128 | + |
| 129 | +```jsx |
| 130 | +<Button.Split actions={actions} defaultActionKey="copy" type="primary" /> |
| 131 | +<Button.Split actions={actions} defaultActionKey="copy" type="secondary" /> |
| 132 | +<Button.Split actions={actions} defaultActionKey="copy" type="outline" /> |
| 133 | +<Button.Split actions={actions} defaultActionKey="copy" type="primary" theme="danger" /> |
| 134 | +<Button.Split actions={actions} defaultActionKey="copy" type="primary" isDisabled /> |
| 135 | +``` |
| 136 | + |
| 137 | +### Customizing Sub-Components |
| 138 | + |
| 139 | +Use `actionProps`, `triggerProps`, and `menuProps` to customise individual parts. |
| 140 | + |
| 141 | +```jsx |
| 142 | +<Button.Split |
| 143 | + actions={actions} |
| 144 | + defaultActionKey="deploy" |
| 145 | + type="primary" |
| 146 | + actionProps={{ size: 'large' }} |
| 147 | + triggerProps={{ 'aria-label': 'Select deploy target' }} |
| 148 | + menuProps={{ placement: 'bottom start' }} |
| 149 | +/> |
| 150 | +``` |
| 151 | + |
| 152 | +## Accessibility |
| 153 | + |
| 154 | +### Keyboard Navigation |
| 155 | + |
| 156 | +- `Tab` -- Moves focus between the action button and the trigger button. |
| 157 | +- `Space` / `Enter` -- Activates the focused button. On the trigger, opens the dropdown menu. |
| 158 | +- `Arrow Down` -- When the menu is open, navigates menu items. |
| 159 | +- `Escape` -- Closes the dropdown menu and returns focus to the trigger. |
| 160 | + |
| 161 | +### Screen Reader Support |
| 162 | + |
| 163 | +- The action button announces its label. |
| 164 | +- The trigger button has `aria-label="More options"` by default (override via `triggerProps`). |
| 165 | +- The dropdown menu announces selection changes. |
| 166 | + |
| 167 | +### ARIA Properties |
| 168 | + |
| 169 | +- `aria-label` -- Provide on the trigger for context (e.g., "Select deploy target"). |
| 170 | +- Menu items support `aria-disabled` via the `isDisabled` property on actions. |
| 171 | + |
| 172 | +## Best Practices |
| 173 | + |
| 174 | +1. **Do**: Use strict mode for standard action+trigger patterns. |
| 175 | + ```jsx |
| 176 | + <Button.Split actions={actions} defaultActionKey="deploy" type="primary" /> |
| 177 | + ``` |
| 178 | + |
| 179 | +2. **Do**: Use custom mode when you need full control over button content and behavior. |
| 180 | + ```jsx |
| 181 | + <Button.Split> |
| 182 | + <Button>Primary</Button> |
| 183 | + <Button icon={<DirectionIcon to="down" />} aria-label="Options" /> |
| 184 | + </Button.Split> |
| 185 | + ``` |
| 186 | + |
| 187 | +3. **Don't**: Mix `actions` prop with `children`. Use one mode or the other. |
| 188 | + |
| 189 | +4. **Accessibility**: Always provide `aria-label` on icon-only trigger buttons. |
| 190 | + |
| 191 | +## Suggested Improvements |
| 192 | + |
| 193 | +- Add `size` variants demo for strict mode across all sizes. |
| 194 | +- Support `isLoading` on the action button in strict mode. |
| 195 | +- Add keyboard shortcut display in menu items via `hotkeys` prop. |
| 196 | + |
| 197 | +## Related Components |
| 198 | + |
| 199 | +- [Button](/docs/actions-button--docs) -- Standalone action button. |
| 200 | +- [Menu](/docs/actions-menu--docs) -- Dropdown menu for action lists. |
| 201 | +- [Button.Group](/docs/actions-buttongroup--docs) -- Layout wrapper for spacing multiple buttons. |
0 commit comments