@khanacademy/wonder-blocks-button 2.9.13 → 2.10.2

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.
@@ -1,75 +1,62 @@
1
1
  // @flow
2
2
  import * as React from "react";
3
+ import {StyleSheet} from "aphrodite";
4
+ import {withDesign} from "storybook-addon-designs";
3
5
  import {action} from "@storybook/addon-actions";
4
- import {text, radios, object, boolean} from "@storybook/addon-knobs";
5
6
 
7
+ import {MemoryRouter, Route, Switch} from "react-router-dom";
8
+
9
+ import Color from "@khanacademy/wonder-blocks-color";
6
10
  import {View} from "@khanacademy/wonder-blocks-core";
11
+ import {icons} from "@khanacademy/wonder-blocks-icon";
7
12
  import {Strut} from "@khanacademy/wonder-blocks-layout";
8
- import Color from "@khanacademy/wonder-blocks-color";
9
-
13
+ import Spacing from "@khanacademy/wonder-blocks-spacing";
10
14
  import type {StoryComponentType} from "@storybook/react";
11
15
  import Button from "./button.js";
12
16
 
17
+ import ComponentInfo from "../../../../.storybook/components/component-info.js";
18
+ import ButtonArgTypes from "./__docs__/button.argtypes.js";
19
+ import {name, version} from "../../package.json";
20
+
13
21
  export default {
14
- title: "Button",
22
+ title: "Navigation/Button",
23
+ component: Button,
24
+ parameters: {
25
+ componentSubtitle: ((
26
+ <ComponentInfo name={name} version={version} />
27
+ ): any),
28
+ },
29
+ decorators: [withDesign],
30
+ argTypes: ButtonArgTypes,
15
31
  };
16
32
 
17
- export const buttonsWithKnobs: StoryComponentType = () => {
18
- const children = text("children", "Hello, world!");
19
- const kind = radios(
20
- "kind",
21
- {
22
- "primary (default)": "primary",
23
- secondary: "secondary",
24
- tertiary: "tertiary",
25
- },
26
- "primary",
27
- );
28
- const color = radios(
29
- "color",
30
- {
31
- "default (default)": "default",
32
- destructive: "destructive",
33
- },
34
- "default",
35
- );
36
- const size = radios(
37
- "size",
38
- {large: "large", "medium (default)": "medium", small: "small"},
39
- "medium",
40
- );
41
- const light = boolean("light", false);
42
- const disabled = boolean("disabled", false);
43
- const style = object("style", {maxWidth: 200});
44
-
45
- return (
46
- <Button
47
- kind={kind}
48
- color={color}
49
- size={size}
50
- light={light}
51
- disabled={disabled}
52
- style={style}
53
- onClick={action("onClick")}
54
- >
55
- {children}
56
- </Button>
57
- );
33
+ const Template = (args) => <Button {...args} />;
34
+
35
+ export const DefaultButton: StoryComponentType = Template.bind({});
36
+
37
+ DefaultButton.args = {
38
+ children: "Hello, world!",
39
+ kind: "primary",
40
+ color: "default",
41
+ size: "medium",
42
+ light: false,
43
+ disabled: false,
44
+ style: {maxWidth: 200},
45
+ onClick: () => {},
58
46
  };
59
47
 
60
- buttonsWithKnobs.story = {
61
- parameters: {
62
- options: {
63
- showAddonPanel: true,
64
- },
65
- chromatic: {
66
- // We already have screenshots of other stories that cover more of the button states
67
- disable: true,
68
- },
48
+ DefaultButton.parameters = {
49
+ design: {
50
+ type: "figma",
51
+ url: "https://www.figma.com/file/VbVu3h2BpBhH80niq101MHHE/Wonder-Blocks-(Web)?node-id=401%3A307",
52
+ },
53
+ chromatic: {
54
+ // We already have screenshots of other stories that cover more of the button states
55
+ disableSnapshot: true,
69
56
  },
70
57
  };
71
58
 
72
- export const basicButtons: StoryComponentType = () => (
59
+ export const BasicButtons: StoryComponentType = () => (
73
60
  <View>
74
61
  <View style={{flexDirection: "row"}}>
75
62
  <Button onClick={() => {}}>Hello, world!</Button>
@@ -113,7 +100,45 @@ export const basicButtons: StoryComponentType = () => (
113
100
  </View>
114
101
  );
115
102
 
116
- export const darkBackgroundButtons: StoryComponentType = () => (
103
+ BasicButtons.parameters = {
104
+ docs: {
105
+ storyDescription:
106
+ "There are three kinds of buttons: `primary` (default), `secondary`, and `tertiary`.",
107
+ },
108
+ };
109
+
110
+ export const ButtonsWithColors: StoryComponentType = () => (
111
+ <View style={styles.row}>
112
+ <Button style={styles.button} onClick={() => {}} color="destructive">
113
+ Primary
114
+ </Button>
115
+ <Button
116
+ style={styles.button}
117
+ onClick={() => {}}
118
+ kind="secondary"
119
+ color="destructive"
120
+ >
121
+ Secondary
122
+ </Button>
123
+ <Button
124
+ style={styles.button}
125
+ onClick={() => {}}
126
+ kind="tertiary"
127
+ color="destructive"
128
+ >
129
+ Tertiary
130
+ </Button>
131
+ </View>
132
+ );
133
+
134
+ ButtonsWithColors.parameters = {
135
+ docs: {
136
+ storyDescription:
137
+ "Buttons have a `color` that is either `default` (the default, as shown above) or `destructive` (as can seen below):",
138
+ },
139
+ };
140
+
141
+ export const DarkBackgroundButtons: StoryComponentType = () => (
117
142
  <View style={{backgroundColor: Color.darkBlue}}>
118
143
  <View style={{flexDirection: "row"}}>
119
144
  <Button onClick={() => {}} light={true}>
@@ -179,51 +204,92 @@ export const darkBackgroundButtons: StoryComponentType = () => (
179
204
  </View>
180
205
  );
181
206
 
182
- darkBackgroundButtons.story = {
183
- parameters: {
184
- backgrounds: [{name: "darkBlue", value: Color.darkBlue, default: true}],
207
+ DarkBackgroundButtons.parameters = {
208
+ backgrounds: {
209
+ default: "darkBlue",
210
+ },
211
+ docs: {
212
+ storyDescription:
213
+ "Buttons on a `darkBlue` background should set `light` to `true`.",
185
214
  },
186
215
  };
187
216
 
188
- export const smallButtons: StoryComponentType = () => (
189
- <View style={{flexDirection: "row"}}>
190
- <Button onClick={() => {}} size="small">
191
- Hello, world!
192
- </Button>
193
- <Strut size={16} />
194
- <Button onClick={() => {}} size="small" kind="secondary">
195
- Hello, world!
196
- </Button>
197
- <Strut size={16} />
198
- <Button onClick={() => {}} size="small" kind="tertiary">
199
- Hello, world!
200
- </Button>
201
- </View>
202
- );
203
-
204
- export const xlargeButtons: StoryComponentType = () => (
205
- <View style={{flexDirection: "row"}}>
206
- <Button onClick={() => {}} size="xlarge">
207
- Hello, world!
208
- </Button>
209
- <Strut size={16} />
210
- <Button onClick={() => {}} size="xlarge" kind="secondary">
211
- Hello, world!
212
- </Button>
213
- <Strut size={16} />
214
- <Button onClick={() => {}} size="xlarge" kind="tertiary">
215
- Hello, world!
216
- </Button>
217
+ export const ButtonsWithSize: StoryComponentType = () => (
218
+ <View>
219
+ <View style={styles.row}>
220
+ <Button style={styles.button} onClick={() => {}} size="small">
221
+ Label
222
+ </Button>
223
+ <Button
224
+ style={styles.button}
225
+ onClick={() => {}}
226
+ kind="secondary"
227
+ size="small"
228
+ >
229
+ Label
230
+ </Button>
231
+ <Button
232
+ style={styles.button}
233
+ onClick={() => {}}
234
+ kind="tertiary"
235
+ size="small"
236
+ >
237
+ Label
238
+ </Button>
239
+ </View>
240
+ <View style={styles.row}>
241
+ <Button style={styles.button} onClick={() => {}} size="medium">
242
+ Label
243
+ </Button>
244
+ <Button
245
+ style={styles.button}
246
+ onClick={() => {}}
247
+ kind="secondary"
248
+ size="medium"
249
+ >
250
+ Label
251
+ </Button>
252
+ <Button
253
+ style={styles.button}
254
+ onClick={() => {}}
255
+ kind="tertiary"
256
+ size="medium"
257
+ >
258
+ Label
259
+ </Button>
260
+ </View>
261
+ <View style={styles.row}>
262
+ <Button style={styles.button} onClick={() => {}} size="xlarge">
263
+ Label
264
+ </Button>
265
+ <Button
266
+ style={styles.button}
267
+ onClick={() => {}}
268
+ kind="secondary"
269
+ size="xlarge"
270
+ >
271
+ Label
272
+ </Button>
273
+ <Button
274
+ style={styles.button}
275
+ onClick={() => {}}
276
+ kind="tertiary"
277
+ size="xlarge"
278
+ >
279
+ Label
280
+ </Button>
281
+ </View>
217
282
  </View>
218
283
  );
219
284
 
220
- export const longLabelsAreEllipsized: StoryComponentType = () => (
221
- <Button onClick={() => {}} style={{maxWidth: 200}}>
222
- label too long for the parent container
223
- </Button>
224
- );
285
+ ButtonsWithSize.parameters = {
286
+ docs: {
287
+ storyDescription:
288
+ "Buttons have a size that's either `medium` (default), `small`, or `xlarge`.",
289
+ },
290
+ };
225
291
 
226
- export const buttonWithSpinner: StoryComponentType = () => (
292
+ export const ButtonWithSpinner: StoryComponentType = () => (
227
293
  <View style={{flexDirection: "row"}}>
228
294
  <Button
229
295
  onClick={() => {}}
@@ -249,7 +315,241 @@ export const buttonWithSpinner: StoryComponentType = () => (
249
315
  </View>
250
316
  );
251
317
 
252
- export const submitButtonInForm: StoryComponentType = () => (
318
+ ButtonWithSpinner.parameters = {
319
+ docs: {
320
+ storyDescription:
321
+ "Buttons can show a spinner. This is useful when indicating to a user that their input has been recognized but that the operation will take some time. While the spinner property is set to true the button is disabled.",
322
+ },
323
+ };
324
+
325
+ export const ButtonsWithRouter: StoryComponentType = () => (
326
+ <MemoryRouter>
327
+ <View style={styles.row}>
328
+ <Button href="/foo" style={styles.button}>
329
+ Uses Client-side Nav
330
+ </Button>
331
+ <Button href="/foo" style={styles.button} skipClientNav>
332
+ Avoids Client-side Nav
333
+ </Button>
334
+ <Switch>
335
+ <Route path="/foo">
336
+ <View id="foo">Hello, world!</View>
337
+ </Route>
338
+ </Switch>
339
+ </View>
340
+ </MemoryRouter>
341
+ );
342
+
343
+ ButtonsWithRouter.storyName = "Navigation with React Router";
344
+
345
+ ButtonsWithRouter.parameters = {
346
+ docs: {
347
+ storyDescription:
348
+ "Buttons do client-side navigation by default, if React Router exists:",
349
+ },
350
+ chromatic: {
351
+ disableSnapshot: true,
352
+ },
353
+ };
354
+
355
+ export const BeforeNavCallbacks: StoryComponentType = () => (
356
+ <MemoryRouter>
357
+ <View style={styles.row}>
358
+ <Button
359
+ href="/foo"
360
+ style={styles.button}
361
+ beforeNav={() =>
362
+ new Promise((resolve, reject) => {
363
+ setTimeout(resolve, 1000);
364
+ })
365
+ }
366
+ >
367
+ beforeNav, client-side nav
368
+ </Button>
369
+ <Button
370
+ href="/foo"
371
+ style={styles.button}
372
+ skipClientNav={true}
373
+ beforeNav={() =>
374
+ new Promise((resolve, reject) => {
375
+ setTimeout(resolve, 1000);
376
+ })
377
+ }
378
+ >
379
+ beforeNav, server-side nav
380
+ </Button>
381
+ <Button
382
+ href="https://google.com"
383
+ style={styles.button}
384
+ skipClientNav={true}
385
+ beforeNav={() =>
386
+ new Promise((resolve, reject) => {
387
+ setTimeout(resolve, 1000);
388
+ })
389
+ }
390
+ >
391
+ beforeNav, open URL in new tab
392
+ </Button>
393
+ <Switch>
394
+ <Route path="/foo">
395
+ <View id="foo">Hello, world!</View>
396
+ </Route>
397
+ </Switch>
398
+ </View>
399
+ </MemoryRouter>
400
+ );
401
+
402
+ BeforeNavCallbacks.storyName = "beforeNav Callbacks";
403
+
404
+ BeforeNavCallbacks.parameters = {
405
+ docs: {
406
+ storyDescription:
407
+ "These buttons always wait until the async callback code completes before starting navigation.",
408
+ },
409
+ chromatic: {
410
+ disableSnapshot: true,
411
+ },
412
+ };
413
+
414
+ export const SafeWithNavCallbacks: StoryComponentType = () => (
415
+ <MemoryRouter>
416
+ <View style={styles.row}>
417
+ <Button
418
+ href="/foo"
419
+ style={styles.button}
420
+ safeWithNav={() =>
421
+ new Promise((resolve, reject) => {
422
+ setTimeout(resolve, 1000);
423
+ })
424
+ }
425
+ >
426
+ safeWithNav, client-side nav
427
+ </Button>
428
+ <Button
429
+ href="/foo"
430
+ style={styles.button}
431
+ skipClientNav={true}
432
+ safeWithNav={() =>
433
+ new Promise((resolve, reject) => {
434
+ setTimeout(resolve, 1000);
435
+ })
436
+ }
437
+ >
438
+ safeWithNav, server-side nav
439
+ </Button>
440
+ <Button
441
+ href="https://google.com"
442
+ style={styles.button}
443
+ skipClientNav={true}
444
+ safeWithNav={() =>
445
+ new Promise((resolve, reject) => {
446
+ setTimeout(resolve, 1000);
447
+ })
448
+ }
449
+ >
450
+ safeWithNav, open URL in new tab
451
+ </Button>
452
+ <Switch>
453
+ <Route path="/foo">
454
+ <View id="foo">Hello, world!</View>
455
+ </Route>
456
+ </Switch>
457
+ </View>
458
+ </MemoryRouter>
459
+ );
460
+
461
+ SafeWithNavCallbacks.storyName = "safeWithNav Callbacks";
462
+
463
+ SafeWithNavCallbacks.parameters = {
464
+ docs: {
465
+ storyDescription:
466
+ "If the `onClick` callback calls `preventDefault()`, then navigation will not occur.",
467
+ },
468
+ chromatic: {
469
+ disableSnapshot: true,
470
+ },
471
+ };
472
+
473
+ export const PreventNavigation: StoryComponentType = () => (
474
+ <MemoryRouter>
475
+ <View style={styles.row}>
476
+ <Button
477
+ href="/foo"
478
+ style={styles.button}
479
+ onClick={(e) => {
480
+ action("clicked")(e);
481
+ e.preventDefault();
482
+ }}
483
+ >
484
+ This button prevents navigation.
485
+ </Button>
486
+ <Switch>
487
+ <Route path="/foo">
488
+ <View id="foo">Hello, world!</View>
489
+ </Route>
490
+ </Switch>
491
+ </View>
492
+ </MemoryRouter>
493
+ );
494
+
495
+ PreventNavigation.storyName =
496
+ "Prevent navigation by calling e.preventDefault()";
497
+
498
+ PreventNavigation.parameters = {
499
+ docs: {
500
+ storyDescription:
501
+ "Sometimes you may need to perform an async action either before or during navigation. This can be accomplished with `beforeNav` and `safeWithNav` respectively.",
502
+ },
503
+ chromatic: {
504
+ disableSnapshot: true,
505
+ },
506
+ };
507
+
508
+ const kinds = ["primary", "secondary", "tertiary"];
509
+
510
+ export const ButtonsWithIcons: StoryComponentType = () => (
511
+ <View>
512
+ <View style={styles.row}>
513
+ {kinds.map((kind, idx) => (
514
+ <Button
515
+ kind={kind}
516
+ icon={icons.contentExercise}
517
+ style={styles.button}
518
+ key={idx}
519
+ >
520
+ {kind}
521
+ </Button>
522
+ ))}
523
+ </View>
524
+ <View style={styles.row}>
525
+ {kinds.map((kind, idx) => (
526
+ <Button
527
+ kind={kind}
528
+ icon={icons.contentExercise}
529
+ style={styles.button}
530
+ key={idx}
531
+ size="small"
532
+ >
533
+ {`${kind} small`}
534
+ </Button>
535
+ ))}
536
+ </View>
537
+ </View>
538
+ );
539
+
540
+ ButtonsWithIcons.parameters = {
541
+ docs: {
542
+ storyDescription: "Buttons can have an icon on it's left side.",
543
+ },
544
+ };
545
+
546
+ export const LongLabelsAreEllipsized: StoryComponentType = () => (
547
+ <Button onClick={() => {}} style={{maxWidth: 200}}>
548
+ label too long for the parent container
549
+ </Button>
550
+ );
551
+
552
+ export const SubmitButtonInForm: StoryComponentType = () => (
253
553
  <form
254
554
  onSubmit={(e) => {
255
555
  e.preventDefault();
@@ -263,14 +563,23 @@ export const submitButtonInForm: StoryComponentType = () => (
263
563
  </form>
264
564
  );
265
565
 
266
- submitButtonInForm.story = {
267
- parameters: {
268
- options: {
269
- showAddonPanel: true,
270
- },
271
- chromatic: {
272
- // We already have screenshots of other stories that cover more of the button states
273
- disable: true,
274
- },
566
+ SubmitButtonInForm.parameters = {
567
+ options: {
568
+ showAddonPanel: true,
569
+ },
570
+ chromatic: {
571
+ // We already have screenshots of other stories that cover more of the
572
+ // button states.
573
+ disableSnapshot: true,
275
574
  },
276
575
  };
576
+
577
+ const styles = StyleSheet.create({
578
+ row: {
579
+ flexDirection: "row",
580
+ marginBottom: Spacing.xSmall_8,
581
+ },
582
+ button: {
583
+ marginRight: Spacing.xSmall_8,
584
+ },
585
+ });