@lumx/react 3.9.7-alpha.0 → 3.9.7-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/_internal/{types.d.ts → index.d.ts} +1 -44
- package/index.d.ts +46 -3
- package/index.js +174 -204
- package/index.js.map +1 -1
- package/package.json +11 -12
- package/utils/index.d.ts +1 -1
- package/utils/index.js +96 -1
- package/utils/index.js.map +1 -1
- package/_internal/ClickAwayProvider.js +0 -97
- package/_internal/ClickAwayProvider.js.map +0 -1
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.
|
|
10
|
-
"@lumx/icons": "^3.9.7-alpha.
|
|
9
|
+
"@lumx/core": "^3.9.7-alpha.2",
|
|
10
|
+
"@lumx/icons": "^3.9.7-alpha.2",
|
|
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-
|
|
30
|
-
"@rollup/plugin-
|
|
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": "
|
|
62
|
-
"rollup-plugin-analyzer": "^
|
|
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.
|
|
66
|
-
"rollup-plugin-dts": "^
|
|
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.
|
|
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.
|
|
113
|
+
"version": "3.9.7-alpha.2"
|
|
115
114
|
}
|
package/utils/index.d.ts
CHANGED
package/utils/index.js
CHANGED
|
@@ -1,2 +1,97 @@
|
|
|
1
|
-
|
|
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
|
package/utils/index.js.map
CHANGED
|
@@ -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,CA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BAAiB,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,CAA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