@navikt/ds-react 5.15.0 → 5.15.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/_docs.json CHANGED
@@ -10641,6 +10641,107 @@
10641
10641
  }
10642
10642
  }
10643
10643
  },
10644
+ {
10645
+ "filePath": "src/util/create-context.tsx",
10646
+ "displayName": "createContext",
10647
+ "props": {
10648
+ "hookName": {
10649
+ "defaultValue": null,
10650
+ "description": "",
10651
+ "name": "hookName",
10652
+ "parent": {
10653
+ "fileName": "src/util/create-context.tsx",
10654
+ "name": "CreateContextOptions"
10655
+ },
10656
+ "declarations": [
10657
+ {
10658
+ "fileName": "src/util/create-context.tsx",
10659
+ "name": "CreateContextOptions"
10660
+ }
10661
+ ],
10662
+ "required": false,
10663
+ "type": {
10664
+ "name": "string"
10665
+ }
10666
+ },
10667
+ "providerName": {
10668
+ "defaultValue": null,
10669
+ "description": "",
10670
+ "name": "providerName",
10671
+ "parent": {
10672
+ "fileName": "src/util/create-context.tsx",
10673
+ "name": "CreateContextOptions"
10674
+ },
10675
+ "declarations": [
10676
+ {
10677
+ "fileName": "src/util/create-context.tsx",
10678
+ "name": "CreateContextOptions"
10679
+ }
10680
+ ],
10681
+ "required": false,
10682
+ "type": {
10683
+ "name": "string"
10684
+ }
10685
+ },
10686
+ "errorMessage": {
10687
+ "defaultValue": null,
10688
+ "description": "",
10689
+ "name": "errorMessage",
10690
+ "parent": {
10691
+ "fileName": "src/util/create-context.tsx",
10692
+ "name": "CreateContextOptions"
10693
+ },
10694
+ "declarations": [
10695
+ {
10696
+ "fileName": "src/util/create-context.tsx",
10697
+ "name": "CreateContextOptions"
10698
+ }
10699
+ ],
10700
+ "required": false,
10701
+ "type": {
10702
+ "name": "string"
10703
+ }
10704
+ },
10705
+ "name": {
10706
+ "defaultValue": null,
10707
+ "description": "",
10708
+ "name": "name",
10709
+ "parent": {
10710
+ "fileName": "src/util/create-context.tsx",
10711
+ "name": "CreateContextOptions"
10712
+ },
10713
+ "declarations": [
10714
+ {
10715
+ "fileName": "src/util/create-context.tsx",
10716
+ "name": "CreateContextOptions"
10717
+ }
10718
+ ],
10719
+ "required": false,
10720
+ "type": {
10721
+ "name": "string"
10722
+ }
10723
+ },
10724
+ "defaultValue": {
10725
+ "defaultValue": null,
10726
+ "description": "",
10727
+ "name": "defaultValue",
10728
+ "parent": {
10729
+ "fileName": "src/util/create-context.tsx",
10730
+ "name": "CreateContextOptions"
10731
+ },
10732
+ "declarations": [
10733
+ {
10734
+ "fileName": "src/util/create-context.tsx",
10735
+ "name": "CreateContextOptions"
10736
+ }
10737
+ ],
10738
+ "required": false,
10739
+ "type": {
10740
+ "name": "T"
10741
+ }
10742
+ }
10743
+ }
10744
+ },
10644
10745
  {
10645
10746
  "filePath": "src/date/context/useSharedMonthContext.tsx",
10646
10747
  "displayName": "SharedMonthProvider",
@@ -63,7 +63,7 @@ exports.HelpText = (0, react_1.forwardRef)((_a, ref) => {
63
63
  const mergedRef = (0, useMergeRefs_1.useMergeRefs)(buttonRef, ref);
64
64
  const [open, setOpen] = (0, react_1.useState)(false);
65
65
  return (react_1.default.createElement("div", { className: (0, clsx_1.default)("navds-help-text", wrapperClassName) },
66
- react_1.default.createElement("button", Object.assign({}, rest, { ref: mergedRef, onClick: (0, composeEventHandlers_1.composeEventHandlers)(onClick, () => setOpen((x) => x)), className: (0, clsx_1.default)(className, "navds-help-text__button"), type: "button", "aria-expanded": open }),
66
+ react_1.default.createElement("button", Object.assign({}, rest, { ref: mergedRef, onClick: (0, composeEventHandlers_1.composeEventHandlers)(onClick, () => setOpen((x) => !x)), className: (0, clsx_1.default)(className, "navds-help-text__button"), type: "button", "aria-expanded": open }),
67
67
  react_1.default.createElement(HelpTextIcon_1.HelpTextIcon, { title: title }),
68
68
  react_1.default.createElement(HelpTextIcon_1.HelpTextIcon, { filled: true, title: title })),
69
69
  react_1.default.createElement(popover_1.Popover, { onClose: () => setOpen(false), className: "navds-help-text__popover", open: open, anchorEl: buttonRef.current, placement: placement, strategy: strategy, offset: 12 },
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __rest = (this && this.__rest) || function (s, e) {
26
+ var t = {};
27
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
28
+ t[p] = s[p];
29
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
30
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
31
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
32
+ t[p[i]] = s[p[i]];
33
+ }
34
+ return t;
35
+ };
36
+ Object.defineProperty(exports, "__esModule", { value: true });
37
+ exports.createContext = void 0;
38
+ /**
39
+ * Custom createContext to consolidate context-implementation across the system
40
+ * Inspired by:
41
+ * - https://github.com/radix-ui/primitives/blob/main/packages/react/context/src/createContext.tsx
42
+ * - https://github.com/chakra-ui/chakra-ui/blob/5ec0be610b5a69afba01a9c22365155c1b519136/packages/hooks/context/src/index.ts
43
+ */
44
+ const react_1 = __importStar(require("react"));
45
+ function getErrorMessage(hook, provider) {
46
+ return `${hook} returned \`undefined\`. Seems you forgot to wrap component within ${provider}`;
47
+ }
48
+ function createContext(options = {}) {
49
+ const { name, hookName = "useContext", providerName = "Provider", errorMessage, defaultValue, } = options;
50
+ const Context = (0, react_1.createContext)(defaultValue);
51
+ function Provider(_a) {
52
+ var { children } = _a, context = __rest(_a, ["children"]);
53
+ // Only re-memoize when prop values change
54
+ // eslint-disable-next-line react-hooks/exhaustive-deps
55
+ const value = react_1.default.useMemo(() => context, Object.values(context));
56
+ return react_1.default.createElement(Context.Provider, { value: value }, children);
57
+ }
58
+ function useContext() {
59
+ var _a;
60
+ const context = (0, react_1.useContext)(Context);
61
+ if (!context) {
62
+ const error = new Error(errorMessage !== null && errorMessage !== void 0 ? errorMessage : getErrorMessage(hookName, providerName));
63
+ error.name = "ContextError";
64
+ (_a = Error.captureStackTrace) === null || _a === void 0 ? void 0 : _a.call(Error, error, useContext);
65
+ throw error;
66
+ }
67
+ return context;
68
+ }
69
+ Context.displayName = name;
70
+ return [Provider, useContext];
71
+ }
72
+ exports.createContext = createContext;
@@ -34,7 +34,7 @@ export const HelpText = forwardRef((_a, ref) => {
34
34
  const mergedRef = useMergeRefs(buttonRef, ref);
35
35
  const [open, setOpen] = useState(false);
36
36
  return (React.createElement("div", { className: cl("navds-help-text", wrapperClassName) },
37
- React.createElement("button", Object.assign({}, rest, { ref: mergedRef, onClick: composeEventHandlers(onClick, () => setOpen((x) => x)), className: cl(className, "navds-help-text__button"), type: "button", "aria-expanded": open }),
37
+ React.createElement("button", Object.assign({}, rest, { ref: mergedRef, onClick: composeEventHandlers(onClick, () => setOpen((x) => !x)), className: cl(className, "navds-help-text__button"), type: "button", "aria-expanded": open }),
38
38
  React.createElement(HelpTextIcon, { title: title }),
39
39
  React.createElement(HelpTextIcon, { filled: true, title: title })),
40
40
  React.createElement(Popover, { onClose: () => setOpen(false), className: "navds-help-text__popover", open: open, anchorEl: buttonRef.current, placement: placement, strategy: strategy, offset: 12 },
@@ -1 +1 @@
1
- {"version":3,"file":"HelpText.js","sourceRoot":"","sources":["../../src/help-text/HelpText.tsx"],"names":[],"mappings":";;;;;;;;;;;AAAA,OAAO,EAAE,MAAM,MAAM,CAAC;AACtB,OAAO,KAAK,EAAE,EAAE,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAC5D,OAAO,EAAE,OAAO,EAAgB,MAAM,YAAY,CAAC;AACnD,OAAO,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AACpE,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAiB9C;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,UAAU,CAChC,CACE,EASC,EACD,GAAG,EACH,EAAE;QAXF,EACE,SAAS,EACT,QAAQ,EACR,SAAS,EACT,QAAQ,GAAG,UAAU,EACrB,KAAK,GAAG,OAAO,EACf,OAAO,EACP,gBAAgB,OAEjB,EADI,IAAI,cART,0FASC,CADQ;IAIT,MAAM,SAAS,GAAG,MAAM,CAA2B,IAAI,CAAC,CAAC;IACzD,MAAM,SAAS,GAAG,YAAY,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC;IAE/C,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;IAExC,OAAO,CACL,6BAAK,SAAS,EAAE,EAAE,CAAC,iBAAiB,EAAE,gBAAgB,CAAC;QACrD,gDACM,IAAI,IACR,GAAG,EAAE,SAAS,EACd,OAAO,EAAE,oBAAoB,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,EAC/D,SAAS,EAAE,EAAE,CAAC,SAAS,EAAE,yBAAyB,CAAC,EACnD,IAAI,EAAC,QAAQ,mBACE,IAAI;YAEnB,oBAAC,YAAY,IAAC,KAAK,EAAE,KAAK,GAAI;YAC9B,oBAAC,YAAY,IAAC,MAAM,QAAC,KAAK,EAAE,KAAK,GAAI,CAC9B;QACT,oBAAC,OAAO,IACN,OAAO,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,EAC7B,SAAS,EAAC,0BAA0B,EACpC,IAAI,EAAE,IAAI,EACV,QAAQ,EAAE,SAAS,CAAC,OAAO,EAC3B,SAAS,EAAE,SAAS,EACpB,QAAQ,EAAE,QAAQ,EAClB,MAAM,EAAE,EAAE;YAEV,oBAAC,OAAO,CAAC,OAAO,IAAC,SAAS,EAAC,kBAAkB,IAC1C,QAAQ,CACO,CACV,CACN,CACP,CAAC;AACJ,CAAC,CACF,CAAC;AAEF,eAAe,QAAQ,CAAC"}
1
+ {"version":3,"file":"HelpText.js","sourceRoot":"","sources":["../../src/help-text/HelpText.tsx"],"names":[],"mappings":";;;;;;;;;;;AAAA,OAAO,EAAE,MAAM,MAAM,CAAC;AACtB,OAAO,KAAK,EAAE,EAAE,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAC5D,OAAO,EAAE,OAAO,EAAgB,MAAM,YAAY,CAAC;AACnD,OAAO,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AACpE,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAiB9C;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,UAAU,CAChC,CACE,EASC,EACD,GAAG,EACH,EAAE;QAXF,EACE,SAAS,EACT,QAAQ,EACR,SAAS,EACT,QAAQ,GAAG,UAAU,EACrB,KAAK,GAAG,OAAO,EACf,OAAO,EACP,gBAAgB,OAEjB,EADI,IAAI,cART,0FASC,CADQ;IAIT,MAAM,SAAS,GAAG,MAAM,CAA2B,IAAI,CAAC,CAAC;IACzD,MAAM,SAAS,GAAG,YAAY,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC;IAE/C,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;IAExC,OAAO,CACL,6BAAK,SAAS,EAAE,EAAE,CAAC,iBAAiB,EAAE,gBAAgB,CAAC;QACrD,gDACM,IAAI,IACR,GAAG,EAAE,SAAS,EACd,OAAO,EAAE,oBAAoB,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,EAChE,SAAS,EAAE,EAAE,CAAC,SAAS,EAAE,yBAAyB,CAAC,EACnD,IAAI,EAAC,QAAQ,mBACE,IAAI;YAEnB,oBAAC,YAAY,IAAC,KAAK,EAAE,KAAK,GAAI;YAC9B,oBAAC,YAAY,IAAC,MAAM,QAAC,KAAK,EAAE,KAAK,GAAI,CAC9B;QACT,oBAAC,OAAO,IACN,OAAO,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,EAC7B,SAAS,EAAC,0BAA0B,EACpC,IAAI,EAAE,IAAI,EACV,QAAQ,EAAE,SAAS,CAAC,OAAO,EAC3B,SAAS,EAAE,SAAS,EACpB,QAAQ,EAAE,QAAQ,EAClB,MAAM,EAAE,EAAE;YAEV,oBAAC,OAAO,CAAC,OAAO,IAAC,SAAS,EAAC,kBAAkB,IAC1C,QAAQ,CACO,CACV,CACN,CACP,CAAC;AACJ,CAAC,CACF,CAAC;AAEF,eAAe,QAAQ,CAAC"}
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Custom createContext to consolidate context-implementation across the system
3
+ * Inspired by:
4
+ * - https://github.com/radix-ui/primitives/blob/main/packages/react/context/src/createContext.tsx
5
+ * - https://github.com/chakra-ui/chakra-ui/blob/5ec0be610b5a69afba01a9c22365155c1b519136/packages/hooks/context/src/index.ts
6
+ */
7
+ import React from "react";
8
+ export interface CreateContextOptions<T> {
9
+ hookName?: string;
10
+ providerName?: string;
11
+ errorMessage?: string;
12
+ name?: string;
13
+ defaultValue?: T;
14
+ }
15
+ type ProviderProps<T> = T & {
16
+ children: React.ReactNode;
17
+ };
18
+ export type CreateContextReturn<T> = [
19
+ (contextValues: ProviderProps<T>) => React.JSX.Element,
20
+ () => T
21
+ ];
22
+ export declare function createContext<T>(options?: CreateContextOptions<T>): CreateContextReturn<T>;
23
+ export {};
@@ -0,0 +1,46 @@
1
+ var __rest = (this && this.__rest) || function (s, e) {
2
+ var t = {};
3
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
+ t[p] = s[p];
5
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
+ t[p[i]] = s[p[i]];
9
+ }
10
+ return t;
11
+ };
12
+ /**
13
+ * Custom createContext to consolidate context-implementation across the system
14
+ * Inspired by:
15
+ * - https://github.com/radix-ui/primitives/blob/main/packages/react/context/src/createContext.tsx
16
+ * - https://github.com/chakra-ui/chakra-ui/blob/5ec0be610b5a69afba01a9c22365155c1b519136/packages/hooks/context/src/index.ts
17
+ */
18
+ import React, { createContext as createReactContext, useContext as useReactContext, } from "react";
19
+ function getErrorMessage(hook, provider) {
20
+ return `${hook} returned \`undefined\`. Seems you forgot to wrap component within ${provider}`;
21
+ }
22
+ export function createContext(options = {}) {
23
+ const { name, hookName = "useContext", providerName = "Provider", errorMessage, defaultValue, } = options;
24
+ const Context = createReactContext(defaultValue);
25
+ function Provider(_a) {
26
+ var { children } = _a, context = __rest(_a, ["children"]);
27
+ // Only re-memoize when prop values change
28
+ // eslint-disable-next-line react-hooks/exhaustive-deps
29
+ const value = React.useMemo(() => context, Object.values(context));
30
+ return React.createElement(Context.Provider, { value: value }, children);
31
+ }
32
+ function useContext() {
33
+ var _a;
34
+ const context = useReactContext(Context);
35
+ if (!context) {
36
+ const error = new Error(errorMessage !== null && errorMessage !== void 0 ? errorMessage : getErrorMessage(hookName, providerName));
37
+ error.name = "ContextError";
38
+ (_a = Error.captureStackTrace) === null || _a === void 0 ? void 0 : _a.call(Error, error, useContext);
39
+ throw error;
40
+ }
41
+ return context;
42
+ }
43
+ Context.displayName = name;
44
+ return [Provider, useContext];
45
+ }
46
+ //# sourceMappingURL=create-context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-context.js","sourceRoot":"","sources":["../../src/util/create-context.tsx"],"names":[],"mappings":";;;;;;;;;;;AAAA;;;;;GAKG;AACH,OAAO,KAAK,EAAE,EACZ,aAAa,IAAI,kBAAkB,EACnC,UAAU,IAAI,eAAe,GAC9B,MAAM,OAAO,CAAC;AAiBf,SAAS,eAAe,CAAC,IAAY,EAAE,QAAgB;IACrD,OAAO,GAAG,IAAI,sEAAsE,QAAQ,EAAE,CAAC;AACjG,CAAC;AAED,MAAM,UAAU,aAAa,CAAI,UAAmC,EAAE;IACpE,MAAM,EACJ,IAAI,EACJ,QAAQ,GAAG,YAAY,EACvB,YAAY,GAAG,UAAU,EACzB,YAAY,EACZ,YAAY,GACb,GAAG,OAAO,CAAC;IAEZ,MAAM,OAAO,GAAG,kBAAkB,CAAgB,YAAY,CAAC,CAAC;IAEhE,SAAS,QAAQ,CAAC,EAA0C;YAA1C,EAAE,QAAQ,OAAgC,EAA3B,OAAO,cAAtB,YAAwB,CAAF;QACtC,0CAA0C;QAC1C,uDAAuD;QACvD,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAM,CAAC;QACxE,OAAO,oBAAC,OAAO,CAAC,QAAQ,IAAC,KAAK,EAAE,KAAK,IAAG,QAAQ,CAAoB,CAAC;IACvE,CAAC;IAED,SAAS,UAAU;;QACjB,MAAM,OAAO,GAAG,eAAe,CAAC,OAAO,CAAC,CAAC;QAEzC,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,MAAM,KAAK,GAAG,IAAI,KAAK,CACrB,YAAY,aAAZ,YAAY,cAAZ,YAAY,GAAI,eAAe,CAAC,QAAQ,EAAE,YAAY,CAAC,CACxD,CAAC;YACF,KAAK,CAAC,IAAI,GAAG,cAAc,CAAC;YAC5B,MAAA,KAAK,CAAC,iBAAiB,sDAAG,KAAK,EAAE,UAAU,CAAC,CAAC;YAC7C,MAAM,KAAK,CAAC;QACd,CAAC;QAED,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC;IAE3B,OAAO,CAAC,QAAQ,EAAE,UAAU,CAA2B,CAAC;AAC1D,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@navikt/ds-react",
3
- "version": "5.15.0",
3
+ "version": "5.15.1",
4
4
  "description": "Aksel react-components for NAV designsystem",
5
5
  "author": "Aksel | NAV designsystem team",
6
6
  "license": "MIT",
@@ -38,8 +38,8 @@
38
38
  },
39
39
  "dependencies": {
40
40
  "@floating-ui/react": "0.25.4",
41
- "@navikt/aksel-icons": "^5.15.0",
42
- "@navikt/ds-tokens": "^5.15.0",
41
+ "@navikt/aksel-icons": "^5.15.1",
42
+ "@navikt/ds-tokens": "^5.15.1",
43
43
  "@radix-ui/react-tabs": "1.0.0",
44
44
  "@radix-ui/react-toggle-group": "1.0.0",
45
45
  "clsx": "^1.2.1",
@@ -57,7 +57,7 @@ export const HelpText = forwardRef<HTMLButtonElement, HelpTextProps>(
57
57
  <button
58
58
  {...rest}
59
59
  ref={mergedRef}
60
- onClick={composeEventHandlers(onClick, () => setOpen((x) => x))}
60
+ onClick={composeEventHandlers(onClick, () => setOpen((x) => !x))}
61
61
  className={cl(className, "navds-help-text__button")}
62
62
  type="button"
63
63
  aria-expanded={open}
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Custom createContext to consolidate context-implementation across the system
3
+ * Inspired by:
4
+ * - https://github.com/radix-ui/primitives/blob/main/packages/react/context/src/createContext.tsx
5
+ * - https://github.com/chakra-ui/chakra-ui/blob/5ec0be610b5a69afba01a9c22365155c1b519136/packages/hooks/context/src/index.ts
6
+ */
7
+ import React, {
8
+ createContext as createReactContext,
9
+ useContext as useReactContext,
10
+ } from "react";
11
+
12
+ export interface CreateContextOptions<T> {
13
+ hookName?: string;
14
+ providerName?: string;
15
+ errorMessage?: string;
16
+ name?: string;
17
+ defaultValue?: T;
18
+ }
19
+
20
+ type ProviderProps<T> = T & { children: React.ReactNode };
21
+
22
+ export type CreateContextReturn<T> = [
23
+ (contextValues: ProviderProps<T>) => React.JSX.Element,
24
+ () => T,
25
+ ];
26
+
27
+ function getErrorMessage(hook: string, provider: string) {
28
+ return `${hook} returned \`undefined\`. Seems you forgot to wrap component within ${provider}`;
29
+ }
30
+
31
+ export function createContext<T>(options: CreateContextOptions<T> = {}) {
32
+ const {
33
+ name,
34
+ hookName = "useContext",
35
+ providerName = "Provider",
36
+ errorMessage,
37
+ defaultValue,
38
+ } = options;
39
+
40
+ const Context = createReactContext<T | undefined>(defaultValue);
41
+
42
+ function Provider({ children, ...context }: ProviderProps<T>) {
43
+ // Only re-memoize when prop values change
44
+ // eslint-disable-next-line react-hooks/exhaustive-deps
45
+ const value = React.useMemo(() => context, Object.values(context)) as T;
46
+ return <Context.Provider value={value}>{children}</Context.Provider>;
47
+ }
48
+
49
+ function useContext() {
50
+ const context = useReactContext(Context);
51
+
52
+ if (!context) {
53
+ const error = new Error(
54
+ errorMessage ?? getErrorMessage(hookName, providerName),
55
+ );
56
+ error.name = "ContextError";
57
+ Error.captureStackTrace?.(error, useContext);
58
+ throw error;
59
+ }
60
+
61
+ return context;
62
+ }
63
+
64
+ Context.displayName = name;
65
+
66
+ return [Provider, useContext] as CreateContextReturn<T>;
67
+ }