@khanacademy/wonder-blocks-clickable 2.4.4 → 2.4.6

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.
Files changed (30) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/dist/components/clickable-behavior.d.ts +248 -0
  3. package/dist/components/clickable-behavior.js.flow +296 -0
  4. package/dist/components/clickable.d.ts +150 -0
  5. package/dist/components/clickable.js.flow +176 -0
  6. package/dist/es/index.js +147 -147
  7. package/dist/index.d.ts +7 -0
  8. package/dist/index.js +169 -171
  9. package/dist/index.js.flow +18 -2
  10. package/dist/util/get-clickable-behavior.d.ts +25 -0
  11. package/dist/util/get-clickable-behavior.js.flow +19 -0
  12. package/dist/util/is-client-side-url.d.ts +7 -0
  13. package/dist/util/is-client-side-url.js.flow +14 -0
  14. package/package.json +5 -5
  15. package/src/components/__tests__/{clickable-behavior.test.js → clickable-behavior.test.tsx} +140 -84
  16. package/src/components/__tests__/{clickable.test.js → clickable.test.tsx} +28 -27
  17. package/src/components/{clickable-behavior.js → clickable-behavior.ts} +82 -104
  18. package/src/components/{clickable.js → clickable.tsx} +111 -168
  19. package/src/{index.js → index.ts} +5 -6
  20. package/src/util/__tests__/{get-clickable-behavior.test.js → get-clickable-behavior.test.tsx} +2 -3
  21. package/src/util/__tests__/{is-client-side-url.js.test.js → is-client-side-url.js.test.ts} +3 -4
  22. package/src/util/{get-clickable-behavior.js → get-clickable-behavior.ts} +11 -5
  23. package/src/util/{is-client-side-url.js → is-client-side-url.ts} +0 -1
  24. package/tsconfig.json +12 -0
  25. package/tsconfig.tsbuildinfo +1 -0
  26. package/src/components/__docs__/accessibility.stories.mdx +0 -152
  27. package/src/components/__docs__/clickable-behavior.argtypes.js +0 -64
  28. package/src/components/__docs__/clickable-behavior.stories.js +0 -178
  29. package/src/components/__docs__/clickable.argtypes.js +0 -237
  30. package/src/components/__docs__/clickable.stories.js +0 -307
@@ -0,0 +1,150 @@
1
+ import * as React from "react";
2
+ import type { AriaProps, StyleType } from "@khanacademy/wonder-blocks-core";
3
+ import type { ClickableRole, ClickableState } from "./clickable-behavior";
4
+ type Props =
5
+ /**
6
+ * aria-label should be used when `spinner={true}` to let people using screen
7
+ * readers that the action taken by clicking the button will take some
8
+ * time to complete.
9
+ */
10
+ Partial<Omit<AriaProps, "aria-disabled">> & {
11
+ /**
12
+ * The child of Clickable must be a function which returns the component
13
+ * which should be made Clickable. The function is passed an object with
14
+ * three boolean properties: hovered, focused, and pressed.
15
+ */
16
+ children: (arg1: ClickableState) => React.ReactNode;
17
+ /**
18
+ * An onClick function which Clickable can execute when clicked
19
+ */
20
+ onClick?: (e: React.SyntheticEvent) => unknown;
21
+ /**
22
+ * Optional href which Clickable should direct to, uses client-side routing
23
+ * by default if react-router is present
24
+ */
25
+ href?: string;
26
+ /**
27
+ * Styles to apply to the Clickable component
28
+ */
29
+ style?: StyleType;
30
+ /**
31
+ * Adds CSS classes to the Clickable.
32
+ */
33
+ className?: string;
34
+ /**
35
+ * Whether the Clickable is on a dark colored background.
36
+ * Sets the default focus ring color to white, instead of blue.
37
+ * Defaults to false.
38
+ */
39
+ light: boolean;
40
+ /**
41
+ * Disables or enables the child; defaults to false
42
+ */
43
+ disabled: boolean;
44
+ /**
45
+ * An optional id attribute.
46
+ */
47
+ id?: string;
48
+ /**
49
+ * Specifies the type of relationship between the current document and the
50
+ * linked document. Should only be used when `href` is specified. This
51
+ * defaults to "noopener noreferrer" when `target="_blank"`, but can be
52
+ * overridden by setting this prop to something else.
53
+ */
54
+ rel?: string;
55
+ /**
56
+ * The role of the component, can be a role of type ClickableRole
57
+ */
58
+ role?: ClickableRole;
59
+ /**
60
+ * Avoids client-side routing in the presence of the href prop
61
+ */
62
+ skipClientNav?: boolean;
63
+ /**
64
+ * Test ID used for e2e testing.
65
+ */
66
+ testId?: string;
67
+ /**
68
+ * Respond to raw "keydown" event.
69
+ */
70
+ onKeyDown?: (e: React.KeyboardEvent) => unknown;
71
+ /**
72
+ * Respond to raw "keyup" event.
73
+ */
74
+ onKeyUp?: (e: React.KeyboardEvent) => unknown;
75
+ /**
76
+ * Don't show the default focus ring. This should be used when implementing
77
+ * a custom focus ring within your own component that uses Clickable.
78
+ */
79
+ hideDefaultFocusRing?: boolean;
80
+ /**
81
+ * A target destination window for a link to open in.
82
+ *
83
+ * TODO(WB-1262): only allow this prop when `href` is also set.t
84
+ */
85
+ target?: "_blank";
86
+ /**
87
+ * Run async code before navigating. If the promise returned rejects then
88
+ * navigation will not occur.
89
+ *
90
+ * If both safeWithNav and beforeNav are provided, beforeNav will be run
91
+ * first and safeWithNav will only be run if beforeNav does not reject.
92
+ *
93
+ * WARNING: This prop must be used with `href` and should not be used with
94
+ * `target="blank"`.
95
+ */
96
+ beforeNav?: () => Promise<unknown>;
97
+ /**
98
+ * Run async code in the background while client-side navigating. If the
99
+ * browser does a full page load navigation, the callback promise must be
100
+ * settled before the navigation will occur. Errors are ignored so that
101
+ * navigation is guaranteed to succeed.
102
+ */
103
+ safeWithNav?: () => Promise<unknown>;
104
+ };
105
+ type DefaultProps = {
106
+ light: Props["light"];
107
+ disabled: Props["disabled"];
108
+ };
109
+ /**
110
+ * A component to turn any custom component into a clickable one.
111
+ *
112
+ * Works by wrapping `ClickableBehavior` around the child element and styling
113
+ * the child appropriately and encapsulates routing logic which can be
114
+ * customized. Expects a function which returns an element as its child.
115
+ *
116
+ * Clickable allows your components to:
117
+ *
118
+ * - Handle mouse / touch / keyboard events
119
+ * - Match the standard behavior of the given role
120
+ * - Apply custom styles based on pressed / focused / hovered state
121
+ * - Perform Client Side Navigation when href is passed and the component is a
122
+ * descendent of a react-router Router.
123
+ *
124
+ * ### Usage
125
+ *
126
+ * ```jsx
127
+ * <Clickable onClick={() => alert("You clicked me!")}>
128
+ * {({hovered, focused, pressed}) =>
129
+ * <div
130
+ * style={[
131
+ * hovered && styles.hovered,
132
+ * focused && styles.focused,
133
+ * pressed && styles.pressed,
134
+ * ]}
135
+ * >
136
+ * Click Me!
137
+ * </div>
138
+ * }
139
+ * </Clickable>
140
+ * ```
141
+ */
142
+ export default class Clickable extends React.Component<Props> {
143
+ static defaultProps: DefaultProps;
144
+ getCorrectTag: (clickableState: ClickableState, router: any, commonProps: {
145
+ [key: string]: any;
146
+ }) => React.ReactElement;
147
+ renderClickableBehavior(router: any): React.ReactNode;
148
+ render(): React.ReactElement;
149
+ }
150
+ export {};
@@ -0,0 +1,176 @@
1
+ /**
2
+ * Flowtype definitions for clickable
3
+ * Generated by Flowgen from a Typescript Definition
4
+ * Flowgen v1.21.0
5
+ * @flow
6
+ */
7
+
8
+ import * as React from "react";
9
+ import type { AriaProps, StyleType } from "@khanacademy/wonder-blocks-core";
10
+ import type { ClickableRole, ClickableState } from "./clickable-behavior";
11
+ declare type Props = {
12
+ ...$Rest<$Diff<AriaProps, { "aria-disabled": any }>, { ... }>,
13
+ ...{
14
+ /**
15
+ * The child of Clickable must be a function which returns the component
16
+ * which should be made Clickable. The function is passed an object with
17
+ * three boolean properties: hovered, focused, and pressed.
18
+ */
19
+ children: (arg1: ClickableState) => React.Node,
20
+
21
+ /**
22
+ * An onClick function which Clickable can execute when clicked
23
+ */
24
+ onClick?: (e: React.SyntheticEvent<>) => mixed,
25
+
26
+ /**
27
+ * Optional href which Clickable should direct to, uses client-side routing
28
+ * by default if react-router is present
29
+ */
30
+ href?: string,
31
+
32
+ /**
33
+ * Styles to apply to the Clickable component
34
+ */
35
+ style?: StyleType,
36
+
37
+ /**
38
+ * Adds CSS classes to the Clickable.
39
+ */
40
+ className?: string,
41
+
42
+ /**
43
+ * Whether the Clickable is on a dark colored background.
44
+ * Sets the default focus ring color to white, instead of blue.
45
+ * Defaults to false.
46
+ */
47
+ light: boolean,
48
+
49
+ /**
50
+ * Disables or enables the child; defaults to false
51
+ */
52
+ disabled: boolean,
53
+
54
+ /**
55
+ * An optional id attribute.
56
+ */
57
+ id?: string,
58
+
59
+ /**
60
+ * Specifies the type of relationship between the current document and the
61
+ * linked document. Should only be used when `href` is specified. This
62
+ * defaults to "noopener noreferrer" when `target="_blank"`, but can be
63
+ * overridden by setting this prop to something else.
64
+ */
65
+ rel?: string,
66
+
67
+ /**
68
+ * The role of the component, can be a role of type ClickableRole
69
+ */
70
+ role?: ClickableRole,
71
+
72
+ /**
73
+ * Avoids client-side routing in the presence of the href prop
74
+ */
75
+ skipClientNav?: boolean,
76
+
77
+ /**
78
+ * Test ID used for e2e testing.
79
+ */
80
+ testId?: string,
81
+
82
+ /**
83
+ * Respond to raw "keydown" event.
84
+ */
85
+ onKeyDown?: (e: React.KeyboardEvent<>) => mixed,
86
+
87
+ /**
88
+ * Respond to raw "keyup" event.
89
+ */
90
+ onKeyUp?: (e: React.KeyboardEvent<>) => mixed,
91
+
92
+ /**
93
+ * Don't show the default focus ring. This should be used when implementing
94
+ * a custom focus ring within your own component that uses Clickable.
95
+ */
96
+ hideDefaultFocusRing?: boolean,
97
+
98
+ /**
99
+ * A target destination window for a link to open in.
100
+ *
101
+ * TODO(WB-1262): only allow this prop when `href` is also set.t
102
+ */
103
+ target?: "_blank",
104
+
105
+ /**
106
+ * Run async code before navigating. If the promise returned rejects then
107
+ * navigation will not occur.
108
+ *
109
+ * If both safeWithNav and beforeNav are provided, beforeNav will be run
110
+ * first and safeWithNav will only be run if beforeNav does not reject.
111
+ *
112
+ * WARNING: This prop must be used with `href` and should not be used with
113
+ * `target="blank"`.
114
+ */
115
+ beforeNav?: () => Promise<mixed>,
116
+
117
+ /**
118
+ * Run async code in the background while client-side navigating. If the
119
+ * browser does a full page load navigation, the callback promise must be
120
+ * settled before the navigation will occur. Errors are ignored so that
121
+ * navigation is guaranteed to succeed.
122
+ */
123
+ safeWithNav?: () => Promise<mixed>,
124
+ ...
125
+ },
126
+ };
127
+ declare type DefaultProps = {
128
+ light: $PropertyType<Props, "light">,
129
+ disabled: $PropertyType<Props, "disabled">,
130
+ ...
131
+ };
132
+ /**
133
+ * A component to turn any custom component into a clickable one.
134
+ *
135
+ * Works by wrapping `ClickableBehavior` around the child element and styling
136
+ * the child appropriately and encapsulates routing logic which can be
137
+ * customized. Expects a function which returns an element as its child.
138
+ *
139
+ * Clickable allows your components to:
140
+ *
141
+ * - Handle mouse / touch / keyboard events
142
+ * - Match the standard behavior of the given role
143
+ * - Apply custom styles based on pressed / focused / hovered state
144
+ * - Perform Client Side Navigation when href is passed and the component is a
145
+ * descendent of a react-router Router.
146
+ *
147
+ * ### Usage
148
+ *
149
+ * ```jsx
150
+ * <Clickable onClick={() => alert("You clicked me!")}>
151
+ * {({hovered, focused, pressed}) =>
152
+ * <div
153
+ * style={[
154
+ * hovered && styles.hovered,
155
+ * focused && styles.focused,
156
+ * pressed && styles.pressed,
157
+ * ]}
158
+ * >
159
+ * Click Me!
160
+ * </div>
161
+ * }
162
+ * </Clickable>
163
+ * ```
164
+ */
165
+ declare export default class Clickable mixins React.Component<Props> {
166
+ static defaultProps: DefaultProps;
167
+ getCorrectTag: (
168
+ clickableState: ClickableState,
169
+ router: any,
170
+ commonProps: {
171
+ [key: string]: any,
172
+ }
173
+ ) => React.Element<>;
174
+ renderClickableBehavior(router: any): React.Node;
175
+ render(): React.Element<>;
176
+ }