@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,347 @@
1
+ // @flow
2
+ import * as React from "react";
3
+ import * as PropTypes from "prop-types";
4
+
5
+ import {getClickableBehavior} from "@khanacademy/wonder-blocks-clickable";
6
+ import type {AriaProps, StyleType} from "@khanacademy/wonder-blocks-core";
7
+ import type {IconAsset} from "@khanacademy/wonder-blocks-icon";
8
+ import ButtonCore from "./button-core.js";
9
+
10
+ export type SharedProps = {|
11
+ /**
12
+ * aria-label should be used when `spinner={true}` to let people using screen
13
+ * readers that the action taken by clicking the button will take some
14
+ * time to complete.
15
+ */
16
+ ...$Rest<AriaProps, {|"aria-disabled": "true" | "false" | void|}>,
17
+
18
+ /**
19
+ * Text to appear on the button.
20
+ */
21
+ children: string,
22
+
23
+ /**
24
+ * An icon, displayed to the left of the title.
25
+ */
26
+ icon?: IconAsset,
27
+
28
+ /**
29
+ * If true, replaces the contents with a spinner.
30
+ *
31
+ * Note: setting this prop to `true` will disable the button.
32
+ *
33
+ * TODO(kevinb): support spinner + light once we have designs
34
+ */
35
+ spinner: boolean,
36
+
37
+ /**
38
+ * The color of the button, either blue or red.
39
+ */
40
+ color: "default" | "destructive",
41
+
42
+ /**
43
+ * The kind of the button, either primary, secondary, or tertiary.
44
+ *
45
+ * In default state:
46
+ *
47
+ * - Primary buttons have background colors
48
+ * - Secondary buttons have a border and no background color
49
+ * - Tertiary buttons have no background or border
50
+ */
51
+ kind: "primary" | "secondary" | "tertiary",
52
+
53
+ /**
54
+ * Whether the button is on a dark/colored background.
55
+ *
56
+ * Sets primary button background color to white, and secondary and
57
+ * tertiary button title to color.
58
+ */
59
+ light: boolean,
60
+
61
+ /**
62
+ * The size of the button. "medium" = height: 40; "small" = height: 32;
63
+ * "xlarge" = height: 60;
64
+ */
65
+ size: "medium" | "small" | "xlarge",
66
+
67
+ /**
68
+ * Whether the button is disabled.
69
+ */
70
+ disabled: boolean,
71
+
72
+ /**
73
+ * An optional id attribute.
74
+ */
75
+ id?: string,
76
+
77
+ /**
78
+ * Test ID used for e2e testing.
79
+ */
80
+ testId?: string,
81
+
82
+ /**
83
+ * Specifies the type of relationship between the current document and the
84
+ * linked document. Should only be used when `href` is specified. This
85
+ * defaults to "noopener noreferrer" when `target="_blank"`, but can be
86
+ * overridden by setting this prop to something else.
87
+ */
88
+ rel?: string,
89
+
90
+ /**
91
+ * A target destination window for a link to open in. Should only be used
92
+ * when `href` is specified.
93
+ */
94
+ target?: "_blank",
95
+
96
+ /**
97
+ * Set the tabindex attribute on the rendered element.
98
+ */
99
+ tabIndex?: number,
100
+
101
+ /**
102
+ * Whether to avoid using client-side navigation.
103
+ *
104
+ * If the URL passed to href is local to the client-side, e.g.
105
+ * /math/algebra/eval-exprs, then it tries to use react-router-dom's Link
106
+ * component which handles the client-side navigation. You can set
107
+ * `skipClientNav` to true avoid using client-side nav entirely.
108
+ *
109
+ * NOTE: All URLs containing a protocol are considered external, e.g.
110
+ * https://khanacademy.org/math/algebra/eval-exprs will trigger a full
111
+ * page reload.
112
+ */
113
+ skipClientNav?: boolean,
114
+
115
+ /**
116
+ * Optional custom styles.
117
+ */
118
+ style?: StyleType,
119
+ // TODO(yejia): use this if ADR #47 has been implemented
120
+ /*
121
+ style?: Style<Exact<{
122
+ width?: number | string
123
+ position: Position,
124
+ ...MarginStyles,
125
+ ...FlexItemStyles,
126
+ }>>,
127
+ */
128
+
129
+ /**
130
+ * Adds CSS classes to the Button.
131
+ */
132
+ className?: string,
133
+
134
+ // NOTE(jeresig): Currently React Docgen (used by Styleguidist) doesn't
135
+ // support ... inside of an exact object type. Thus we had to move the
136
+ // following propers into this SharedProps, even though they should be
137
+ // external. Once that's fixed we can split them back apart.
138
+
139
+ /**
140
+ * Function to call when button is clicked.
141
+ *
142
+ * This callback should be used for things like marking BigBingo
143
+ * conversions. It should NOT be used to redirect to a different URL or to
144
+ * prevent navigation via e.preventDefault(). The event passed to this
145
+ * handler will have its preventDefault() and stopPropagation() methods
146
+ * stubbed out.
147
+ *
148
+ * Note: onClick is optional if href is present, but must be defined if
149
+ * href is not
150
+ */
151
+ onClick?: (e: SyntheticEvent<>) => mixed,
152
+ |};
153
+
154
+ // We structure the props in this way to ensure that whenever we're using
155
+ // beforeNav or safeWithNav that we're also using href. We also need to specify
156
+ // a number of different variations to avoid ambigious situations where flow
157
+ // finds more than one valid object type in the disjoint union.
158
+ type Props =
159
+ | {|
160
+ ...SharedProps,
161
+
162
+ /**
163
+ * URL to navigate to.
164
+ */
165
+ href?: string,
166
+ |}
167
+ | {|
168
+ ...SharedProps,
169
+
170
+ /**
171
+ * Used for buttons within <form>s.
172
+ */
173
+ type: "submit",
174
+ |}
175
+ | {|
176
+ ...SharedProps,
177
+
178
+ href: string,
179
+
180
+ /**
181
+ * Run async code before navigating. If the promise returned rejects then
182
+ * navigation will not occur.
183
+ *
184
+ * If both safeWithNav and beforeNav are provided, beforeNav will be run
185
+ * first and safeWithNav will only be run if beforeNav does not reject.
186
+ */
187
+ beforeNav: () => Promise<mixed>,
188
+ |}
189
+ | {|
190
+ ...SharedProps,
191
+
192
+ href: string,
193
+
194
+ /**
195
+ * Run async code in the background while client-side navigating. If the
196
+ * browser does a full page load navigation, the callback promise must be
197
+ * settled before the navigation will occur. Errors are ignored so that
198
+ * navigation is guaranteed to succeed.
199
+ */
200
+ safeWithNav: () => Promise<mixed>,
201
+ |}
202
+ | {|
203
+ ...SharedProps,
204
+
205
+ href: string,
206
+
207
+ /**
208
+ * Run async code before navigating. If the promise returned rejects then
209
+ * navigation will not occur.
210
+ *
211
+ * If both safeWithNav and beforeNav are provided, beforeNav will be run
212
+ * first and safeWithNav will only be run if beforeNav does not reject.
213
+ */
214
+ beforeNav: () => Promise<mixed>,
215
+
216
+ /**
217
+ * Run async code in the background while client-side navigating. If the
218
+ * browser does a full page load navigation, the callback promise must be
219
+ * settled before the navigation will occur. Errors are ignored so that
220
+ * navigation is guaranteed to succeed.
221
+ */
222
+ safeWithNav: () => Promise<mixed>,
223
+ |};
224
+
225
+ type ContextTypes = {|
226
+ router: $FlowFixMe,
227
+ |};
228
+
229
+ type DefaultProps = {|
230
+ color: $PropertyType<Props, "color">,
231
+ kind: $PropertyType<Props, "kind">,
232
+ light: $PropertyType<Props, "light">,
233
+ size: $PropertyType<Props, "size">,
234
+ disabled: $PropertyType<Props, "disabled">,
235
+ spinner: $PropertyType<Props, "spinner">,
236
+ |};
237
+
238
+ /**
239
+ * Reusable button component.
240
+ *
241
+ * Consisting of a [`ClickableBehavior`](#clickablebehavior) surrounding a
242
+ * `ButtonCore`. `ClickableBehavior` handles interactions and state changes.
243
+ * `ButtonCore` is a stateless component which displays the different states
244
+ * the `Button` can take.
245
+ *
246
+ * Example usage:
247
+ * ```jsx
248
+ * <Button
249
+ * onClick={(e) => console.log("Hello, world!")}
250
+ * >
251
+ * Label
252
+ * </Button>
253
+ * ```
254
+ */
255
+ export default class Button extends React.Component<Props> {
256
+ static contextTypes: ContextTypes = {router: PropTypes.any};
257
+
258
+ static defaultProps: DefaultProps = {
259
+ color: "default",
260
+ kind: "primary",
261
+ light: false,
262
+ size: "medium",
263
+ disabled: false,
264
+ spinner: false,
265
+ };
266
+
267
+ render(): React.Node {
268
+ const {
269
+ href = undefined,
270
+ type = undefined,
271
+ children,
272
+ skipClientNav,
273
+ spinner,
274
+ disabled,
275
+ onClick,
276
+ beforeNav = undefined,
277
+ safeWithNav = undefined,
278
+ tabIndex,
279
+ target,
280
+ rel,
281
+ ...sharedButtonCoreProps
282
+ } = this.props;
283
+
284
+ const ClickableBehavior = getClickableBehavior(
285
+ href,
286
+ skipClientNav,
287
+ this.context.router,
288
+ );
289
+
290
+ const renderProp = (
291
+ state,
292
+ {tabIndex: clickableTabIndex, ...restChildProps},
293
+ ) => {
294
+ return (
295
+ <ButtonCore
296
+ {...sharedButtonCoreProps}
297
+ {...state}
298
+ {...restChildProps}
299
+ disabled={disabled}
300
+ spinner={spinner || state.waiting}
301
+ skipClientNav={skipClientNav}
302
+ href={href}
303
+ target={target}
304
+ type={type}
305
+ // If tabIndex is provide to the component we allow
306
+ // it to override the tabIndex provide to use by
307
+ // ClickableBehavior.
308
+ tabIndex={tabIndex || clickableTabIndex}
309
+ >
310
+ {children}
311
+ </ButtonCore>
312
+ );
313
+ };
314
+
315
+ if (beforeNav) {
316
+ return (
317
+ <ClickableBehavior
318
+ disabled={spinner || disabled}
319
+ href={href}
320
+ role="button"
321
+ type={type}
322
+ onClick={onClick}
323
+ beforeNav={beforeNav}
324
+ safeWithNav={safeWithNav}
325
+ rel={rel}
326
+ >
327
+ {renderProp}
328
+ </ClickableBehavior>
329
+ );
330
+ } else {
331
+ return (
332
+ <ClickableBehavior
333
+ disabled={spinner || disabled}
334
+ href={href}
335
+ role="button"
336
+ type={type}
337
+ onClick={onClick}
338
+ safeWithNav={safeWithNav}
339
+ target={target}
340
+ rel={rel}
341
+ >
342
+ {renderProp}
343
+ </ClickableBehavior>
344
+ );
345
+ }
346
+ }
347
+ }