@khanacademy/wonder-blocks-core 10.0.0 → 11.0.1

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,17 @@
1
1
  # @khanacademy/wonder-blocks-core
2
2
 
3
+ ## 11.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 11a0f5c6: No functional changes. Adding prepublishOnly script.
8
+
9
+ ## 11.0.0
10
+
11
+ ### Major Changes
12
+
13
+ - d23c9c5f: Delete the custom identifier generation API
14
+
3
15
  ## 10.0.0
4
16
 
5
17
  ### Major Changes
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 { useId, useContext as useContext$1, useRef, useEffect as useEffect$1 } from 'react';
4
+ import { useId, useEffect as useEffect$1, useContext as useContext$1 } from 'react';
5
5
  import { StyleSheet, css } from 'aphrodite';
6
6
 
7
7
  function flatten(list) {
@@ -248,95 +248,6 @@ class InitialFallback extends React.Component {
248
248
  }
249
249
  }
250
250
 
251
- class UniqueIDFactory {
252
- constructor(scope) {
253
- this._uniqueFactoryName = void 0;
254
- this.get = key => {
255
- const normalizedKey = key.toLowerCase();
256
- if (!this._hasValidIdChars(key)) {
257
- throw new Error(`Invalid identifier key: ${key}`);
258
- }
259
- return `${this._uniqueFactoryName}-${normalizedKey}`;
260
- };
261
- scope = typeof scope === "string" ? scope : "";
262
- const normalizedScope = scope.toLowerCase();
263
- if (!this._hasValidIdChars(normalizedScope)) {
264
- throw new Error(`Invalid factory scope: ${scope}`);
265
- }
266
- this._uniqueFactoryName = `uid-${normalizedScope}-${UniqueIDFactory._factoryUniquenessCounter++}`;
267
- }
268
- _hasValidIdChars(value) {
269
- return typeof value === "string" ? !/\s/.test(value) : false;
270
- }
271
- }
272
- UniqueIDFactory._factoryUniquenessCounter = 0;
273
-
274
- class SsrIDFactory {
275
- get(id) {
276
- return id;
277
- }
278
- }
279
- SsrIDFactory.Default = new SsrIDFactory();
280
- var SsrIDFactory$1 = SsrIDFactory.Default;
281
-
282
- class UniqueIDProvider extends React.Component {
283
- constructor(...args) {
284
- super(...args);
285
- this._idFactory = void 0;
286
- }
287
- _performRender(firstRender) {
288
- const {
289
- children,
290
- mockOnFirstRender,
291
- scope
292
- } = this.props;
293
- if (firstRender) {
294
- if (mockOnFirstRender) {
295
- return children(SsrIDFactory$1);
296
- }
297
- return null;
298
- }
299
- if (!this._idFactory) {
300
- this._idFactory = new UniqueIDFactory(scope);
301
- }
302
- return children(this._idFactory);
303
- }
304
- render() {
305
- return React.createElement(InitialFallback, {
306
- fallback: () => this._performRender(true)
307
- }, () => this._performRender(false));
308
- }
309
- }
310
-
311
- class IDProvider extends React.Component {
312
- renderChildren(ids) {
313
- const {
314
- id,
315
- children
316
- } = this.props;
317
- const uniqueId = ids ? ids.get(IDProvider.defaultId) : id;
318
- if (!uniqueId) {
319
- throw new Error("Did not get an identifier factory nor a id prop");
320
- }
321
- return children(uniqueId);
322
- }
323
- render() {
324
- const {
325
- id,
326
- scope
327
- } = this.props;
328
- if (id) {
329
- return this.renderChildren();
330
- } else {
331
- return React.createElement(UniqueIDProvider, {
332
- scope: scope,
333
- mockOnFirstRender: true
334
- }, ids => this.renderChildren(ids));
335
- }
336
- }
337
- }
338
- IDProvider.defaultId = "wb-id";
339
-
340
251
  let serverSide = false;
341
252
  var Server = {
342
253
  isServerSide: () => serverSide,
@@ -353,38 +264,6 @@ const Id = ({
353
264
  return React.createElement(React.Fragment, null, children(id != null ? id : generatedId));
354
265
  };
355
266
 
356
- const useRenderState = () => {
357
- const rawRenderState = useContext$1(RenderStateContext);
358
- if (rawRenderState === RenderStateInternal.Standard) {
359
- return RenderState.Standard;
360
- } else {
361
- return RenderState.Initial;
362
- }
363
- };
364
-
365
- const useUniqueIdWithMock = scope => {
366
- const renderState = useRenderState();
367
- const idFactory = useRef(null);
368
- if (renderState === RenderState.Initial) {
369
- return SsrIDFactory$1;
370
- }
371
- if (!idFactory.current) {
372
- idFactory.current = new UniqueIDFactory(scope);
373
- }
374
- return idFactory.current;
375
- };
376
- const useUniqueIdWithoutMock = scope => {
377
- const renderState = useRenderState();
378
- const idFactory = useRef(null);
379
- if (renderState === RenderState.Initial) {
380
- return null;
381
- }
382
- if (!idFactory.current) {
383
- idFactory.current = new UniqueIDFactory(scope);
384
- }
385
- return idFactory.current;
386
- };
387
-
388
267
  const useForceUpdate = () => {
389
268
  const [, setUpdateState] = React.useState({});
390
269
  const forceUpdate = React.useCallback(() => {
@@ -434,6 +313,15 @@ const usePreHydrationEffect = effect => {
434
313
  effectCallRef.current(effect, []);
435
314
  };
436
315
 
316
+ const useRenderState = () => {
317
+ const rawRenderState = useContext$1(RenderStateContext);
318
+ if (rawRenderState === RenderStateInternal.Standard) {
319
+ return RenderState.Standard;
320
+ } else {
321
+ return RenderState.Initial;
322
+ }
323
+ };
324
+
437
325
  const {
438
326
  useEffect,
439
327
  useState,
@@ -460,4 +348,4 @@ const RenderStateRoot = ({
460
348
  }, children);
461
349
  };
462
350
 
463
- export { IDProvider, Id, InitialFallback, RenderState, RenderStateRoot, Server, Text, UniqueIDProvider, View, addStyle, useForceUpdate, useIsMounted, useLatestRef, useOnMountEffect, useOnline, usePreHydrationEffect, useRenderState, useUniqueIdWithMock, useUniqueIdWithoutMock };
351
+ export { Id, InitialFallback, RenderState, RenderStateRoot, Server, Text, View, addStyle, useForceUpdate, useIsMounted, useLatestRef, useOnMountEffect, useOnline, usePreHydrationEffect, useRenderState };
package/dist/index.d.ts CHANGED
@@ -1,13 +1,9 @@
1
- import type { AriaProps, IIdentifierFactory, StyleType, PropsFor } from "./util/types";
2
1
  export { default as Text } from "./components/text";
3
2
  export { default as View } from "./components/view";
4
3
  export { default as InitialFallback } from "./components/initial-fallback";
5
- export { default as IDProvider } from "./components/id-provider";
6
- export { default as UniqueIDProvider } from "./components/unique-id-provider";
7
4
  export { default as addStyle } from "./util/add-style";
8
5
  export { default as Server } from "./util/server";
9
6
  export { Id } from "./components/id";
10
- export { useUniqueIdWithMock, useUniqueIdWithoutMock, } from "./hooks/use-unique-id";
11
7
  export { useForceUpdate } from "./hooks/use-force-update";
12
8
  export { useIsMounted } from "./hooks/use-is-mounted";
13
9
  export { useLatestRef } from "./hooks/use-latest-ref";
@@ -18,4 +14,4 @@ export { useRenderState } from "./hooks/use-render-state";
18
14
  export { RenderStateRoot } from "./components/render-state-root";
19
15
  export { RenderState } from "./components/render-state-context";
20
16
  export type { AriaRole, AriaAttributes } from "./util/aria-types";
21
- export type { AriaProps, IIdentifierFactory, StyleType, PropsFor };
17
+ export type { AriaProps, StyleType, PropsFor } from "./util/types";
package/dist/index.js CHANGED
@@ -275,95 +275,6 @@ class InitialFallback extends React__namespace.Component {
275
275
  }
276
276
  }
277
277
 
278
- class UniqueIDFactory {
279
- constructor(scope) {
280
- this._uniqueFactoryName = void 0;
281
- this.get = key => {
282
- const normalizedKey = key.toLowerCase();
283
- if (!this._hasValidIdChars(key)) {
284
- throw new Error(`Invalid identifier key: ${key}`);
285
- }
286
- return `${this._uniqueFactoryName}-${normalizedKey}`;
287
- };
288
- scope = typeof scope === "string" ? scope : "";
289
- const normalizedScope = scope.toLowerCase();
290
- if (!this._hasValidIdChars(normalizedScope)) {
291
- throw new Error(`Invalid factory scope: ${scope}`);
292
- }
293
- this._uniqueFactoryName = `uid-${normalizedScope}-${UniqueIDFactory._factoryUniquenessCounter++}`;
294
- }
295
- _hasValidIdChars(value) {
296
- return typeof value === "string" ? !/\s/.test(value) : false;
297
- }
298
- }
299
- UniqueIDFactory._factoryUniquenessCounter = 0;
300
-
301
- class SsrIDFactory {
302
- get(id) {
303
- return id;
304
- }
305
- }
306
- SsrIDFactory.Default = new SsrIDFactory();
307
- var SsrIDFactory$1 = SsrIDFactory.Default;
308
-
309
- class UniqueIDProvider extends React__namespace.Component {
310
- constructor(...args) {
311
- super(...args);
312
- this._idFactory = void 0;
313
- }
314
- _performRender(firstRender) {
315
- const {
316
- children,
317
- mockOnFirstRender,
318
- scope
319
- } = this.props;
320
- if (firstRender) {
321
- if (mockOnFirstRender) {
322
- return children(SsrIDFactory$1);
323
- }
324
- return null;
325
- }
326
- if (!this._idFactory) {
327
- this._idFactory = new UniqueIDFactory(scope);
328
- }
329
- return children(this._idFactory);
330
- }
331
- render() {
332
- return React__namespace.createElement(InitialFallback, {
333
- fallback: () => this._performRender(true)
334
- }, () => this._performRender(false));
335
- }
336
- }
337
-
338
- class IDProvider extends React__namespace.Component {
339
- renderChildren(ids) {
340
- const {
341
- id,
342
- children
343
- } = this.props;
344
- const uniqueId = ids ? ids.get(IDProvider.defaultId) : id;
345
- if (!uniqueId) {
346
- throw new Error("Did not get an identifier factory nor a id prop");
347
- }
348
- return children(uniqueId);
349
- }
350
- render() {
351
- const {
352
- id,
353
- scope
354
- } = this.props;
355
- if (id) {
356
- return this.renderChildren();
357
- } else {
358
- return React__namespace.createElement(UniqueIDProvider, {
359
- scope: scope,
360
- mockOnFirstRender: true
361
- }, ids => this.renderChildren(ids));
362
- }
363
- }
364
- }
365
- IDProvider.defaultId = "wb-id";
366
-
367
278
  let serverSide = false;
368
279
  var Server = {
369
280
  isServerSide: () => serverSide,
@@ -380,38 +291,6 @@ const Id = ({
380
291
  return React__namespace.createElement(React__namespace.Fragment, null, children(id != null ? id : generatedId));
381
292
  };
382
293
 
383
- const useRenderState = () => {
384
- const rawRenderState = React.useContext(RenderStateContext);
385
- if (rawRenderState === RenderStateInternal.Standard) {
386
- return RenderState.Standard;
387
- } else {
388
- return RenderState.Initial;
389
- }
390
- };
391
-
392
- const useUniqueIdWithMock = scope => {
393
- const renderState = useRenderState();
394
- const idFactory = React.useRef(null);
395
- if (renderState === RenderState.Initial) {
396
- return SsrIDFactory$1;
397
- }
398
- if (!idFactory.current) {
399
- idFactory.current = new UniqueIDFactory(scope);
400
- }
401
- return idFactory.current;
402
- };
403
- const useUniqueIdWithoutMock = scope => {
404
- const renderState = useRenderState();
405
- const idFactory = React.useRef(null);
406
- if (renderState === RenderState.Initial) {
407
- return null;
408
- }
409
- if (!idFactory.current) {
410
- idFactory.current = new UniqueIDFactory(scope);
411
- }
412
- return idFactory.current;
413
- };
414
-
415
294
  const useForceUpdate = () => {
416
295
  const [, setUpdateState] = React__namespace.useState({});
417
296
  const forceUpdate = React__namespace.useCallback(() => {
@@ -461,6 +340,15 @@ const usePreHydrationEffect = effect => {
461
340
  effectCallRef.current(effect, []);
462
341
  };
463
342
 
343
+ const useRenderState = () => {
344
+ const rawRenderState = React.useContext(RenderStateContext);
345
+ if (rawRenderState === RenderStateInternal.Standard) {
346
+ return RenderState.Standard;
347
+ } else {
348
+ return RenderState.Initial;
349
+ }
350
+ };
351
+
464
352
  const {
465
353
  useEffect,
466
354
  useState,
@@ -487,14 +375,12 @@ const RenderStateRoot = ({
487
375
  }, children);
488
376
  };
489
377
 
490
- exports.IDProvider = IDProvider;
491
378
  exports.Id = Id;
492
379
  exports.InitialFallback = InitialFallback;
493
380
  exports.RenderState = RenderState;
494
381
  exports.RenderStateRoot = RenderStateRoot;
495
382
  exports.Server = Server;
496
383
  exports.Text = Text;
497
- exports.UniqueIDProvider = UniqueIDProvider;
498
384
  exports.View = View;
499
385
  exports.addStyle = addStyle;
500
386
  exports.useForceUpdate = useForceUpdate;
@@ -504,5 +390,3 @@ exports.useOnMountEffect = useOnMountEffect;
504
390
  exports.useOnline = useOnline;
505
391
  exports.usePreHydrationEffect = usePreHydrationEffect;
506
392
  exports.useRenderState = useRenderState;
507
- exports.useUniqueIdWithMock = useUniqueIdWithMock;
508
- exports.useUniqueIdWithoutMock = useUniqueIdWithoutMock;
@@ -78,12 +78,4 @@ export type TextViewSharedProps = {
78
78
  "data-modal-launcher-portal"?: boolean;
79
79
  "data-placement"?: string;
80
80
  } & AriaProps & EventHandlers;
81
- /**
82
- * Interface implemented by types that can provide identifiers.
83
- * @deprecated Use `useId` for your ID needs. If you are in a class-based
84
- * component and cannot use hooks, then use the `Id` component.
85
- */
86
- export interface IIdentifierFactory {
87
- get(id: string): string;
88
- }
89
81
  export * from "./types.propsfor";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@khanacademy/wonder-blocks-core",
3
- "version": "10.0.0",
3
+ "version": "11.0.1",
4
4
  "design": "v1",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -10,7 +10,8 @@
10
10
  "module": "dist/es/index.js",
11
11
  "types": "dist/index.d.ts",
12
12
  "scripts": {
13
- "test": "echo \"Error: no test specified\" && exit 1"
13
+ "test": "echo \"Error: no test specified\" && exit 1",
14
+ "prepublishOnly": "../../utils/publish/package-pre-publish-check.sh"
14
15
  },
15
16
  "dependencies": {
16
17
  "@babel/runtime": "^7.18.6"
@@ -24,7 +25,7 @@
24
25
  },
25
26
  "devDependencies": {
26
27
  "@khanacademy/wb-dev-build-settings": "^2.0.0",
27
- "@khanacademy/wonder-blocks-testing-core": "^2.0.0"
28
+ "@khanacademy/wonder-blocks-testing-core": "^2.0.1"
28
29
  },
29
30
  "author": "",
30
31
  "license": "MIT"
@@ -1,60 +0,0 @@
1
- import * as React from "react";
2
- import type { IIdentifierFactory } from "../util/types";
3
- type Props = {
4
- /**
5
- * Use the children-as-function pattern to pass a uniqueId string for
6
- * use anywhere within children. This provides a way of adding a unique identifier
7
- * to a given component for a11y purposes.
8
- */
9
- children: (uniqueId: string) => React.ReactNode;
10
- /**
11
- * Scope for the unique identifier
12
- */
13
- scope: string;
14
- /**
15
- * An optional id parameter for the title. If one is
16
- * not provided, a unique id will be generated.
17
- */
18
- id?: string;
19
- /**
20
- * Test ID used for e2e testing.
21
- */
22
- testId?: string;
23
- };
24
- /**
25
- * @deprecated This component is deprecated and will be removed in an
26
- * upcoming release. Migrate existing code to use `useId` or the `Id` component.
27
- *
28
- * This is a wrapper that returns an identifier. If the `id` prop is set, the
29
- * component will return the same id to be consumed by its children. Otherwise,
30
- * a unique id will be provided. This is beneficial for accessibility purposes,
31
- * among other things.
32
- *
33
- * The main difference with `UniqueIDProvider` is that `IDProvider` has a single
34
- * responsibility, to return an identifier that can by used by the children that
35
- * are rendered internally.
36
- *
37
- * This way, the wrapped component will receive this custom ID and will use it
38
- * to connect different elements.
39
- *
40
- * e.g. It uses the same generated id to connect a Dialog with its main title,
41
- * or form label with the associated input element, etc.
42
- *
43
- * ## Usage
44
- *
45
- * ```jsx
46
- * import {IDProvider} from "@khanacademy/wonder-blocks-core";
47
- *
48
- * <IDProvider scope="field">
49
- * {(uniqueId) => (
50
- * Unique ID: {uniqueId}
51
- * )}
52
- * </IDProvider>
53
- * ```
54
- */
55
- export default class IDProvider extends React.Component<Props> {
56
- static defaultId: string;
57
- renderChildren(ids?: IIdentifierFactory): React.ReactNode;
58
- render(): React.ReactNode;
59
- }
60
- export {};
@@ -1,72 +0,0 @@
1
- import * as React from "react";
2
- import type { IIdentifierFactory } from "../util/types";
3
- type Props = {
4
- /**
5
- * A render prop that takes an instance of IIdentifierFactory and returns
6
- * the content to be rendered.
7
- *
8
- * If mockOnFirstRender is false, this is only called after
9
- * the initial render has occurred -- it will be blank for the
10
- * the first render -- and will always be called with the same
11
- * IIdentifierFactory instance.
12
- *
13
- * If mockOnFirstRender is true, this is called once with
14
- * a mock IIdentifierFactory for the initial render, and then a unique ID
15
- * factory thereafter.
16
- *
17
- * Full type with `IIdentifierFactory` definition inlined is:
18
- *
19
- * `{get(id: string): string} => React.Node`
20
- */
21
- children: (arg1: IIdentifierFactory) => React.ReactNode;
22
- /**
23
- * If mockOnFirstRender is false, children is only called
24
- * after the initial render has occurred.
25
- * If mockOnFirstRender is true, children is called once with
26
- * a mock IIdentifierFactory for the initial render, and then a unique ID
27
- * factory thereafter.
28
- */
29
- mockOnFirstRender: boolean;
30
- /**
31
- * If this prop is specified, any identifiers provided will contain the
32
- * given scope. This can be useful for making easily readable identifiers.
33
- */
34
- readonly scope?: string;
35
- };
36
- /**
37
- * @deprecated This component is deprecated and will be removed in an
38
- * upcoming release. Migrate existing code to use `useId` or the `Id` component.
39
- *
40
- * The `UniqueIDProvider` component is how Wonder Blocks components obtain
41
- * unique identifiers. This component ensures that server-side rendering and
42
- * initial client rendering match while allowing the provision of unique
43
- * identifiers for the client.
44
- *
45
- * In all but the first render, the children are rendered with the same
46
- * `IIdentifierFactory` instance, ensuring that the same calls will return the
47
- * same identifiers.
48
- *
49
- * The `get` method of the identifier factory ensures that the same identifier
50
- * is returned for like requests, but also that all identifiers provided are
51
- * unique. Therefore, `get("test")` will always equal `get("test")`, and
52
- * `get("test2")` will always equal `get("test2")`, but `get("test")` will
53
- * never equal `get("test2")`.
54
- *
55
- * ## Usage
56
- *
57
- * ```jsx
58
- * import {UniqueIDProvider} from "@khanacademy/wonder-blocks-core";
59
- *
60
- * <UniqueIDProvider mockOnFirstRender={false} scope="field">
61
- * {(ids) => (
62
- * <>The id returned for "my-identifier": {ids.get("my-identifier")}</>
63
- * )}
64
- * </UniqueIDProvider>
65
- * ```
66
- */
67
- export default class UniqueIDProvider extends React.Component<Props> {
68
- _idFactory: IIdentifierFactory;
69
- _performRender(firstRender: boolean): React.ReactNode;
70
- render(): React.ReactNode;
71
- }
72
- export {};
@@ -1,24 +0,0 @@
1
- import type { IIdentifierFactory } from "../util/types";
2
- /**
3
- * @deprecated Use `useId` for your ID needs. If you are in a class-based
4
- * component and cannot use hooks, then use the `Id` component.
5
- *
6
- * Returns a unique identifier factory. If the parent component hasn't
7
- * been mounted yet, the global SsrIDFactory will be returned until the
8
- * component becomes mounted.
9
- *
10
- * @param {string} [scope] optional string to prefix generated ids with.
11
- * @returns {IIdentifierFactory}
12
- */
13
- export declare const useUniqueIdWithMock: (scope?: string) => IIdentifierFactory;
14
- /**
15
- * @deprecated Use `useId` for your ID needs. If you are in a class-based
16
- * component and cannot use hooks, then use the `Id` component.
17
- *
18
- * Returns a unique identifier factory. If the parent component hasn't
19
- * been mounted yet, null will be returned.
20
- *
21
- * @param {string} [scope] optional string to prefix generated ids with.
22
- * @returns {?IIdentifierFactory}
23
- */
24
- export declare const useUniqueIdWithoutMock: (scope?: string) => IIdentifierFactory | null | undefined;
@@ -1,3 +0,0 @@
1
- import type { IIdentifierFactory } from "./types";
2
- declare const _default: IIdentifierFactory;
3
- export default _default;
@@ -1,44 +0,0 @@
1
- import type { IIdentifierFactory } from "./types";
2
- /**
3
- * This is NOT for direct use. Instead, see the UniqueIDProvider component.
4
- *
5
- * Implements IIdentifierFactory to provide unique identifiers.
6
- *
7
- * @deprecated Use `useId` for your ID needs. If you are in a class-based
8
- * component and cannot use hooks, then use the `Id` component.
9
- */
10
- export default class UniqueIDFactory implements IIdentifierFactory {
11
- _uniqueFactoryName: string;
12
- static _factoryUniquenessCounter: number;
13
- /**
14
- * Creates a UniqueIDFactory instance.
15
- *
16
- * @param {string} scope An optional case-insensitive scope for the
17
- * factory. This will be used as part of the identifier. Useful for
18
- * providing context to the identifiers, which can be useful in
19
- * differentiating elements when debugging the DOM. This must contain only
20
- * hyphen and alphanumeric characters.
21
- */
22
- constructor(scope?: string);
23
- /**
24
- * This method verifies that a string contains valid characters for an
25
- * identifier. It does not assert that a string IS a valid identifier (for
26
- * example, that it doesn't start with numbers). We don't need to do that
27
- * here because all identifiers are prefixed to avoid needing that check.
28
- *
29
- * According to this post:
30
- * https://stackoverflow.com/questions/70579/html-valid-id-attribute-values/31773673#31773673
31
- * The only characters that are not allowed in an id are whitespace characters.
32
- */
33
- _hasValidIdChars(value?: string | null): boolean;
34
- /**
35
- * Provides a unique identifier with the given key.
36
- *
37
- * @param {string} key The case-insensitive key of the identifier.
38
- *
39
- * @returns {string} A unique identifier that will remain the same for this
40
- * key in this factory. This must contain only hyphen and alphanumeric
41
- * characters.
42
- */
43
- get: (key: string) => string;
44
- }