@khanacademy/wonder-blocks-button 2.9.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,810 @@
1
+ #### Example: kind
2
+
3
+ There are three `kind`s of buttons: `"primary"` (default), `"secondary"`, and
4
+ `"tertiary"`:
5
+ ```jsx
6
+ import Button from "@khanacademy/wonder-blocks-button";
7
+ import {View} from "@khanacademy/wonder-blocks-core";
8
+ import {StyleSheet} from "aphrodite";
9
+
10
+ const styles = StyleSheet.create({
11
+ row: {
12
+ flexDirection: "row",
13
+ },
14
+ button: {
15
+ marginRight: 10,
16
+ }
17
+ });
18
+
19
+ <View style={styles.row}>
20
+ <Button
21
+ style={styles.button}
22
+ onClick={(e) => window.alert("Hello, world!")}
23
+ >
24
+ Primary
25
+ </Button>
26
+ <Button
27
+ style={styles.button}
28
+ onClick={(e) => window.alert("Hello, world!")}
29
+ kind="secondary"
30
+ >
31
+ Secondary
32
+ </Button>
33
+ <Button
34
+ style={styles.button}
35
+ onClick={(e) => window.alert("Hello, world!")}
36
+ kind="tertiary"
37
+ >
38
+ Tertiary
39
+ </Button>
40
+ </View>
41
+ ```
42
+
43
+ #### Example: color
44
+
45
+ Buttons have a `color` that is either `"default"` (the default, as shown above) or `"destructive"` (as can seen below):
46
+
47
+ ```jsx
48
+ import Button from "@khanacademy/wonder-blocks-button";
49
+ import {View} from "@khanacademy/wonder-blocks-core";
50
+ import {StyleSheet} from "aphrodite";
51
+
52
+ const styles = StyleSheet.create({
53
+ row: {
54
+ flexDirection: "row",
55
+ },
56
+ button: {
57
+ marginRight: 10,
58
+ }
59
+ });
60
+
61
+ <View style={styles.row}>
62
+ <Button
63
+ style={styles.button}
64
+ onClick={(e) => window.alert("Hello, world!")}
65
+ color="destructive"
66
+ >
67
+ Primary
68
+ </Button>
69
+ <Button
70
+ style={styles.button}
71
+ onClick={(e) => window.alert("Hello, world!")}
72
+ kind="secondary"
73
+ color="destructive"
74
+ >
75
+ Secondary
76
+ </Button>
77
+ <Button
78
+ style={styles.button}
79
+ onClick={(e) => window.alert("Hello, world!")}
80
+ kind="tertiary"
81
+ color="destructive"
82
+ >
83
+ Tertiary
84
+ </Button>
85
+ </View>
86
+ ```
87
+
88
+ #### Example: disabled
89
+
90
+ Buttons can be `disabled`:
91
+
92
+ ⚠️ Buttons do not need an `aria-disabled` attribute, if it also has a `disabled` attribute.
93
+ Users operating the web page through a screen reader may not be able to fully evaluate the implied
94
+ behaviors of the button element itself.
95
+
96
+ Links:
97
+ - [Implicit ARIA semantics](https://www.w3.org/TR/wai-aria-1.1/#implicit_semantics)
98
+ - [Document conformance requirements](https://www.w3.org/TR/html-aria/#document-conformance-requirements-for-use-of-aria-attributes-in-html)
99
+
100
+
101
+ ```jsx
102
+ import Button from "@khanacademy/wonder-blocks-button";
103
+ import {View} from "@khanacademy/wonder-blocks-core";
104
+ import {StyleSheet} from "aphrodite";
105
+
106
+ const styles = StyleSheet.create({
107
+ row: {
108
+ flexDirection: "row",
109
+ },
110
+ button: {
111
+ marginRight: 10,
112
+ }
113
+ });
114
+
115
+ <View style={styles.row}>
116
+ <Button
117
+ style={styles.button}
118
+ onClick={(e) => window.alert("Hello, world!")}
119
+ disabled={true}
120
+ >
121
+ Primary
122
+ </Button>
123
+ <Button
124
+ style={styles.button}
125
+ href={"/foo"}
126
+ kind="secondary"
127
+ disabled={true}
128
+ >
129
+ Secondary
130
+ </Button>
131
+ <Button
132
+ style={styles.button}
133
+ onClick={(e) => window.alert("Hello, world!")}
134
+ kind="tertiary"
135
+ disabled={true}
136
+ >
137
+ Tertiary
138
+ </Button>
139
+ </View>
140
+ ```
141
+
142
+ #### Example: dark
143
+
144
+ Buttons on a `darkBlue` background should set `light` to `true`.
145
+ ```jsx
146
+ import Button from "@khanacademy/wonder-blocks-button";
147
+ import Color from "@khanacademy/wonder-blocks-color";
148
+ import {View} from "@khanacademy/wonder-blocks-core";
149
+ import {StyleSheet} from "aphrodite";
150
+
151
+ const styles = StyleSheet.create({
152
+ row: {
153
+ flexDirection: "row",
154
+ backgroundColor: Color.darkBlue,
155
+ padding: 10,
156
+ },
157
+ button: {
158
+ marginRight: 10,
159
+ }
160
+ });
161
+
162
+ <View style={styles.row}>
163
+ <Button
164
+ light={true}
165
+ style={styles.button}
166
+ onClick={(e) => window.alert("Hello, world!")}
167
+ >
168
+ Primary
169
+ </Button>
170
+ <Button
171
+ light={true}
172
+ style={styles.button}
173
+ onClick={(e) => window.alert("Hello, world!")}
174
+ kind="secondary"
175
+ >
176
+ Secondary
177
+ </Button>
178
+ <Button
179
+ light={true}
180
+ style={styles.button}
181
+ onClick={(e) => window.alert("Hello, world!")}
182
+ kind="tertiary"
183
+ >
184
+ Tertiary
185
+ </Button>
186
+ <Button
187
+ light={true}
188
+ style={styles.button}
189
+ onClick={(e) => window.alert("Hello, world!")}
190
+ disabled={true}
191
+ >
192
+ Primary
193
+ </Button>
194
+ <Button
195
+ light={true}
196
+ style={styles.button}
197
+ onClick={(e) => window.alert("Hello, world!")}
198
+ kind="secondary"
199
+ disabled={true}
200
+ >
201
+ Secondary
202
+ </Button>
203
+ <Button
204
+ light={true}
205
+ style={styles.button}
206
+ onClick={(e) => window.alert("Hello, world!")}
207
+ kind="tertiary"
208
+ disabled={true}
209
+ >
210
+ Tertiary
211
+ </Button>
212
+ </View>
213
+ ```
214
+
215
+ #### Example: size
216
+
217
+ Buttons have a `size` that's either `"medium"` (default), `"small"`, or `"xlarge"`.
218
+ ```js
219
+ import Button from "@khanacademy/wonder-blocks-button";
220
+ import {View} from "@khanacademy/wonder-blocks-core";
221
+ import {StyleSheet} from "aphrodite";
222
+
223
+ const styles = StyleSheet.create({
224
+ row: {
225
+ flexDirection: "row",
226
+ marginBottom: 8,
227
+ },
228
+ button: {
229
+ marginRight: 10,
230
+ }
231
+ });
232
+
233
+ <View>
234
+ <View style={styles.row}>
235
+ <Button
236
+ style={styles.button}
237
+ onClick={(e) => window.alert("Hello, world!")}
238
+ size="small"
239
+ >
240
+ Label
241
+ </Button>
242
+ <Button
243
+ style={styles.button}
244
+ onClick={(e) => window.alert("Hello, world!")}
245
+ kind="secondary"
246
+ size="small"
247
+ >
248
+ Label
249
+ </Button>
250
+ <Button
251
+ style={styles.button}
252
+ onClick={(e) => window.alert("Hello, world!")}
253
+ kind="tertiary"
254
+ size="small"
255
+ >
256
+ Label
257
+ </Button>
258
+ </View>
259
+ <View style={styles.row}>
260
+ <Button
261
+ style={styles.button}
262
+ onClick={(e) => window.alert("Hello, world!")}
263
+ size="medium"
264
+ >
265
+ Label
266
+ </Button>
267
+ <Button
268
+ style={styles.button}
269
+ onClick={(e) => window.alert("Hello, world!")}
270
+ kind="secondary"
271
+ size="medium"
272
+ >
273
+ Label
274
+ </Button>
275
+ <Button
276
+ style={styles.button}
277
+ onClick={(e) => window.alert("Hello, world!")}
278
+ kind="tertiary"
279
+ size="medium"
280
+ >
281
+ Label
282
+ </Button>
283
+ </View>
284
+ <View style={styles.row}>
285
+ <Button
286
+ style={styles.button}
287
+ onClick={(e) => window.alert("Hello, world!")}
288
+ size="xlarge"
289
+ >
290
+ Label
291
+ </Button>
292
+ <Button
293
+ style={styles.button}
294
+ onClick={(e) => window.alert("Hello, world!")}
295
+ kind="secondary"
296
+ size="xlarge"
297
+ >
298
+ Label
299
+ </Button>
300
+ <Button
301
+ style={styles.button}
302
+ onClick={(e) => window.alert("Hello, world!")}
303
+ kind="tertiary"
304
+ size="xlarge"
305
+ >
306
+ Label
307
+ </Button>
308
+ </View>
309
+ </View>
310
+ ```
311
+
312
+ #### Example: spinner
313
+
314
+ Buttons can show a `spinner`. This is useful when indicating to a user that
315
+ their input has been recognized but that the operation will take some time.
316
+ While the `spinner` property is set to `true` the button is disabled.
317
+
318
+ ```jsx
319
+ import Button from "@khanacademy/wonder-blocks-button";
320
+ import {View} from "@khanacademy/wonder-blocks-core";
321
+ import {StyleSheet} from "aphrodite";
322
+
323
+ const styles = StyleSheet.create({
324
+ row: {
325
+ flexDirection: "row",
326
+ alignItems: "center",
327
+ },
328
+ button: {
329
+ marginRight: 10,
330
+ }
331
+ });
332
+
333
+ <View style={styles.row}>
334
+ <Button spinner={true} aria-label="loading" size="xlarge" style={styles.button}>
335
+ Click me!
336
+ </Button>
337
+ <Button spinner={true} aria-label="loading" style={styles.button} href="/foo">
338
+ Click me!
339
+ </Button>
340
+ <Button spinner={true} aria-label="loading" size="small" style={styles.button}>
341
+ Click me!
342
+ </Button>
343
+ </View>
344
+ ```
345
+
346
+ #### Example: Navigation
347
+
348
+ ```jsx
349
+ import Button from "@khanacademy/wonder-blocks-button";
350
+ import {View} from "@khanacademy/wonder-blocks-core";
351
+ import {StyleSheet} from "aphrodite";
352
+
353
+ const styles = StyleSheet.create({
354
+ row: {
355
+ flexDirection: "row",
356
+ },
357
+ button: {
358
+ marginRight: 10,
359
+ }
360
+ });
361
+
362
+ <View style={styles.row}>
363
+ <Button
364
+ href="#button-1"
365
+ style={styles.button}
366
+ >
367
+ href
368
+ </Button>
369
+ <Button
370
+ kind="secondary"
371
+ onClick={(e) => window.alert("Hello, world!")}
372
+ style={styles.button}
373
+ >
374
+ onClick
375
+ </Button>
376
+ <Button
377
+ kind="tertiary"
378
+ href="#button-1"
379
+ onClick={(e) => window.alert("Hello, world!")}
380
+ style={styles.button}
381
+ >
382
+ both
383
+ </Button>
384
+ </View>
385
+ ```
386
+
387
+ #### Example: Navigation with React Router
388
+
389
+ Buttons do client-side navigation by default, if React Router exists:
390
+ ```jsx
391
+ import Button from "@khanacademy/wonder-blocks-button";
392
+ import {View} from "@khanacademy/wonder-blocks-core";
393
+ import {StyleSheet} from "aphrodite";
394
+ import {MemoryRouter, Route, Switch} from "react-router-dom";
395
+
396
+ const styles = StyleSheet.create({
397
+ row: {
398
+ flexDirection: "row",
399
+ alignItems: "center",
400
+ },
401
+ button: {
402
+ marginRight: 10,
403
+ }
404
+ });
405
+
406
+ // NOTE: In actual code you would use BrowserRouter instead
407
+ <MemoryRouter>
408
+ <View style={styles.row}>
409
+ <Button href="/foo" style={styles.button}>
410
+ Uses Client-side Nav
411
+ </Button>
412
+ <Button href="/foo" style={styles.button} skipClientNav>
413
+ Avoids Client-side Nav
414
+ </Button>
415
+ <Switch>
416
+ <Route path="/foo">
417
+ <View id="foo">Hello, world!</View>
418
+ </Route>
419
+ </Switch>
420
+ </View>
421
+ </MemoryRouter>
422
+ ```
423
+
424
+ #### Example: Navigation with async action
425
+
426
+ Sometimes you may need to perform an async action either before or during
427
+ navigation. This can be accomplished with `beforeNav` and `safeWithNav`
428
+ respectively.
429
+ ```jsx
430
+ import Button from "@khanacademy/wonder-blocks-button";
431
+ import {View} from "@khanacademy/wonder-blocks-core";
432
+ import {StyleSheet} from "aphrodite";
433
+ import {MemoryRouter, Route, Switch} from "react-router-dom";
434
+
435
+ const styles = StyleSheet.create({
436
+ row: {
437
+ flexDirection: "row",
438
+ alignItems: "center",
439
+ },
440
+ button: {
441
+ marginRight: 10,
442
+ }
443
+ });
444
+
445
+ // NOTE: In actual code you would use BrowserRouter instead
446
+ <MemoryRouter>
447
+ <View style={styles.row}>
448
+ <Button
449
+ href="/foo"
450
+ style={styles.button}
451
+ beforeNav={() => new Promise((resolve, reject) => {
452
+ setTimeout(resolve, 1000);
453
+ })}
454
+ >
455
+ Async action, client-side nav
456
+ </Button>
457
+ <Button
458
+ href="/foo"
459
+ style={styles.button}
460
+ skipClientNav={true}
461
+ beforeNav={() => new Promise((resolve, reject) => {
462
+ setTimeout(resolve, 1000);
463
+ })}
464
+ >
465
+ Async action, server-side nav
466
+ </Button>
467
+ <Button
468
+ href="https://google.com"
469
+ target="_blank"
470
+ style={styles.button}
471
+ skipClientNav={true}
472
+ beforeNav={() => new Promise((resolve, reject) => {
473
+ setTimeout(resolve, 1000);
474
+ })}
475
+ >
476
+ Async action, open URL in new tab
477
+ </Button>
478
+ <Switch>
479
+ <Route path="/foo">
480
+ <View id="foo">Hello, world!</View>
481
+ </Route>
482
+ </Switch>
483
+ </View>
484
+ </MemoryRouter>
485
+ ```
486
+
487
+ #### Example: Prevent navigation by calling e.preventDefault()
488
+
489
+ Sometimes you may need to perform an async action either before or during
490
+ navigation. This can be accomplished with `beforeNav` and `safeWithNav`
491
+ respectively.
492
+ ```jsx
493
+ import Button from "@khanacademy/wonder-blocks-button";
494
+ import {View} from "@khanacademy/wonder-blocks-core";
495
+ import {StyleSheet} from "aphrodite";
496
+ import {MemoryRouter, Route, Switch} from "react-router-dom";
497
+
498
+ const styles = StyleSheet.create({
499
+ row: {
500
+ flexDirection: "row",
501
+ alignItems: "center",
502
+ },
503
+ button: {
504
+ marginRight: 10,
505
+ }
506
+ });
507
+
508
+ // NOTE: In actual code you would use BrowserRouter instead
509
+ <MemoryRouter>
510
+ <View style={styles.row}>
511
+ <Button
512
+ href="/foo"
513
+ style={styles.button}
514
+ onClick={e => e.preventDefault()}
515
+ >
516
+ This button prevent navigation.
517
+ </Button>
518
+ <Switch>
519
+ <Route path="/foo">
520
+ <View id="foo">Hello, world!</View>
521
+ </Route>
522
+ </Switch>
523
+ </View>
524
+ </MemoryRouter>
525
+ ```
526
+
527
+ #### Example: style
528
+
529
+ Buttons can have a `style` props which supports width, position, margin,
530
+ and flex styles.
531
+
532
+ Buttons can have an icon on it's left side.
533
+
534
+ ```jsx
535
+ import Button from "@khanacademy/wonder-blocks-button";
536
+ import {View} from "@khanacademy/wonder-blocks-core";
537
+ import {StyleSheet} from "aphrodite";
538
+ import {icons} from "@khanacademy/wonder-blocks-icon";
539
+
540
+ const styles = StyleSheet.create({
541
+ row: {
542
+ flexDirection: "row",
543
+ marginBottom: 10,
544
+ },
545
+ button: {
546
+ marginRight: 10,
547
+ },
548
+ });
549
+
550
+ const kinds = ["primary", "secondary", "tertiary"];
551
+
552
+ <View>
553
+ <View style={styles.row}>
554
+ {
555
+ kinds.map((kind, idx) => (
556
+ <Button
557
+ kind={kind}
558
+ icon={icons.contentExercise}
559
+ style={styles.button}
560
+ key={idx}
561
+ >
562
+ {kind}
563
+ </Button>
564
+ ))
565
+ }
566
+ </View>
567
+ <View style={styles.row}>
568
+ {
569
+ kinds.map((kind, idx) => (
570
+ <Button
571
+ kind={kind}
572
+ icon={icons.contentExercise}
573
+ style={styles.button}
574
+ key={idx}
575
+ size="small"
576
+ >
577
+ {kind} small
578
+ </Button>
579
+ ))
580
+ }
581
+ </View>
582
+ </View>
583
+ ```
584
+
585
+ #### Example: "submit" buttons in forms
586
+
587
+ ```jsx
588
+ import Button from "@khanacademy/wonder-blocks-button";
589
+ import {View} from "@khanacademy/wonder-blocks-core";
590
+
591
+ <View>
592
+ <form onSubmit={() => alert("the form was submitted")}>
593
+ <Button type="submit">Submit</Button>
594
+ </form>
595
+ </View>
596
+ ```
597
+
598
+ ### Best Practices
599
+
600
+ In vertical layouts, buttons will stretch horizontally to fill the available
601
+ space. This is probably not what you want unless you're on a very narrow
602
+ screen.
603
+
604
+ ```jsx
605
+ import Button from "@khanacademy/wonder-blocks-button";
606
+ import {View} from "@khanacademy/wonder-blocks-core";
607
+
608
+ <View>
609
+ <Button>
610
+ Label
611
+ </Button>
612
+ </View>
613
+ ```
614
+
615
+ This can be corrected by applying appropriate flex styles to the container.
616
+
617
+ ```jsx
618
+ import Button from "@khanacademy/wonder-blocks-button";
619
+ import {View} from "@khanacademy/wonder-blocks-core";
620
+ import {StyleSheet} from "aphrodite";
621
+
622
+ const styles = StyleSheet.create({
623
+ column: {
624
+ alignItems: "flex-start",
625
+ },
626
+ row: {
627
+ flexDirection: "row",
628
+ },
629
+ gap: {
630
+ height: 16,
631
+ },
632
+ button: {
633
+ marginRight: 10,
634
+ },
635
+ });
636
+
637
+ <View>
638
+ <View style={styles.row}>
639
+ <Button>
640
+ Button in a row
641
+ </Button>
642
+ </View>
643
+ <View style={styles.gap} />
644
+ <View style={styles.column}>
645
+ <Button>
646
+ Button in a column
647
+ </Button>
648
+ </View>
649
+ </View>
650
+ ```
651
+
652
+ Layouts often specify a specific width of button. When implementing such
653
+ designs use `minWidth` instead of `width`. `minWidth` allows the button
654
+ to resize to fit the content whereas `width` does not. This is important
655
+ for international sites since sometimes strings for UI elements can be much
656
+ longer in other languages. Both of the buttons below have a "natural" width
657
+ of 144px. The one on the right is wider but it accommodates the full string
658
+ instead of wrapping it.
659
+ ```jsx
660
+ import Button from "@khanacademy/wonder-blocks-button";
661
+ import {View} from "@khanacademy/wonder-blocks-core";
662
+ import {StyleSheet} from "aphrodite";
663
+
664
+ const styles = StyleSheet.create({
665
+ row: {
666
+ flexDirection: "row",
667
+ },
668
+ gap: {
669
+ height: 16,
670
+ },
671
+ button: {
672
+ marginRight: 10,
673
+ minWidth: 144,
674
+ },
675
+ });
676
+
677
+ <View style={styles.row}>
678
+ <Button
679
+ style={styles.button}
680
+ kind="secondary"
681
+ >
682
+ label
683
+ </Button>
684
+ <Button
685
+ style={styles.button}
686
+ >
687
+ label in a different language
688
+ </Button>
689
+ </View>
690
+ ```
691
+
692
+ If the parent container of the button doesn't have enough room to accommodate
693
+ the width of the button, the text will truncate. This should ideally never
694
+ happen, but it's sometimes a necessary fallback.
695
+ ```jsx
696
+ import Button from "@khanacademy/wonder-blocks-button";
697
+ import {View} from "@khanacademy/wonder-blocks-core";
698
+ import {StyleSheet} from "aphrodite";
699
+
700
+ const styles = StyleSheet.create({
701
+ row: {
702
+ flexDirection: "row",
703
+ width: 300,
704
+ },
705
+ gap: {
706
+ height: 16,
707
+ },
708
+ button: {
709
+ marginRight: 10,
710
+ minWidth: 144,
711
+ },
712
+ });
713
+
714
+ <View style={styles.row}>
715
+ <Button
716
+ style={styles.button}
717
+ kind="secondary"
718
+ >
719
+ label
720
+ </Button>
721
+ <Button
722
+ style={styles.button}
723
+ >
724
+ label too long for the parent container
725
+ </Button>
726
+ </View>
727
+ ```
728
+
729
+ Only one button in a layout should be `primary`.
730
+ ```jsx
731
+ import Button from "@khanacademy/wonder-blocks-button";
732
+ import {View} from "@khanacademy/wonder-blocks-core";
733
+ import {StyleSheet} from "aphrodite";
734
+
735
+ const styles = StyleSheet.create({
736
+ row: {
737
+ flexDirection: "row",
738
+ },
739
+ button: {
740
+ marginRight: 10,
741
+ },
742
+ });
743
+
744
+ <View>
745
+ <View style={styles.row}>
746
+ <Button
747
+ style={styles.button}
748
+ kind="tertiary"
749
+ >
750
+ Tertiary
751
+ </Button>
752
+ <Button
753
+ style={styles.badButton}
754
+ >
755
+ Primary
756
+ </Button>
757
+ </View>
758
+ </View>
759
+ ```
760
+
761
+ When an action is going to take a while, show a spinner during that time.
762
+
763
+ ```jsx
764
+ import Button from "@khanacademy/wonder-blocks-button";
765
+ import {View} from "@khanacademy/wonder-blocks-core";
766
+ import {StyleSheet} from "aphrodite";
767
+
768
+ const styles = StyleSheet.create({
769
+ row: {
770
+ flexDirection: "row",
771
+ },
772
+ button: {
773
+ marginRight: 10,
774
+ },
775
+ });
776
+
777
+ class Example extends React.Component {
778
+ constructor(props) {
779
+ super(props);
780
+ this.state = {
781
+ waiting: false,
782
+ }
783
+ }
784
+
785
+ componentWillUnmount() {
786
+ this.timeout && this.timeout.clear();
787
+ }
788
+
789
+ handleClick() {
790
+ this.setState({waiting: true});
791
+ this.timeout = setTimeout(() => {
792
+ this.setState({waiting: false});
793
+ }, 2000);
794
+ }
795
+
796
+ render() {
797
+ return <View style={styles.row}>
798
+ <Button
799
+ spinner={this.state.waiting}
800
+ aria-label={this.state.waiting ? "waiting" : ""}
801
+ onClick={() => this.handleClick()}
802
+ >
803
+ Click me!
804
+ </Button>
805
+ </View>
806
+ }
807
+ }
808
+
809
+ <Example />
810
+ ```