UNPKG

5.28 kBMarkdownView Raw
1---
2title: Button
3storybookPath: forms-buttons-button--default
4isExperimentalPackage: true
5---
6
7Buttons are clickable elements that are used to trigger actions. They
8communicate calls to action to the user and allow users to interact with pages
9in a variety of ways. Button labels express what action will occur when the user
10interacts with it.
11
12## Tone
13
14Button tones can be broken up into two types; decorative and semantic.
15
16For destructive actions like “delete” you should use the semantic `tone` of
17`critical`.
18
19For buttons that have no semantic action type (more common on marketing pages)
20use one of our decorative `tones`.
21
22Defaults to `primary`.
23
24```jsx live
25<Stack gap="large">
26 <Text weight="semibold">Decorative tones</Text>
27 <Inline gap="small">
28 <Button tone="primary">Primary</Button>
29 <Button tone="secondary">Secondary</Button>
30 </Inline>
31 <Divider />
32 <Text weight="semibold">Semantic tones</Text>
33 <Inline gap="small">
34 <Button tone="neutral">Neutral</Button>
35 <Button tone="positive">Positive</Button>
36 <Button tone="critical">Critical</Button>
37 </Inline>
38</Stack>
39```
40
41## Prominence
42
43The appearance of the button can be customised with the prominence prop. Valid
44options are: `low` and `high`.
45
46Defaults to `high`.
47
48```jsx live
49const baseButtonTones = [
50 { label: 'Primary', tone: 'primary' },
51 { label: 'Secondary', tone: 'secondary' },
52 { label: 'Neutral', tone: 'neutral' },
53 { label: 'Positive', tone: 'positive' },
54 { label: 'Critical', tone: 'critical' },
55];
56
57const extraButtonTones = [
58 { label: 'Caution', tone: 'caution' },
59 { label: 'Informative', tone: 'info' },
60];
61
62return (
63 <Stack gap="large" dividers>
64 <Stack gap="large">
65 <Text weight="semibold">High prominence</Text>
66 <Inline gap="small">
67 {baseButtonTones.map(({ label, tone }) => (
68 <Button key={label} tone={tone} prominence="high">
69 <LightBulbIcon />
70 {label}
71 </Button>
72 ))}
73 </Inline>
74 </Stack>
75 <Stack gap="large">
76 <Text weight="semibold">Low prominence</Text>
77 <Inline gap="small">
78 {baseButtonTones.concat(extraButtonTones).map(({ label, tone }) => (
79 <Button key={label} tone={tone} prominence="low">
80 <LightBulbIcon />
81 {label}
82 </Button>
83 ))}
84 </Inline>
85 </Stack>
86 <Stack gap="large">
87 <Text weight="semibold">None prominence</Text>
88 <Inline gap="small">
89 {baseButtonTones.concat(extraButtonTones).map(({ label, tone }) => (
90 <Button key={label} tone={tone} prominence="none">
91 <LightBulbIcon />
92 {label}
93 </Button>
94 ))}
95 </Inline>
96 </Stack>
97 </Stack>
98);
99```
100
101## Size
102
103Button's are available in two size: `medium` and `large`.
104
105Defaults to `medium`.
106
107```jsx live
108<Inline gap="small">
109 <Button size="medium">Medium</Button>
110 <Button size="large">Large</Button>
111</Inline>
112```
113
114## Icons
115
116Icons can be placed next to labels to both clarify an action and call attention
117to a button.
118
119```jsx live
120<Inline gap="small">
121 <Button>
122 <DownloadIcon />
123 Download
124 </Button>
125 <Button tone="critical">
126 <TrashIcon />
127 Delete
128 </Button>
129</Inline>
130```
131
132### Icon only
133
134When using buttons that contain only an icon, you must provide a `label` for
135users of assistive technology.
136
137```jsx live
138<Inline gap="small">
139 <Button label="Download PDF">
140 <DownloadIcon />
141 </Button>
142 <Button tone="critical" label="Delete item">
143 <TrashIcon />
144 </Button>
145 <Button tone="neutral" label="Dismiss">
146 <XIcon size="xxsmall" />
147 </Button>
148</Inline>
149```
150
151## Loading
152
153Buttons have an optional `loading` prop to indicate that an action is in
154progress. When this is true a spinner will be displayed.
155
156Note: buttons will not be interative when `loading` is true.
157
158```jsx live
159const [loading, setLoading] = React.useState(false);
160const toggle = event => setLoading(event.target.checked);
161
162return (
163 <Stack gap="large">
164 <Checkbox size="medium" checked={loading} onChange={toggle}>
165 <Text>Toggle loading state</Text>
166 </Checkbox>
167 <Inline gap="large">
168 <Button label="Download" loading={loading}>
169 <DownloadIcon />
170 </Button>
171 <Button loading={loading}>
172 <DownloadIcon />
173 Download
174 </Button>
175 </Inline>
176 <Inline gap="large">
177 <Button label="Download" size="large" loading={loading}>
178 <DownloadIcon />
179 </Button>
180 <Button size="large" loading={loading}>
181 <DownloadIcon />
182 Download
183 </Button>
184 </Inline>
185 </Stack>
186);
187```
188
189## ButtonLink
190
191The appearance of a button, with the semantics of a link — shares `Button` API,
192with the exception of `href` vs `onClick` props.
193
194```jsx live
195<Text>
196 <ButtonLink href="#">Visually a link, with button semantics</ButtonLink>
197</Text>
198```
199
200## BaseButton
201
202Unstyled button primitive that:
203
204- Forwards the button ref
205- Provides a default type of `button` (so it doesn't accidently submit forms if
206 left off)
207- Prevents `onClick` from firing when disabled without disabling the button
208- Forces focus of the underlying button when clicked (to address a bug in
209 Safari)
210
211## Button Props
212
213<PropsTable displayName="Button" />
214
215## Button Link Props
216
217<PropsTable displayName="ButtonLink" />