@khanacademy/wonder-blocks-link 4.2.7 → 4.3.0

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,4 +1,5 @@
1
1
  import * as React from "react";
2
+ import { Link as ReactRouterLink } from "react-router-dom";
2
3
  import type { AriaProps, StyleType } from "@khanacademy/wonder-blocks-core";
3
4
  import type { Typography } from "@khanacademy/wonder-blocks-typography";
4
5
  import type { IconAsset } from "@khanacademy/wonder-blocks-icon";
@@ -20,20 +21,20 @@ export type SharedProps = AriaProps & {
20
21
  * This styles the link with an underline to distinguish it
21
22
  * from surrounding text.
22
23
  */
23
- inline: boolean;
24
+ inline?: boolean;
24
25
  /**
25
26
  * Kind of Link. Note: Secondary light Links are not supported.
26
27
  */
27
- kind: "primary" | "secondary";
28
+ kind?: "primary" | "secondary";
28
29
  /**
29
30
  * Whether the button is on a dark/colored background.
30
31
  */
31
- light: boolean;
32
+ light?: boolean;
32
33
  /**
33
34
  * Whether the link should change color once it's visited.
34
35
  * secondary or primary (light) links are not allowed to be visitable.
35
36
  */
36
- visitable: boolean;
37
+ visitable?: boolean;
37
38
  /**
38
39
  * Specifies the type of relationship between the current document and the
39
40
  * linked document. Should only be used when `href` is specified. This
@@ -131,12 +132,6 @@ export type SharedProps = AriaProps & {
131
132
  */
132
133
  endIcon?: IconAsset;
133
134
  };
134
- type DefaultProps = {
135
- inline: SharedProps["inline"];
136
- kind: SharedProps["kind"];
137
- light: SharedProps["light"];
138
- visitable: SharedProps["visitable"];
139
- };
140
135
  /**
141
136
  * Reusable link component.
142
137
  *
@@ -155,9 +150,135 @@ type DefaultProps = {
155
150
  * </Link>
156
151
  * ```
157
152
  */
158
- export default class Link extends React.Component<SharedProps> {
159
- static defaultProps: DefaultProps;
160
- renderClickableBehavior(router: any): React.ReactNode;
161
- render(): React.ReactNode;
162
- }
163
- export {};
153
+ declare const Link: React.ForwardRefExoticComponent<Readonly<import("../../../wonder-blocks-core/src/util/aria-types").AriaAttributes> & Readonly<{
154
+ role?: import("../../../wonder-blocks-core/src/util/aria-types").AriaRole | undefined;
155
+ }> & {
156
+ /**
157
+ * Text to appear on the link. It can be a plain text or a Typography element.
158
+ */
159
+ children: string | React.ReactElement<React.ComponentProps<Typography>>;
160
+ /**
161
+ * URL to navigate to.
162
+ */
163
+ href: string;
164
+ /**
165
+ * An optional id attribute.
166
+ */
167
+ id?: string | undefined;
168
+ /**
169
+ * Indicates that this link is used within a body of text.
170
+ * This styles the link with an underline to distinguish it
171
+ * from surrounding text.
172
+ */
173
+ inline?: boolean | undefined;
174
+ /**
175
+ * Kind of Link. Note: Secondary light Links are not supported.
176
+ */
177
+ kind?: "primary" | "secondary" | undefined;
178
+ /**
179
+ * Whether the button is on a dark/colored background.
180
+ */
181
+ light?: boolean | undefined;
182
+ /**
183
+ * Whether the link should change color once it's visited.
184
+ * secondary or primary (light) links are not allowed to be visitable.
185
+ */
186
+ visitable?: boolean | undefined;
187
+ /**
188
+ * Specifies the type of relationship between the current document and the
189
+ * linked document. Should only be used when `href` is specified. This
190
+ * defaults to "noopener noreferrer" when `target="_blank"`, but can be
191
+ * overridden by setting this prop to something else.
192
+ */
193
+ rel?: string | undefined;
194
+ /**
195
+ * Set the tabindex attribute on the rendered element.
196
+ */
197
+ tabIndex?: number | undefined;
198
+ /**
199
+ * Test ID used for e2e testing.
200
+ */
201
+ testId?: string | undefined;
202
+ /**
203
+ * Whether to avoid using client-side navigation.
204
+ *
205
+ * If the URL passed to href is local to the client-side, e.g.
206
+ * /math/algebra/eval-exprs, then it tries to use react-router-dom's Link
207
+ * component which handles the client-side navigation. You can set
208
+ * `skipClientNav` to true avoid using client-side nav entirely.
209
+ *
210
+ * NOTE: All URLs containing a protocol are considered external, e.g.
211
+ * https://khanacademy.org/math/algebra/eval-exprs will trigger a full
212
+ * page reload.
213
+ */
214
+ skipClientNav?: boolean | undefined;
215
+ /**
216
+ * Custom styles.
217
+ */
218
+ style?: StyleType;
219
+ /**
220
+ * Adds CSS classes to the Link.
221
+ */
222
+ className?: string | undefined;
223
+ /**
224
+ * Function to call when button is clicked.
225
+ *
226
+ * This callback should be used for things like marking BigBingo
227
+ * conversions. It should NOT be used to redirect to a different URL or to
228
+ * prevent navigation via e.preventDefault(). The event passed to this
229
+ * handler will have its preventDefault() and stopPropagation() methods
230
+ * stubbed out.
231
+ */
232
+ onClick?: ((e: React.SyntheticEvent) => unknown) | undefined;
233
+ /**
234
+ * Run async code in the background while client-side navigating. If the
235
+ * browser does a full page load navigation, the callback promise must be
236
+ * settled before the navigation will occur. Errors are ignored so that
237
+ * navigation is guaranteed to succeed.
238
+ */
239
+ safeWithNav?: (() => Promise<unknown>) | undefined;
240
+ /**
241
+ * Respond to raw "keydown" event.
242
+ */
243
+ onKeyDown?: ((e: React.KeyboardEvent) => unknown) | undefined;
244
+ /**
245
+ * Respond to raw "keyup" event.
246
+ */
247
+ onKeyUp?: ((e: React.KeyboardEvent) => unknown) | undefined;
248
+ /**
249
+ * A target destination window for a link to open in. We only support
250
+ * "_blank" which opens the URL in a new tab.
251
+ *
252
+ * TODO(WB-1262): only allow this prop when `href` is also set.t
253
+ */
254
+ target?: "_blank" | undefined;
255
+ /**
256
+ * Run async code before navigating to the URL passed to `href`. If the
257
+ * promise returned rejects then navigation will not occur.
258
+ *
259
+ * If both safeWithNav and beforeNav are provided, beforeNav will be run
260
+ * first and safeWithNav will only be run if beforeNav does not reject.
261
+ *
262
+ * WARNING: Using this with `target="_blank"` will trigger built-in popup
263
+ * blockers in Firefox and Safari. This is because we do navigation
264
+ * programmatically and `beforeNav` causes a delay which means that the
265
+ * browser can't make a directly link between a user action and the
266
+ * navigation.
267
+ */
268
+ beforeNav?: (() => Promise<unknown>) | undefined;
269
+ /**
270
+ * An optional title attribute.
271
+ */
272
+ title?: string | undefined;
273
+ /**
274
+ * An optional icon displayed before the link label.
275
+ */
276
+ startIcon?: IconAsset | undefined;
277
+ /**
278
+ * An optional icon displayed after the link label.
279
+ * If `target="_blank"` and `endIcon` is passed in, `endIcon` will override
280
+ * the default `externalIcon`.
281
+ */
282
+ endIcon?: IconAsset | undefined;
283
+ } & React.RefAttributes<HTMLAnchorElement | typeof ReactRouterLink>>;
284
+ export default Link;
@@ -4,7 +4,9 @@
4
4
  * Flowgen v1.21.0
5
5
  * @flow
6
6
  */
7
+ import * as $Flowgen$Import$_2e__2e__2f__2e__2e__2f__2e__2e__2f_wonder_2d_blocks_2d_core_2f_src_2f_util_2f_aria_2d_types from "../../../wonder-blocks-core/src/util/aria-types";
7
8
  import * as React from "react";
9
+ import { Link as ReactRouterLink } from "react-router-dom";
8
10
  import type { AriaProps, StyleType } from "@khanacademy/wonder-blocks-core";
9
11
  import type { Typography } from "@khanacademy/wonder-blocks-typography";
10
12
  import type { IconAsset } from "@khanacademy/wonder-blocks-icon";
@@ -31,23 +33,23 @@ export type SharedProps = {|
31
33
  * This styles the link with an underline to distinguish it
32
34
  * from surrounding text.
33
35
  */
34
- inline: boolean,
36
+ inline?: boolean,
35
37
 
36
38
  /**
37
39
  * Kind of Link. Note: Secondary light Links are not supported.
38
40
  */
39
- kind: "primary" | "secondary",
41
+ kind?: "primary" | "secondary",
40
42
 
41
43
  /**
42
44
  * Whether the button is on a dark/colored background.
43
45
  */
44
- light: boolean,
46
+ light?: boolean,
45
47
 
46
48
  /**
47
49
  * Whether the link should change color once it's visited.
48
50
  * secondary or primary (light) links are not allowed to be visitable.
49
51
  */
50
- visitable: boolean,
52
+ visitable?: boolean,
51
53
 
52
54
  /**
53
55
  * Specifies the type of relationship between the current document and the
@@ -161,12 +163,6 @@ export type SharedProps = {|
161
163
  endIcon?: IconAsset,
162
164
  |},
163
165
  |};
164
- declare type DefaultProps = {|
165
- inline: $PropertyType<SharedProps, "inline">,
166
- kind: $PropertyType<SharedProps, "kind">,
167
- light: $PropertyType<SharedProps, "light">,
168
- visitable: $PropertyType<SharedProps, "visitable">,
169
- |};
170
166
  /**
171
167
  * Reusable link component.
172
168
  *
@@ -185,8 +181,161 @@ declare type DefaultProps = {|
185
181
  * </Link>
186
182
  * ```
187
183
  */
188
- declare export default class Link extends React.Component<SharedProps> {
189
- static defaultProps: DefaultProps;
190
- renderClickableBehavior(router: any): React.Node;
191
- render(): React.Node;
192
- }
184
+ declare var Link: React.ForwardRefExoticComponent<{|
185
+ ...$ReadOnly<$Flowgen$Import$_2e__2e__2f__2e__2e__2f__2e__2e__2f_wonder_2d_blocks_2d_core_2f_src_2f_util_2f_aria_2d_types.AriaAttributes>,
186
+ ...$ReadOnly<{|
187
+ role?: $Flowgen$Import$_2e__2e__2f__2e__2e__2f__2e__2e__2f_wonder_2d_blocks_2d_core_2f_src_2f_util_2f_aria_2d_types.AriaRole | void,
188
+ |}>,
189
+ ...{|
190
+ /**
191
+ * Text to appear on the link. It can be a plain text or a Typography element.
192
+ */
193
+ children: string | React.Element<Typography>,
194
+
195
+ /**
196
+ * URL to navigate to.
197
+ */
198
+ href: string,
199
+
200
+ /**
201
+ * An optional id attribute.
202
+ */
203
+ id?: string | void,
204
+
205
+ /**
206
+ * Indicates that this link is used within a body of text.
207
+ * This styles the link with an underline to distinguish it
208
+ * from surrounding text.
209
+ */
210
+ inline?: boolean | void,
211
+
212
+ /**
213
+ * Kind of Link. Note: Secondary light Links are not supported.
214
+ */
215
+ kind?: "primary" | "secondary" | void,
216
+
217
+ /**
218
+ * Whether the button is on a dark/colored background.
219
+ */
220
+ light?: boolean | void,
221
+
222
+ /**
223
+ * Whether the link should change color once it's visited.
224
+ * secondary or primary (light) links are not allowed to be visitable.
225
+ */
226
+ visitable?: boolean | void,
227
+
228
+ /**
229
+ * Specifies the type of relationship between the current document and the
230
+ * linked document. Should only be used when `href` is specified. This
231
+ * defaults to "noopener noreferrer" when `target="_blank"`, but can be
232
+ * overridden by setting this prop to something else.
233
+ */
234
+ rel?: string | void,
235
+
236
+ /**
237
+ * Set the tabindex attribute on the rendered element.
238
+ */
239
+ tabIndex?: number | void,
240
+
241
+ /**
242
+ * Test ID used for e2e testing.
243
+ */
244
+ testId?: string | void,
245
+
246
+ /**
247
+ * Whether to avoid using client-side navigation.
248
+ *
249
+ * If the URL passed to href is local to the client-side, e.g.
250
+ * /math/algebra/eval-exprs, then it tries to use react-router-dom's Link
251
+ * component which handles the client-side navigation. You can set
252
+ * `skipClientNav` to true avoid using client-side nav entirely.
253
+ *
254
+ * NOTE: All URLs containing a protocol are considered external, e.g.
255
+ * https://khanacademy.org/math/algebra/eval-exprs will trigger a full
256
+ * page reload.
257
+ */
258
+ skipClientNav?: boolean | void,
259
+
260
+ /**
261
+ * Custom styles.
262
+ */
263
+ style?: StyleType,
264
+
265
+ /**
266
+ * Adds CSS classes to the Link.
267
+ */
268
+ className?: string | void,
269
+
270
+ /**
271
+ * Function to call when button is clicked.
272
+ *
273
+ * This callback should be used for things like marking BigBingo
274
+ * conversions. It should NOT be used to redirect to a different URL or to
275
+ * prevent navigation via e.preventDefault(). The event passed to this
276
+ * handler will have its preventDefault() and stopPropagation() methods
277
+ * stubbed out.
278
+ */
279
+ onClick?: ((e: SyntheticEvent<>) => mixed) | void,
280
+
281
+ /**
282
+ * Run async code in the background while client-side navigating. If the
283
+ * browser does a full page load navigation, the callback promise must be
284
+ * settled before the navigation will occur. Errors are ignored so that
285
+ * navigation is guaranteed to succeed.
286
+ */
287
+ safeWithNav?: (() => Promise<mixed>) | void,
288
+
289
+ /**
290
+ * Respond to raw "keydown" event.
291
+ */
292
+ onKeyDown?: ((e: SyntheticKeyboardEvent<>) => mixed) | void,
293
+
294
+ /**
295
+ * Respond to raw "keyup" event.
296
+ */
297
+ onKeyUp?: ((e: SyntheticKeyboardEvent<>) => mixed) | void,
298
+
299
+ /**
300
+ * A target destination window for a link to open in. We only support
301
+ * "_blank" which opens the URL in a new tab.
302
+ *
303
+ * TODO(WB-1262): only allow this prop when `href` is also set.t
304
+ */
305
+ target?: "_blank" | void,
306
+
307
+ /**
308
+ * Run async code before navigating to the URL passed to `href`. If the
309
+ * promise returned rejects then navigation will not occur.
310
+ *
311
+ * If both safeWithNav and beforeNav are provided, beforeNav will be run
312
+ * first and safeWithNav will only be run if beforeNav does not reject.
313
+ *
314
+ * WARNING: Using this with `target="_blank"` will trigger built-in popup
315
+ * blockers in Firefox and Safari. This is because we do navigation
316
+ * programmatically and `beforeNav` causes a delay which means that the
317
+ * browser can't make a directly link between a user action and the
318
+ * navigation.
319
+ */
320
+ beforeNav?: (() => Promise<mixed>) | void,
321
+
322
+ /**
323
+ * An optional title attribute.
324
+ */
325
+ title?: string | void,
326
+
327
+ /**
328
+ * An optional icon displayed before the link label.
329
+ */
330
+ startIcon?: IconAsset | void,
331
+
332
+ /**
333
+ * An optional icon displayed after the link label.
334
+ * If `target="_blank"` and `endIcon` is passed in, `endIcon` will override
335
+ * the default `externalIcon`.
336
+ */
337
+ endIcon?: IconAsset | void,
338
+ |},
339
+ ...React.RefAttributes<HTMLAnchorElement | typeof ReactRouterLink>,
340
+ |}>;
341
+ declare export default typeof Link;
package/dist/es/index.js CHANGED
@@ -43,32 +43,29 @@ function _objectWithoutPropertiesLoose(source, excluded) {
43
43
  const _excluded$1 = ["children", "skipClientNav", "focused", "hovered", "href", "inline", "kind", "light", "visitable", "pressed", "style", "testId", "waiting", "target", "startIcon", "endIcon"];
44
44
  const StyledAnchor = addStyle("a");
45
45
  const StyledLink = addStyle(Link$1);
46
- const StyledSpan = addStyle("span");
47
- class LinkCore extends React.Component {
48
- renderInner(router) {
49
- const _this$props = this.props,
50
- {
46
+ const LinkCore = React.forwardRef((props, ref) => {
47
+ const renderInner = router => {
48
+ const {
51
49
  children,
52
50
  skipClientNav,
53
51
  focused,
54
52
  hovered,
55
53
  href,
56
- inline,
57
- kind,
58
- light,
59
- visitable,
54
+ inline = false,
55
+ kind = "primary",
56
+ light = false,
57
+ visitable = false,
60
58
  pressed,
61
59
  style,
62
60
  testId,
63
61
  target,
64
62
  startIcon,
65
63
  endIcon
66
- } = _this$props,
67
- restProps = _objectWithoutPropertiesLoose(_this$props, _excluded$1);
68
- const withIcon = startIcon || endIcon || target === "_blank";
64
+ } = props,
65
+ restProps = _objectWithoutPropertiesLoose(props, _excluded$1);
69
66
  const linkStyles = _generateStyles(inline, kind, light, visitable);
70
67
  const restingStyles = inline ? linkStyles.restingInline : linkStyles.resting;
71
- const defaultStyles = [sharedStyles.shared, restingStyles, pressed && linkStyles.active, !pressed && hovered && linkStyles.hover, !pressed && focused && linkStyles.focus, withIcon && sharedStyles.withIcon];
68
+ const defaultStyles = [sharedStyles.shared, restingStyles, pressed && linkStyles.active, !pressed && hovered && linkStyles.hover, !pressed && focused && linkStyles.focus];
72
69
  const commonProps = _extends({
73
70
  "data-test-id": testId,
74
71
  style: [defaultStyles, style],
@@ -91,9 +88,7 @@ class LinkCore extends React.Component {
91
88
  style: [linkContentStyles.startIcon, linkContentStyles.centered],
92
89
  testId: "start-icon",
93
90
  "aria-hidden": "true"
94
- }), withIcon ? React.createElement(StyledSpan, {
95
- style: linkContentStyles.centered
96
- }, children) : children, endIcon ? React.createElement(Icon, {
91
+ }), children, endIcon ? React.createElement(Icon, {
97
92
  icon: endIcon,
98
93
  size: "small",
99
94
  style: [linkContentStyles.endIcon, linkContentStyles.centered],
@@ -101,15 +96,15 @@ class LinkCore extends React.Component {
101
96
  "aria-hidden": "true"
102
97
  }) : isExternalLink && target === "_blank" && externalIcon);
103
98
  return router && !skipClientNav && isClientSideUrl(href) ? React.createElement(StyledLink, _extends({}, commonProps, {
104
- to: href
99
+ to: href,
100
+ ref: ref
105
101
  }), linkContent) : React.createElement(StyledAnchor, _extends({}, commonProps, {
106
- href: href
102
+ href: href,
103
+ ref: ref
107
104
  }), linkContent);
108
- }
109
- render() {
110
- return React.createElement(__RouterContext.Consumer, null, router => this.renderInner(router));
111
- }
112
- }
105
+ };
106
+ return React.createElement(__RouterContext.Consumer, null, router => renderInner(router));
107
+ });
113
108
  const styles = {};
114
109
  const linkContentStyles = StyleSheet.create({
115
110
  startIcon: {
@@ -119,7 +114,7 @@ const linkContentStyles = StyleSheet.create({
119
114
  marginInlineStart: Spacing.xxxSmall_4
120
115
  },
121
116
  centered: {
122
- verticalAlign: "middle"
117
+ verticalAlign: "-10%"
123
118
  }
124
119
  });
125
120
  const sharedStyles = StyleSheet.create({
@@ -128,10 +123,6 @@ const sharedStyles = StyleSheet.create({
128
123
  textDecoration: "none",
129
124
  outline: "none",
130
125
  alignItems: "center"
131
- },
132
- withIcon: {
133
- verticalAlign: "bottom",
134
- textUnderlineOffset: 4
135
126
  }
136
127
  });
137
128
  const _generateStyles = (inline, kind, light, visitable) => {
@@ -203,23 +194,26 @@ const _generateStyles = (inline, kind, light, visitable) => {
203
194
  return styles[buttonType];
204
195
  };
205
196
 
206
- const _excluded = ["onClick", "beforeNav", "safeWithNav", "href", "skipClientNav", "children", "tabIndex", "onKeyDown", "onKeyUp", "target"];
207
- class Link extends React.Component {
208
- renderClickableBehavior(router) {
209
- const _this$props = this.props,
210
- {
211
- onClick,
212
- beforeNav = undefined,
213
- safeWithNav,
214
- href,
215
- skipClientNav,
216
- children,
217
- tabIndex,
218
- onKeyDown,
219
- onKeyUp,
220
- target = undefined
221
- } = _this$props,
222
- sharedProps = _objectWithoutPropertiesLoose(_this$props, _excluded);
197
+ const _excluded = ["onClick", "beforeNav", "safeWithNav", "href", "skipClientNav", "children", "tabIndex", "onKeyDown", "onKeyUp", "target", "inline", "kind", "light", "visitable"];
198
+ const Link = React.forwardRef((props, ref) => {
199
+ const {
200
+ onClick,
201
+ beforeNav = undefined,
202
+ safeWithNav,
203
+ href,
204
+ skipClientNav,
205
+ children,
206
+ tabIndex,
207
+ onKeyDown,
208
+ onKeyUp,
209
+ target = undefined,
210
+ inline = false,
211
+ kind = "primary",
212
+ light = false,
213
+ visitable = false
214
+ } = props,
215
+ sharedProps = _objectWithoutPropertiesLoose(props, _excluded);
216
+ const renderClickableBehavior = router => {
223
217
  const ClickableBehavior = getClickableBehavior(href, skipClientNav, router);
224
218
  if (beforeNav) {
225
219
  return React.createElement(ClickableBehavior, {
@@ -237,7 +231,12 @@ class Link extends React.Component {
237
231
  skipClientNav: skipClientNav,
238
232
  href: href,
239
233
  target: target,
240
- tabIndex: tabIndex
234
+ tabIndex: tabIndex,
235
+ inline: inline,
236
+ kind: kind,
237
+ light: light,
238
+ visitable: visitable,
239
+ ref: ref
241
240
  }), children);
242
241
  });
243
242
  } else {
@@ -256,20 +255,17 @@ class Link extends React.Component {
256
255
  skipClientNav: skipClientNav,
257
256
  href: href,
258
257
  target: target,
259
- tabIndex: tabIndex
258
+ tabIndex: tabIndex,
259
+ inline: inline,
260
+ kind: kind,
261
+ light: light,
262
+ visitable: visitable,
263
+ ref: ref
260
264
  }), children);
261
265
  });
262
266
  }
263
- }
264
- render() {
265
- return React.createElement(__RouterContext.Consumer, null, router => this.renderClickableBehavior(router));
266
- }
267
- }
268
- Link.defaultProps = {
269
- inline: false,
270
- kind: "primary",
271
- light: false,
272
- visitable: false
273
- };
267
+ };
268
+ return React.createElement(__RouterContext.Consumer, null, router => renderClickableBehavior(router));
269
+ });
274
270
 
275
271
  export { Link as default };