@lumx/react 3.15.2-alpha.5 → 3.16.1-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/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.15.2-alpha.5",
10
- "@lumx/icons": "^3.15.2-alpha.5",
9
+ "@lumx/core": "^3.16.1-alpha.0",
10
+ "@lumx/icons": "^3.16.1-alpha.0",
11
11
  "@popperjs/core": "^2.5.4",
12
12
  "body-scroll-lock": "^3.1.5",
13
13
  "classnames": "^2.3.2",
@@ -26,15 +26,13 @@
26
26
  "@babel/preset-env": "^7.26.9",
27
27
  "@babel/preset-react": "^7.26.3",
28
28
  "@babel/preset-typescript": "^7.26.0",
29
+ "@chromatic-com/storybook": "^4.1.1",
29
30
  "@rollup/plugin-babel": "^6.0.4",
30
31
  "@rollup/plugin-commonjs": "^19.0.2",
31
32
  "@rollup/plugin-node-resolve": "16.0.0",
32
- "@storybook/addon-a11y": "^7.6.3",
33
- "@storybook/addon-essentials": "^7.6.3",
34
- "@storybook/addon-interactions": "^7.6.3",
35
- "@storybook/blocks": "^7.6.3",
36
- "@storybook/react": "^7.6.3",
37
- "@storybook/react-vite": "^7.6.3",
33
+ "@storybook/addon-a11y": "^9.1.4",
34
+ "@storybook/addon-docs": "^9.1.4",
35
+ "@storybook/react-vite": "^9.1.4",
38
36
  "@testing-library/dom": "^9.3.4",
39
37
  "@testing-library/jest-dom": "^5.16.4",
40
38
  "@testing-library/react": "^12.1.2",
@@ -50,7 +48,7 @@
50
48
  "autoprefixer": "^9.7.4",
51
49
  "babel-jest": "29.1.2",
52
50
  "babel-loader": "^8.0.6",
53
- "chromatic": "^6.0.6",
51
+ "chromatic": "^13.1.4",
54
52
  "core-js": "^3.6.4",
55
53
  "focus-visible": "^5.0.2",
56
54
  "glob": "^7.1.6",
@@ -68,10 +66,10 @@
68
66
  "rollup-plugin-dts": "^6.1.1",
69
67
  "rollup-plugin-peer-deps-external": "^2.2.4",
70
68
  "rollup-plugin-ts-paths-resolve": "^1.7.1",
71
- "storybook": "^7.6.3",
69
+ "storybook": "^9.1.4",
72
70
  "typescript": "^5.4.3",
73
- "vite": "^4.2.1",
74
- "vite-tsconfig-paths": "^4.0.7",
71
+ "vite": "^6.3.5",
72
+ "vite-tsconfig-paths": "^5.1.4",
75
73
  "yargs": "^15.4.1"
76
74
  },
77
75
  "peerDependencies": {
@@ -107,5 +105,5 @@
107
105
  "build:storybook": "storybook build"
108
106
  },
109
107
  "sideEffects": false,
110
- "version": "3.15.2-alpha.5"
108
+ "version": "3.16.1-alpha.0"
111
109
  }
@@ -1,5 +1,4 @@
1
1
  import React, { Children, ReactElement, ReactNode, Ref, RefObject, useMemo, useRef, useState } from 'react';
2
- import { createPortal } from 'react-dom';
3
2
 
4
3
  import classNames from 'classnames';
5
4
 
@@ -20,6 +19,7 @@ import { forwardRef } from '@lumx/react/utils/react/forwardRef';
20
19
  import { useDisableBodyScroll } from '@lumx/react/hooks/useDisableBodyScroll';
21
20
  import { useTransitionVisibility } from '@lumx/react/hooks/useTransitionVisibility';
22
21
  import { ThemeProvider } from '@lumx/react/utils/theme/ThemeContext';
22
+ import { Portal } from '@lumx/react/utils/PortalProvider';
23
23
 
24
24
  /**
25
25
  * Defines the props of the component.
@@ -194,7 +194,7 @@ export const Dialog = forwardRef<DialogProps, HTMLDivElement>((props, ref) => {
194
194
  const shouldPreventCloseOnClickAway = preventAutoClose || preventCloseOnClick;
195
195
 
196
196
  return isOpen || isVisible
197
- ? createPortal(
197
+ ? <Portal>
198
198
  <div
199
199
  ref={mergeRefs(rootRef, ref)}
200
200
  {...forwardedProps}
@@ -278,9 +278,8 @@ export const Dialog = forwardRef<DialogProps, HTMLDivElement>((props, ref) => {
278
278
  </div>
279
279
  </ThemeProvider>
280
280
  </HeadingLevelProvider>
281
- </div>,
282
- document.body,
283
- )
281
+ </div>
282
+ </Portal>
284
283
  : null;
285
284
  });
286
285
  Dialog.displayName = COMPONENT_NAME;
@@ -1,7 +1,6 @@
1
1
  import React, { RefObject, useRef, useEffect, AriaAttributes } from 'react';
2
2
 
3
3
  import classNames from 'classnames';
4
- import { createPortal } from 'react-dom';
5
4
 
6
5
  import { mdiClose } from '@lumx/icons';
7
6
  import { HeadingLevelProvider, IconButton, IconButtonProps } from '@lumx/react';
@@ -17,6 +16,7 @@ import { useCallbackOnEscape } from '@lumx/react/hooks/useCallbackOnEscape';
17
16
  import { useTransitionVisibility } from '@lumx/react/hooks/useTransitionVisibility';
18
17
  import { ThemeProvider } from '@lumx/react/utils/theme/ThemeContext';
19
18
  import { forwardRef } from '@lumx/react/utils/react/forwardRef';
19
+ import { Portal } from '@lumx/react/utils/PortalProvider';
20
20
 
21
21
  /**
22
22
  * Defines the props of the component.
@@ -127,52 +127,52 @@ export const Lightbox = forwardRef<LightboxProps, HTMLDivElement>((props, ref) =
127
127
 
128
128
  if (!isOpen && !isVisible) return null;
129
129
 
130
- return createPortal(
131
- /* eslint-disable-next-line jsx-a11y/click-events-have-key-events,jsx-a11y/no-static-element-interactions */
132
- <div
133
- ref={mergeRefs(ref, wrapperRef)}
134
- {...forwardedProps}
135
- aria-label={ariaLabel}
136
- aria-labelledby={ariaLabelledBy}
137
- aria-modal="true"
138
- role="dialog"
139
- tabIndex={-1}
140
- className={classNames(
141
- className,
142
- handleBasicClasses({
143
- prefix: CLASSNAME,
144
- isHidden: !isOpen,
145
- isShown: isOpen || isVisible,
146
- theme,
147
- }),
148
- )}
149
- style={{ zIndex }}
150
- >
151
- {closeButtonProps && (
152
- <div className={`${CLASSNAME}__close`}>
153
- <IconButton
154
- {...closeButtonProps}
155
- ref={closeButtonRef}
156
- emphasis="low"
157
- hasBackground
158
- icon={mdiClose}
159
- theme="dark"
160
- type="button"
161
- onClick={onClose}
162
- />
163
- </div>
164
- )}
165
- <HeadingLevelProvider level={2}>
166
- <ThemeProvider value={undefined}>
167
- <ClickAwayProvider callback={!preventAutoClose && onClose} childrenRefs={clickAwayRefs}>
168
- <div ref={childrenRef} className={`${CLASSNAME}__wrapper`} role="presentation">
169
- {children}
170
- </div>
171
- </ClickAwayProvider>
172
- </ThemeProvider>
173
- </HeadingLevelProvider>
174
- </div>,
175
- document.body,
130
+ return (
131
+ <Portal>
132
+ <div
133
+ ref={mergeRefs(ref, wrapperRef)}
134
+ {...forwardedProps}
135
+ aria-label={ariaLabel}
136
+ aria-labelledby={ariaLabelledBy}
137
+ aria-modal="true"
138
+ role="dialog"
139
+ tabIndex={-1}
140
+ className={classNames(
141
+ className,
142
+ handleBasicClasses({
143
+ prefix: CLASSNAME,
144
+ isHidden: !isOpen,
145
+ isShown: isOpen || isVisible,
146
+ theme,
147
+ }),
148
+ )}
149
+ style={{ zIndex }}
150
+ >
151
+ {closeButtonProps && (
152
+ <div className={`${CLASSNAME}__close`}>
153
+ <IconButton
154
+ {...closeButtonProps}
155
+ ref={closeButtonRef}
156
+ emphasis="low"
157
+ hasBackground
158
+ icon={mdiClose}
159
+ theme="dark"
160
+ type="button"
161
+ onClick={onClose}
162
+ />
163
+ </div>
164
+ )}
165
+ <HeadingLevelProvider level={2}>
166
+ <ThemeProvider value={undefined}>
167
+ <ClickAwayProvider callback={!preventAutoClose && onClose} childrenRefs={clickAwayRefs}>
168
+ <div ref={childrenRef} className={`${CLASSNAME}__wrapper`} role="presentation">
169
+ {children}
170
+ </div>
171
+ </ClickAwayProvider>
172
+ </ThemeProvider>
173
+ </HeadingLevelProvider>
174
+ </div>
175
+ </Portal>
176
176
  );
177
177
  });
178
178
  Lightbox.displayName = COMPONENT_NAME;
@@ -13,6 +13,7 @@ import { useTransitionVisibility } from '@lumx/react/hooks/useTransitionVisibili
13
13
  import { mergeRefs } from '@lumx/react/utils/react/mergeRefs';
14
14
  import { useTheme } from '@lumx/react/utils/theme/ThemeContext';
15
15
  import { forwardRef } from '@lumx/react/utils/react/forwardRef';
16
+ import { Portal } from '@lumx/react/utils/PortalProvider';
16
17
 
17
18
  /**
18
19
  * Defines the props of the component.
@@ -130,7 +131,7 @@ export const Notification = forwardRef<NotificationProps, HTMLDivElement>((props
130
131
  </div>
131
132
  );
132
133
 
133
- return usePortal ? createPortal(notification, document.body) : notification;
134
+ return usePortal ? <Portal>{notification}</Portal> : notification;
134
135
  });
135
136
  Notification.displayName = COMPONENT_NAME;
136
137
  Notification.className = CLASSNAME;
@@ -1,5 +1,4 @@
1
1
  import React, { ReactNode, RefObject, useRef } from 'react';
2
- import { createPortal } from 'react-dom';
3
2
 
4
3
  import classNames from 'classnames';
5
4
 
@@ -15,6 +14,7 @@ import { skipRender } from '@lumx/react/utils/react/skipRender';
15
14
  import { forwardRef } from '@lumx/react/utils/react/forwardRef';
16
15
 
17
16
  import { ThemeProvider } from '@lumx/react/utils/theme/ThemeContext';
17
+ import { Portal } from '@lumx/react/utils/PortalProvider';
18
18
  import { useRestoreFocusOnClose } from './useRestoreFocusOnClose';
19
19
  import { usePopoverStyle } from './usePopoverStyle';
20
20
  import { Elevation, FitAnchorWidth, Offset, Placement, POPOVER_ZINDEX } from './constants';
@@ -95,7 +95,7 @@ const DEFAULT_PROPS: Partial<PopoverProps> = {
95
95
 
96
96
  /** Method to render the popover inside a portal if usePortal is true */
97
97
  const renderPopover = (children: ReactNode, usePortal?: boolean): any => {
98
- return usePortal ? createPortal(children, document.body) : children;
98
+ return usePortal ? <Portal>{children}</Portal> : children;
99
99
  };
100
100
 
101
101
  // Inner component (must be wrapped before export)
@@ -11,7 +11,7 @@ import { DESIGN_TOKENS } from '@lumx/core/js/constants/design-tokens';
11
11
  import { disableArgTypes } from '@lumx/react/stories/utils/disableArgTypes';
12
12
  import { iconArgType } from '@lumx/react/stories/controls/icons';
13
13
  import { withWrapper } from '@lumx/react/stories/decorators/withWrapper';
14
- import { action } from '@storybook/addon-actions';
14
+ import { action } from 'storybook/actions';
15
15
 
16
16
  const EMPHASIS = [Emphasis.high, Emphasis.medium, Emphasis.low];
17
17
 
@@ -1,6 +1,5 @@
1
1
  /* eslint-disable react-hooks/rules-of-hooks */
2
2
  import React, { ReactNode, useState } from 'react';
3
- import { createPortal } from 'react-dom';
4
3
 
5
4
  import classNames from 'classnames';
6
5
 
@@ -15,6 +14,7 @@ import { usePopper } from '@lumx/react/hooks/usePopper';
15
14
  import { forwardRef } from '@lumx/react/utils/react/forwardRef';
16
15
 
17
16
  import { ARIA_LINK_MODES, TOOLTIP_ZINDEX } from '@lumx/react/components/tooltip/constants';
17
+ import { Portal } from '@lumx/react/utils/PortalProvider';
18
18
  import { useInjectTooltipRef } from './useInjectTooltipRef';
19
19
  import { useTooltipOpen } from './useTooltipOpen';
20
20
 
@@ -129,8 +129,8 @@ export const Tooltip = forwardRef<TooltipProps, HTMLDivElement>((props, ref) =>
129
129
  return (
130
130
  <>
131
131
  <TooltipContextProvider>{wrappedChildren}</TooltipContextProvider>
132
- {isMounted &&
133
- createPortal(
132
+ {isMounted && (
133
+ <Portal>
134
134
  <div
135
135
  ref={tooltipRef}
136
136
  {...forwardedProps}
@@ -154,9 +154,9 @@ export const Tooltip = forwardRef<TooltipProps, HTMLDivElement>((props, ref) =>
154
154
  <p key={line}>{line}</p>
155
155
  ))}
156
156
  </div>
157
- </div>,
158
- document.body,
159
- )}
157
+ </div>
158
+ </Portal>
159
+ )}
160
160
  </>
161
161
  );
162
162
  });
@@ -0,0 +1,40 @@
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
+ };
@@ -0,0 +1,43 @@
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
+ };
@@ -3,3 +3,4 @@
3
3
  */
4
4
 
5
5
  export * from './ClickAwayProvider';
6
+ export * from './PortalProvider';
package/utils/index.d.ts CHANGED
@@ -30,4 +30,27 @@ interface ClickAwayProviderProps extends ClickAwayParameters {
30
30
  */
31
31
  declare const ClickAwayProvider: React.FC<ClickAwayProviderProps>;
32
32
 
33
- export { ClickAwayProvider };
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
+ /**
41
+ * Customize where <Portal> wrapped elements render (tooltip, popover, dialog, etc.)
42
+ */
43
+ declare const PortalProvider: React.FC<{
44
+ children?: React.ReactNode;
45
+ value: PortalInit;
46
+ }>;
47
+ /**
48
+ * Render children in a portal outside the current DOM position
49
+ * (defaults to document.body but can be customized with the PortalContextProvider)
50
+ */
51
+ declare const Portal: React.FC<{
52
+ enabled?: boolean;
53
+ children: React.ReactNode;
54
+ }>;
55
+
56
+ export { ClickAwayProvider, Portal, PortalProvider };
package/utils/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import React__default, { useEffect, useContext, useMemo, useRef, createContext } from 'react';
2
+ import { createPortal } from 'react-dom';
2
3
  import isEmpty from 'lodash/isEmpty';
3
4
 
4
5
  const EVENT_TYPES = ['mousedown', 'touchstart'];
@@ -91,5 +92,45 @@ const ClickAwayProvider = ({
91
92
  };
92
93
  ClickAwayProvider.displayName = 'ClickAwayProvider';
93
94
 
94
- export { ClickAwayProvider };
95
+ const PortalContext = /*#__PURE__*/React__default.createContext(() => ({
96
+ mount: () => document.body
97
+ }));
98
+
99
+ /**
100
+ * Customize where <Portal> wrapped elements render (tooltip, popover, dialog, etc.)
101
+ */
102
+ const PortalProvider = PortalContext.Provider;
103
+
104
+ /**
105
+ * Render children in a portal outside the current DOM position
106
+ * (defaults to document.body but can be customized with the PortalContextProvider)
107
+ */
108
+ const Portal = ({
109
+ children,
110
+ enabled = true
111
+ }) => {
112
+ const init = React__default.useContext(PortalContext);
113
+ const {
114
+ context,
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
+ };
124
+ },
125
+ // Only update on 'enabled'
126
+ // eslint-disable-next-line react-hooks/exhaustive-deps
127
+ [enabled]);
128
+ React__default.useLayoutEffect(() => {
129
+ return context === null || context === void 0 ? void 0 : context.unmount;
130
+ }, [context === null || context === void 0 ? void 0 : context.unmount, enabled]);
131
+ if (!enabled) return /*#__PURE__*/React__default.createElement(React__default.Fragment, null, children);
132
+ return /*#__PURE__*/createPortal(children, container);
133
+ };
134
+
135
+ export { ClickAwayProvider, Portal, PortalProvider };
95
136
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../../src/hooks/useClickAway.tsx","../../src/utils/ClickAwayProvider/ClickAwayProvider.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"],"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"],"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,KAAC,GAAGF,YAAY,CAAA;IAC7C,IAAI,CAACD,QAAQ,IAAI,CAACG,WAAW,IAAIC,OAAO,CAACD,WAAW,CAAC,EAAE;AACnD,MAAA,OAAOE,SAAS,CAAA;AACpB,KAAA;IACA,MAAMC,QAAuB,GAAIC,GAAG,IAAK;MACrC,IAAIhB,WAAW,CAACgB,GAAG,CAACf,MAAM,EAAiBW,WAAW,CAAC,EAAE;QACrDH,QAAQ,CAACO,GAAG,CAAC,CAAA;AACjB,OAAA;KACH,CAAA;AAEDjB,I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ACZ,MAAM;AAAEL,MAAAA,OAAO,EAAEM,WAAAA;AAAY,KAAC,GAAGF,YAAY,CAAA;IAC7C,IAAI,CAACE,WAAW,EAAE;AACd,MAAA,OAAA;AACJ,KAAA;AACAiB,IAAAA,cAAc,CAACG,OAAO,CAAC,GAAGpB,WAAW,CAAC,CAAA;AAC1C,GAAC,EAAE,CAACiB,cAAc,EAAEnB,YAAY,CAAC,CAAC,CAAA;AAElCF,EAAAA,YAAY,CAAC;IAAEC,QAAQ;AAAEC,IAAAA,YAAY,EAAEyB,MAAM,CAACN,cAAc,CAACnB,YAAY,CAAA;AAAE,GAAC,CAAC,CAAA;AAC7E,EAAA,oBAAO0B,cAAA,CAAAC,aAAA,CAACf,wBAAwB,CAACgB,QAAQ,EAAA;AAACC,IAAAA,KAAK,EAAEV,cAAAA;AAAe,GAAA,EAAEJ,QAA4C,CAAC,CAAA;AACnH,EAAC;AACDD,iBAAiB,CAACgB,WAAW,GAAG,mBAAmB;;;;"}
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,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