@lumx/react 3.9.7-alpha.3 → 3.9.7

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.9.7-alpha.3",
10
- "@lumx/icons": "^3.9.7-alpha.3",
9
+ "@lumx/core": "^3.9.7",
10
+ "@lumx/icons": "^3.9.7",
11
11
  "@popperjs/core": "^2.5.4",
12
12
  "body-scroll-lock": "^3.1.5",
13
13
  "classnames": "^2.3.2",
@@ -26,8 +26,9 @@
26
26
  "@babel/preset-env": "^7.18.10",
27
27
  "@babel/preset-react": "^7.18.6",
28
28
  "@babel/preset-typescript": "^7.18.6",
29
- "@rollup/plugin-commonjs": "^15.0.0",
30
- "@rollup/plugin-node-resolve": "9.0.0",
29
+ "@rollup/plugin-babel": "^6.0.4",
30
+ "@rollup/plugin-commonjs": "^19.0.2",
31
+ "@rollup/plugin-node-resolve": "16.0.0",
31
32
  "@storybook/addon-a11y": "^7.6.3",
32
33
  "@storybook/addon-essentials": "^7.6.3",
33
34
  "@storybook/addon-interactions": "^7.6.3",
@@ -58,15 +59,13 @@
58
59
  "jest-environment-jsdom": "29.1.2",
59
60
  "react": "^17.0.2",
60
61
  "react-dom": "^17.0.2",
61
- "rollup": "2.79.2",
62
- "rollup-plugin-analyzer": "^3.3.0",
63
- "rollup-plugin-babel": "^4.4.0",
62
+ "rollup": "3.29.5",
63
+ "rollup-plugin-analyzer": "^4.0.0",
64
64
  "rollup-plugin-cleaner": "^1.0.0",
65
- "rollup-plugin-copy": "^3.3.0",
66
- "rollup-plugin-dts": "^4.2.2",
65
+ "rollup-plugin-copy": "^3.5.0",
66
+ "rollup-plugin-dts": "^6.1.1",
67
67
  "rollup-plugin-peer-deps-external": "^2.2.4",
68
- "rollup-plugin-ts-paths-resolve": "^1.3.0",
69
- "rollup-plugin-typescript-paths": "^1.2.2",
68
+ "rollup-plugin-ts-paths-resolve": "^1.7.1",
70
69
  "storybook": "^7.6.3",
71
70
  "typescript": "^5.4.3",
72
71
  "vite": "^4.2.1",
@@ -111,5 +110,5 @@
111
110
  "build:storybook": "storybook build"
112
111
  },
113
112
  "sideEffects": false,
114
- "version": "3.9.7-alpha.3"
113
+ "version": "3.9.7"
115
114
  }
@@ -1,108 +1,170 @@
1
- import { Button, Dialog, Tab, TabList, TabPanel, TabProvider } from '@lumx/react';
2
- import { loremIpsum } from '@lumx/react/stories/utils/lorem';
1
+ /* eslint-disable react-hooks/rules-of-hooks */
2
+
3
+ import { Alignment, Button, Dialog, Tab, TabList, TabListLayout, TabPanel, TabProvider } from '@lumx/react';
4
+ import { iconArgType } from '@lumx/react/stories/controls/icons';
5
+ import { getSelectArgType } from '@lumx/react/stories/controls/selectArgType';
6
+ import { withNestedProps } from '@lumx/react/stories/decorators/withNestedProps';
7
+ import { toFlattenProps } from '@lumx/react/stories/utils/toFlattenProps';
8
+ import { withCategory } from '@lumx/react/stories/utils/withCategory';
3
9
  import get from 'lodash/get';
4
10
  import times from 'lodash/times';
5
11
  import React, { useState } from 'react';
6
12
 
7
- export default { title: 'LumX components/tabs' };
13
+ export default {
14
+ title: 'LumX components/tabs',
15
+ decorators: [withNestedProps()],
16
+ parameters: { controls: { sort: 'alpha' } },
17
+ };
18
+
19
+ /** Default tab behavior with some controllable args */
20
+ export const Default = {
21
+ render: ({ theme, tabProviderProps, tabListProps, tabProps }: any) => (
22
+ <TabProvider {...tabProviderProps}>
23
+ <TabList theme={theme} aria-label="Tab list" {...tabListProps}>
24
+ <Tab {...tabProps[0]} />
25
+ <Tab {...tabProps[1]} />
26
+ <Tab {...tabProps[2]} />
27
+ </TabList>
28
+ <TabPanel className="lumx-spacing-padding-huge">{tabProps[0].label} content</TabPanel>
29
+ <TabPanel className="lumx-spacing-padding-huge">{tabProps[1].label} content</TabPanel>
30
+ <TabPanel className="lumx-spacing-padding-huge">{tabProps[2].label} content</TabPanel>
31
+ </TabProvider>
32
+ ),
33
+ args: toFlattenProps({
34
+ tabProps: [
35
+ { label: 'Tab 1' },
36
+ {
37
+ label: 'Tab 2',
38
+ isDisabled: true,
39
+ },
40
+ { label: 'Tab 3' },
41
+ ],
42
+ }),
43
+ argTypes: toFlattenProps({
44
+ tabProviderProps: withCategory('Tab Provider', {
45
+ isLazy: { control: 'boolean' },
46
+ shouldActivateOnFocus: { control: 'boolean' },
47
+ }),
48
+ tabListProps: withCategory('Tab List', {
49
+ layout: getSelectArgType(TabListLayout),
50
+ position: getSelectArgType([Alignment.left, Alignment.center, Alignment.right]),
51
+ }),
52
+ tabProps: times(3, (index) =>
53
+ withCategory(`Tab ${index + 1}`, {
54
+ label: { control: 'text' },
55
+ icon: iconArgType,
56
+ isDisabled: { control: 'boolean' },
57
+ }),
58
+ ),
59
+ }),
60
+ };
8
61
 
9
62
  /* Control active tab externally (with activate tab on focus). */
10
- export const Controlled = ({ theme }: any) => {
11
- const [activeTab, setActiveTab] = useState(1);
12
- const changeActiveTabIndex = (evt: any) => setActiveTab(parseInt(get(evt, 'target.value', '0'), 10));
63
+ export const Controlled = {
64
+ render({ theme }: any) {
65
+ const [activeTab, setActiveTab] = useState(1);
66
+ const changeActiveTabIndex = (evt: any) => setActiveTab(parseInt(get(evt, 'target.value', '0'), 10));
13
67
 
14
- const [isLazy, setIsLazy] = useState(true);
15
- const changeIsLazy = (evt: any) => setIsLazy(get(evt, 'target.checked'));
68
+ const [isLazy, setIsLazy] = useState(true);
69
+ const changeIsLazy = (evt: any) => setIsLazy(get(evt, 'target.checked'));
16
70
 
17
- const [shouldActivateOnFocus, setShouldActivateOnFocus] = useState(true);
18
- const changeShouldActivateOnFocus = (evt: any) => setShouldActivateOnFocus(get(evt, 'target.checked'));
71
+ const [shouldActivateOnFocus, setShouldActivateOnFocus] = useState(true);
72
+ const changeShouldActivateOnFocus = (evt: any) => setShouldActivateOnFocus(get(evt, 'target.checked'));
19
73
 
20
- return (
21
- <>
22
- <div>
23
- Active tab index:
24
- <input type="number" min={0} max={2} value={activeTab} onChange={changeActiveTabIndex} />
25
- </div>
74
+ return (
75
+ <>
76
+ <div>
77
+ Active tab index:
78
+ <input type="number" min={0} max={2} value={activeTab} onChange={changeActiveTabIndex} />
79
+ </div>
26
80
 
27
- <div>
28
- Lazy render tab panel content:
29
- <input type="checkbox" checked={isLazy} onChange={changeIsLazy} />
30
- </div>
81
+ <div>
82
+ Lazy render tab panel content:
83
+ <input type="checkbox" checked={isLazy} onChange={changeIsLazy} />
84
+ </div>
31
85
 
32
- <div>
33
- Activate tab on focus:
34
- <input type="checkbox" checked={shouldActivateOnFocus} onChange={changeShouldActivateOnFocus} />
35
- </div>
36
- <TabProvider
37
- activeTabIndex={activeTab}
38
- onChange={setActiveTab}
39
- isLazy={isLazy}
40
- shouldActivateOnFocus={shouldActivateOnFocus}
41
- >
42
- <TabList theme={theme} aria-label="Tab list">
43
- <Tab label="Tab a" />
44
- <Tab label="Tab b" />
45
- <Tab label="Tab c" />
46
- </TabList>
86
+ <div>
87
+ Activate tab on focus:
88
+ <input type="checkbox" checked={shouldActivateOnFocus} onChange={changeShouldActivateOnFocus} />
89
+ </div>
90
+ <TabProvider
91
+ activeTabIndex={activeTab}
92
+ onChange={setActiveTab}
93
+ isLazy={isLazy}
94
+ shouldActivateOnFocus={shouldActivateOnFocus}
95
+ >
96
+ <TabList theme={theme} aria-label="Tab list">
97
+ <Tab label="Tab a" />
98
+ <Tab label="Tab b" />
99
+ <Tab label="Tab c" />
100
+ </TabList>
47
101
 
48
- <TabPanel className="lumx-spacing-padding-huge">Tab a content</TabPanel>
49
- <TabPanel className="lumx-spacing-padding-huge">Tab b content</TabPanel>
50
- <TabPanel className="lumx-spacing-padding-huge">Tab c content</TabPanel>
51
- </TabProvider>
52
- </>
53
- );
102
+ <TabPanel className="lumx-spacing-padding-huge">Tab a content</TabPanel>
103
+ <TabPanel className="lumx-spacing-padding-huge">Tab b content</TabPanel>
104
+ <TabPanel className="lumx-spacing-padding-huge">Tab c content</TabPanel>
105
+ </TabProvider>
106
+ </>
107
+ );
108
+ },
109
+ chromatic: { disable: true },
54
110
  };
55
111
 
56
112
  /* Display tabs far from their tab panels. */
57
- export const SplitTabListAndTabPanels = ({ theme }: any) => {
58
- const [isOpen, setOpen] = useState(true);
59
- const [activeTabIndex, onChange] = useState(1);
113
+ export const SplitTabListAndTabPanels = {
114
+ render({ theme }: any) {
115
+ const [isOpen, setOpen] = useState(true);
116
+ const [activeTabIndex, onChange] = useState(1);
60
117
 
61
- return (
62
- <TabProvider activeTabIndex={activeTabIndex} onChange={onChange} isLazy={false}>
63
- <Button
64
- onClick={() => {
65
- setOpen(!isOpen);
66
- onChange(1);
67
- }}
68
- >
69
- Open dialog with tabs in footer
70
- </Button>
71
- <Dialog isOpen={isOpen} forceFooterDivider onClose={() => setOpen(false)}>
72
- <TabPanel className="lumx-spacing-padding-huge">Tab 1 content</TabPanel>
73
- <TabPanel className="lumx-spacing-padding-huge">Tab 2 content</TabPanel>
74
- <TabPanel className="lumx-spacing-padding-huge">Tab 3 content</TabPanel>
118
+ return (
119
+ <TabProvider activeTabIndex={activeTabIndex} onChange={onChange} isLazy={false}>
120
+ <Button
121
+ onClick={() => {
122
+ setOpen(!isOpen);
123
+ onChange(1);
124
+ }}
125
+ >
126
+ Open dialog with tabs in footer
127
+ </Button>
128
+ <Dialog isOpen={isOpen} forceFooterDivider onClose={() => setOpen(false)}>
129
+ <TabPanel className="lumx-spacing-padding-huge">Tab 1 content</TabPanel>
130
+ <TabPanel className="lumx-spacing-padding-huge">Tab 2 content</TabPanel>
131
+ <TabPanel className="lumx-spacing-padding-huge">Tab 3 content</TabPanel>
75
132
 
76
- <footer>
77
- <TabList theme={theme} aria-label="Tab list">
78
- <Tab label="Tab 1" />
79
- <Tab label="Tab 2" />
80
- <Tab label="Tab 3" />
81
- </TabList>
82
- </footer>
83
- </Dialog>
84
- </TabProvider>
85
- );
133
+ <footer>
134
+ <TabList theme={theme} aria-label="Tab list">
135
+ <Tab label="Tab 1" />
136
+ <Tab label="Tab 2" />
137
+ <Tab label="Tab 3" />
138
+ </TabList>
139
+ </footer>
140
+ </Dialog>
141
+ </TabProvider>
142
+ );
143
+ },
144
+ chromatic: { disable: true },
86
145
  };
87
146
 
88
147
  /* Dynamically generate tabs. */
89
- export const DynamicTabs = ({ theme, tabCount }: any) => {
90
- return (
91
- <TabProvider>
92
- <TabList theme={theme} aria-label="Tab list">
148
+ export const DynamicTabs = {
149
+ render({ theme, tabCount }: any) {
150
+ return (
151
+ <TabProvider>
152
+ <TabList theme={theme} aria-label="Tab list">
153
+ {times(tabCount, (tabNumber) => (
154
+ <Tab key={tabNumber} label={`Tab ${tabNumber}`} />
155
+ ))}
156
+ </TabList>
157
+
93
158
  {times(tabCount, (tabNumber) => (
94
- <Tab key={tabNumber} label={`Tab ${tabNumber}`} />
159
+ <TabPanel key={tabNumber} className="lumx-spacing-padding-huge">
160
+ Tab {tabNumber} content
161
+ </TabPanel>
95
162
  ))}
96
- </TabList>
97
-
98
- {times(tabCount, (tabNumber) => (
99
- <TabPanel key={tabNumber} className="lumx-spacing-padding-huge">
100
- Tab {tabNumber} content
101
- </TabPanel>
102
- ))}
103
- </TabProvider>
104
- );
105
- };
106
- DynamicTabs.args = {
107
- tabCount: 3,
163
+ </TabProvider>
164
+ );
165
+ },
166
+ args: {
167
+ tabCount: 3,
168
+ },
169
+ chromatic: { disable: true },
108
170
  };
@@ -0,0 +1,17 @@
1
+ export type PathPart = string | number;
2
+ export type Path = PathPart | Array<PathPart>;
3
+
4
+ /**
5
+ * Concat flatten object path
6
+ *
7
+ * @example concatPath('foo', 'bar') // => 'foo.bar'
8
+ * @example concatPath(['foo', 0]) // => 'foo[0]'
9
+ * @example concatPath('foo', 0, ['bar']) // => 'foo[0].bar'
10
+ */
11
+ export const concatPath = (...prefix: Path[]) => {
12
+ const [first, ...rest] = prefix.flat();
13
+ return rest.reduce<string>((acc, part) => {
14
+ if (typeof part === 'number') return `${acc}[${part}]`;
15
+ return `${acc}.${part}`;
16
+ }, String(first));
17
+ };
@@ -0,0 +1,28 @@
1
+ import castArray from 'lodash/castArray';
2
+ import { concatPath } from './concatPath';
3
+
4
+ type Props = Record<string, any>;
5
+ type OneOrMoreProps = Props | Array<Props>;
6
+
7
+ /**
8
+ * Build a flat props object from the given nested props
9
+ *
10
+ * @example toFlattenProps({ foo: { bar: 4 } }) // => { 'foo.bar': 4 }
11
+ * @example toFlattenProps({ foo: [{ bar: 4 }, { bar: 5 }] }) // => { 'foo[0].bar': 4, 'foo[0].bar': 5 }
12
+ */
13
+ export function toFlattenProps(props: { [prefix: string]: OneOrMoreProps }): Props {
14
+ const out: Props = {};
15
+ for (const [prefix, oneOrMoreProps] of Object.entries(props)) {
16
+ const propsArray = castArray(oneOrMoreProps);
17
+
18
+ for (let i = 0; i < propsArray.length; i += 1) {
19
+ const path = Array.isArray(oneOrMoreProps) ? concatPath(prefix, i) : prefix;
20
+ const subProps = propsArray[i];
21
+
22
+ for (const [key, value] of Object.entries(subProps)) {
23
+ out[concatPath(path, key)] = value;
24
+ }
25
+ }
26
+ }
27
+ return out;
28
+ }
@@ -0,0 +1,12 @@
1
+ import setWith from 'lodash/setWith';
2
+
3
+ /**
4
+ * Add table.category to the provided argTypes
5
+ */
6
+ export function withCategory(category: string, argTypes: Record<string, any>) {
7
+ const out: typeof argTypes = {};
8
+ for (const [key, value] of Object.entries(argTypes)) {
9
+ out[key] = setWith({ ...value }, 'table.category', category);
10
+ }
11
+ return out;
12
+ }
package/utils/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import React, { RefObject } from 'react';
2
- import { F as Falsy } from '../_internal/types.js';
2
+ import { F as Falsy } from '../_internal/index.js';
3
3
 
4
4
  interface ClickAwayParameters {
5
5
  /**
package/utils/index.js CHANGED
@@ -1,2 +1,97 @@
1
- export { C as ClickAwayProvider } from '../_internal/ClickAwayProvider.js';
1
+ import React__default, { useEffect, useContext, useMemo, useRef, createContext } from 'react';
2
+ import isEmpty from 'lodash/isEmpty';
3
+
4
+ const EVENT_TYPES = ['mousedown', 'touchstart'];
5
+ function isClickAway(target, refs) {
6
+ // The target element is not contained in any of the listed element references.
7
+ return !refs.some(e => {
8
+ var _e$current;
9
+ return e === null || e === void 0 ? void 0 : (_e$current = e.current) === null || _e$current === void 0 ? void 0 : _e$current.contains(target);
10
+ });
11
+ }
12
+ /**
13
+ * Listen to clicks away from the given elements and callback the passed in function.
14
+ *
15
+ * Warning: If you need to detect click away on nested React portals, please use the `ClickAwayProvider` component.
16
+ */
17
+ function useClickAway(_ref) {
18
+ let {
19
+ callback,
20
+ childrenRefs
21
+ } = _ref;
22
+ useEffect(() => {
23
+ const {
24
+ current: currentRefs
25
+ } = childrenRefs;
26
+ if (!callback || !currentRefs || isEmpty(currentRefs)) {
27
+ return undefined;
28
+ }
29
+ const listener = evt => {
30
+ if (isClickAway(evt.target, currentRefs)) {
31
+ callback(evt);
32
+ }
33
+ };
34
+ EVENT_TYPES.forEach(evtType => document.addEventListener(evtType, listener));
35
+ return () => {
36
+ EVENT_TYPES.forEach(evtType => document.removeEventListener(evtType, listener));
37
+ };
38
+ }, [callback, childrenRefs]);
39
+ }
40
+
41
+ const ClickAwayAncestorContext = /*#__PURE__*/createContext(null);
42
+ /**
43
+ * Component combining the `useClickAway` hook with a React context to hook into the React component tree and make sure
44
+ * we take into account both the DOM tree and the React tree to detect click away.
45
+ *
46
+ * @return the react component.
47
+ */
48
+ const ClickAwayProvider = _ref => {
49
+ let {
50
+ children,
51
+ callback,
52
+ childrenRefs,
53
+ parentRef
54
+ } = _ref;
55
+ const parentContext = useContext(ClickAwayAncestorContext);
56
+ const currentContext = useMemo(() => {
57
+ const context = {
58
+ childrenRefs: [],
59
+ /**
60
+ * Add element refs to the current context and propagate to the parent context.
61
+ */
62
+ addRefs() {
63
+ // Add element refs that should be considered as inside the click away context.
64
+ context.childrenRefs.push(...arguments);
65
+ if (parentContext) {
66
+ // Also add then to the parent context
67
+ parentContext.addRefs(...arguments);
68
+ if (parentRef) {
69
+ // The parent element is also considered as inside the parent click away context but not inside the current context
70
+ parentContext.addRefs(parentRef);
71
+ }
72
+ }
73
+ }
74
+ };
75
+ return context;
76
+ }, [parentContext, parentRef]);
77
+ useEffect(() => {
78
+ const {
79
+ current: currentRefs
80
+ } = childrenRefs;
81
+ if (!currentRefs) {
82
+ return;
83
+ }
84
+ currentContext.addRefs(...currentRefs);
85
+ }, [currentContext, childrenRefs]);
86
+ useClickAway({
87
+ callback,
88
+ childrenRefs: useRef(currentContext.childrenRefs)
89
+ });
90
+ return /*#__PURE__*/React__default.createElement(ClickAwayAncestorContext.Provider, {
91
+ value: currentContext
92
+ }, children);
93
+ };
94
+ ClickAwayProvider.displayName = 'ClickAwayProvider';
95
+
96
+ export { ClickAwayProvider };
2
97
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
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\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","_ref","callback","childrenRefs","useEffect","currentRefs","isEmpty","undefined","listener","evt","forEach","evtType","document","addEventListener","removeEventListener","ClickAwayAncestorContext","createContext","ClickAwayProvider","children","parentRef","parentContext","useContext","currentContext","useMemo","context","addRefs","push","arguments","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,CAAAC,IAAA,EAAwD;EAAA,IAAvD;IAAEC,QAAQ;AAAEC,IAAAA,YAAAA;AAAkC,GAAC,GAAAF,IAAA,CAAA;AACxEG,EAAAA,SAAS,CAAC,MAAM;IACZ,MAAM;AAAEN,MAAAA,OAAO,EAAEO,WAAAA;AAAY,KAAC,GAAGF,YAAY,CAAA;IAC7C,IAAI,CAACD,QAAQ,IAAI,CAACG,WAAW,IAAIC,OAAO,CAACD,W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AC,CAAA;AACzC,UAAA,IAAIR,SAAS,EAAE;AACX;AACAC,YAAAA,aAAa,CAACK,OAAO,CAACN,SAAS,CAAC,CAAA;AACpC,WAAA;AACJ,SAAA;AACJ,OAAA;KACH,CAAA;AACD,IAAA,OAAOK,OAAO,CAAA;AAClB,GAAC,EAAE,CAACJ,aAAa,EAAED,SAAS,CAAC,CAAC,CAAA;AAE9Bf,EAAAA,SAAS,CAAC,MAAM;IACZ,MAAM;AAAEN,MAAAA,OAAO,EAAEO,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;AAElCH,EAAAA,YAAY,CAAC;IAAEE,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,97 +0,0 @@
1
- import React__default, { useEffect, useContext, useMemo, useRef, createContext } from 'react';
2
- import isEmpty from 'lodash/isEmpty';
3
-
4
- const EVENT_TYPES = ['mousedown', 'touchstart'];
5
- function isClickAway(target, refs) {
6
- // The target element is not contained in any of the listed element references.
7
- return !refs.some(e => {
8
- var _e$current;
9
- return e === null || e === void 0 ? void 0 : (_e$current = e.current) === null || _e$current === void 0 ? void 0 : _e$current.contains(target);
10
- });
11
- }
12
- /**
13
- * Listen to clicks away from the given elements and callback the passed in function.
14
- *
15
- * Warning: If you need to detect click away on nested React portals, please use the `ClickAwayProvider` component.
16
- */
17
- function useClickAway(_ref) {
18
- let {
19
- callback,
20
- childrenRefs
21
- } = _ref;
22
- useEffect(() => {
23
- const {
24
- current: currentRefs
25
- } = childrenRefs;
26
- if (!callback || !currentRefs || isEmpty(currentRefs)) {
27
- return undefined;
28
- }
29
- const listener = evt => {
30
- if (isClickAway(evt.target, currentRefs)) {
31
- callback(evt);
32
- }
33
- };
34
- EVENT_TYPES.forEach(evtType => document.addEventListener(evtType, listener));
35
- return () => {
36
- EVENT_TYPES.forEach(evtType => document.removeEventListener(evtType, listener));
37
- };
38
- }, [callback, childrenRefs]);
39
- }
40
-
41
- const ClickAwayAncestorContext = /*#__PURE__*/createContext(null);
42
- /**
43
- * Component combining the `useClickAway` hook with a React context to hook into the React component tree and make sure
44
- * we take into account both the DOM tree and the React tree to detect click away.
45
- *
46
- * @return the react component.
47
- */
48
- const ClickAwayProvider = _ref => {
49
- let {
50
- children,
51
- callback,
52
- childrenRefs,
53
- parentRef
54
- } = _ref;
55
- const parentContext = useContext(ClickAwayAncestorContext);
56
- const currentContext = useMemo(() => {
57
- const context = {
58
- childrenRefs: [],
59
- /**
60
- * Add element refs to the current context and propagate to the parent context.
61
- */
62
- addRefs() {
63
- // Add element refs that should be considered as inside the click away context.
64
- context.childrenRefs.push(...arguments);
65
- if (parentContext) {
66
- // Also add then to the parent context
67
- parentContext.addRefs(...arguments);
68
- if (parentRef) {
69
- // The parent element is also considered as inside the parent click away context but not inside the current context
70
- parentContext.addRefs(parentRef);
71
- }
72
- }
73
- }
74
- };
75
- return context;
76
- }, [parentContext, parentRef]);
77
- useEffect(() => {
78
- const {
79
- current: currentRefs
80
- } = childrenRefs;
81
- if (!currentRefs) {
82
- return;
83
- }
84
- currentContext.addRefs(...currentRefs);
85
- }, [currentContext, childrenRefs]);
86
- useClickAway({
87
- callback,
88
- childrenRefs: useRef(currentContext.childrenRefs)
89
- });
90
- return /*#__PURE__*/React__default.createElement(ClickAwayAncestorContext.Provider, {
91
- value: currentContext
92
- }, children);
93
- };
94
- ClickAwayProvider.displayName = 'ClickAwayProvider';
95
-
96
- export { ClickAwayProvider as C };
97
- //# sourceMappingURL=ClickAwayProvider.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ClickAwayProvider.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\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","_ref","callback","childrenRefs","useEffect","currentRefs","isEmpty","undefined","listener","evt","forEach","evtType","document","addEventListener","removeEventListener","ClickAwayAncestorContext","createContext","ClickAwayProvider","children","parentRef","parentContext","useContext","currentContext","useMemo","context","addRefs","push","arguments","useRef","React","createElement","Provider","value","displayName"],"mappings":";;;AAMA,MAAMA,WAAW,GAAG,CAAC,WAAW,EAAE,YAAY,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