@lumx/react 3.16.1-alpha.0 → 3.16.1-alpha.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/index.js +8 -13
- package/index.js.map +1 -1
- package/package.json +3 -3
- package/src/components/dialog/Dialog.tsx +89 -88
- package/src/components/lightbox/Lightbox.tsx +2 -1
- package/src/components/notification/Notification.tsx +34 -33
- package/src/components/popover/Popover.tsx +33 -39
- package/src/components/tooltip/Tooltip.tsx +1 -1
- package/src/utils/Portal/Portal.test.tsx +32 -0
- package/src/utils/Portal/Portal.tsx +33 -0
- package/src/utils/Portal/PortalProvider.stories.jsx +34 -0
- package/src/utils/Portal/PortalProvider.test.tsx +73 -0
- package/src/utils/Portal/PortalProvider.tsx +24 -0
- package/src/utils/Portal/index.tsx +2 -0
- package/src/utils/index.ts +2 -2
- package/utils/index.d.ts +19 -14
- package/utils/index.js +16 -19
- package/utils/index.js.map +1 -1
- package/src/utils/PortalProvider/PortalProvider.stories.jsx +0 -40
- package/src/utils/PortalProvider/index.tsx +0 -43
package/utils/index.d.ts
CHANGED
|
@@ -31,26 +31,31 @@ interface ClickAwayProviderProps extends ClickAwayParameters {
|
|
|
31
31
|
declare const ClickAwayProvider: React.FC<ClickAwayProviderProps>;
|
|
32
32
|
|
|
33
33
|
type Container = DocumentFragment | Element;
|
|
34
|
-
type PortalInit = () => PortalContext;
|
|
35
|
-
interface PortalContext {
|
|
36
|
-
mount(): Container;
|
|
37
|
-
unmount?(): void;
|
|
38
|
-
}
|
|
39
|
-
declare const PortalContext: React.Context<PortalInit>;
|
|
40
34
|
/**
|
|
41
|
-
*
|
|
35
|
+
* Portal initializing function.
|
|
36
|
+
* If it does not provide a container, the Portal children will render in classic React tree and not in a portal.
|
|
42
37
|
*/
|
|
43
|
-
|
|
38
|
+
type PortalInit = () => {
|
|
39
|
+
container?: Container;
|
|
40
|
+
teardown?: () => void;
|
|
41
|
+
};
|
|
42
|
+
interface PortalProviderProps {
|
|
44
43
|
children?: React.ReactNode;
|
|
45
44
|
value: PortalInit;
|
|
46
|
-
}
|
|
45
|
+
}
|
|
47
46
|
/**
|
|
48
|
-
*
|
|
49
|
-
* (defaults to document.body but can be customized with the PortalContextProvider)
|
|
47
|
+
* Customize where <Portal> wrapped elements render (tooltip, popover, dialog, etc.)
|
|
50
48
|
*/
|
|
51
|
-
declare const
|
|
49
|
+
declare const PortalProvider: React.FC<PortalProviderProps>;
|
|
50
|
+
|
|
51
|
+
interface PortalProps {
|
|
52
52
|
enabled?: boolean;
|
|
53
53
|
children: React.ReactNode;
|
|
54
|
-
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Render children in a portal outside the current DOM position
|
|
57
|
+
* (defaults to `document.body` but can be customized with the PortalContextProvider)
|
|
58
|
+
*/
|
|
59
|
+
declare const Portal: React.FC<PortalProps>;
|
|
55
60
|
|
|
56
|
-
export { ClickAwayProvider, Portal, PortalProvider };
|
|
61
|
+
export { ClickAwayProvider, Portal, type PortalInit, type PortalProps, PortalProvider, type PortalProviderProps };
|
package/utils/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import React__default, { useEffect, useContext, useMemo, useRef, createContext } from 'react';
|
|
2
|
-
import { createPortal } from 'react-dom';
|
|
3
2
|
import isEmpty from 'lodash/isEmpty';
|
|
3
|
+
import { createPortal } from 'react-dom';
|
|
4
4
|
|
|
5
5
|
const EVENT_TYPES = ['mousedown', 'touchstart'];
|
|
6
6
|
function isClickAway(target, refs) {
|
|
@@ -92,10 +92,14 @@ const ClickAwayProvider = ({
|
|
|
92
92
|
};
|
|
93
93
|
ClickAwayProvider.displayName = 'ClickAwayProvider';
|
|
94
94
|
|
|
95
|
+
/**
|
|
96
|
+
* Portal initializing function.
|
|
97
|
+
* If it does not provide a container, the Portal children will render in classic React tree and not in a portal.
|
|
98
|
+
*/
|
|
99
|
+
|
|
95
100
|
const PortalContext = /*#__PURE__*/React__default.createContext(() => ({
|
|
96
|
-
|
|
101
|
+
container: document.body
|
|
97
102
|
}));
|
|
98
|
-
|
|
99
103
|
/**
|
|
100
104
|
* Customize where <Portal> wrapped elements render (tooltip, popover, dialog, etc.)
|
|
101
105
|
*/
|
|
@@ -103,33 +107,26 @@ const PortalProvider = PortalContext.Provider;
|
|
|
103
107
|
|
|
104
108
|
/**
|
|
105
109
|
* Render children in a portal outside the current DOM position
|
|
106
|
-
* (defaults to document.body but can be customized with the PortalContextProvider)
|
|
110
|
+
* (defaults to `document.body` but can be customized with the PortalContextProvider)
|
|
107
111
|
*/
|
|
108
112
|
const Portal = ({
|
|
109
113
|
children,
|
|
110
114
|
enabled = true
|
|
111
115
|
}) => {
|
|
112
116
|
const init = React__default.useContext(PortalContext);
|
|
113
|
-
const {
|
|
114
|
-
|
|
115
|
-
container
|
|
116
|
-
} = React__default.useMemo(() => {
|
|
117
|
-
if (!enabled) return {};
|
|
118
|
-
const context = init();
|
|
119
|
-
const container = context.mount();
|
|
120
|
-
return {
|
|
121
|
-
context,
|
|
122
|
-
container
|
|
123
|
-
};
|
|
117
|
+
const context = React__default.useMemo(() => {
|
|
118
|
+
return enabled ? init() : null;
|
|
124
119
|
},
|
|
125
120
|
// Only update on 'enabled'
|
|
126
121
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
127
122
|
[enabled]);
|
|
128
123
|
React__default.useLayoutEffect(() => {
|
|
129
|
-
return context === null || context === void 0 ? void 0 : context.
|
|
130
|
-
}, [context === null || context === void 0 ? void 0 : context.
|
|
131
|
-
if (!
|
|
132
|
-
|
|
124
|
+
return context === null || context === void 0 ? void 0 : context.teardown;
|
|
125
|
+
}, [context === null || context === void 0 ? void 0 : context.teardown, enabled]);
|
|
126
|
+
if (!(context !== null && context !== void 0 && context.container)) {
|
|
127
|
+
return /*#__PURE__*/React__default.createElement(React__default.Fragment, null, children);
|
|
128
|
+
}
|
|
129
|
+
return /*#__PURE__*/createPortal(children, context.container);
|
|
133
130
|
};
|
|
134
131
|
|
|
135
132
|
export { ClickAwayProvider, Portal, PortalProvider };
|
package/utils/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../src/hooks/useClickAway.tsx","../../src/utils/ClickAwayProvider/ClickAwayProvider.tsx","../../src/utils/PortalProvider/index.tsx"],"sourcesContent":["import { RefObject, useEffect } from 'react';\n\nimport { Falsy } from '@lumx/react/utils/type';\n\nimport isEmpty from 'lodash/isEmpty';\n\nconst EVENT_TYPES = ['mousedown', 'touchstart'];\n\nfunction isClickAway(target: HTMLElement, refs: Array<RefObject<HTMLElement>>): boolean {\n // The target element is not contained in any of the listed element references.\n return !refs.some((e) => e?.current?.contains(target));\n}\n\nexport interface ClickAwayParameters {\n /**\n * A callback function to call when the user clicks away from the elements.\n */\n callback: EventListener | Falsy;\n /**\n * Elements considered within the click away context (clicking outside them will trigger the click away callback).\n */\n childrenRefs: RefObject<Array<RefObject<HTMLElement>>>;\n}\n\n/**\n * Listen to clicks away from the given elements and callback the passed in function.\n *\n * Warning: If you need to detect click away on nested React portals, please use the `ClickAwayProvider` component.\n */\nexport function useClickAway({ callback, childrenRefs }: ClickAwayParameters): void {\n useEffect(() => {\n const { current: currentRefs } = childrenRefs;\n if (!callback || !currentRefs || isEmpty(currentRefs)) {\n return undefined;\n }\n const listener: EventListener = (evt) => {\n if (isClickAway(evt.target as HTMLElement, currentRefs)) {\n callback(evt);\n }\n };\n\n EVENT_TYPES.forEach((evtType) => document.addEventListener(evtType, listener));\n return () => {\n EVENT_TYPES.forEach((evtType) => document.removeEventListener(evtType, listener));\n };\n }, [callback, childrenRefs]);\n}\n","import React, { createContext, RefObject, useContext, useEffect, useMemo, useRef } from 'react';\nimport { ClickAwayParameters, useClickAway } from '@lumx/react/hooks/useClickAway';\n\ninterface ContextValue {\n childrenRefs: Array<RefObject<HTMLElement>>;\n addRefs(...newChildrenRefs: Array<RefObject<HTMLElement>>): void;\n}\n\nconst ClickAwayAncestorContext = createContext<ContextValue | null>(null);\n\ninterface ClickAwayProviderProps extends ClickAwayParameters {\n /**\n * (Optional) Element that should be considered as part of the parent\n */\n parentRef?: RefObject<HTMLElement>;\n /**\n * Children\n */\n children?: React.ReactNode;\n}\n\n/**\n * Component combining the `useClickAway` hook with a React context to hook into the React component tree and make sure\n * we take into account both the DOM tree and the React tree to detect click away.\n *\n * @return the react component.\n */\nexport const ClickAwayProvider: React.FC<ClickAwayProviderProps> = ({\n children,\n callback,\n childrenRefs,\n parentRef,\n}) => {\n const parentContext = useContext(ClickAwayAncestorContext);\n const currentContext = useMemo(() => {\n const context: ContextValue = {\n childrenRefs: [],\n /**\n * Add element refs to the current context and propagate to the parent context.\n */\n addRefs(...newChildrenRefs) {\n // Add element refs that should be considered as inside the click away context.\n context.childrenRefs.push(...newChildrenRefs);\n\n if (parentContext) {\n // Also add then to the parent context\n parentContext.addRefs(...newChildrenRefs);\n if (parentRef) {\n // The parent element is also considered as inside the parent click away context but not inside the current context\n parentContext.addRefs(parentRef);\n }\n }\n },\n };\n return context;\n }, [parentContext, parentRef]);\n\n useEffect(() => {\n const { current: currentRefs } = childrenRefs;\n if (!currentRefs) {\n return;\n }\n currentContext.addRefs(...currentRefs);\n }, [currentContext, childrenRefs]);\n\n useClickAway({ callback, childrenRefs: useRef(currentContext.childrenRefs) });\n return <ClickAwayAncestorContext.Provider value={currentContext}>{children}</ClickAwayAncestorContext.Provider>;\n};\nClickAwayProvider.displayName = 'ClickAwayProvider';\n","import React from 'react';\nimport { createPortal } from 'react-dom';\n\ntype Container = DocumentFragment | Element;\ninterface PortalContext {\n mount(): Container;\n unmount?(): void;\n}\ntype PortalInit = () => PortalContext;\n\nconst PortalContext = React.createContext<PortalInit>(() => ({ mount: () => document.body }));\n\n/**\n * Customize where <Portal> wrapped elements render (tooltip, popover, dialog, etc.)\n */\nexport const PortalProvider: React.FC<{ children?: React.ReactNode; value: PortalInit }> = PortalContext.Provider;\n\n/**\n * Render children in a portal outside the current DOM position\n * (defaults to document.body but can be customized with the PortalContextProvider)\n */\nexport const Portal: React.FC<{ enabled?: boolean; children: React.ReactNode }> = ({ children, enabled = true }) => {\n const init = React.useContext(PortalContext);\n const { context, container } = React.useMemo(\n () => {\n if (!enabled) return {};\n const context = init();\n const container = context.mount();\n return { context, container };\n },\n\n // Only update on 'enabled'\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [enabled],\n );\n\n React.useLayoutEffect(() => {\n return context?.unmount;\n }, [context?.unmount, enabled]);\n\n if (!enabled) return <>{children}</>;\n return createPortal(children, container as Container);\n};\n"],"names":["EVENT_TYPES","isClickAway","target","refs","some","e","_e$current","current","contains","useClickAway","callback","childrenRefs","useEffect","currentRefs","isEmpty","undefined","listener","evt","forEach","evtType","document","addEventListener","removeEventListener","ClickAwayAncestorContext","createContext","ClickAwayProvider","children","parentRef","parentContext","useContext","currentContext","useMemo","context","addRefs","newChildrenRefs","push","useRef","React","createElement","Provider","value","displayName","PortalContext","mount","body","PortalProvider","Portal","enabled","init","container","useLayoutEffect","unmount","Fragment","createPortal"],"mappings":";;;;AAMA,MAAMA,WAAW,GAAG,CAAC,WAAW,EAAE,YAAY,CAAC,CAAA;AAE/C,SAASC,WAAWA,CAACC,MAAmB,EAAEC,IAAmC,EAAW;AACpF;AACA,EAAA,OAAO,CAACA,IAAI,CAACC,IAAI,CAAEC,CAAC,IAAA;AAAA,IAAA,IAAAC,UAAA,CAAA;AAAA,IAAA,OAAKD,CAAC,KAADA,IAAAA,IAAAA,CAAC,KAAAC,KAAAA,CAAAA,GAAAA,KAAAA,CAAAA,GAAAA,CAAAA,UAAA,GAADD,CAAC,CAAEE,OAAO,MAAA,IAAA,IAAAD,UAAA,KAAVA,KAAAA,CAAAA,GAAAA,KAAAA,CAAAA,GAAAA,UAAA,CAAYE,QAAQ,CAACN,MAAM,CAAC,CAAA;GAAC,CAAA,CAAA;AAC1D,CAAA;AAaA;AACA;AACA;AACA;AACA;AACO,SAASO,YAAYA,CAAC;EAAEC,QAAQ;AAAEC,EAA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Provider.tsx","../../src/utils/Portal/PortalProvider.tsx","../../src/utils/Portal/Portal.tsx"],"sourcesContent":["import { RefObject, useEffect } from 'react';\n\nimport { Falsy } from '@lumx/react/utils/type';\n\nimport isEmpty from 'lodash/isEmpty';\n\nconst EVENT_TYPES = ['mousedown', 'touchstart'];\n\nfunction isClickAway(target: HTMLElement, refs: Array<RefObject<HTMLElement>>): boolean {\n // The target element is not contained in any of the listed element references.\n return !refs.some((e) => e?.current?.contains(target));\n}\n\nexport interface ClickAwayParameters {\n /**\n * A callback function to call when the user clicks away from the elements.\n */\n callback: EventListener | Falsy;\n /**\n * Elements considered within the click away context (clicking outside them will trigger the click away callback).\n */\n childrenRefs: RefObject<Array<RefObject<HTMLElement>>>;\n}\n\n/**\n * Listen to clicks away from the given elements and callback the passed in function.\n *\n * Warning: If you need to detect click away on nested React portals, please use the `ClickAwayProvider` component.\n */\nexport function useClickAway({ callback, childrenRefs }: ClickAwayParameters): void {\n useEffect(() => {\n const { current: currentRefs } = childrenRefs;\n if (!callback || !currentRefs || isEmpty(currentRefs)) {\n return undefined;\n }\n const listener: EventListener = (evt) => {\n if (isClickAway(evt.target as HTMLElement, currentRefs)) {\n callback(evt);\n }\n };\n\n EVENT_TYPES.forEach((evtType) => document.addEventListener(evtType, listener));\n return () => {\n EVENT_TYPES.forEach((evtType) => document.removeEventListener(evtType, listener));\n };\n }, [callback, childrenRefs]);\n}\n","import React, { createContext, RefObject, useContext, useEffect, useMemo, useRef } from 'react';\nimport { ClickAwayParameters, useClickAway } from '@lumx/react/hooks/useClickAway';\n\ninterface ContextValue {\n childrenRefs: Array<RefObject<HTMLElement>>;\n addRefs(...newChildrenRefs: Array<RefObject<HTMLElement>>): void;\n}\n\nconst ClickAwayAncestorContext = createContext<ContextValue | null>(null);\n\ninterface ClickAwayProviderProps extends ClickAwayParameters {\n /**\n * (Optional) Element that should be considered as part of the parent\n */\n parentRef?: RefObject<HTMLElement>;\n /**\n * Children\n */\n children?: React.ReactNode;\n}\n\n/**\n * Component combining the `useClickAway` hook with a React context to hook into the React component tree and make sure\n * we take into account both the DOM tree and the React tree to detect click away.\n *\n * @return the react component.\n */\nexport const ClickAwayProvider: React.FC<ClickAwayProviderProps> = ({\n children,\n callback,\n childrenRefs,\n parentRef,\n}) => {\n const parentContext = useContext(ClickAwayAncestorContext);\n const currentContext = useMemo(() => {\n const context: ContextValue = {\n childrenRefs: [],\n /**\n * Add element refs to the current context and propagate to the parent context.\n */\n addRefs(...newChildrenRefs) {\n // Add element refs that should be considered as inside the click away context.\n context.childrenRefs.push(...newChildrenRefs);\n\n if (parentContext) {\n // Also add then to the parent context\n parentContext.addRefs(...newChildrenRefs);\n if (parentRef) {\n // The parent element is also considered as inside the parent click away context but not inside the current context\n parentContext.addRefs(parentRef);\n }\n }\n },\n };\n return context;\n }, [parentContext, parentRef]);\n\n useEffect(() => {\n const { current: currentRefs } = childrenRefs;\n if (!currentRefs) {\n return;\n }\n currentContext.addRefs(...currentRefs);\n }, [currentContext, childrenRefs]);\n\n useClickAway({ callback, childrenRefs: useRef(currentContext.childrenRefs) });\n return <ClickAwayAncestorContext.Provider value={currentContext}>{children}</ClickAwayAncestorContext.Provider>;\n};\nClickAwayProvider.displayName = 'ClickAwayProvider';\n","import React from 'react';\n\ntype Container = DocumentFragment | Element;\n\n/**\n * Portal initializing function.\n * If it does not provide a container, the Portal children will render in classic React tree and not in a portal.\n */\nexport type PortalInit = () => {\n container?: Container;\n teardown?: () => void;\n};\n\nexport const PortalContext = React.createContext<PortalInit>(() => ({ container: document.body }));\n\nexport interface PortalProviderProps {\n children?: React.ReactNode;\n value: PortalInit;\n}\n\n/**\n * Customize where <Portal> wrapped elements render (tooltip, popover, dialog, etc.)\n */\nexport const PortalProvider: React.FC<PortalProviderProps> = PortalContext.Provider;\n","import React from 'react';\nimport { createPortal } from 'react-dom';\nimport { PortalContext } from './PortalProvider';\n\nexport interface PortalProps {\n enabled?: boolean;\n children: React.ReactNode;\n}\n\n/**\n * Render children in a portal outside the current DOM position\n * (defaults to `document.body` but can be customized with the PortalContextProvider)\n */\nexport const Portal: React.FC<PortalProps> = ({ children, enabled = true }) => {\n const init = React.useContext(PortalContext);\n const context = React.useMemo(\n () => {\n return enabled ? init() : null;\n },\n // Only update on 'enabled'\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [enabled],\n );\n\n React.useLayoutEffect(() => {\n return context?.teardown;\n }, [context?.teardown, enabled]);\n\n if (!context?.container) {\n return <>{children}</>;\n }\n return createPortal(children, context.container);\n};\n"],"names":["EVENT_TYPES","isClickAway","target","refs","some","e","_e$current","current","contains","useClickAway","callback","childrenRefs","useEffect","currentRefs","isEmpty","undefined","listener","evt","forEach","evtType","document","addEventListener","removeEventListener","ClickAwayAncestorContext","createContext","ClickAwayProvider","children","parentRef","parentContext","useContext","currentContext","useMemo","context","addRefs","newChildrenRefs","push","useRef","React","createElement","Provider","value","displayName","PortalContext","container","body","PortalProvider","Portal","enabled","init","useLayoutEffect","teardown","Fragment","createPortal"],"mappings":";;;;AAMA,MAAMA,WAAW,GAAG,CAAC,WAAW,EAAE,YAAY,CAAC,CAAA;AAE/C,SAASC,WAAWA,CAACC,MAAmB,EAAEC,IAAmC,EAAW;AACpF;AACA,EAAA,OAAO,CAACA,IAAI,CAACC,IAAI,CAAEC,CAAC,IAAA;AAAA,IAAA,IAAAC,UAAA,CAAA;AAAA,IAAA,OAAKD,CAAC,KAADA,IAAAA,IAAAA,CAAC,KAAAC,KAAAA,CAAAA,GAAAA,KAAAA,CAAAA,GAAAA,CAAAA,UAAA,GAADD,CAAC,CAAEE,OAAO,MAAA,IAAA,IAAAD,UAAA,KAAVA,KAAAA,CAAAA,GAAAA,KAAAA,CAAAA,GAAAA,UAAA,CAAYE,QAAQ,CAACN,MAAM,CAAC,CAAA;GAAC,CAAA,CAAA;AAC1D,CAAA;AAaA;AACA;AACA;AACA;AACA;AACO,SAASO,YAAYA,CAAC;EAAEC,QAAQ;AAAEC,EAAAA,YAAAA;AAAkC,CAAC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Aa,CAACH;;ACd3E;AACA;AACA;AACA;AACO,MAAMO,MAA6B,GAAGA,CAAC;EAAEpB,QAAQ;AAAEqB,EAAAA,OAAO,GAAG,IAAA;AAAK,CAAC,KAAK;AAC3E,EAAA,MAAMC,IAAI,GAAGX,cAAK,CAACR,UAAU,CAACa,aAAa,CAAC,CAAA;AAC5C,EAAA,MAAMV,OAAO,GAAGK,cAAK,CAACN,OAAO,CACzB,MAAM;AACF,IAAA,OAAOgB,OAAO,GAAGC,IAAI,EAAE,GAAG,IAAI,CAAA;GACjC;AACD;AACA;EACA,CAACD,OAAO,CACZ,CAAC,CAAA;EAEDV,cAAK,CAACY,eAAe,CAAC,MAAM;AACxB,IAAA,OAAOjB,OAAO,KAAPA,IAAAA,IAAAA,OAAO,KAAPA,KAAAA,CAAAA,GAAAA,KAAAA,CAAAA,GAAAA,OAAO,CAAEkB,QAAQ,CAAA;AAC5B,GAAC,EAAE,CAAClB,OAAO,KAAA,IAAA,IAAPA,OAAO,KAAA,KAAA,CAAA,GAAA,KAAA,CAAA,GAAPA,OAAO,CAAEkB,QAAQ,EAAEH,OAAO,CAAC,CAAC,CAAA;EAEhC,IAAI,EAACf,OAAO,KAAPA,IAAAA,IAAAA,OAAO,eAAPA,OAAO,CAAEW,SAAS,CAAE,EAAA;IACrB,oBAAON,cAAA,CAAAC,aAAA,CAAAD,cAAA,CAAAc,QAAA,EAAGzB,IAAAA,EAAAA,QAAW,CAAC,CAAA;AAC1B,GAAA;AACA,EAAA,oBAAO0B,YAAY,CAAC1B,QAAQ,EAAEM,OAAO,CAACW,SAAS,CAAC,CAAA;AACpD;;;;"}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { Button, Switch } from '@lumx/react';
|
|
2
|
-
import { useBooleanState } from '@lumx/react/hooks/useBooleanState';
|
|
3
|
-
import { Portal, PortalProvider } from '@lumx/react/utils';
|
|
4
|
-
import React from 'react';
|
|
5
|
-
|
|
6
|
-
export default {
|
|
7
|
-
title: 'LumX components/PortalProvider',
|
|
8
|
-
component: PortalProvider,
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
const initShadowDOMPortal = () => {
|
|
12
|
-
let container;
|
|
13
|
-
return {
|
|
14
|
-
mount() {
|
|
15
|
-
container = document.createElement('div');
|
|
16
|
-
document.body.appendChild(container);
|
|
17
|
-
return container.attachShadow({ mode: 'closed' });
|
|
18
|
-
},
|
|
19
|
-
unmount() {
|
|
20
|
-
container?.remove();
|
|
21
|
-
},
|
|
22
|
-
};
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Demonstrate how to customize portals to render into a custom shadow root
|
|
27
|
-
*/
|
|
28
|
-
export const RenderInShadowDOM = {
|
|
29
|
-
args: { enabled: true },
|
|
30
|
-
render({ enabled }) {
|
|
31
|
-
return (
|
|
32
|
-
<PortalProvider value={initShadowDOMPortal}>
|
|
33
|
-
<Portal enabled={enabled}>
|
|
34
|
-
<Button>My button in a portal</Button>
|
|
35
|
-
</Portal>
|
|
36
|
-
</PortalProvider>
|
|
37
|
-
);
|
|
38
|
-
},
|
|
39
|
-
parameters: { chromatic: { disable: true } },
|
|
40
|
-
};
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { createPortal } from 'react-dom';
|
|
3
|
-
|
|
4
|
-
type Container = DocumentFragment | Element;
|
|
5
|
-
interface PortalContext {
|
|
6
|
-
mount(): Container;
|
|
7
|
-
unmount?(): void;
|
|
8
|
-
}
|
|
9
|
-
type PortalInit = () => PortalContext;
|
|
10
|
-
|
|
11
|
-
const PortalContext = React.createContext<PortalInit>(() => ({ mount: () => document.body }));
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Customize where <Portal> wrapped elements render (tooltip, popover, dialog, etc.)
|
|
15
|
-
*/
|
|
16
|
-
export const PortalProvider: React.FC<{ children?: React.ReactNode; value: PortalInit }> = PortalContext.Provider;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Render children in a portal outside the current DOM position
|
|
20
|
-
* (defaults to document.body but can be customized with the PortalContextProvider)
|
|
21
|
-
*/
|
|
22
|
-
export const Portal: React.FC<{ enabled?: boolean; children: React.ReactNode }> = ({ children, enabled = true }) => {
|
|
23
|
-
const init = React.useContext(PortalContext);
|
|
24
|
-
const { context, container } = React.useMemo(
|
|
25
|
-
() => {
|
|
26
|
-
if (!enabled) return {};
|
|
27
|
-
const context = init();
|
|
28
|
-
const container = context.mount();
|
|
29
|
-
return { context, container };
|
|
30
|
-
},
|
|
31
|
-
|
|
32
|
-
// Only update on 'enabled'
|
|
33
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
34
|
-
[enabled],
|
|
35
|
-
);
|
|
36
|
-
|
|
37
|
-
React.useLayoutEffect(() => {
|
|
38
|
-
return context?.unmount;
|
|
39
|
-
}, [context?.unmount, enabled]);
|
|
40
|
-
|
|
41
|
-
if (!enabled) return <>{children}</>;
|
|
42
|
-
return createPortal(children, container as Container);
|
|
43
|
-
};
|