@khanacademy/wonder-blocks-timing 2.1.0 → 2.1.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.
Files changed (70) hide show
  1. package/CHANGELOG.md +26 -6
  2. package/dist/components/action-scheduler-provider.d.ts +26 -0
  3. package/dist/components/action-scheduler-provider.js.flow +35 -0
  4. package/dist/components/with-action-scheduler.d.ts +14 -0
  5. package/dist/components/with-action-scheduler.js.flow +26 -0
  6. package/dist/es/index.js +165 -380
  7. package/dist/hooks/internal/use-updating-ref.d.ts +13 -0
  8. package/dist/hooks/internal/use-updating-ref.js.flow +22 -0
  9. package/dist/hooks/use-interval.d.ts +8 -0
  10. package/dist/hooks/use-interval.js.flow +18 -0
  11. package/dist/hooks/use-scheduled-interval.d.ts +2 -0
  12. package/dist/hooks/use-scheduled-interval.js.flow +13 -0
  13. package/dist/hooks/use-scheduled-timeout.d.ts +2 -0
  14. package/dist/hooks/use-scheduled-timeout.js.flow +13 -0
  15. package/dist/hooks/use-timeout.d.ts +8 -0
  16. package/dist/hooks/use-timeout.js.flow +18 -0
  17. package/dist/index.d.ts +8 -0
  18. package/dist/index.js +335 -778
  19. package/dist/index.js.flow +31 -2
  20. package/dist/util/action-scheduler.d.ts +21 -0
  21. package/dist/util/action-scheduler.js.flow +39 -0
  22. package/dist/util/animation-frame.d.ts +62 -0
  23. package/dist/util/animation-frame.js.flow +71 -0
  24. package/dist/util/interval.d.ts +60 -0
  25. package/dist/util/interval.js.flow +70 -0
  26. package/dist/util/policies.d.ts +8 -0
  27. package/dist/util/policies.js.flow +17 -0
  28. package/dist/util/timeout.d.ts +62 -0
  29. package/dist/util/timeout.js.flow +72 -0
  30. package/dist/util/types.d.ts +228 -0
  31. package/dist/util/types.js.flow +235 -0
  32. package/dist/util/types.typestest.d.ts +1 -0
  33. package/dist/util/types.typestest.js.flow +6 -0
  34. package/package.json +3 -3
  35. package/src/__docs__/_overview_.stories.mdx +17 -0
  36. package/src/components/__tests__/{action-scheduler-provider.test.js → action-scheduler-provider.test.tsx} +7 -10
  37. package/src/components/__tests__/{with-action-scheduler.test.js → with-action-scheduler.test.tsx} +11 -12
  38. package/src/components/{action-scheduler-provider.js → action-scheduler-provider.ts} +6 -7
  39. package/src/components/{with-action-scheduler.js → with-action-scheduler.tsx} +11 -19
  40. package/src/hooks/__tests__/{use-interval.test.js → use-interval.test.ts} +6 -7
  41. package/src/hooks/__tests__/{use-scheduled-interval.test.js → use-scheduled-interval.test.ts} +15 -8
  42. package/src/hooks/__tests__/{use-scheduled-timeout.test.js → use-scheduled-timeout.test.ts} +22 -13
  43. package/src/hooks/__tests__/{use-timeout.test.js → use-timeout.test.ts} +6 -7
  44. package/src/hooks/internal/{use-updating-ref.js → use-updating-ref.ts} +5 -2
  45. package/src/hooks/{use-interval.js → use-interval.ts} +2 -3
  46. package/src/hooks/{use-scheduled-interval.js → use-scheduled-interval.ts} +6 -6
  47. package/src/hooks/{use-scheduled-timeout.js → use-scheduled-timeout.ts} +6 -6
  48. package/src/hooks/{use-timeout.js → use-timeout.ts} +2 -3
  49. package/src/{index.js → index.ts} +7 -8
  50. package/src/util/__tests__/{action-scheduler.test.js → action-scheduler.test.ts} +14 -24
  51. package/src/util/__tests__/{animation-frame.test.js → animation-frame.test.ts} +10 -9
  52. package/src/util/__tests__/{interval.test.js → interval.test.ts} +3 -4
  53. package/src/util/__tests__/{timeout.test.js → timeout.test.ts} +3 -4
  54. package/src/util/{action-scheduler.js → action-scheduler.ts} +17 -10
  55. package/src/util/{animation-frame.js → animation-frame.ts} +6 -6
  56. package/src/util/{interval.js → interval.ts} +7 -6
  57. package/src/util/{policies.js → policies.ts} +2 -3
  58. package/src/util/{timeout.js → timeout.ts} +7 -6
  59. package/src/util/{types.js → types.ts} +21 -28
  60. package/src/util/{types.flowtest.js → types.typestest.tsx} +22 -20
  61. package/tsconfig.json +11 -0
  62. package/tsconfig.tsbuildinfo +1 -0
  63. package/docs.md +0 -406
  64. package/src/__tests__/__snapshots__/generated-snapshot.test.js.snap +0 -353
  65. package/src/__tests__/generated-snapshot.test.js +0 -155
  66. package/src/components/with-action-scheduler.md +0 -18
  67. package/src/hooks/use-interval.stories.mdx +0 -80
  68. package/src/hooks/use-scheduled-interval.stories.mdx +0 -147
  69. package/src/hooks/use-scheduled-timeout.stories.mdx +0 -148
  70. package/src/hooks/use-timeout.stories.mdx +0 -80
@@ -1,353 +0,0 @@
1
- // Jest Snapshot v1, https://goo.gl/fbAQLP
2
-
3
- exports[`wonder-blocks-timing example 1 1`] = `
4
- <div
5
- className=""
6
- style={
7
- Object {
8
- "alignItems": "stretch",
9
- "borderStyle": "solid",
10
- "borderWidth": 0,
11
- "boxSizing": "border-box",
12
- "display": "flex",
13
- "flexDirection": "column",
14
- "margin": 0,
15
- "minHeight": 0,
16
- "minWidth": 0,
17
- "padding": 0,
18
- "position": "relative",
19
- "zIndex": 0,
20
- }
21
- }
22
- >
23
- <div
24
- className=""
25
- style={
26
- Object {
27
- "alignItems": "stretch",
28
- "borderStyle": "solid",
29
- "borderWidth": 0,
30
- "boxSizing": "border-box",
31
- "display": "flex",
32
- "flexDirection": "column",
33
- "margin": 0,
34
- "minHeight": 0,
35
- "minWidth": 0,
36
- "padding": 0,
37
- "position": "relative",
38
- "zIndex": 0,
39
- }
40
- }
41
- >
42
- <button
43
- className=""
44
- disabled={false}
45
- onBlur={[Function]}
46
- onClick={[Function]}
47
- onDragStart={[Function]}
48
- onFocus={[Function]}
49
- onKeyDown={[Function]}
50
- onKeyUp={[Function]}
51
- onMouseDown={[Function]}
52
- onMouseEnter={[Function]}
53
- onMouseLeave={[Function]}
54
- onMouseUp={[Function]}
55
- onTouchCancel={[Function]}
56
- onTouchEnd={[Function]}
57
- onTouchStart={[Function]}
58
- role="button"
59
- style={
60
- Object {
61
- "::MozFocusInner": Object {
62
- "border": 0,
63
- },
64
- ":focus": Object {
65
- "WebkitTapHighlightColor": "rgba(0,0,0,0)",
66
- },
67
- "alignItems": "center",
68
- "background": "#1865f2",
69
- "border": "none",
70
- "borderRadius": 4,
71
- "boxSizing": "border-box",
72
- "color": "#ffffff",
73
- "cursor": "pointer",
74
- "display": "inline-flex",
75
- "height": 40,
76
- "justifyContent": "center",
77
- "margin": 0,
78
- "outline": "none",
79
- "paddingBottom": 0,
80
- "paddingLeft": 16,
81
- "paddingRight": 16,
82
- "paddingTop": 0,
83
- "position": "relative",
84
- "textDecoration": "none",
85
- "touchAction": "manipulation",
86
- "userSelect": "none",
87
- }
88
- }
89
- tabIndex={0}
90
- type="button"
91
- >
92
- <span
93
- className=""
94
- style={
95
- Object {
96
- "MozOsxFontSmoothing": "grayscale",
97
- "WebkitFontSmoothing": "antialiased",
98
- "alignItems": "center",
99
- "display": "inline-block",
100
- "fontFamily": "Lato, \\"Noto Sans\\", sans-serif",
101
- "fontSize": 16,
102
- "fontWeight": "bold",
103
- "lineHeight": "20px",
104
- "overflow": "hidden",
105
- "pointerEvents": "none",
106
- "textOverflow": "ellipsis",
107
- "whiteSpace": "nowrap",
108
- }
109
- }
110
- >
111
- Unmount
112
- </span>
113
- </button>
114
- <div
115
- className=""
116
- style={
117
- Object {
118
- "alignItems": "stretch",
119
- "borderStyle": "solid",
120
- "borderWidth": 0,
121
- "boxSizing": "border-box",
122
- "display": "flex",
123
- "flexDirection": "column",
124
- "margin": 0,
125
- "minHeight": 0,
126
- "minWidth": 0,
127
- "padding": 0,
128
- "position": "relative",
129
- "zIndex": 0,
130
- }
131
- }
132
- >
133
- NaughtyComponent here
134
- </div>
135
- </div>
136
- <div
137
- className=""
138
- style={
139
- Object {
140
- "alignItems": "stretch",
141
- "borderStyle": "solid",
142
- "borderWidth": 0,
143
- "boxSizing": "border-box",
144
- "display": "flex",
145
- "flexDirection": "column",
146
- "margin": 0,
147
- "minHeight": 0,
148
- "minWidth": 0,
149
- "padding": 0,
150
- "position": "relative",
151
- "zIndex": 0,
152
- }
153
- }
154
- >
155
- <div
156
- className=""
157
- id="uid--0-wb-id"
158
- style={
159
- Object {
160
- "alignItems": "stretch",
161
- "borderStyle": "solid",
162
- "borderWidth": 0,
163
- "boxSizing": "border-box",
164
- "display": "flex",
165
- "flexDirection": "column",
166
- "margin": 0,
167
- "minHeight": 0,
168
- "minWidth": 0,
169
- "padding": 0,
170
- "position": "relative",
171
- "zIndex": 0,
172
- }
173
- }
174
- />
175
- </div>
176
- </div>
177
- `;
178
-
179
- exports[`wonder-blocks-timing example 2 1`] = `
180
- <div
181
- className=""
182
- style={
183
- Object {
184
- "alignItems": "stretch",
185
- "borderStyle": "solid",
186
- "borderWidth": 0,
187
- "boxSizing": "border-box",
188
- "display": "flex",
189
- "flexDirection": "column",
190
- "margin": 0,
191
- "minHeight": 0,
192
- "minWidth": 0,
193
- "padding": 0,
194
- "position": "relative",
195
- "zIndex": 0,
196
- }
197
- }
198
- >
199
- <div
200
- className=""
201
- style={
202
- Object {
203
- "alignItems": "stretch",
204
- "borderStyle": "solid",
205
- "borderWidth": 0,
206
- "boxSizing": "border-box",
207
- "display": "flex",
208
- "flexDirection": "column",
209
- "margin": 0,
210
- "minHeight": 0,
211
- "minWidth": 0,
212
- "padding": 0,
213
- "position": "relative",
214
- "zIndex": 0,
215
- }
216
- }
217
- >
218
- <button
219
- className=""
220
- disabled={false}
221
- onBlur={[Function]}
222
- onClick={[Function]}
223
- onDragStart={[Function]}
224
- onFocus={[Function]}
225
- onKeyDown={[Function]}
226
- onKeyUp={[Function]}
227
- onMouseDown={[Function]}
228
- onMouseEnter={[Function]}
229
- onMouseLeave={[Function]}
230
- onMouseUp={[Function]}
231
- onTouchCancel={[Function]}
232
- onTouchEnd={[Function]}
233
- onTouchStart={[Function]}
234
- role="button"
235
- style={
236
- Object {
237
- "::MozFocusInner": Object {
238
- "border": 0,
239
- },
240
- ":focus": Object {
241
- "WebkitTapHighlightColor": "rgba(0,0,0,0)",
242
- },
243
- "alignItems": "center",
244
- "background": "#1865f2",
245
- "border": "none",
246
- "borderRadius": 4,
247
- "boxSizing": "border-box",
248
- "color": "#ffffff",
249
- "cursor": "pointer",
250
- "display": "inline-flex",
251
- "height": 40,
252
- "justifyContent": "center",
253
- "margin": 0,
254
- "outline": "none",
255
- "paddingBottom": 0,
256
- "paddingLeft": 16,
257
- "paddingRight": 16,
258
- "paddingTop": 0,
259
- "position": "relative",
260
- "textDecoration": "none",
261
- "touchAction": "manipulation",
262
- "userSelect": "none",
263
- }
264
- }
265
- tabIndex={0}
266
- type="button"
267
- >
268
- <span
269
- className=""
270
- style={
271
- Object {
272
- "MozOsxFontSmoothing": "grayscale",
273
- "WebkitFontSmoothing": "antialiased",
274
- "alignItems": "center",
275
- "display": "inline-block",
276
- "fontFamily": "Lato, \\"Noto Sans\\", sans-serif",
277
- "fontSize": 16,
278
- "fontWeight": "bold",
279
- "lineHeight": "20px",
280
- "overflow": "hidden",
281
- "pointerEvents": "none",
282
- "textOverflow": "ellipsis",
283
- "whiteSpace": "nowrap",
284
- }
285
- }
286
- >
287
- Unmount
288
- </span>
289
- </button>
290
- <div
291
- className=""
292
- style={
293
- Object {
294
- "alignItems": "stretch",
295
- "borderStyle": "solid",
296
- "borderWidth": 0,
297
- "boxSizing": "border-box",
298
- "display": "flex",
299
- "flexDirection": "column",
300
- "margin": 0,
301
- "minHeight": 0,
302
- "minWidth": 0,
303
- "padding": 0,
304
- "position": "relative",
305
- "zIndex": 0,
306
- }
307
- }
308
- >
309
- GoodComponent here
310
- </div>
311
- </div>
312
- <div
313
- className=""
314
- style={
315
- Object {
316
- "alignItems": "stretch",
317
- "borderStyle": "solid",
318
- "borderWidth": 0,
319
- "boxSizing": "border-box",
320
- "display": "flex",
321
- "flexDirection": "column",
322
- "margin": 0,
323
- "minHeight": 0,
324
- "minWidth": 0,
325
- "padding": 0,
326
- "position": "relative",
327
- "zIndex": 0,
328
- }
329
- }
330
- >
331
- <div
332
- className=""
333
- id="uid--1-wb-id"
334
- style={
335
- Object {
336
- "alignItems": "stretch",
337
- "borderStyle": "solid",
338
- "borderWidth": 0,
339
- "boxSizing": "border-box",
340
- "display": "flex",
341
- "flexDirection": "column",
342
- "margin": 0,
343
- "minHeight": 0,
344
- "minWidth": 0,
345
- "padding": 0,
346
- "position": "relative",
347
- "zIndex": 0,
348
- }
349
- }
350
- />
351
- </div>
352
- </div>
353
- `;
@@ -1,155 +0,0 @@
1
- // This file is auto-generated by gen-snapshot-tests.js
2
- // Do not edit this file. To make changes to these snapshot tests:
3
- // 1. edit the markdown documentation files in the package,
4
- // packages/wonder-blocks-timing
5
- // 2. Run `yarn run gen-snapshot-tests`.
6
- import React from "react";
7
- import renderer from "react-test-renderer";
8
-
9
- // Mock react-dom as jest doesn't like findDOMNode.
10
- jest.mock("react-dom");
11
- import Button from "@khanacademy/wonder-blocks-button";
12
- import {IDProvider, View} from "@khanacademy/wonder-blocks-core";
13
- import {withActionScheduler} from "@khanacademy/wonder-blocks-timing";
14
-
15
- describe("wonder-blocks-timing", () => {
16
- it("example 1", () => {
17
- class Unmounter extends React.Component {
18
- constructor() {
19
- super();
20
- this.state = {
21
- mountKids: true,
22
- };
23
- }
24
-
25
- maybeRenderKids() {
26
- if (this.state.mountKids) {
27
- return (
28
- <React.Fragment>
29
- <Button onClick={() => this.onClick()}>
30
- Unmount
31
- </Button>
32
- {this.props.children}
33
- </React.Fragment>
34
- );
35
- } else {
36
- return "Children unmounted";
37
- }
38
- }
39
-
40
- onClick() {
41
- this.setState({
42
- mountKids: false,
43
- });
44
- }
45
-
46
- render() {
47
- return <View>{this.maybeRenderKids()}</View>;
48
- }
49
- }
50
-
51
- class MyNaughtyComponent extends React.Component {
52
- componentDidMount() {
53
- const {targetId} = this.props;
54
- let counter = 0;
55
- const domElement = document.getElementById(targetId);
56
- setInterval(() => {
57
- domElement.innerText =
58
- "Naughty interval logged: " + counter++;
59
- }, 200);
60
- }
61
-
62
- render() {
63
- return <View>NaughtyComponent here</View>;
64
- }
65
- }
66
-
67
- const example = (
68
- <IDProvider>
69
- {(id) => (
70
- <View>
71
- <Unmounter>
72
- <MyNaughtyComponent targetId={id} />
73
- </Unmounter>
74
- <View>
75
- <View id={id}></View>
76
- </View>
77
- </View>
78
- )}
79
- </IDProvider>
80
- );
81
- const tree = renderer.create(example).toJSON();
82
- expect(tree).toMatchSnapshot();
83
- });
84
-
85
- it("example 2", () => {
86
- class Unmounter extends React.Component {
87
- constructor() {
88
- super();
89
- this.state = {
90
- mountKids: true,
91
- };
92
- }
93
-
94
- maybeRenderKids() {
95
- if (this.state.mountKids) {
96
- return (
97
- <React.Fragment>
98
- <Button onClick={() => this.onClick()}>
99
- Unmount
100
- </Button>
101
- {this.props.children}
102
- </React.Fragment>
103
- );
104
- } else {
105
- return "Children unmounted";
106
- }
107
- }
108
-
109
- onClick() {
110
- this.setState({
111
- mountKids: false,
112
- });
113
- }
114
-
115
- render() {
116
- return <View>{this.maybeRenderKids()}</View>;
117
- }
118
- }
119
-
120
- class MyGoodComponent extends React.Component {
121
- componentDidMount() {
122
- const {targetId, schedule} = this.props;
123
- let counter = 0;
124
- const domElement = document.getElementById(targetId);
125
- schedule.interval(() => {
126
- domElement.innerText =
127
- "Naughty interval logged: " + counter++;
128
- }, 200);
129
- }
130
-
131
- render() {
132
- return <View>GoodComponent here</View>;
133
- }
134
- }
135
-
136
- const MyGoodComponentWithScheduler =
137
- withActionScheduler(MyGoodComponent);
138
- const example = (
139
- <IDProvider>
140
- {(id) => (
141
- <View>
142
- <Unmounter>
143
- <MyGoodComponentWithScheduler targetId={id} />
144
- </Unmounter>
145
- <View>
146
- <View id={id}></View>
147
- </View>
148
- </View>
149
- )}
150
- </IDProvider>
151
- );
152
- const tree = renderer.create(example).toJSON();
153
- expect(tree).toMatchSnapshot();
154
- });
155
- });
@@ -1,18 +0,0 @@
1
- This is a higher order component (HOC) that attaches the given component to an
2
- [`IScheduleActions`](#ischeduleactions) instance. Any actions scheduled will automatically be
3
- cleared on unmount. This allows for "set it and forget it" behavior that won't
4
- leave timers dangling when the component's lifecycle ends.
5
-
6
- For more details on using this component and the [`IScheduleActions`](#ischeduleactions) interface,
7
- see the [API overview](#timing-api-overview).
8
-
9
- ### Flow Types
10
- If you are using Flow typing, you can use the `WithActionSchedulerProps` type
11
- to build the props for the component that you will pass to the `withActionScheduler`
12
- function by spreading the type into your component's `Props` type.
13
-
14
- The added `schedule` prop is of type [`IScheduleActions`](#ischeduleactions). This is what the
15
- `withActionScheduler` function injects to your component.
16
-
17
- The returned value from `withActionScheduler` is a React component with props of
18
- type `TProps`.
@@ -1,80 +0,0 @@
1
- import {Meta, Story, Source, Canvas} from "@storybook/addon-docs";
2
-
3
- import {Body, HeadingSmall} from "@khanacademy/wonder-blocks-typography";
4
- import {View} from "@khanacademy/wonder-blocks-core";
5
- import Button from "@khanacademy/wonder-blocks-button";
6
-
7
- import {useInterval} from "./use-interval.js";
8
-
9
- <Meta
10
- title="Timing/useInterval"
11
- parameters={{
12
- chromatic: {
13
- disableSnapshot: true,
14
- },
15
- }}
16
- />
17
-
18
- # `useInterval`
19
-
20
- `useInterval` is a hook that provides a simple API for using intervals safely.
21
- It is defined as follows:
22
-
23
- ```ts
24
- function useInterval(
25
- action: () => mixed,
26
- intervalMs: number,
27
- active: boolean,
28
- ): void;
29
- ```
30
-
31
- Notes:
32
-
33
- - Setting `active` to `true` will start the interval and setting it to `false`
34
- will stop it
35
- - Changing the value of `timeoutMs` will reset the interval, changing `action`
36
- will not.
37
-
38
- export const BasicUsage = () => {
39
- const [callCount, setCallCount] = React.useState(0);
40
- const [active, setActive] = React.useState(false);
41
- const callback = React.useCallback(() => {
42
- setCallCount((callCount) => callCount + 1);
43
- }, []);
44
- useInterval(callback, 1000, active);
45
- return (
46
- <View>
47
- <View>callCount = {callCount}</View>
48
- <View>active = {active.toString()}</View>
49
- <Button onClick={() => setActive(!active)} style={{width: 200}}>
50
- Toggle active
51
- </Button>
52
- </View>
53
- );
54
- };
55
-
56
- <Canvas>
57
- <Story name="BasicUsage">
58
- <BasicUsage />
59
- </Story>
60
- </Canvas>
61
-
62
- ```jsx
63
- export const BasicUsage = () => {
64
- const [callCount, setCallCount] = React.useState(0);
65
- const [active, setActive] = React.useState(false);
66
- const callback = React.useCallback(() => {
67
- setCallCount((callCount) => callCount + 1);
68
- }, []);
69
- useInterval(callback, 1000, active);
70
- return (
71
- <View>
72
- <View>callCount = {callCount}</View>
73
- <View>active = {active.toString()}</View>
74
- <Button onClick={() => setActive(!active)} style={{width: 200}}>
75
- Toggle active
76
- </Button>
77
- </View>
78
- );
79
- };
80
- ```