@lumx/react 3.17.1 → 3.17.3-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.17.1",
10
- "@lumx/icons": "^3.17.1",
9
+ "@lumx/core": "^3.17.3-alpha.0",
10
+ "@lumx/icons": "^3.17.3-alpha.0",
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.17.1"
108
+ "version": "3.17.3-alpha.0"
109
109
  }
@@ -18,7 +18,11 @@ export default {
18
18
  component: ExpansionPanel,
19
19
  args: {
20
20
  'toggleButtonProps.label': 'Toggle',
21
- children: 'Content',
21
+ children: (
22
+ <Text as="p" typography="body1" color="dark-L2" className="lumx-spacing-padding-big">
23
+ content
24
+ </Text>
25
+ ),
22
26
  label: 'Label',
23
27
  },
24
28
  decorators: [withNestedProps()],
@@ -77,3 +81,11 @@ export const Nested = {
77
81
  );
78
82
  },
79
83
  };
84
+
85
+ /** Hide component instead of unmounting it */
86
+ export const HideChildren = {
87
+ args: {
88
+ hasBackground: true,
89
+ closeMode: 'hide',
90
+ },
91
+ };
@@ -134,11 +134,11 @@ describe(`<${ExpansionPanel.displayName}>`, () => {
134
134
  expect(onToggleOpen).toHaveBeenCalledWith(false, expect.anything());
135
135
  });
136
136
 
137
- it('should hide children after toggling the expansion panel', async () => {
137
+ it('should unmount children after toggling the expansion panel', async () => {
138
138
  const user = userEvent.setup();
139
139
  const { query } = setup({}, { controlled: true });
140
140
 
141
- // Content is not visible by default
141
+ // Content is not mounted by default
142
142
  expect(query.content()).not.toBeInTheDocument();
143
143
 
144
144
  await user.click(query.header() as any);
@@ -149,6 +149,25 @@ describe(`<${ExpansionPanel.displayName}>`, () => {
149
149
 
150
150
  expect(query.content()).not.toBeInTheDocument();
151
151
  });
152
+
153
+ it('should hide children after toggling the expansion panel', async () => {
154
+ const user = userEvent.setup();
155
+ const { element, query } = setup({ closeMode: 'hide' }, { controlled: true });
156
+
157
+ // Content is hidden (but mounted) by default
158
+ expect(query.content()).toBeInTheDocument();
159
+ expect(element).toHaveClass(`${CLASSNAME}--is-close`);
160
+
161
+ await user.click(query.header() as any);
162
+
163
+ expect(query.content()).toBeInTheDocument();
164
+ expect(element).toHaveClass(`${CLASSNAME}--is-open`);
165
+
166
+ await user.click(query.header() as any);
167
+
168
+ expect(query.content()).toBeInTheDocument();
169
+ expect(element).toHaveClass(`${CLASSNAME}--is-close`);
170
+ });
152
171
  });
153
172
 
154
173
  // Common tests suite.
@@ -7,7 +7,7 @@ import { mdiChevronDown, mdiChevronUp } from '@lumx/icons';
7
7
  import isEmpty from 'lodash/isEmpty';
8
8
 
9
9
  import { ColorPalette, DragHandle, Emphasis, IconButton, IconButtonProps, Theme } from '@lumx/react';
10
- import { GenericProps, HasTheme, isComponent } from '@lumx/react/utils/type';
10
+ import { GenericProps, HasCloseMode, HasTheme, isComponent } from '@lumx/react/utils/type';
11
11
  import { getRootClassName, handleBasicClasses } from '@lumx/react/utils/className';
12
12
  import { partitionMulti } from '@lumx/react/utils/partitionMulti';
13
13
  import { useTheme } from '@lumx/react/utils/theme/ThemeContext';
@@ -17,7 +17,7 @@ import { IS_BROWSER } from '@lumx/react/constants';
17
17
  /**
18
18
  * Defines the props of the component.
19
19
  */
20
- export interface ExpansionPanelProps extends GenericProps, HasTheme {
20
+ export interface ExpansionPanelProps extends GenericProps, HasCloseMode, HasTheme {
21
21
  /** Whether the expansion panel has a background. */
22
22
  hasBackground?: boolean;
23
23
  /** Whether the header has a divider. */
@@ -52,7 +52,9 @@ const CLASSNAME = getRootClassName(COMPONENT_NAME);
52
52
  /**
53
53
  * Component default props.
54
54
  */
55
- const DEFAULT_PROPS: Partial<ExpansionPanelProps> = {};
55
+ const DEFAULT_PROPS: Partial<ExpansionPanelProps> = {
56
+ closeMode: 'unmount',
57
+ };
56
58
 
57
59
  const isDragHandle = isComponent(DragHandle);
58
60
  const isHeader = isComponent('header');
@@ -69,6 +71,7 @@ export const ExpansionPanel = forwardRef<ExpansionPanelProps, HTMLDivElement>((p
69
71
  const defaultTheme = useTheme() || Theme.light;
70
72
  const {
71
73
  className,
74
+ closeMode = DEFAULT_PROPS.closeMode,
72
75
  children: anyChildren,
73
76
  hasBackground,
74
77
  hasHeaderDivider,
@@ -127,32 +130,32 @@ export const ExpansionPanel = forwardRef<ExpansionPanelProps, HTMLDivElement>((p
127
130
 
128
131
  const wrapperRef = useRef<HTMLDivElement>(null);
129
132
 
130
- // Children visible while the open/close transition is running
133
+ // Children stay visible while the open/close transition is running
131
134
  const [isChildrenVisible, setChildrenVisible] = React.useState(isOpen);
132
135
 
133
136
  const isOpenRef = React.useRef(isOpen);
134
137
  React.useEffect(() => {
135
- if (isOpen) {
138
+ if (isOpen || closeMode === 'hide') {
136
139
  setChildrenVisible(true);
137
140
  } else if (!IS_BROWSER) {
138
141
  // Outside a browser we can't wait for the transition
139
142
  setChildrenVisible(false);
140
143
  }
141
144
  isOpenRef.current = isOpen;
142
- }, [isOpen]);
145
+ }, [closeMode, isOpen]);
143
146
 
144
- // Change children visibility on transition end
147
+ // Change children's visibility on the transition end
145
148
  React.useEffect(() => {
146
149
  const { current: wrapper } = wrapperRef;
147
150
  if (!IS_BROWSER || !wrapper) {
148
151
  return undefined;
149
152
  }
150
153
  const onTransitionEnd = () => {
151
- setChildrenVisible(isOpenRef.current);
154
+ setChildrenVisible(isOpenRef.current || closeMode === 'hide');
152
155
  };
153
156
  wrapper.addEventListener('transitionend', onTransitionEnd);
154
157
  return () => wrapper.removeEventListener('transitionend', onTransitionEnd);
155
- }, []);
158
+ }, [closeMode]);
156
159
 
157
160
  return (
158
161
  <section ref={ref} {...forwardedProps} className={rootClassName}>
@@ -6,9 +6,9 @@ import isEmpty from 'lodash/isEmpty';
6
6
 
7
7
  const EVENT_TYPES = ['mousedown', 'touchstart'];
8
8
 
9
- function isClickAway(target: HTMLElement, refs: Array<RefObject<HTMLElement>>): boolean {
10
- // The target element is not contained in any of the listed element references.
11
- return !refs.some((e) => e?.current?.contains(target));
9
+ function isClickAway(targets: HTMLElement[], refs: Array<RefObject<HTMLElement>>): boolean {
10
+ // The targets elements are not contained in any of the listed element references.
11
+ return !refs.some((ref) => targets.some((target) => ref?.current?.contains(target)));
12
12
  }
13
13
 
14
14
  export interface ClickAwayParameters {
@@ -34,7 +34,8 @@ export function useClickAway({ callback, childrenRefs }: ClickAwayParameters): v
34
34
  return undefined;
35
35
  }
36
36
  const listener: EventListener = (evt) => {
37
- if (isClickAway(evt.target as HTMLElement, currentRefs)) {
37
+ const targets = [evt.composedPath?.()[0], evt.target] as HTMLElement[];
38
+ if (isClickAway(targets, currentRefs)) {
38
39
  callback(evt);
39
40
  }
40
41
  };
@@ -0,0 +1,10 @@
1
+ /** Init a demo shadow DOM to use as portal container */
2
+ export const initDemoShadowDOMPortal = () => {
3
+ const div = document.createElement('div');
4
+ document.body.appendChild(div);
5
+ const container = div.attachShadow({ mode: 'open' });
6
+ const style = document.createElement('style');
7
+ style.innerText = `button { color: red; }`;
8
+ container.appendChild(style);
9
+ return { container, teardown: () => div.remove() };
10
+ };
@@ -1,8 +1,11 @@
1
+ /* eslint-disable react-hooks/rules-of-hooks */
1
2
  /* eslint-disable @typescript-eslint/no-use-before-define */
2
- import { Button } from '@lumx/react';
3
- import { ClickAwayProvider } from '@lumx/react/utils/ClickAwayProvider';
4
3
  import React, { useCallback, useRef, useState } from 'react';
5
- import { createPortal } from 'react-dom';
4
+ import { Button, Popover } from '@lumx/react';
5
+ import { ClickAwayProvider } from '@lumx/react/utils/ClickAwayProvider';
6
+ import { initDemoShadowDOMPortal } from '@lumx/react/stories/utils/initDemoShadowDOMPortal';
7
+ import { PortalProvider, Portal } from '@lumx/react/utils/Portal';
8
+ import { useBooleanState } from '@lumx/react/hooks/useBooleanState';
6
9
 
7
10
  export default {
8
11
  title: 'LumX components/ClickAwayProvider',
@@ -15,16 +18,15 @@ export default {
15
18
  // eslint-disable-next-line react/display-name
16
19
  const Card = React.forwardRef(({ top, children, close }, ref) => (
17
20
  <ClickAwayProvider callback={close} childrenRefs={useRef([ref])}>
18
- {createPortal(
21
+ <Portal>
19
22
  <div
20
23
  className="lumx-spacing-padding"
21
24
  ref={ref}
22
25
  style={{ position: 'absolute', top, border: '1px solid red' }}
23
26
  >
24
27
  {children}
25
- </div>,
26
- document.body,
27
- )}
28
+ </div>
29
+ </Portal>
28
30
  </ClickAwayProvider>
29
31
  ));
30
32
 
@@ -56,3 +58,21 @@ export const NestedClickAway = () => (
56
58
  <ButtonWithCard level={0} />
57
59
  </>
58
60
  );
61
+
62
+ /**
63
+ * Testing close on click away for a popover rendered in a shadow DOM
64
+ */
65
+ export const InShadowDOM = () => {
66
+ const [isOpen, close, , toggle] = useBooleanState(false);
67
+ const anchorRef = React.useRef();
68
+ return (
69
+ <PortalProvider value={initDemoShadowDOMPortal}>
70
+ <Button ref={anchorRef} onClick={toggle}>
71
+ Toggle popover
72
+ </Button>
73
+ <Popover isOpen={isOpen} anchorRef={anchorRef} onClose={close} closeOnClickAway>
74
+ <p style={{ padding: 16, border: '1px solid gray' }}>Popover in shadow DOM</p>
75
+ </Popover>
76
+ </PortalProvider>
77
+ );
78
+ };
@@ -1,34 +1,23 @@
1
- import { Button } from '@lumx/react';
2
1
  import React from 'react';
2
+ import { Button } from '@lumx/react';
3
+ import { initDemoShadowDOMPortal } from '@lumx/react/stories/utils/initDemoShadowDOMPortal';
3
4
  import { PortalProvider } from './PortalProvider';
4
5
  import { Portal } from './Portal';
5
6
 
6
7
  export default {
7
8
  title: 'LumX components/PortalProvider',
8
9
  component: PortalProvider,
9
- };
10
-
11
- const initShadowDOMPortal = () => {
12
- const div = document.createElement('div');
13
- document.body.appendChild(div);
14
- const container = div.attachShadow({ mode: 'closed' });
15
- const style = document.createElement('style');
16
- style.innerText = `button { color: red; }`;
17
- container.appendChild(style);
18
- return { container, teardown: () => div.remove() };
10
+ args: { enabled: true },
11
+ parameters: { chromatic: { disable: true } },
19
12
  };
20
13
 
21
14
  /**
22
15
  * Demonstrate how to customize portals to render into a custom shadow root
23
16
  */
24
- export const RenderInShadowDOM = {
25
- args: { enabled: true },
26
- render: ({ enabled }) => (
27
- <PortalProvider value={initShadowDOMPortal}>
28
- <Portal enabled={enabled}>
29
- <Button>My button {!enabled && 'not'} in a shadow DOM portal</Button>
30
- </Portal>
31
- </PortalProvider>
32
- ),
33
- parameters: { chromatic: { disable: true } },
34
- };
17
+ export const RenderInShadowDOM = ({ enabled }) => (
18
+ <PortalProvider value={initDemoShadowDOMPortal}>
19
+ <Portal enabled={enabled}>
20
+ <Button>My button {!enabled && 'not'} in a shadow DOM portal</Button>
21
+ </Portal>
22
+ </PortalProvider>
23
+ );
package/utils/index.js CHANGED
@@ -3,12 +3,12 @@ import isEmpty from 'lodash/isEmpty';
3
3
  import { createPortal } from 'react-dom';
4
4
 
5
5
  const EVENT_TYPES = ['mousedown', 'touchstart'];
6
- function isClickAway(target, refs) {
7
- // The target element is not contained in any of the listed element references.
8
- return !refs.some(e => {
9
- var _e$current;
10
- return e === null || e === void 0 ? void 0 : (_e$current = e.current) === null || _e$current === void 0 ? void 0 : _e$current.contains(target);
11
- });
6
+ function isClickAway(targets, refs) {
7
+ // The targets elements are not contained in any of the listed element references.
8
+ return !refs.some(ref => targets.some(target => {
9
+ var _ref$current;
10
+ return ref === null || ref === void 0 ? void 0 : (_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.contains(target);
11
+ }));
12
12
  }
13
13
  /**
14
14
  * Listen to clicks away from the given elements and callback the passed in function.
@@ -27,7 +27,9 @@ function useClickAway({
27
27
  return undefined;
28
28
  }
29
29
  const listener = evt => {
30
- if (isClickAway(evt.target, currentRefs)) {
30
+ var _evt$composedPath;
31
+ const targets = [(_evt$composedPath = evt.composedPath) === null || _evt$composedPath === void 0 ? void 0 : _evt$composedPath.call(evt)[0], evt.target];
32
+ if (isClickAway(targets, currentRefs)) {
31
33
  callback(evt);
32
34
  }
33
35
  };
@@ -1 +1 @@
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,GA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M,OAAO,CAACW,SAAS,CAAC,CAAA;AACpD;;;;"}
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(targets: HTMLElement[], refs: Array<RefObject<HTMLElement>>): boolean {\n // The targets elements are not contained in any of the listed element references.\n return !refs.some((ref) => targets.some((target) => ref?.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 const targets = [evt.composedPath?.()[0], evt.target] as HTMLElement[];\n if (isClickAway(targets, 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","targets","refs","some","ref","target","_ref$current","current","contains","useClickAway","callback","childrenRefs","useEffect","currentRefs","isEmpty","undefined","listener","evt","_evt$composedPath","composedPath","call","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,OAAsB,EAAEC,IAAmC,EAAW;AACvF;EACA,OAAO,CAACA,IAAI,CAACC,IAAI,CAAEC,GAAG,IAAKH,OAAO,CAACE,IAAI,CAAEE,MAAM,IAAA;AAAA,IAAA,IAAAC,YAAA,CAAA;AAAA,IAAA,OAAKF,GAAG,KAAHA,IAAAA,IAAAA,GAAG,KAAAE,KAAAA,CAAAA,GAAAA,KAAAA,CAAAA,GAAAA,CAAAA,YAAA,GAAHF,GAAG,CAAEG,OAAO,MAAA,IAAA,IAAAD,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