|
| 1 | +import { Meta, Canvas, Story, Controls } from '@storybook/blocks'; |
| 2 | +import * as Stories from './ToggleButton.stories'; |
| 3 | +import { |
| 4 | + ToggleButton, |
| 5 | + ToggleButtonGroup, |
| 6 | + ToggleButtonIcon, |
| 7 | + ToggleButtonText, |
| 8 | + Box, |
| 9 | + Center, |
| 10 | +} from '../'; |
| 11 | +import { BackToTopButton, UsageWrap } from '../../../docs/components'; |
| 12 | +import { MoonSmallIcon, SunSmallIcon, StarSmallIcon } from '@utilitywarehouse/react-native-icons'; |
| 13 | + |
| 14 | +<Meta title="Components / Toggle Button" /> |
| 15 | + |
| 16 | +<BackToTopButton /> |
| 17 | + |
| 18 | +# ToggleButton |
| 19 | + |
| 20 | +The `ToggleButton` component provides an interactive selection control where users can toggle between multiple options with a sliding indicator. |
| 21 | + |
| 22 | +- [Playground](#playground) |
| 23 | +- [Usage](#usage) |
| 24 | +- [Props](#props) |
| 25 | +- [Advanced Usage](#advanced-usage) |
| 26 | +- [Examples](#examples) |
| 27 | + |
| 28 | +## Playground |
| 29 | + |
| 30 | +<Canvas of={Stories.Playground} /> |
| 31 | + |
| 32 | +<Controls of={Stories.Playground} /> |
| 33 | + |
| 34 | +## Usage |
| 35 | + |
| 36 | +<UsageWrap> |
| 37 | + <Center> |
| 38 | + <Box width="100%" maxWidth={400}> |
| 39 | + <ToggleButtonGroup value="option1"> |
| 40 | + <ToggleButton value="option1">Option 1</ToggleButton> |
| 41 | + <ToggleButton value="option2">Option 2</ToggleButton> |
| 42 | + <ToggleButton value="option3">Option 3</ToggleButton> |
| 43 | + </ToggleButtonGroup> |
| 44 | + </Box> |
| 45 | + </Center> |
| 46 | +</UsageWrap> |
| 47 | + |
| 48 | +```tsx |
| 49 | +import { ToggleButton, ToggleButtonGroup } from '@utilitywarehouse/native-ui'; |
| 50 | + |
| 51 | +const MyComponent = () => { |
| 52 | + const [value, setValue] = useState('option1'); |
| 53 | + |
| 54 | + return ( |
| 55 | + <ToggleButtonGroup value={value} onChange={setValue}> |
| 56 | + <ToggleButton value="option1">Option 1</ToggleButton> |
| 57 | + <ToggleButton value="option2">Option 2</ToggleButton> |
| 58 | + <ToggleButton value="option3">Option 3</ToggleButton> |
| 59 | + </ToggleButtonGroup> |
| 60 | + ); |
| 61 | +}; |
| 62 | +``` |
| 63 | + |
| 64 | +## Props |
| 65 | + |
| 66 | +### ToggleButton Props |
| 67 | + |
| 68 | +| Property | Type | Description | Default | |
| 69 | +| ---------- | ----------- | ---------------------------------- | ------- | |
| 70 | +| `value` | `any` | The value of the button (required) | - | |
| 71 | +| `children` | `ReactNode` | The text to display (required) | - | |
| 72 | +| `style` | `ViewStyle` | Additional styles for the button | - | |
| 73 | +| `disabled` | `boolean` | Whether the button is disabled | `false` | |
| 74 | + |
| 75 | +### ToggleButtonGroup Props |
| 76 | + |
| 77 | +| Property | Type | Description | Default | |
| 78 | +| ---------- | ---------------------- | ---------------------------------------- | --------- | |
| 79 | +| `value` | `any` | The currently selected value (required) | - | |
| 80 | +| `onChange` | `(value: any) => void` | Callback function when selection changes | - | |
| 81 | +| `style` | `ViewStyle` | Additional styles for the container | - | |
| 82 | +| `children` | `ReactNode` | The toggle button elements | - | |
| 83 | +| `disabled` | `boolean` | Whether the group is disabled | `false` | |
| 84 | +| `size` | `'small' \| 'base'` | The size of the toggle button | `'small'` | |
| 85 | + |
| 86 | +### ToggleButtonIcon Props |
| 87 | + |
| 88 | +| Property | Type | Description | Default | |
| 89 | +| -------- | ----------- | ------------------------------ | ------- | |
| 90 | +| `as` | `ReactNode` | The icon to display (required) | - | |
| 91 | +| `style` | `ViewStyle` | Additional styles for the icon | - | |
| 92 | + |
| 93 | +### ToggleButtonText Props |
| 94 | + |
| 95 | +| Property | Type | Description | Default | |
| 96 | +| -------- | ----------- | ------------------------------ | ------- | |
| 97 | +| `style` | `TextStyle` | Additional styles for the text | - | |
| 98 | + |
| 99 | +## Advanced Usage |
| 100 | + |
| 101 | +Although we do not recommend, you can use the `ToggleButton` component with icons and custom text styles. |
| 102 | + |
| 103 | +<UsageWrap> |
| 104 | + <Center> |
| 105 | + <Box width="100%" maxWidth={400}> |
| 106 | + <ToggleButtonGroup value="sun"> |
| 107 | + <ToggleButton value="sun"> |
| 108 | + <ToggleButtonIcon as={SunSmallIcon} /> |
| 109 | + <ToggleButtonText style={{ color: 'yellow' }}>Sun</ToggleButtonText> |
| 110 | + </ToggleButton> |
| 111 | + <ToggleButton value="star"> |
| 112 | + <ToggleButtonIcon as={StarSmallIcon} /> |
| 113 | + <ToggleButtonText style={{ color: 'orange' }}>Star</ToggleButtonText> |
| 114 | + </ToggleButton> |
| 115 | + <ToggleButton value="moon"> |
| 116 | + <ToggleButtonIcon as={MoonSmallIcon} /> |
| 117 | + <ToggleButtonText style={{ color: 'white' }}>Moon</ToggleButtonText> |
| 118 | + </ToggleButton> |
| 119 | + </ToggleButtonGroup> |
| 120 | + </Box> |
| 121 | + </Center> |
| 122 | +</UsageWrap> |
| 123 | + |
| 124 | +```tsx |
| 125 | +import { |
| 126 | + ToggleButton, |
| 127 | + ToggleButtonGroup, |
| 128 | + ToggleButtonIcon, |
| 129 | + ToggleButtonText, |
| 130 | +} from '@utilitywarehouse/native-ui'; |
| 131 | +import { MoonSmallIcon, SunSmallIcon, StarSmallIcon } from '@utilitywarehouse/react-native-icons'; |
| 132 | + |
| 133 | +const MyComponent = () => { |
| 134 | + const [value, setValue] = useState('sun'); |
| 135 | + |
| 136 | + return ( |
| 137 | + <ToggleButtonGroup value={value} onChange={setValue}> |
| 138 | + <ToggleButton value="sun"> |
| 139 | + <ToggleButtonIcon as={SunSmallIcon} /> |
| 140 | + <ToggleButtonText style={{ color: 'yellow' }}>Sun</ToggleButtonText> |
| 141 | + </ToggleButton> |
| 142 | + <ToggleButton value="star"> |
| 143 | + <ToggleButtonIcon as={StarSmallIcon} /> |
| 144 | + <ToggleButtonText style={{ color: 'orange' }}>Star</ToggleButtonText> |
| 145 | + </ToggleButton> |
| 146 | + <ToggleButton value="moon"> |
| 147 | + <ToggleButtonIcon as={MoonSmallIcon} /> |
| 148 | + <ToggleButtonText style={{ color: 'white' }}>Moon</ToggleButtonText> |
| 149 | + </ToggleButton> |
| 150 | + </ToggleButtonGroup> |
| 151 | + ); |
| 152 | +}; |
| 153 | +``` |
| 154 | + |
| 155 | +## Examples |
| 156 | + |
| 157 | +### Disabled |
| 158 | + |
| 159 | +The `ToggleButton` can be disabled. |
| 160 | + |
| 161 | +<Canvas of={Stories.Disabled} /> |
| 162 | + |
| 163 | +```tsx |
| 164 | +import { ToggleButton, ToggleButtonGroup } from '@utilitywarehouse/native-ui'; |
| 165 | + |
| 166 | +const MyComponent = () => { |
| 167 | + const [value, setValue] = useState('dark'); |
| 168 | + |
| 169 | + return ( |
| 170 | + <ToggleButtonGroup value={value} onChange={setValue} disabled> |
| 171 | + <ToggleButton value="light" label="Light" /> |
| 172 | + <ToggleButton value="dark" label="Dark" /> |
| 173 | + </ToggleButtonGroup> |
| 174 | + ); |
| 175 | +}; |
| 176 | +``` |
| 177 | + |
| 178 | +## With Icons |
| 179 | + |
| 180 | +The `ToggleButton` can be used with icons. |
| 181 | + |
| 182 | +<Canvas of={Stories.Icons} /> |
| 183 | + |
| 184 | +```tsx |
| 185 | +import { ToggleButton, ToggleButtonGroup, ToggleButtonIcon } from '@utilitywarehouse/native-ui'; |
| 186 | +import { EnvironmentMediumIcon, StarMediumIcon } from '@utilitywarehouse/native-icons'; |
| 187 | + |
| 188 | +const MyComponent = () => { |
| 189 | + const [value, setValue] = useState('light'); |
| 190 | + |
| 191 | + return ( |
| 192 | + <Box width="100%" maxWidth={200}> |
| 193 | + <ToggleButtonGroup value={value} onChange={setValue}> |
| 194 | + <ToggleButton value="light"> |
| 195 | + <ToggleButtonIcon as={EnvironmentMediumIcon} /> |
| 196 | + </ToggleButton> |
| 197 | + <ToggleButton value="dark"> |
| 198 | + <ToggleButtonIcon as={StarMediumIcon} /> |
| 199 | + </ToggleButton> |
| 200 | + </ToggleButtonGroup> |
| 201 | + </Box> |
| 202 | + ); |
| 203 | +}; |
| 204 | +``` |
0 commit comments