@lumx/react 3.16.1-alpha.1 → 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.map +1 -1
- package/package.json +3 -3
- package/src/components/dialog/Dialog.tsx +2 -1
- package/src/components/lightbox/Lightbox.tsx +2 -1
- package/src/components/notification/Notification.tsx +2 -1
- package/src/components/popover/Popover.tsx +1 -1
- 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/{PortalProvider → Portal}/PortalProvider.stories.jsx +2 -1
- 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 +17 -10
- package/utils/index.js +11 -6
- package/utils/index.js.map +1 -1
- package/src/utils/PortalProvider/index.tsx +0 -36
package/package.json
CHANGED
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
"url": "https://github.com/lumapps/design-system/issues"
|
|
7
7
|
},
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@lumx/core": "^3.16.1-alpha.
|
|
10
|
-
"@lumx/icons": "^3.16.1-alpha.
|
|
9
|
+
"@lumx/core": "^3.16.1-alpha.2",
|
|
10
|
+
"@lumx/icons": "^3.16.1-alpha.2",
|
|
11
11
|
"@popperjs/core": "^2.5.4",
|
|
12
12
|
"body-scroll-lock": "^3.1.5",
|
|
13
13
|
"classnames": "^2.3.2",
|
|
@@ -105,5 +105,5 @@
|
|
|
105
105
|
"build:storybook": "storybook build"
|
|
106
106
|
},
|
|
107
107
|
"sideEffects": false,
|
|
108
|
-
"version": "3.16.1-alpha.
|
|
108
|
+
"version": "3.16.1-alpha.2"
|
|
109
109
|
}
|
|
@@ -19,7 +19,8 @@ import { forwardRef } from '@lumx/react/utils/react/forwardRef';
|
|
|
19
19
|
import { useDisableBodyScroll } from '@lumx/react/hooks/useDisableBodyScroll';
|
|
20
20
|
import { useTransitionVisibility } from '@lumx/react/hooks/useTransitionVisibility';
|
|
21
21
|
import { ThemeProvider } from '@lumx/react/utils/theme/ThemeContext';
|
|
22
|
-
|
|
22
|
+
|
|
23
|
+
import { Portal } from '@lumx/react/utils';
|
|
23
24
|
|
|
24
25
|
/**
|
|
25
26
|
* Defines the props of the component.
|
|
@@ -16,7 +16,8 @@ import { useCallbackOnEscape } from '@lumx/react/hooks/useCallbackOnEscape';
|
|
|
16
16
|
import { useTransitionVisibility } from '@lumx/react/hooks/useTransitionVisibility';
|
|
17
17
|
import { ThemeProvider } from '@lumx/react/utils/theme/ThemeContext';
|
|
18
18
|
import { forwardRef } from '@lumx/react/utils/react/forwardRef';
|
|
19
|
-
|
|
19
|
+
|
|
20
|
+
import { Portal } from '@lumx/react/utils';
|
|
20
21
|
|
|
21
22
|
/**
|
|
22
23
|
* Defines the props of the component.
|
|
@@ -12,7 +12,8 @@ import { useTransitionVisibility } from '@lumx/react/hooks/useTransitionVisibili
|
|
|
12
12
|
import { mergeRefs } from '@lumx/react/utils/react/mergeRefs';
|
|
13
13
|
import { useTheme } from '@lumx/react/utils/theme/ThemeContext';
|
|
14
14
|
import { forwardRef } from '@lumx/react/utils/react/forwardRef';
|
|
15
|
-
|
|
15
|
+
|
|
16
|
+
import { Portal } from '@lumx/react/utils';
|
|
16
17
|
|
|
17
18
|
/**
|
|
18
19
|
* Defines the props of the component.
|
|
@@ -14,7 +14,7 @@ import { skipRender } from '@lumx/react/utils/react/skipRender';
|
|
|
14
14
|
import { forwardRef } from '@lumx/react/utils/react/forwardRef';
|
|
15
15
|
|
|
16
16
|
import { ThemeProvider } from '@lumx/react/utils/theme/ThemeContext';
|
|
17
|
-
import { Portal } from '@lumx/react/utils
|
|
17
|
+
import { Portal } from '@lumx/react/utils';
|
|
18
18
|
import { useRestoreFocusOnClose } from './useRestoreFocusOnClose';
|
|
19
19
|
import { usePopoverStyle } from './usePopoverStyle';
|
|
20
20
|
import { Elevation, FitAnchorWidth, Offset, Placement, POPOVER_ZINDEX } from './constants';
|
|
@@ -14,7 +14,7 @@ import { usePopper } from '@lumx/react/hooks/usePopper';
|
|
|
14
14
|
import { forwardRef } from '@lumx/react/utils/react/forwardRef';
|
|
15
15
|
|
|
16
16
|
import { ARIA_LINK_MODES, TOOLTIP_ZINDEX } from '@lumx/react/components/tooltip/constants';
|
|
17
|
-
import { Portal } from '@lumx/react/utils
|
|
17
|
+
import { Portal } from '@lumx/react/utils';
|
|
18
18
|
import { useInjectTooltipRef } from './useInjectTooltipRef';
|
|
19
19
|
import { useTooltipOpen } from './useTooltipOpen';
|
|
20
20
|
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { render, screen } from '@testing-library/react';
|
|
3
|
+
import { Portal } from './Portal';
|
|
4
|
+
|
|
5
|
+
const TEST_ID = 'portal-child';
|
|
6
|
+
const PortalChild = () => <div data-testid={TEST_ID}>Hello from portal</div>;
|
|
7
|
+
|
|
8
|
+
describe(Portal, () => {
|
|
9
|
+
it('should render portal content in document.body by default', () => {
|
|
10
|
+
render(
|
|
11
|
+
<Portal>
|
|
12
|
+
<PortalChild />
|
|
13
|
+
</Portal>,
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
const child = screen.getByTestId(TEST_ID);
|
|
17
|
+
expect(child.parentElement).toBe(document.body);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should render inline when enabled is false', () => {
|
|
21
|
+
const { container } = render(
|
|
22
|
+
<div id="wrapper">
|
|
23
|
+
<Portal enabled={false}>
|
|
24
|
+
<PortalChild />
|
|
25
|
+
</Portal>
|
|
26
|
+
</div>,
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
const child = screen.getByTestId(TEST_ID);
|
|
30
|
+
expect(child.parentElement).toBe(container.querySelector('#wrapper'));
|
|
31
|
+
});
|
|
32
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { createPortal } from 'react-dom';
|
|
3
|
+
import { PortalContext } from './PortalProvider';
|
|
4
|
+
|
|
5
|
+
export interface PortalProps {
|
|
6
|
+
enabled?: boolean;
|
|
7
|
+
children: React.ReactNode;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Render children in a portal outside the current DOM position
|
|
12
|
+
* (defaults to `document.body` but can be customized with the PortalContextProvider)
|
|
13
|
+
*/
|
|
14
|
+
export const Portal: React.FC<PortalProps> = ({ children, enabled = true }) => {
|
|
15
|
+
const init = React.useContext(PortalContext);
|
|
16
|
+
const context = React.useMemo(
|
|
17
|
+
() => {
|
|
18
|
+
return enabled ? init() : null;
|
|
19
|
+
},
|
|
20
|
+
// Only update on 'enabled'
|
|
21
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
22
|
+
[enabled],
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
React.useLayoutEffect(() => {
|
|
26
|
+
return context?.teardown;
|
|
27
|
+
}, [context?.teardown, enabled]);
|
|
28
|
+
|
|
29
|
+
if (!context?.container) {
|
|
30
|
+
return <>{children}</>;
|
|
31
|
+
}
|
|
32
|
+
return createPortal(children, context.container);
|
|
33
|
+
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Button } from '@lumx/react';
|
|
2
|
-
import { Portal, PortalProvider } from '@lumx/react/utils';
|
|
3
2
|
import React from 'react';
|
|
3
|
+
import { PortalProvider } from './PortalProvider';
|
|
4
|
+
import { Portal } from './Portal';
|
|
4
5
|
|
|
5
6
|
export default {
|
|
6
7
|
title: 'LumX components/PortalProvider',
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { render, screen } from '@testing-library/react';
|
|
3
|
+
import { PortalInit, PortalProvider } from './PortalProvider';
|
|
4
|
+
import { Portal } from './Portal';
|
|
5
|
+
|
|
6
|
+
const TEST_ID = 'portal-child';
|
|
7
|
+
const PORTAL_CONTAINER_ID = 'portal-container';
|
|
8
|
+
|
|
9
|
+
const PortalChild = () => <div data-testid={TEST_ID}>Hello from portal</div>;
|
|
10
|
+
|
|
11
|
+
describe(PortalProvider, () => {
|
|
12
|
+
it('should render portal content in the provided container', () => {
|
|
13
|
+
const portalContainer = document.createElement('div');
|
|
14
|
+
portalContainer.id = PORTAL_CONTAINER_ID;
|
|
15
|
+
document.body.appendChild(portalContainer);
|
|
16
|
+
|
|
17
|
+
const getContainer: PortalInit = () => ({ container: portalContainer });
|
|
18
|
+
|
|
19
|
+
render(
|
|
20
|
+
<PortalProvider value={getContainer}>
|
|
21
|
+
<Portal>
|
|
22
|
+
<PortalChild />
|
|
23
|
+
</Portal>
|
|
24
|
+
</PortalProvider>,
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
const child = screen.getByTestId(TEST_ID);
|
|
28
|
+
expect(child.parentElement).toBe(portalContainer);
|
|
29
|
+
portalContainer.remove();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should call teardown on unmount', () => {
|
|
33
|
+
const teardownMock = jest.fn();
|
|
34
|
+
const portalContainer = document.createElement('div');
|
|
35
|
+
portalContainer.id = PORTAL_CONTAINER_ID;
|
|
36
|
+
document.body.appendChild(portalContainer);
|
|
37
|
+
|
|
38
|
+
const getContainer: PortalInit = () => ({
|
|
39
|
+
container: portalContainer,
|
|
40
|
+
teardown: teardownMock,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const { unmount } = render(
|
|
44
|
+
<PortalProvider value={getContainer}>
|
|
45
|
+
<Portal>
|
|
46
|
+
<PortalChild />
|
|
47
|
+
</Portal>
|
|
48
|
+
</PortalProvider>,
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
expect(teardownMock).not.toHaveBeenCalled();
|
|
52
|
+
unmount();
|
|
53
|
+
expect(teardownMock).toHaveBeenCalledTimes(1);
|
|
54
|
+
portalContainer.remove();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('should render inline if provider does not return a container', () => {
|
|
58
|
+
const getContainer: PortalInit = () => ({}); // No container
|
|
59
|
+
|
|
60
|
+
const { container } = render(
|
|
61
|
+
<div id="wrapper">
|
|
62
|
+
<PortalProvider value={getContainer}>
|
|
63
|
+
<Portal>
|
|
64
|
+
<PortalChild />
|
|
65
|
+
</Portal>
|
|
66
|
+
</PortalProvider>
|
|
67
|
+
</div>,
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
const child = screen.getByTestId(TEST_ID);
|
|
71
|
+
expect(child.parentElement).toBe(container.querySelector('#wrapper'));
|
|
72
|
+
});
|
|
73
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
type Container = DocumentFragment | Element;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Portal initializing function.
|
|
7
|
+
* If it does not provide a container, the Portal children will render in classic React tree and not in a portal.
|
|
8
|
+
*/
|
|
9
|
+
export type PortalInit = () => {
|
|
10
|
+
container?: Container;
|
|
11
|
+
teardown?: () => void;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const PortalContext = React.createContext<PortalInit>(() => ({ container: document.body }));
|
|
15
|
+
|
|
16
|
+
export interface PortalProviderProps {
|
|
17
|
+
children?: React.ReactNode;
|
|
18
|
+
value: PortalInit;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Customize where <Portal> wrapped elements render (tooltip, popover, dialog, etc.)
|
|
23
|
+
*/
|
|
24
|
+
export const PortalProvider: React.FC<PortalProviderProps> = PortalContext.Provider;
|
package/src/utils/index.ts
CHANGED
|
@@ -2,5 +2,5 @@
|
|
|
2
2
|
* WARNING: All modules exported here are exposed to NPM in '@lumx/react/utils'.
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
export
|
|
6
|
-
export
|
|
5
|
+
export { ClickAwayProvider } from './ClickAwayProvider';
|
|
6
|
+
export { Portal, type PortalProps, type PortalInit, PortalProvider, type PortalProviderProps } from './Portal';
|
package/utils/index.d.ts
CHANGED
|
@@ -31,24 +31,31 @@ interface ClickAwayProviderProps extends ClickAwayParameters {
|
|
|
31
31
|
declare const ClickAwayProvider: React.FC<ClickAwayProviderProps>;
|
|
32
32
|
|
|
33
33
|
type Container = DocumentFragment | Element;
|
|
34
|
+
/**
|
|
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.
|
|
37
|
+
*/
|
|
34
38
|
type PortalInit = () => {
|
|
35
|
-
container
|
|
39
|
+
container?: Container;
|
|
36
40
|
teardown?: () => void;
|
|
37
41
|
};
|
|
42
|
+
interface PortalProviderProps {
|
|
43
|
+
children?: React.ReactNode;
|
|
44
|
+
value: PortalInit;
|
|
45
|
+
}
|
|
38
46
|
/**
|
|
39
47
|
* Customize where <Portal> wrapped elements render (tooltip, popover, dialog, etc.)
|
|
40
48
|
*/
|
|
41
|
-
declare const PortalProvider: React.FC<
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
49
|
+
declare const PortalProvider: React.FC<PortalProviderProps>;
|
|
50
|
+
|
|
51
|
+
interface PortalProps {
|
|
52
|
+
enabled?: boolean;
|
|
53
|
+
children: React.ReactNode;
|
|
54
|
+
}
|
|
45
55
|
/**
|
|
46
56
|
* Render children in a portal outside the current DOM position
|
|
47
57
|
* (defaults to `document.body` but can be customized with the PortalContextProvider)
|
|
48
58
|
*/
|
|
49
|
-
declare const Portal: React.FC<
|
|
50
|
-
enabled?: boolean;
|
|
51
|
-
children: React.ReactNode;
|
|
52
|
-
}>;
|
|
59
|
+
declare const Portal: React.FC<PortalProps>;
|
|
53
60
|
|
|
54
|
-
export { ClickAwayProvider, Portal, type PortalInit, 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
|
*/
|
|
@@ -111,8 +115,7 @@ const Portal = ({
|
|
|
111
115
|
}) => {
|
|
112
116
|
const init = React__default.useContext(PortalContext);
|
|
113
117
|
const context = React__default.useMemo(() => {
|
|
114
|
-
|
|
115
|
-
return init();
|
|
118
|
+
return enabled ? init() : null;
|
|
116
119
|
},
|
|
117
120
|
// Only update on 'enabled'
|
|
118
121
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
@@ -120,8 +123,10 @@ const Portal = ({
|
|
|
120
123
|
React__default.useLayoutEffect(() => {
|
|
121
124
|
return context === null || context === void 0 ? void 0 : context.teardown;
|
|
122
125
|
}, [context === null || context === void 0 ? void 0 : context.teardown, enabled]);
|
|
123
|
-
if (!
|
|
124
|
-
|
|
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);
|
|
125
130
|
};
|
|
126
131
|
|
|
127
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;\nexport type PortalInit = () => { container: Container; teardown?: () => void };\n\nconst PortalContext = React.createContext<PortalInit>(() => ({ container: 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 = React.useMemo(\n () => {\n if (!enabled) return undefined;\n return init();\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 (!enabled) return <>{children}</>;\n return createPortal(children, context?.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","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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AAGH,aAAa,CAACH,SAAQ;;AAEjH;AACA;AACA;AACA;AACO,MAAMO,MAAkE,GAAGA,CAAC;EAAEpB,QAAQ;AAAEqB,EAAAA,OAAO,GAAG,IAAA;AAAK,CAAC,KAAK;AAChH,EAAA,MAAMC,IAAI,GAAGX,cAAK,CAACR,UAAU,CAACa,aAAa,CAAC,CAAA;AAC5C,EAAA,MAAMV,OAAO,GAAGK,cAAK,CAACN,OAAO,CACzB,MAAM;AACF,IAAA,IAAI,CAACgB,OAAO,EAAE,OAAOhC,SAAS,CAAA;IAC9B,OAAOiC,IAAI,EAAE,CAAA;GAChB;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;AAEhC,EAAA,IAAI,CAACA,OAAO,EAAE,oBAAOV,cAAA,CAAAC,aAAA,CAAAD,cAAA,CAAAc,QAAA,EAAA,IAAA,EAAGzB,QAAW,CAAC,CAAA;EACpC,oBAAO0B,YAAY,CAAC1B,QAAQ,EAAEM,OAAO,KAAPA,IAAAA,IAAAA,OAAO,KAAPA,KAAAA,CAAAA,GAAAA,KAAAA,CAAAA,GAAAA,OAAO,CAAEW,SAAsB,CAAC,CAAA;AAClE;;;;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/hooks/useClickAway.tsx","../../src/utils/ClickAwayProvider/ClickAwayProvider.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,EAAQ;AAChFC,EAAAA,SAAS,CAAC,MAAM;IACZ,MAAM;AAAEL,MAAAA,OAAO,EAAEM,WAAAA;AAAY,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A,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,36 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { createPortal } from 'react-dom';
|
|
3
|
-
|
|
4
|
-
type Container = DocumentFragment | Element;
|
|
5
|
-
export type PortalInit = () => { container: Container; teardown?: () => void };
|
|
6
|
-
|
|
7
|
-
const PortalContext = React.createContext<PortalInit>(() => ({ container: document.body }));
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Customize where <Portal> wrapped elements render (tooltip, popover, dialog, etc.)
|
|
11
|
-
*/
|
|
12
|
-
export const PortalProvider: React.FC<{ children?: React.ReactNode; value: PortalInit }> = PortalContext.Provider;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Render children in a portal outside the current DOM position
|
|
16
|
-
* (defaults to `document.body` but can be customized with the PortalContextProvider)
|
|
17
|
-
*/
|
|
18
|
-
export const Portal: React.FC<{ enabled?: boolean; children: React.ReactNode }> = ({ children, enabled = true }) => {
|
|
19
|
-
const init = React.useContext(PortalContext);
|
|
20
|
-
const context = React.useMemo(
|
|
21
|
-
() => {
|
|
22
|
-
if (!enabled) return undefined;
|
|
23
|
-
return init();
|
|
24
|
-
},
|
|
25
|
-
// Only update on 'enabled'
|
|
26
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
27
|
-
[enabled],
|
|
28
|
-
);
|
|
29
|
-
|
|
30
|
-
React.useLayoutEffect(() => {
|
|
31
|
-
return context?.teardown;
|
|
32
|
-
}, [context?.teardown, enabled]);
|
|
33
|
-
|
|
34
|
-
if (!enabled) return <>{children}</>;
|
|
35
|
-
return createPortal(children, context?.container as Container);
|
|
36
|
-
};
|