@khanacademy/wonder-blocks-core 8.0.0 → 9.0.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # @khanacademy/wonder-blocks-core
2
2
 
3
+ ## 9.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - f4abd572: - Remove `RenderState.Root` from exported enum
8
+ - Change `useRenderState` to only return `RenderState.Initial` or `RenderState.Standard`
9
+
3
10
  ## 8.0.0
4
11
 
5
12
  ### Major Changes
@@ -1,5 +1,5 @@
1
1
  import * as React from "react";
2
- import { RenderState } from "./render-state-context";
2
+ import { RenderStateInternal } from "./render-state-context";
3
3
  /**
4
4
  * We use render functions so that we don't do any work unless we need to.
5
5
  * This avoids rendering but not mounting potentially complex component trees.
@@ -56,7 +56,7 @@ export default class InitialFallback extends React.Component<Props, State> {
56
56
  componentDidMount(): void;
57
57
  _isTheRootComponent: boolean;
58
58
  _renderAsRootComponent(): React.ReactNode;
59
- _maybeRender(renderState: typeof RenderState[keyof typeof RenderState]): React.ReactNode;
59
+ _maybeRender(renderState: RenderStateInternal): React.ReactNode;
60
60
  render(): React.ReactNode;
61
61
  }
62
62
  export {};
@@ -1,7 +1,52 @@
1
1
  import * as React from "react";
2
+ /**
3
+ * The possible states of rendering.
4
+ *
5
+ * This is used to determine if we are rendering our initial, hydrateable state
6
+ * or not. Initial renders must be consistent between the server and client so
7
+ * that hydration will succeed.
8
+ *
9
+ * We use a render state like this instead of a simple check for being mounted
10
+ * or not, or some other way of each component knowing if it is rendering itself
11
+ * for the first time so that we can avoid cascading initial renders where each
12
+ * component has to render itself and its children multiple times to reach a
13
+ * stable state. Instead, we track the initial render from the root of the tree
14
+ * and switch everything accordingly so that there are fewer additional renders.
15
+ */
2
16
  export declare enum RenderState {
17
+ /**
18
+ * The initial render, either on the server or client.
19
+ */
20
+ Initial = "initial",
21
+ /**
22
+ * Any render after the initial render. Only occurs on the client.
23
+ */
24
+ Standard = "standard"
25
+ }
26
+ /**
27
+ * The internal states of rendering.
28
+ *
29
+ * This is different to the `RenderState` enum as this is internal to the
30
+ * Core package and solely for components that are going to provide new values
31
+ * to the render state context.
32
+ */
33
+ export declare enum RenderStateInternal {
34
+ /**
35
+ * This is the root state. It indicates that nothing has actually changed
36
+ * then context value that tracks this. This is used solely by components
37
+ * that control the rendering state to know that they are in charge of
38
+ * that process.
39
+ */
3
40
  Root = "root",
41
+ /**
42
+ * This indicates something has taken charge of the rendering state and
43
+ * components should render their initial render state that is hydrateable.
44
+ */
4
45
  Initial = "initial",
46
+ /**
47
+ * This indicates that things are now rendering after the initial render
48
+ * and components can render without worrying about hydration.
49
+ */
5
50
  Standard = "standard"
6
51
  }
7
52
  /**
@@ -19,5 +64,5 @@ export declare enum RenderState {
19
64
  * standard:
20
65
  * means that we're all now doing non-SSR rendering
21
66
  */
22
- declare const RenderStateContext: React.Context<RenderState>;
67
+ declare const RenderStateContext: React.Context<RenderStateInternal>;
23
68
  export { RenderStateContext };
package/dist/es/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import _extends from '@babel/runtime/helpers/extends';
2
2
  import _objectWithoutPropertiesLoose from '@babel/runtime/helpers/objectWithoutPropertiesLoose';
3
3
  import * as React from 'react';
4
- import { useContext, useRef, useEffect as useEffect$1 } from 'react';
4
+ import { useContext as useContext$1, useRef, useEffect as useEffect$1 } from 'react';
5
5
  import { StyleSheet, css } from 'aphrodite';
6
6
 
7
7
  function flatten(list) {
@@ -175,12 +175,17 @@ const View = React.forwardRef(function View(props, ref) {
175
175
  });
176
176
 
177
177
  let RenderState = function (RenderState) {
178
- RenderState["Root"] = "root";
179
178
  RenderState["Initial"] = "initial";
180
179
  RenderState["Standard"] = "standard";
181
180
  return RenderState;
182
181
  }({});
183
- const RenderStateContext = React.createContext(RenderState.Root);
182
+ let RenderStateInternal = function (RenderStateInternal) {
183
+ RenderStateInternal["Root"] = "root";
184
+ RenderStateInternal["Initial"] = "initial";
185
+ RenderStateInternal["Standard"] = "standard";
186
+ return RenderStateInternal;
187
+ }({});
188
+ const RenderStateContext = React.createContext(RenderStateInternal.Root);
184
189
  RenderStateContext.displayName = "RenderStateContext";
185
190
 
186
191
  class InitialFallback extends React.Component {
@@ -209,12 +214,12 @@ class InitialFallback extends React.Component {
209
214
  this._isTheRootComponent = true;
210
215
  if (mounted) {
211
216
  return React.createElement(RenderStateContext.Provider, {
212
- value: RenderState.Standard
217
+ value: RenderStateInternal.Standard
213
218
  }, children());
214
219
  }
215
220
  if (fallback) {
216
221
  return React.createElement(RenderStateContext.Provider, {
217
- value: RenderState.Initial
222
+ value: RenderStateInternal.Initial
218
223
  }, fallback());
219
224
  }
220
225
  return null;
@@ -225,17 +230,17 @@ class InitialFallback extends React.Component {
225
230
  fallback
226
231
  } = this.props;
227
232
  switch (renderState) {
228
- case RenderState.Root:
233
+ case RenderStateInternal.Root:
229
234
  return this._renderAsRootComponent();
230
- case RenderState.Initial:
235
+ case RenderStateInternal.Initial:
231
236
  return fallback ? fallback() : null;
232
- case RenderState.Standard:
237
+ case RenderStateInternal.Standard:
233
238
  return children();
234
239
  }
235
240
  {
236
241
  var _JSON$stringify;
237
242
  console.log(`We got a render state we don't understand: "${(_JSON$stringify = JSON.stringify(renderState)) != null ? _JSON$stringify : ""}"`);
238
- return this._maybeRender(RenderState.Root);
243
+ return this._maybeRender(RenderStateInternal.Root);
239
244
  }
240
245
  }
241
246
  render() {
@@ -340,14 +345,18 @@ var Server = {
340
345
  }
341
346
  };
342
347
 
343
- const useRenderState = () => useContext(RenderStateContext);
348
+ const useRenderState = () => {
349
+ const rawRenderState = useContext$1(RenderStateContext);
350
+ if (rawRenderState === RenderStateInternal.Standard) {
351
+ return RenderState.Standard;
352
+ } else {
353
+ return RenderState.Initial;
354
+ }
355
+ };
344
356
 
345
357
  const useUniqueIdWithMock = scope => {
346
358
  const renderState = useRenderState();
347
359
  const idFactory = useRef(null);
348
- if (renderState === RenderState.Root) {
349
- throw new Error("Components using useUniqueIdWithMock() should be descendants of <RenderStateRoot>");
350
- }
351
360
  if (renderState === RenderState.Initial) {
352
361
  return SsrIDFactory$1;
353
362
  }
@@ -359,9 +368,6 @@ const useUniqueIdWithMock = scope => {
359
368
  const useUniqueIdWithoutMock = scope => {
360
369
  const renderState = useRenderState();
361
370
  const idFactory = useRef(null);
362
- if (renderState === RenderState.Root) {
363
- throw new Error("Components using useUniqueIdWithoutMock() should be descendants of <RenderStateRoot>");
364
- }
365
371
  if (renderState === RenderState.Initial) {
366
372
  return null;
367
373
  }
@@ -422,24 +428,25 @@ const usePreHydrationEffect = effect => {
422
428
 
423
429
  const {
424
430
  useEffect,
425
- useState
431
+ useState,
432
+ useContext
426
433
  } = React;
427
434
  const RenderStateRoot = ({
428
435
  children,
429
436
  throwIfNested: _throwIfNested = true
430
437
  }) => {
431
438
  const [firstRender, setFirstRender] = useState(true);
432
- const renderState = useRenderState();
439
+ const renderState = useContext(RenderStateContext);
433
440
  useEffect(() => {
434
441
  setFirstRender(false);
435
442
  }, []);
436
- if (renderState !== RenderState.Root) {
443
+ if (renderState !== RenderStateInternal.Root) {
437
444
  if (_throwIfNested) {
438
445
  throw new Error("There's already a <RenderStateRoot> above this instance in " + "the render tree. This instance should be removed.");
439
446
  }
440
447
  return React.createElement(React.Fragment, null, children);
441
448
  }
442
- const value = firstRender ? RenderState.Initial : RenderState.Standard;
449
+ const value = firstRender ? RenderStateInternal.Initial : RenderStateInternal.Standard;
443
450
  return React.createElement(RenderStateContext.Provider, {
444
451
  value: value
445
452
  }, children);
@@ -1,2 +1,2 @@
1
1
  import { RenderState } from "../components/render-state-context";
2
- export declare const useRenderState: () => (typeof RenderState)[keyof typeof RenderState];
2
+ export declare const useRenderState: () => RenderState;
package/dist/index.js CHANGED
@@ -202,12 +202,17 @@ const View = React__namespace.forwardRef(function View(props, ref) {
202
202
  });
203
203
 
204
204
  let RenderState = function (RenderState) {
205
- RenderState["Root"] = "root";
206
205
  RenderState["Initial"] = "initial";
207
206
  RenderState["Standard"] = "standard";
208
207
  return RenderState;
209
208
  }({});
210
- const RenderStateContext = React__namespace.createContext(RenderState.Root);
209
+ let RenderStateInternal = function (RenderStateInternal) {
210
+ RenderStateInternal["Root"] = "root";
211
+ RenderStateInternal["Initial"] = "initial";
212
+ RenderStateInternal["Standard"] = "standard";
213
+ return RenderStateInternal;
214
+ }({});
215
+ const RenderStateContext = React__namespace.createContext(RenderStateInternal.Root);
211
216
  RenderStateContext.displayName = "RenderStateContext";
212
217
 
213
218
  class InitialFallback extends React__namespace.Component {
@@ -236,12 +241,12 @@ class InitialFallback extends React__namespace.Component {
236
241
  this._isTheRootComponent = true;
237
242
  if (mounted) {
238
243
  return React__namespace.createElement(RenderStateContext.Provider, {
239
- value: RenderState.Standard
244
+ value: RenderStateInternal.Standard
240
245
  }, children());
241
246
  }
242
247
  if (fallback) {
243
248
  return React__namespace.createElement(RenderStateContext.Provider, {
244
- value: RenderState.Initial
249
+ value: RenderStateInternal.Initial
245
250
  }, fallback());
246
251
  }
247
252
  return null;
@@ -252,17 +257,17 @@ class InitialFallback extends React__namespace.Component {
252
257
  fallback
253
258
  } = this.props;
254
259
  switch (renderState) {
255
- case RenderState.Root:
260
+ case RenderStateInternal.Root:
256
261
  return this._renderAsRootComponent();
257
- case RenderState.Initial:
262
+ case RenderStateInternal.Initial:
258
263
  return fallback ? fallback() : null;
259
- case RenderState.Standard:
264
+ case RenderStateInternal.Standard:
260
265
  return children();
261
266
  }
262
267
  {
263
268
  var _JSON$stringify;
264
269
  console.log(`We got a render state we don't understand: "${(_JSON$stringify = JSON.stringify(renderState)) != null ? _JSON$stringify : ""}"`);
265
- return this._maybeRender(RenderState.Root);
270
+ return this._maybeRender(RenderStateInternal.Root);
266
271
  }
267
272
  }
268
273
  render() {
@@ -367,14 +372,18 @@ var Server = {
367
372
  }
368
373
  };
369
374
 
370
- const useRenderState = () => React.useContext(RenderStateContext);
375
+ const useRenderState = () => {
376
+ const rawRenderState = React.useContext(RenderStateContext);
377
+ if (rawRenderState === RenderStateInternal.Standard) {
378
+ return RenderState.Standard;
379
+ } else {
380
+ return RenderState.Initial;
381
+ }
382
+ };
371
383
 
372
384
  const useUniqueIdWithMock = scope => {
373
385
  const renderState = useRenderState();
374
386
  const idFactory = React.useRef(null);
375
- if (renderState === RenderState.Root) {
376
- throw new Error("Components using useUniqueIdWithMock() should be descendants of <RenderStateRoot>");
377
- }
378
387
  if (renderState === RenderState.Initial) {
379
388
  return SsrIDFactory$1;
380
389
  }
@@ -386,9 +395,6 @@ const useUniqueIdWithMock = scope => {
386
395
  const useUniqueIdWithoutMock = scope => {
387
396
  const renderState = useRenderState();
388
397
  const idFactory = React.useRef(null);
389
- if (renderState === RenderState.Root) {
390
- throw new Error("Components using useUniqueIdWithoutMock() should be descendants of <RenderStateRoot>");
391
- }
392
398
  if (renderState === RenderState.Initial) {
393
399
  return null;
394
400
  }
@@ -449,24 +455,25 @@ const usePreHydrationEffect = effect => {
449
455
 
450
456
  const {
451
457
  useEffect,
452
- useState
458
+ useState,
459
+ useContext
453
460
  } = React__namespace;
454
461
  const RenderStateRoot = ({
455
462
  children,
456
463
  throwIfNested: _throwIfNested = true
457
464
  }) => {
458
465
  const [firstRender, setFirstRender] = useState(true);
459
- const renderState = useRenderState();
466
+ const renderState = useContext(RenderStateContext);
460
467
  useEffect(() => {
461
468
  setFirstRender(false);
462
469
  }, []);
463
- if (renderState !== RenderState.Root) {
470
+ if (renderState !== RenderStateInternal.Root) {
464
471
  if (_throwIfNested) {
465
472
  throw new Error("There's already a <RenderStateRoot> above this instance in " + "the render tree. This instance should be removed.");
466
473
  }
467
474
  return React__namespace.createElement(React__namespace.Fragment, null, children);
468
475
  }
469
- const value = firstRender ? RenderState.Initial : RenderState.Standard;
476
+ const value = firstRender ? RenderStateInternal.Initial : RenderStateInternal.Standard;
470
477
  return React__namespace.createElement(RenderStateContext.Provider, {
471
478
  value: value
472
479
  }, children);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@khanacademy/wonder-blocks-core",
3
- "version": "8.0.0",
3
+ "version": "9.0.0",
4
4
  "design": "v1",
5
5
  "publishConfig": {
6
6
  "access": "public"