@khanacademy/wonder-blocks-core 4.6.1 → 4.6.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @khanacademy/wonder-blocks-core
2
2
 
3
+ ## 4.6.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 496119f2: Cleanup WB interdependencies
8
+
3
9
  ## 4.6.1
4
10
 
5
11
  ## 4.6.0
package/dist/index.js ADDED
@@ -0,0 +1,550 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var _extends = require('@babel/runtime/helpers/extends');
6
+ var _objectWithoutPropertiesLoose = require('@babel/runtime/helpers/objectWithoutPropertiesLoose');
7
+ var React = require('react');
8
+ var aphrodite = require('aphrodite');
9
+
10
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
+
12
+ function _interopNamespace(e) {
13
+ if (e && e.__esModule) return e;
14
+ var n = Object.create(null);
15
+ if (e) {
16
+ Object.keys(e).forEach(function (k) {
17
+ if (k !== 'default') {
18
+ var d = Object.getOwnPropertyDescriptor(e, k);
19
+ Object.defineProperty(n, k, d.get ? d : {
20
+ enumerable: true,
21
+ get: function () { return e[k]; }
22
+ });
23
+ }
24
+ });
25
+ }
26
+ n["default"] = e;
27
+ return Object.freeze(n);
28
+ }
29
+
30
+ var _extends__default = /*#__PURE__*/_interopDefaultLegacy(_extends);
31
+ var _objectWithoutPropertiesLoose__default = /*#__PURE__*/_interopDefaultLegacy(_objectWithoutPropertiesLoose);
32
+ var React__namespace = /*#__PURE__*/_interopNamespace(React);
33
+
34
+ function flatten(list) {
35
+ const result = [];
36
+
37
+ if (!list) {
38
+ return result;
39
+ } else if (Array.isArray(list)) {
40
+ for (const item of list) {
41
+ result.push.apply(result, flatten(item));
42
+ }
43
+ } else {
44
+ result.push(list);
45
+ }
46
+
47
+ return result;
48
+ }
49
+
50
+ function processStyleList(style) {
51
+ const stylesheetStyles = [];
52
+ const inlineStyles = [];
53
+
54
+ if (!style) {
55
+ return {
56
+ style: {},
57
+ className: ""
58
+ };
59
+ }
60
+
61
+ const shouldInlineStyles = typeof global !== "undefined" && global.SNAPSHOT_INLINE_APHRODITE;
62
+ flatten(style).forEach(child => {
63
+ const _definition = child._definition;
64
+
65
+ if (_definition != null) {
66
+ if (shouldInlineStyles) {
67
+ const def = {};
68
+
69
+ for (const [key, value] of Object.entries(_definition)) {
70
+ def[key.replace(/-[a-z]/g, match => match[1].toUpperCase())] = value;
71
+ }
72
+
73
+ inlineStyles.push(def);
74
+ } else {
75
+ stylesheetStyles.push(child);
76
+ }
77
+ } else {
78
+ inlineStyles.push(child);
79
+ }
80
+ });
81
+ const inlineStylesObject = Object.assign.apply(Object, [{}].concat(inlineStyles));
82
+
83
+ if (inlineStyles.length > 0 && !shouldInlineStyles) {
84
+ const inlineStylesStyleSheet = aphrodite.StyleSheet.create({
85
+ inlineStyles: inlineStylesObject
86
+ });
87
+ stylesheetStyles.push(inlineStylesStyleSheet.inlineStyles);
88
+ }
89
+
90
+ return {
91
+ style: shouldInlineStyles ? inlineStylesObject : {},
92
+ className: aphrodite.css.apply(void 0, stylesheetStyles)
93
+ };
94
+ }
95
+
96
+ const _excluded$2 = ["children", "style", "tag", "testId"];
97
+ const isHeaderRegex = /^h[1-6]$/;
98
+ const styles$1 = aphrodite.StyleSheet.create({
99
+ text: {
100
+ WebkitFontSmoothing: "antialiased",
101
+ MozOsxFontSmoothing: "grayscale"
102
+ },
103
+ header: {
104
+ marginTop: 0,
105
+ marginBottom: 0
106
+ }
107
+ });
108
+ class Text extends React__namespace.Component {
109
+ render() {
110
+ const _this$props = this.props,
111
+ {
112
+ children,
113
+ style,
114
+ tag: Tag,
115
+ testId
116
+ } = _this$props,
117
+ otherProps = _objectWithoutPropertiesLoose__default["default"](_this$props, _excluded$2);
118
+
119
+ const isHeader = isHeaderRegex.test(Tag);
120
+ const styleAttributes = processStyleList([styles$1.text, isHeader && styles$1.header, style]);
121
+ return React__namespace.createElement(Tag, _extends__default["default"]({}, otherProps, {
122
+ style: styleAttributes.style,
123
+ className: styleAttributes.className,
124
+ "data-test-id": testId
125
+ }), children);
126
+ }
127
+
128
+ }
129
+ Text.defaultProps = {
130
+ tag: "span"
131
+ };
132
+
133
+ const _excluded$1 = ["className", "style"];
134
+ function addStyle(Component, defaultStyle) {
135
+ function StyleComponent(props) {
136
+ const {
137
+ className,
138
+ style
139
+ } = props,
140
+ tmpOtherProps = _objectWithoutPropertiesLoose__default["default"](props, _excluded$1);
141
+
142
+ const otherProps = tmpOtherProps;
143
+ const reset = typeof Component === "string" ? overrides[Component] : null;
144
+ const {
145
+ className: aphroditeClassName,
146
+ style: inlineStyles
147
+ } = processStyleList([reset, defaultStyle, style]);
148
+ return React__namespace.createElement(Component, _extends__default["default"]({}, otherProps, {
149
+ className: [aphroditeClassName, className].filter(Boolean).join(" "),
150
+ style: inlineStyles
151
+ }));
152
+ }
153
+
154
+ return StyleComponent;
155
+ }
156
+ const overrides = aphrodite.StyleSheet.create({
157
+ button: {
158
+ margin: 0,
159
+ "::-moz-focus-inner": {
160
+ border: 0
161
+ }
162
+ }
163
+ });
164
+
165
+ const _excluded = ["testId", "tag"];
166
+ const styles = aphrodite.StyleSheet.create({
167
+ default: {
168
+ alignItems: "stretch",
169
+ borderWidth: 0,
170
+ borderStyle: "solid",
171
+ boxSizing: "border-box",
172
+ display: "flex",
173
+ flexDirection: "column",
174
+ margin: 0,
175
+ padding: 0,
176
+ position: "relative",
177
+ zIndex: 0,
178
+ minHeight: 0,
179
+ minWidth: 0
180
+ }
181
+ });
182
+ const StyledDiv = addStyle("div", styles.default);
183
+ const StyledArticle = addStyle("article", styles.default);
184
+ const StyledAside = addStyle("aside", styles.default);
185
+ const StyledNav = addStyle("nav", styles.default);
186
+ const StyledSection = addStyle("section", styles.default);
187
+ class View extends React__namespace.Component {
188
+ render() {
189
+ const _this$props = this.props,
190
+ {
191
+ testId,
192
+ tag
193
+ } = _this$props,
194
+ restProps = _objectWithoutPropertiesLoose__default["default"](_this$props, _excluded);
195
+
196
+ const props = _extends__default["default"]({}, restProps, {
197
+ "data-test-id": testId
198
+ });
199
+
200
+ switch (tag) {
201
+ case "article":
202
+ return React__namespace.createElement(StyledArticle, props);
203
+
204
+ case "aside":
205
+ return React__namespace.createElement(StyledAside, props);
206
+
207
+ case "nav":
208
+ return React__namespace.createElement(StyledNav, props);
209
+
210
+ case "section":
211
+ return React__namespace.createElement(StyledSection, props);
212
+
213
+ case "div":
214
+ return React__namespace.createElement(StyledDiv, props);
215
+
216
+ default:
217
+ throw Error(`${tag} is not an allowed value for the 'tag' prop`);
218
+ }
219
+ }
220
+
221
+ }
222
+ View.defaultProps = {
223
+ tag: "div"
224
+ };
225
+
226
+ const RenderState = require("flow-enums-runtime")({
227
+ Root: "root",
228
+ Initial: "initial",
229
+ Standard: "standard"
230
+ });
231
+ const RenderStateContext = React__namespace.createContext(RenderState.Root);
232
+
233
+ class WithSSRPlaceholder extends React__namespace.Component {
234
+ constructor(...args) {
235
+ super(...args);
236
+ this.state = {
237
+ mounted: false
238
+ };
239
+ this._isTheRootComponent = false;
240
+ }
241
+
242
+ componentDidMount() {
243
+ if (this._isTheRootComponent) {
244
+ this.setState({
245
+ mounted: true
246
+ });
247
+ }
248
+ }
249
+
250
+ _renderAsRootComponent() {
251
+ const {
252
+ mounted
253
+ } = this.state;
254
+ const {
255
+ children,
256
+ placeholder
257
+ } = this.props;
258
+ this._isTheRootComponent = true;
259
+
260
+ if (mounted) {
261
+ return React__namespace.createElement(RenderStateContext.Provider, {
262
+ value: RenderState.Standard
263
+ }, children());
264
+ }
265
+
266
+ if (placeholder) {
267
+ return React__namespace.createElement(RenderStateContext.Provider, {
268
+ value: RenderState.Initial
269
+ }, placeholder());
270
+ }
271
+
272
+ return null;
273
+ }
274
+
275
+ _maybeRender(renderState) {
276
+ const {
277
+ children,
278
+ placeholder
279
+ } = this.props;
280
+
281
+ switch (renderState) {
282
+ case RenderState.Root:
283
+ return this._renderAsRootComponent();
284
+
285
+ case RenderState.Initial:
286
+ if (placeholder) {
287
+ return placeholder();
288
+ }
289
+
290
+ return null;
291
+
292
+ case RenderState.Standard:
293
+ return children();
294
+ }
295
+
296
+ {
297
+ var _JSON$stringify;
298
+
299
+ console.log(`We got a render state we don't understand: "${(_JSON$stringify = JSON.stringify(renderState)) != null ? _JSON$stringify : ""}"`);
300
+ return this._maybeRender(RenderState.Root);
301
+ }
302
+ }
303
+
304
+ render() {
305
+ return React__namespace.createElement(RenderStateContext.Consumer, null, value => this._maybeRender(value));
306
+ }
307
+
308
+ }
309
+
310
+ class UniqueIDFactory {
311
+ constructor(scope) {
312
+ this.get = key => {
313
+ const normalizedKey = key.toLowerCase();
314
+
315
+ if (!this._hasValidIdChars(key)) {
316
+ throw new Error(`Invalid identifier key: ${key}`);
317
+ }
318
+
319
+ return `${this._uniqueFactoryName}-${normalizedKey}`;
320
+ };
321
+
322
+ scope = typeof scope === "string" ? scope : "";
323
+ const normalizedScope = scope.toLowerCase();
324
+
325
+ if (!this._hasValidIdChars(normalizedScope)) {
326
+ throw new Error(`Invalid factory scope: ${scope}`);
327
+ }
328
+
329
+ this._uniqueFactoryName = `uid-${normalizedScope}-${UniqueIDFactory._factoryUniquenessCounter++}`;
330
+ }
331
+
332
+ _hasValidIdChars(value) {
333
+ if (typeof value !== "string") {
334
+ return false;
335
+ }
336
+
337
+ const invalidCharsReplaced = value.replace(/[^\d\w-]/g, "-");
338
+ return value === invalidCharsReplaced;
339
+ }
340
+
341
+ }
342
+ UniqueIDFactory._factoryUniquenessCounter = 0;
343
+
344
+ class SsrIDFactory {
345
+ get(id) {
346
+ return id;
347
+ }
348
+
349
+ }
350
+
351
+ SsrIDFactory.Default = new SsrIDFactory();
352
+ var SsrIDFactory$1 = SsrIDFactory.Default;
353
+
354
+ class UniqueIDProvider extends React__namespace.Component {
355
+ _performRender(firstRender) {
356
+ const {
357
+ children,
358
+ mockOnFirstRender,
359
+ scope
360
+ } = this.props;
361
+
362
+ if (firstRender) {
363
+ if (mockOnFirstRender) {
364
+ return children(SsrIDFactory$1);
365
+ }
366
+
367
+ return null;
368
+ }
369
+
370
+ if (!this._idFactory) {
371
+ this._idFactory = new UniqueIDFactory(scope);
372
+ }
373
+
374
+ return children(this._idFactory);
375
+ }
376
+
377
+ render() {
378
+ return React__namespace.createElement(WithSSRPlaceholder, {
379
+ placeholder: () => this._performRender(true)
380
+ }, () => this._performRender(false));
381
+ }
382
+
383
+ }
384
+
385
+ class IDProvider extends React__namespace.Component {
386
+ renderChildren(ids) {
387
+ const {
388
+ id,
389
+ children
390
+ } = this.props;
391
+ const uniqueId = ids ? ids.get(IDProvider.defaultId) : id;
392
+
393
+ if (!uniqueId) {
394
+ throw new Error("Did not get an identifier factory nor a id prop");
395
+ }
396
+
397
+ return children(uniqueId);
398
+ }
399
+
400
+ render() {
401
+ const {
402
+ id,
403
+ scope
404
+ } = this.props;
405
+
406
+ if (id) {
407
+ return this.renderChildren();
408
+ } else {
409
+ return React__namespace.createElement(UniqueIDProvider, {
410
+ scope: scope,
411
+ mockOnFirstRender: true
412
+ }, ids => this.renderChildren(ids));
413
+ }
414
+ }
415
+
416
+ }
417
+ IDProvider.defaultId = "wb-id";
418
+
419
+ let serverSide = false;
420
+ var server = {
421
+ isServerSide: () => serverSide,
422
+ setServerSide: () => {
423
+ serverSide = true;
424
+ }
425
+ };
426
+
427
+ const useRenderState = () => React.useContext(RenderStateContext);
428
+
429
+ const useUniqueIdWithMock = scope => {
430
+ const renderState = useRenderState();
431
+ const idFactory = React.useRef(null);
432
+
433
+ if (renderState === RenderState.Root) {
434
+ throw new Error("Components using useUniqueIdWithMock() should be descendants of <RenderStateRoot>");
435
+ }
436
+
437
+ if (renderState === RenderState.Initial) {
438
+ return SsrIDFactory$1;
439
+ }
440
+
441
+ if (!idFactory.current) {
442
+ idFactory.current = new UniqueIDFactory(scope);
443
+ }
444
+
445
+ return idFactory.current;
446
+ };
447
+ const useUniqueIdWithoutMock = scope => {
448
+ const renderState = useRenderState();
449
+ const idFactory = React.useRef(null);
450
+
451
+ if (renderState === RenderState.Root) {
452
+ throw new Error("Components using useUniqueIdWithoutMock() should be descendants of <RenderStateRoot>");
453
+ }
454
+
455
+ if (renderState === RenderState.Initial) {
456
+ return null;
457
+ }
458
+
459
+ if (!idFactory.current) {
460
+ idFactory.current = new UniqueIDFactory(scope);
461
+ }
462
+
463
+ return idFactory.current;
464
+ };
465
+
466
+ const useForceUpdate = () => {
467
+ const [, setState] = React__namespace.useState(false);
468
+ const forceUpdate = React__namespace.useCallback(() => setState(state => !state), []);
469
+ return forceUpdate;
470
+ };
471
+
472
+ const useOnMountEffect = callback => {
473
+ React__namespace.useEffect(callback, []);
474
+ };
475
+
476
+ const useIsMounted = () => {
477
+ const isMounted = React__namespace.useRef(false);
478
+ useOnMountEffect(() => {
479
+ isMounted.current = true;
480
+ return () => {
481
+ isMounted.current = false;
482
+ };
483
+ });
484
+ return React__namespace.useCallback(() => isMounted.current, []);
485
+ };
486
+
487
+ const useOnline = () => {
488
+ const forceUpdate = useForceUpdate();
489
+ React.useEffect(() => {
490
+ const handleChange = () => forceUpdate();
491
+
492
+ window.addEventListener("online", handleChange);
493
+ window.addEventListener("offline", handleChange);
494
+ return () => {
495
+ window.removeEventListener("online", handleChange);
496
+ window.removeEventListener("offline", handleChange);
497
+ };
498
+ }, [forceUpdate]);
499
+ return navigator.onLine;
500
+ };
501
+
502
+ const {
503
+ useEffect,
504
+ useState
505
+ } = React__namespace;
506
+
507
+ const RenderStateRoot = ({
508
+ children,
509
+ throwIfNested
510
+ }) => {
511
+ const [firstRender, setFirstRender] = useState(true);
512
+ const renderState = useRenderState();
513
+ useEffect(() => {
514
+ setFirstRender(false);
515
+ }, []);
516
+
517
+ if (renderState !== RenderState.Root) {
518
+ if (throwIfNested) {
519
+ throw new Error("There's already a <RenderStateRoot> above this instance in " + "the render tree. This instance should be removed.");
520
+ }
521
+
522
+ return children;
523
+ }
524
+
525
+ const value = firstRender ? RenderState.Initial : RenderState.Standard;
526
+ return React__namespace.createElement(RenderStateContext.Provider, {
527
+ value: value
528
+ }, children);
529
+ };
530
+
531
+ RenderStateRoot.defaultProps = {
532
+ throwIfNested: true
533
+ };
534
+
535
+ exports.IDProvider = IDProvider;
536
+ exports.RenderState = RenderState;
537
+ exports.RenderStateRoot = RenderStateRoot;
538
+ exports.Server = server;
539
+ exports.Text = Text;
540
+ exports.UniqueIDProvider = UniqueIDProvider;
541
+ exports.View = View;
542
+ exports.WithSSRPlaceholder = WithSSRPlaceholder;
543
+ exports.addStyle = addStyle;
544
+ exports.useForceUpdate = useForceUpdate;
545
+ exports.useIsMounted = useIsMounted;
546
+ exports.useOnMountEffect = useOnMountEffect;
547
+ exports.useOnline = useOnline;
548
+ exports.useRenderState = useRenderState;
549
+ exports.useUniqueIdWithMock = useUniqueIdWithMock;
550
+ exports.useUniqueIdWithoutMock = useUniqueIdWithoutMock;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@khanacademy/wonder-blocks-core",
3
- "version": "4.6.1",
3
+ "version": "4.6.2",
4
4
  "design": "v1",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -13,8 +13,7 @@
13
13
  "test": "echo \"Error: no test specified\" && exit 1"
14
14
  },
15
15
  "dependencies": {
16
- "@babel/runtime": "^7.18.6",
17
- "@khanacademy/wonder-blocks-spacing": "^3.0.5"
16
+ "@babel/runtime": "^7.18.6"
18
17
  },
19
18
  "peerDependencies": {
20
19
  "aphrodite": "^1.2.5",
@@ -25,7 +24,7 @@
25
24
  "react-router-dom": "5.3.0"
26
25
  },
27
26
  "devDependencies": {
28
- "wb-dev-build-settings": "^0.6.0"
27
+ "wb-dev-build-settings": "^0.7.0"
29
28
  },
30
29
  "author": "",
31
30
  "license": "MIT"