dash-ui-kit 1.0.93 → 2.0.0-dev
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/README.md +52 -0
- package/dist/react/components/accordion/index.cjs.js +193 -0
- package/dist/react/components/accordion/index.cjs.js.map +1 -0
- package/dist/react/components/accordion/index.esm.js +169 -0
- package/dist/react/components/accordion/index.esm.js.map +1 -0
- package/dist/react/components/avatar/index.cjs.js +39 -0
- package/dist/react/components/avatar/index.cjs.js.map +1 -0
- package/dist/react/components/avatar/index.esm.js +34 -0
- package/dist/react/components/avatar/index.esm.js.map +1 -0
- package/dist/react/components/badge/index.cjs.js +92 -0
- package/dist/react/components/badge/index.cjs.js.map +1 -0
- package/dist/react/components/badge/index.esm.js +87 -0
- package/dist/react/components/badge/index.esm.js.map +1 -0
- package/dist/react/components/bigNumber/index.cjs.js +100 -0
- package/dist/react/components/bigNumber/index.cjs.js.map +1 -0
- package/dist/react/components/bigNumber/index.esm.js +95 -0
- package/dist/react/components/bigNumber/index.esm.js.map +1 -0
- package/dist/react/components/button/index.cjs.js +535 -0
- package/dist/react/components/button/index.cjs.js.map +1 -0
- package/dist/react/components/button/index.d.ts +3 -1
- package/dist/react/components/button/index.esm.js +530 -0
- package/dist/react/components/button/index.esm.js.map +1 -0
- package/dist/react/components/copyButton/index.cjs.js +95 -0
- package/dist/react/components/copyButton/index.cjs.js.map +1 -0
- package/dist/react/components/copyButton/index.esm.js +71 -0
- package/dist/react/components/copyButton/index.esm.js.map +1 -0
- package/dist/react/components/dashLogo/index.cjs.js +74 -0
- package/dist/react/components/dashLogo/index.cjs.js.map +1 -0
- package/dist/react/components/dashLogo/index.esm.js +69 -0
- package/dist/react/components/dashLogo/index.esm.js.map +1 -0
- package/dist/react/components/dateBlock/index.cjs.js +120 -0
- package/dist/react/components/dateBlock/index.cjs.js.map +1 -0
- package/dist/react/components/dateBlock/index.esm.js +115 -0
- package/dist/react/components/dateBlock/index.esm.js.map +1 -0
- package/dist/react/components/dialog/index.cjs.js +292 -0
- package/dist/react/components/dialog/index.cjs.js.map +1 -0
- package/dist/react/components/dialog/index.esm.js +270 -0
- package/dist/react/components/dialog/index.esm.js.map +1 -0
- package/dist/react/components/heading/index.cjs.js +60 -0
- package/dist/react/components/heading/index.cjs.js.map +1 -0
- package/dist/react/components/heading/index.esm.js +58 -0
- package/dist/react/components/heading/index.esm.js.map +1 -0
- package/dist/react/components/icons/index.cjs.js +1173 -0
- package/dist/react/components/icons/index.cjs.js.map +1 -0
- package/dist/react/components/icons/index.d.ts +2 -0
- package/dist/react/components/icons/index.esm.js +1128 -0
- package/dist/react/components/icons/index.esm.js.map +1 -0
- package/dist/react/components/identifier/index.cjs.js +286 -0
- package/dist/react/components/identifier/index.cjs.js.map +1 -0
- package/dist/react/components/identifier/index.esm.js +282 -0
- package/dist/react/components/identifier/index.esm.js.map +1 -0
- package/dist/react/components/index.cjs.js +101 -0
- package/dist/react/components/index.cjs.js.map +1 -0
- package/dist/react/components/index.d.ts +1 -1
- package/dist/react/components/index.esm.js +29 -0
- package/dist/react/components/index.esm.js.map +1 -0
- package/dist/react/components/input/index.cjs.js +237 -0
- package/dist/react/components/input/index.cjs.js.map +1 -0
- package/dist/react/components/input/index.esm.js +232 -0
- package/dist/react/components/input/index.esm.js.map +1 -0
- package/dist/react/components/list/index.cjs.js +49 -0
- package/dist/react/components/list/index.cjs.js.map +1 -0
- package/dist/react/components/list/index.esm.js +47 -0
- package/dist/react/components/list/index.esm.js.map +1 -0
- package/dist/react/components/notActive/index.cjs.js +40 -0
- package/dist/react/components/notActive/index.cjs.js.map +1 -0
- package/dist/react/components/notActive/index.esm.js +38 -0
- package/dist/react/components/notActive/index.esm.js.map +1 -0
- package/dist/react/components/overlayMenu/index.cjs.js +425 -0
- package/dist/react/components/overlayMenu/index.cjs.js.map +1 -0
- package/dist/react/components/overlayMenu/index.esm.js +420 -0
- package/dist/react/components/overlayMenu/index.esm.js.map +1 -0
- package/dist/react/components/overlaySelect/index.cjs.js +345 -0
- package/dist/react/components/overlaySelect/index.cjs.js.map +1 -0
- package/dist/react/components/overlaySelect/index.esm.js +340 -0
- package/dist/react/components/overlaySelect/index.esm.js.map +1 -0
- package/dist/react/components/progressStepBar/index.cjs.js +49 -0
- package/dist/react/components/progressStepBar/index.cjs.js.map +1 -0
- package/dist/react/components/progressStepBar/index.d.ts +2 -1
- package/dist/react/components/progressStepBar/index.esm.js +47 -0
- package/dist/react/components/progressStepBar/index.esm.js.map +1 -0
- package/dist/react/components/select/index.cjs.js +236 -0
- package/dist/react/components/select/index.cjs.js.map +1 -0
- package/dist/react/components/select/index.esm.js +212 -0
- package/dist/react/components/select/index.esm.js.map +1 -0
- package/dist/react/components/switch/index.cjs.js +184 -0
- package/dist/react/components/switch/index.cjs.js.map +1 -0
- package/dist/react/components/switch/index.esm.js +179 -0
- package/dist/react/components/switch/index.esm.js.map +1 -0
- package/dist/react/components/tabs/index.cjs.js +178 -0
- package/dist/react/components/tabs/index.cjs.js.map +1 -0
- package/dist/react/components/tabs/index.esm.js +154 -0
- package/dist/react/components/tabs/index.esm.js.map +1 -0
- package/dist/react/components/text/index.cjs.js +120 -0
- package/dist/react/components/text/index.cjs.js.map +1 -0
- package/dist/react/components/text/index.esm.js +115 -0
- package/dist/react/components/text/index.esm.js.map +1 -0
- package/dist/react/components/textarea/index.cjs.js +256 -0
- package/dist/react/components/textarea/index.cjs.js.map +1 -0
- package/dist/react/components/textarea/index.esm.js +251 -0
- package/dist/react/components/textarea/index.esm.js.map +1 -0
- package/dist/react/components/timeDelta/index.cjs.js +93 -0
- package/dist/react/components/timeDelta/index.cjs.js.map +1 -0
- package/dist/react/components/timeDelta/index.d.ts +1 -1
- package/dist/react/components/timeDelta/index.esm.js +88 -0
- package/dist/react/components/timeDelta/index.esm.js.map +1 -0
- package/dist/react/components/transactionStatusIcon/index.cjs.js +59 -0
- package/dist/react/components/transactionStatusIcon/index.cjs.js.map +1 -0
- package/dist/react/components/transactionStatusIcon/index.esm.js +54 -0
- package/dist/react/components/transactionStatusIcon/index.esm.js.map +1 -0
- package/dist/react/components/valueCard/index.cjs.js +176 -0
- package/dist/react/components/valueCard/index.cjs.js.map +1 -0
- package/dist/react/components/valueCard/index.d.ts +1 -1
- package/dist/react/components/valueCard/index.esm.js +171 -0
- package/dist/react/components/valueCard/index.esm.js.map +1 -0
- package/dist/react/contexts/ThemeContext.cjs.js +79 -0
- package/dist/react/contexts/ThemeContext.cjs.js.map +1 -0
- package/dist/react/contexts/ThemeContext.esm.js +76 -0
- package/dist/react/contexts/ThemeContext.esm.js.map +1 -0
- package/dist/react/contexts/index.cjs.js +11 -0
- package/dist/react/contexts/index.cjs.js.map +1 -0
- package/dist/react/contexts/index.esm.js +4 -0
- package/dist/react/contexts/index.esm.js.map +1 -0
- package/dist/react/hooks/useDebounce.cjs.js +83 -0
- package/dist/react/hooks/useDebounce.cjs.js.map +1 -0
- package/dist/react/hooks/useDebounce.esm.js +78 -0
- package/dist/react/hooks/useDebounce.esm.js.map +1 -0
- package/dist/react/index.cjs.js +99 -12811
- package/dist/react/index.cjs.js.map +1 -1
- package/dist/react/index.d.ts +1 -1
- package/dist/react/index.esm.js +27 -12725
- package/dist/react/index.esm.js.map +1 -1
- package/dist/react/shared/utils/datetime.cjs.js +59 -0
- package/dist/react/shared/utils/datetime.cjs.js.map +1 -0
- package/dist/react/shared/utils/datetime.esm.js +57 -0
- package/dist/react/shared/utils/datetime.esm.js.map +1 -0
- package/dist/react/utils/copyToClipboard.cjs.js +31 -0
- package/dist/react/utils/copyToClipboard.cjs.js.map +1 -0
- package/dist/react/utils/copyToClipboard.esm.js +26 -0
- package/dist/react/utils/copyToClipboard.esm.js.map +1 -0
- package/dist/react/utils/index.d.ts +1 -1
- package/dist/react-native/components/avatar/index.d.ts +26 -0
- package/dist/react-native/components/avatar/index.web.d.ts +24 -0
- package/dist/react-native/components/badge/index.d.ts +51 -0
- package/dist/react-native/components/bigNumber/index.d.ts +26 -0
- package/dist/react-native/components/button/index.d.ts +39 -0
- package/dist/react-native/components/copyButton/index.d.ts +22 -0
- package/dist/react-native/components/copyButton/index.web.d.ts +20 -0
- package/dist/react-native/components/dashLogo/index.d.ts +30 -0
- package/dist/react-native/components/heading/index.d.ts +25 -0
- package/dist/react-native/components/icons/index.d.ts +43 -0
- package/dist/react-native/components/identifier/index.d.ts +47 -0
- package/dist/react-native/components/index.d.ts +15 -0
- package/dist/react-native/components/input/index.d.ts +53 -0
- package/dist/react-native/components/notActive/index.d.ts +16 -0
- package/dist/react-native/components/tabs/index.d.ts +50 -0
- package/dist/react-native/components/text/index.d.ts +28 -0
- package/dist/react-native/components/transactionStatusIcon/index.d.ts +24 -0
- package/dist/react-native/components/valueCard/index.d.ts +43 -0
- package/dist/react-native/hooks/index.d.ts +1 -0
- package/dist/react-native/hooks/useDebounce.d.ts +43 -0
- package/dist/react-native/index.cjs.js +2856 -0
- package/dist/react-native/index.cjs.js.map +1 -0
- package/dist/react-native/index.d.ts +4 -0
- package/dist/react-native/index.esm.js +2808 -0
- package/dist/react-native/index.esm.js.map +1 -0
- package/dist/react-native/styles/index.d.ts +11 -0
- package/dist/react-native/styles/tokens.d.ts +308 -0
- package/dist/react-native/styles/utils.d.ts +65 -0
- package/dist/react-native/utils/clipboard.d.ts +27 -0
- package/dist/react-native/utils/index.d.ts +2 -0
- package/dist/react-native/utils/tw.d.ts +7 -0
- package/dist/shared/constants/colors.d.ts +25 -0
- package/dist/shared/constants/index.d.ts +2 -0
- package/dist/shared/constants/sizes.d.ts +49 -0
- package/dist/shared/index.cjs.js +171 -0
- package/dist/shared/index.cjs.js.map +1 -0
- package/dist/shared/index.d.ts +3 -0
- package/dist/shared/index.esm.js +161 -0
- package/dist/shared/index.esm.js.map +1 -0
- package/dist/shared/types/common.d.ts +33 -0
- package/dist/shared/types/index.d.ts +1 -0
- package/dist/shared/utils/index.d.ts +1 -0
- package/dist/styles.css +1 -1
- package/package.json +50 -8
- /package/dist/{react → shared}/utils/datetime.d.ts +0 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
6
|
+
var React = require('react');
|
|
7
|
+
|
|
8
|
+
const ThemeContext = /*#__PURE__*/React.createContext(undefined);
|
|
9
|
+
/**
|
|
10
|
+
* Provides theme context to its descendants and syncs theme with localStorage
|
|
11
|
+
* and the root HTML element's class list ('light' or 'dark').
|
|
12
|
+
*
|
|
13
|
+
* @param initialTheme - Optional initial theme override.
|
|
14
|
+
* @param children - React children.
|
|
15
|
+
*/
|
|
16
|
+
const ThemeProvider = ({
|
|
17
|
+
initialTheme,
|
|
18
|
+
children
|
|
19
|
+
}) => {
|
|
20
|
+
/**
|
|
21
|
+
* Retrieve stored theme from localStorage, if available.
|
|
22
|
+
*/
|
|
23
|
+
const getStoredTheme = () => {
|
|
24
|
+
if (typeof window === 'undefined') return null;
|
|
25
|
+
const stored = localStorage.getItem('theme');
|
|
26
|
+
return stored != null && (stored === 'light' || stored === 'dark') ? stored : null;
|
|
27
|
+
};
|
|
28
|
+
const [theme, setThemeState] = React.useState(() => {
|
|
29
|
+
var _a;
|
|
30
|
+
return (_a = initialTheme !== null && initialTheme !== void 0 ? initialTheme : getStoredTheme()) !== null && _a !== void 0 ? _a : 'light';
|
|
31
|
+
});
|
|
32
|
+
// Sync theme changes to document <html> class and localStorage.
|
|
33
|
+
React.useEffect(() => {
|
|
34
|
+
document.documentElement.classList.toggle('dark', theme === 'dark');
|
|
35
|
+
document.documentElement.classList.toggle('light', theme === 'light');
|
|
36
|
+
try {
|
|
37
|
+
localStorage.setItem('theme', theme);
|
|
38
|
+
} catch (_a) {
|
|
39
|
+
// Ignore localStorage.
|
|
40
|
+
}
|
|
41
|
+
}, [theme]);
|
|
42
|
+
/**
|
|
43
|
+
* Update theme state explicitly.
|
|
44
|
+
* @param newTheme - The theme to set ('light' or 'dark').
|
|
45
|
+
*/
|
|
46
|
+
const setTheme = newTheme => {
|
|
47
|
+
setThemeState(newTheme);
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Toggle between 'light' and 'dark' theme.
|
|
51
|
+
*/
|
|
52
|
+
const toggleTheme = () => {
|
|
53
|
+
setThemeState(current => current === 'light' ? 'dark' : 'light');
|
|
54
|
+
};
|
|
55
|
+
return jsxRuntime.jsx(ThemeContext.Provider, {
|
|
56
|
+
value: {
|
|
57
|
+
theme,
|
|
58
|
+
setTheme,
|
|
59
|
+
toggleTheme
|
|
60
|
+
},
|
|
61
|
+
children: children
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Hook to access the theme context.
|
|
66
|
+
* @returns ThemeContextValue - Contains `theme`, `setTheme`, and `toggleTheme`.
|
|
67
|
+
* @throws If used outside of a ThemeProvider.
|
|
68
|
+
*/
|
|
69
|
+
function useTheme() {
|
|
70
|
+
const context = React.useContext(ThemeContext);
|
|
71
|
+
if (context == null) {
|
|
72
|
+
throw new Error('useTheme must be used within a ThemeProvider');
|
|
73
|
+
}
|
|
74
|
+
return context;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
exports.ThemeProvider = ThemeProvider;
|
|
78
|
+
exports.useTheme = useTheme;
|
|
79
|
+
//# sourceMappingURL=ThemeContext.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ThemeContext.cjs.js","sources":["../../../src/react/contexts/ThemeContext.tsx"],"sourcesContent":["import React, { createContext, useContext, useState, useEffect } from 'react'\n\nexport type Theme = 'light' | 'dark'\n\n/**\n * Context value shape for theme switching.\n */\nexport interface ThemeContextValue {\n /** Current theme, either 'light' or 'dark'. */\n theme: Theme\n /** Set the theme explicitly. */\n setTheme: (theme: Theme) => void\n /** Toggle between 'light' and 'dark'. */\n toggleTheme: () => void\n}\n\nconst ThemeContext = createContext<ThemeContextValue | undefined>(undefined)\n\n/**\n * Props for the ThemeProvider component.\n */\nexport interface ThemeProviderProps {\n /** Initial theme; if not provided, reads from localStorage or defaults to 'light'. */\n initialTheme?: Theme\n /** React children that will have access to the theme context. */\n children: React.ReactNode\n}\n\n/**\n * Provides theme context to its descendants and syncs theme with localStorage\n * and the root HTML element's class list ('light' or 'dark').\n *\n * @param initialTheme - Optional initial theme override.\n * @param children - React children.\n */\nexport const ThemeProvider: React.FC<ThemeProviderProps> = ({ initialTheme, children }) => {\n /**\n * Retrieve stored theme from localStorage, if available.\n */\n const getStoredTheme = (): Theme | null => {\n if (typeof window === 'undefined') return null\n const stored = localStorage.getItem('theme')\n return (stored != null && (stored === 'light' || stored === 'dark')) ? stored : null\n }\n\n const [theme, setThemeState] = useState<Theme>(() => {\n return initialTheme ?? getStoredTheme() ?? 'light'\n })\n\n // Sync theme changes to document <html> class and localStorage.\n useEffect(() => {\n document.documentElement.classList.toggle('dark', theme === 'dark')\n document.documentElement.classList.toggle('light', theme === 'light')\n try {\n localStorage.setItem('theme', theme)\n } catch {\n // Ignore localStorage.\n }\n }, [theme])\n\n /**\n * Update theme state explicitly.\n * @param newTheme - The theme to set ('light' or 'dark').\n */\n const setTheme = (newTheme: Theme): void => {\n setThemeState(newTheme)\n }\n\n /**\n * Toggle between 'light' and 'dark' theme.\n */\n const toggleTheme = (): void => {\n setThemeState((current) => (current === 'light' ? 'dark' : 'light'))\n }\n\n return (\n <ThemeContext.Provider value={{ theme, setTheme, toggleTheme }}>\n {children}\n </ThemeContext.Provider>\n )\n}\n\n/**\n * Hook to access the theme context.\n * @returns ThemeContextValue - Contains `theme`, `setTheme`, and `toggleTheme`.\n * @throws If used outside of a ThemeProvider.\n */\nexport function useTheme (): ThemeContextValue {\n const context = useContext(ThemeContext)\n if (context == null) {\n throw new Error('useTheme must be used within a ThemeProvider')\n }\n return context\n}\n"],"names":["ThemeContext","createContext","undefined","ThemeProvider","initialTheme","children","getStoredTheme","window","stored","localStorage","getItem","theme","setThemeState","useState","_a","useEffect","document","documentElement","classList","toggle","setItem","setTheme","newTheme","toggleTheme","current","_jsx","Provider","value","useTheme","context","useContext","Error"],"mappings":";;;;;;;AAgBA,MAAMA,YAAY,gBAAGC,mBAAa,CAAgCC,SAAS,CAAC;AAY5E;;;;;;AAMG;AACI,MAAMC,aAAa,GAAiCA,CAAC;EAAEC,YAAY;AAAEC,EAAAA;AAAU,CAAA,KAAI;AACxF;;AAEG;EACH,MAAMC,cAAc,GAAGA,MAAmB;AACxC,IAAA,IAAI,OAAOC,MAAM,KAAK,WAAW,EAAE,OAAO,IAAI;AAC9C,IAAA,MAAMC,MAAM,GAAGC,YAAY,CAACC,OAAO,CAAC,OAAO,CAAC;AAC5C,IAAA,OAAQF,MAAM,IAAI,IAAI,KAAKA,MAAM,KAAK,OAAO,IAAIA,MAAM,KAAK,MAAM,CAAC,GAAIA,MAAM,GAAG,IAAI;GACrF;EAED,MAAM,CAACG,KAAK,EAAEC,aAAa,CAAC,GAAGC,cAAQ,CAAQ,MAAK;;IAClD,OAAO,CAAAC,EAAA,GAAAV,YAAY,KAAA,IAAA,IAAZA,YAAY,KAAA,MAAA,GAAZA,YAAY,GAAIE,cAAc,EAAE,MAAI,IAAA,IAAAQ,EAAA,KAAA,MAAA,GAAAA,EAAA,GAAA,OAAO;AACpD,GAAC,CAAC;AAEF;AACAC,EAAAA,eAAS,CAAC,MAAK;AACbC,IAAAA,QAAQ,CAACC,eAAe,CAACC,SAAS,CAACC,MAAM,CAAC,MAAM,EAAER,KAAK,KAAK,MAAM,CAAC;AACnEK,IAAAA,QAAQ,CAACC,eAAe,CAACC,SAAS,CAACC,MAAM,CAAC,OAAO,EAAER,KAAK,KAAK,OAAO,CAAC;IACrE,IAAI;AACFF,MAAAA,YAAY,CAACW,OAAO,CAAC,OAAO,EAAET,KAAK,CAAC;KACrC,CAAC,OAAAG,EAAA,EAAM;AACN;AAAA;AAEJ,GAAC,EAAE,CAACH,KAAK,CAAC,CAAC;AAEX;;;AAGG;EACH,MAAMU,QAAQ,GAAIC,QAAe,IAAU;IACzCV,aAAa,CAACU,QAAQ,CAAC;GACxB;AAED;;AAEG;EACH,MAAMC,WAAW,GAAGA,MAAW;IAC7BX,aAAa,CAAEY,OAAO,IAAMA,OAAO,KAAK,OAAO,GAAG,MAAM,GAAG,OAAQ,CAAC;GACrE;AAED,EAAA,OACEC,cAAC,CAAAzB,YAAY,CAAC0B,QAAQ,EAAA;AAACC,IAAAA,KAAK,EAAE;MAAEhB,KAAK;MAAEU,QAAQ;AAAEE,MAAAA;KAAa;cAC3DlB;AAAQ,GAAA,CACa;AAE5B;AAEA;;;;AAIG;SACauB,QAAQA,GAAA;AACtB,EAAA,MAAMC,OAAO,GAAGC,gBAAU,CAAC9B,YAAY,CAAC;EACxC,IAAI6B,OAAO,IAAI,IAAI,EAAE;AACnB,IAAA,MAAM,IAAIE,KAAK,CAAC,8CAA8C,CAAC;AACjE;AACA,EAAA,OAAOF,OAAO;AAChB;;;;;"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { jsx } from 'react/jsx-runtime';
|
|
4
|
+
import { useState, useEffect, createContext, useContext } from 'react';
|
|
5
|
+
|
|
6
|
+
const ThemeContext = /*#__PURE__*/createContext(undefined);
|
|
7
|
+
/**
|
|
8
|
+
* Provides theme context to its descendants and syncs theme with localStorage
|
|
9
|
+
* and the root HTML element's class list ('light' or 'dark').
|
|
10
|
+
*
|
|
11
|
+
* @param initialTheme - Optional initial theme override.
|
|
12
|
+
* @param children - React children.
|
|
13
|
+
*/
|
|
14
|
+
const ThemeProvider = ({
|
|
15
|
+
initialTheme,
|
|
16
|
+
children
|
|
17
|
+
}) => {
|
|
18
|
+
/**
|
|
19
|
+
* Retrieve stored theme from localStorage, if available.
|
|
20
|
+
*/
|
|
21
|
+
const getStoredTheme = () => {
|
|
22
|
+
if (typeof window === 'undefined') return null;
|
|
23
|
+
const stored = localStorage.getItem('theme');
|
|
24
|
+
return stored != null && (stored === 'light' || stored === 'dark') ? stored : null;
|
|
25
|
+
};
|
|
26
|
+
const [theme, setThemeState] = useState(() => {
|
|
27
|
+
var _a;
|
|
28
|
+
return (_a = initialTheme !== null && initialTheme !== void 0 ? initialTheme : getStoredTheme()) !== null && _a !== void 0 ? _a : 'light';
|
|
29
|
+
});
|
|
30
|
+
// Sync theme changes to document <html> class and localStorage.
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
document.documentElement.classList.toggle('dark', theme === 'dark');
|
|
33
|
+
document.documentElement.classList.toggle('light', theme === 'light');
|
|
34
|
+
try {
|
|
35
|
+
localStorage.setItem('theme', theme);
|
|
36
|
+
} catch (_a) {
|
|
37
|
+
// Ignore localStorage.
|
|
38
|
+
}
|
|
39
|
+
}, [theme]);
|
|
40
|
+
/**
|
|
41
|
+
* Update theme state explicitly.
|
|
42
|
+
* @param newTheme - The theme to set ('light' or 'dark').
|
|
43
|
+
*/
|
|
44
|
+
const setTheme = newTheme => {
|
|
45
|
+
setThemeState(newTheme);
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Toggle between 'light' and 'dark' theme.
|
|
49
|
+
*/
|
|
50
|
+
const toggleTheme = () => {
|
|
51
|
+
setThemeState(current => current === 'light' ? 'dark' : 'light');
|
|
52
|
+
};
|
|
53
|
+
return jsx(ThemeContext.Provider, {
|
|
54
|
+
value: {
|
|
55
|
+
theme,
|
|
56
|
+
setTheme,
|
|
57
|
+
toggleTheme
|
|
58
|
+
},
|
|
59
|
+
children: children
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Hook to access the theme context.
|
|
64
|
+
* @returns ThemeContextValue - Contains `theme`, `setTheme`, and `toggleTheme`.
|
|
65
|
+
* @throws If used outside of a ThemeProvider.
|
|
66
|
+
*/
|
|
67
|
+
function useTheme() {
|
|
68
|
+
const context = useContext(ThemeContext);
|
|
69
|
+
if (context == null) {
|
|
70
|
+
throw new Error('useTheme must be used within a ThemeProvider');
|
|
71
|
+
}
|
|
72
|
+
return context;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export { ThemeProvider, useTheme };
|
|
76
|
+
//# sourceMappingURL=ThemeContext.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ThemeContext.esm.js","sources":["../../../src/react/contexts/ThemeContext.tsx"],"sourcesContent":["import React, { createContext, useContext, useState, useEffect } from 'react'\n\nexport type Theme = 'light' | 'dark'\n\n/**\n * Context value shape for theme switching.\n */\nexport interface ThemeContextValue {\n /** Current theme, either 'light' or 'dark'. */\n theme: Theme\n /** Set the theme explicitly. */\n setTheme: (theme: Theme) => void\n /** Toggle between 'light' and 'dark'. */\n toggleTheme: () => void\n}\n\nconst ThemeContext = createContext<ThemeContextValue | undefined>(undefined)\n\n/**\n * Props for the ThemeProvider component.\n */\nexport interface ThemeProviderProps {\n /** Initial theme; if not provided, reads from localStorage or defaults to 'light'. */\n initialTheme?: Theme\n /** React children that will have access to the theme context. */\n children: React.ReactNode\n}\n\n/**\n * Provides theme context to its descendants and syncs theme with localStorage\n * and the root HTML element's class list ('light' or 'dark').\n *\n * @param initialTheme - Optional initial theme override.\n * @param children - React children.\n */\nexport const ThemeProvider: React.FC<ThemeProviderProps> = ({ initialTheme, children }) => {\n /**\n * Retrieve stored theme from localStorage, if available.\n */\n const getStoredTheme = (): Theme | null => {\n if (typeof window === 'undefined') return null\n const stored = localStorage.getItem('theme')\n return (stored != null && (stored === 'light' || stored === 'dark')) ? stored : null\n }\n\n const [theme, setThemeState] = useState<Theme>(() => {\n return initialTheme ?? getStoredTheme() ?? 'light'\n })\n\n // Sync theme changes to document <html> class and localStorage.\n useEffect(() => {\n document.documentElement.classList.toggle('dark', theme === 'dark')\n document.documentElement.classList.toggle('light', theme === 'light')\n try {\n localStorage.setItem('theme', theme)\n } catch {\n // Ignore localStorage.\n }\n }, [theme])\n\n /**\n * Update theme state explicitly.\n * @param newTheme - The theme to set ('light' or 'dark').\n */\n const setTheme = (newTheme: Theme): void => {\n setThemeState(newTheme)\n }\n\n /**\n * Toggle between 'light' and 'dark' theme.\n */\n const toggleTheme = (): void => {\n setThemeState((current) => (current === 'light' ? 'dark' : 'light'))\n }\n\n return (\n <ThemeContext.Provider value={{ theme, setTheme, toggleTheme }}>\n {children}\n </ThemeContext.Provider>\n )\n}\n\n/**\n * Hook to access the theme context.\n * @returns ThemeContextValue - Contains `theme`, `setTheme`, and `toggleTheme`.\n * @throws If used outside of a ThemeProvider.\n */\nexport function useTheme (): ThemeContextValue {\n const context = useContext(ThemeContext)\n if (context == null) {\n throw new Error('useTheme must be used within a ThemeProvider')\n }\n return context\n}\n"],"names":["ThemeContext","createContext","undefined","ThemeProvider","initialTheme","children","getStoredTheme","window","stored","localStorage","getItem","theme","setThemeState","useState","_a","useEffect","document","documentElement","classList","toggle","setItem","setTheme","newTheme","toggleTheme","current","_jsx","Provider","value","useTheme","context","useContext","Error"],"mappings":";;;;;AAgBA,MAAMA,YAAY,gBAAGC,aAAa,CAAgCC,SAAS,CAAC;AAY5E;;;;;;AAMG;AACI,MAAMC,aAAa,GAAiCA,CAAC;EAAEC,YAAY;AAAEC,EAAAA;AAAU,CAAA,KAAI;AACxF;;AAEG;EACH,MAAMC,cAAc,GAAGA,MAAmB;AACxC,IAAA,IAAI,OAAOC,MAAM,KAAK,WAAW,EAAE,OAAO,IAAI;AAC9C,IAAA,MAAMC,MAAM,GAAGC,YAAY,CAACC,OAAO,CAAC,OAAO,CAAC;AAC5C,IAAA,OAAQF,MAAM,IAAI,IAAI,KAAKA,MAAM,KAAK,OAAO,IAAIA,MAAM,KAAK,MAAM,CAAC,GAAIA,MAAM,GAAG,IAAI;GACrF;EAED,MAAM,CAACG,KAAK,EAAEC,aAAa,CAAC,GAAGC,QAAQ,CAAQ,MAAK;;IAClD,OAAO,CAAAC,EAAA,GAAAV,YAAY,KAAA,IAAA,IAAZA,YAAY,KAAA,MAAA,GAAZA,YAAY,GAAIE,cAAc,EAAE,MAAI,IAAA,IAAAQ,EAAA,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":[],"sourcesContent":[],"names":[],"mappings":";;"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
6
|
+
|
|
7
|
+
var React = require('react');
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Hook for debouncing values with extended functionality
|
|
11
|
+
*
|
|
12
|
+
* @param value - Value to debounce
|
|
13
|
+
* @param options - Configuration options
|
|
14
|
+
* @returns Object with debounced value and control methods
|
|
15
|
+
*/
|
|
16
|
+
const useDebounce = (value, options) => {
|
|
17
|
+
// Backward compatibility support - if number is passed, it's delay
|
|
18
|
+
const config = typeof options === 'number' ? {
|
|
19
|
+
delay: options
|
|
20
|
+
} : options;
|
|
21
|
+
const {
|
|
22
|
+
delay,
|
|
23
|
+
callback,
|
|
24
|
+
immediate = false
|
|
25
|
+
} = config;
|
|
26
|
+
const [debouncedValue, setDebouncedValue] = React.useState(value);
|
|
27
|
+
const [isPending, setIsPending] = React.useState(false);
|
|
28
|
+
const timeoutRef = React.useRef(null);
|
|
29
|
+
const isFirstRun = React.useRef(true);
|
|
30
|
+
const cancel = React.useCallback(() => {
|
|
31
|
+
if (timeoutRef.current) {
|
|
32
|
+
clearTimeout(timeoutRef.current);
|
|
33
|
+
timeoutRef.current = null;
|
|
34
|
+
setIsPending(false);
|
|
35
|
+
}
|
|
36
|
+
}, []);
|
|
37
|
+
const flush = React.useCallback(() => {
|
|
38
|
+
cancel();
|
|
39
|
+
setDebouncedValue(value);
|
|
40
|
+
callback === null || callback === void 0 ? void 0 : callback(value);
|
|
41
|
+
setIsPending(false);
|
|
42
|
+
}, [value, callback, cancel]);
|
|
43
|
+
React.useEffect(() => {
|
|
44
|
+
// If immediate === true and this is first run, set value immediately
|
|
45
|
+
if (immediate && isFirstRun.current) {
|
|
46
|
+
setDebouncedValue(value);
|
|
47
|
+
callback === null || callback === void 0 ? void 0 : callback(value);
|
|
48
|
+
isFirstRun.current = false;
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
isFirstRun.current = false;
|
|
52
|
+
setIsPending(true);
|
|
53
|
+
const handler = setTimeout(() => {
|
|
54
|
+
setDebouncedValue(value);
|
|
55
|
+
callback === null || callback === void 0 ? void 0 : callback(value);
|
|
56
|
+
setIsPending(false);
|
|
57
|
+
timeoutRef.current = null;
|
|
58
|
+
}, delay);
|
|
59
|
+
timeoutRef.current = handler;
|
|
60
|
+
return () => {
|
|
61
|
+
clearTimeout(handler);
|
|
62
|
+
setIsPending(false);
|
|
63
|
+
};
|
|
64
|
+
}, [value, delay, callback, immediate]);
|
|
65
|
+
// Cleanup on unmount
|
|
66
|
+
React.useEffect(() => {
|
|
67
|
+
return () => {
|
|
68
|
+
if (timeoutRef.current) {
|
|
69
|
+
clearTimeout(timeoutRef.current);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
}, []);
|
|
73
|
+
return {
|
|
74
|
+
debouncedValue,
|
|
75
|
+
flush,
|
|
76
|
+
cancel,
|
|
77
|
+
isPending
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
exports.default = useDebounce;
|
|
82
|
+
exports.useDebounce = useDebounce;
|
|
83
|
+
//# sourceMappingURL=useDebounce.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDebounce.cjs.js","sources":["../../../src/react/hooks/useDebounce.ts"],"sourcesContent":["import { useState, useEffect, useRef, useCallback } from 'react'\n\ninterface UseDebounceOptions<T> {\n /**\n * Delay in milliseconds\n */\n delay: number\n /**\n * Callback called when debounced value changes\n */\n callback?: (value: T) => void\n /**\n * If true, the value will be set immediately on first call\n */\n immediate?: boolean\n}\n\ninterface UseDebounceReturn<T> {\n /**\n * Current debounced value\n */\n debouncedValue: T\n /**\n * Function to force flush debounce and immediately update value\n */\n flush: () => void\n /**\n * Function to cancel current pending debounce\n */\n cancel: () => void\n /**\n * Whether there's a pending change\n */\n isPending: boolean\n}\n\n/**\n * Hook for debouncing values with extended functionality\n * \n * @param value - Value to debounce\n * @param options - Configuration options\n * @returns Object with debounced value and control methods\n */\nconst useDebounce = <T>(\n value: T, \n options: UseDebounceOptions<T> | number\n): UseDebounceReturn<T> => {\n // Backward compatibility support - if number is passed, it's delay\n const config: UseDebounceOptions<T> = typeof options === 'number' \n ? { delay: options }\n : options\n\n const { delay, callback, immediate = false } = config\n \n const [debouncedValue, setDebouncedValue] = useState<T>(value)\n const [isPending, setIsPending] = useState(false)\n const timeoutRef = useRef<NodeJS.Timeout | null>(null)\n const isFirstRun = useRef(true)\n\n const cancel = useCallback(() => {\n if (timeoutRef.current) {\n clearTimeout(timeoutRef.current)\n timeoutRef.current = null\n setIsPending(false)\n }\n }, [])\n\n const flush = useCallback(() => {\n cancel()\n setDebouncedValue(value)\n callback?.(value)\n setIsPending(false)\n }, [value, callback, cancel])\n\n useEffect(() => {\n // If immediate === true and this is first run, set value immediately\n if (immediate && isFirstRun.current) {\n setDebouncedValue(value)\n callback?.(value)\n isFirstRun.current = false\n return\n }\n\n isFirstRun.current = false\n setIsPending(true)\n\n const handler = setTimeout(() => {\n setDebouncedValue(value)\n callback?.(value)\n setIsPending(false)\n timeoutRef.current = null\n }, delay)\n\n timeoutRef.current = handler\n\n return () => {\n clearTimeout(handler)\n setIsPending(false)\n }\n }, [value, delay, callback, immediate])\n\n // Cleanup on unmount\n useEffect(() => {\n return () => {\n if (timeoutRef.current) {\n clearTimeout(timeoutRef.current)\n }\n }\n }, [])\n\n return {\n debouncedValue,\n flush,\n cancel,\n isPending\n }\n}\n\n// Export for backward compatibility\nexport default useDebounce\n\n// Named export for new API\nexport { useDebounce } "],"names":["useDebounce","value","options","config","delay","callback","immediate","debouncedValue","setDebouncedValue","useState","isPending","setIsPending","timeoutRef","useRef","isFirstRun","cancel","useCallback","current","clearTimeout","flush","useEffect","handler","setTimeout"],"mappings":";;;;;;;;AAoCA;;;;;;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QAAQ,aAARA,QAAQ,KAAA,MAAA,GAAA,MAAA,GAARA,QAAQ,CAAGJ,KAAK,CAAC;MACjBa,UAAU,CAACG,OAAO,GAAG,KAAK;AAC1B,MAAA;AACF;IAEAH,UAAU,CAACG,OAAO,GAAG,KAAK;IAC1BN,YAAY,CAAC,IAAI,CAAC;AAElB,IAAA,MAAMU,OAAO,GAAGC,UAAU,CAAC,MAAK;MAC9Bd,iBAAiB,CAACP,KAAK,CAAC;AACxBI,MAAAA,QAAQ,aAARA,QAAQ,KAAA,MAAA,GAAA,MAAA,GAARA,QAAQ,CAAGJ,KAAK,CAAC;MACjBU,YAAY,CAAC,KAAK,CAAC;MACnBC,UAAU,CAACK,OAAO,GAAG,IAAI;KAC1B,EAAEb,KAAK,CAAC;IAETQ,UAAU,CAACK,OAAO,GAAGI,OAAO;AAE5B,IAAA,OAAO,MAAK;MACVH,YAAY,CAACG,OAAO,CAAC;MACrBV,YAAY,CAAC,KAAK,CAAC;KACpB;GACF,EAAE,CAACV,KAAK,EAAEG,KAAK,EAAEC,QAAQ,EAAEC,SAAS,CAAC,CAAC;AAEvC;AACAc,EAAAA,eAAS,CAAC,MAAK;AACb,IAAA,OAAO,MAAK;MACV,IAAIR,UAAU,CAACK,OAAO,EAAE;AACtBC,QAAAA,YAAY,CAACN,UAAU,CAACK,OAAO,CAAC;AAClC;KACD;GACF,EAAE,EAAE,CAAC;EAEN,OAAO;IACLV,cAAc;IACdY,KAAK;IACLJ,MAAM;AACNL,IAAAA;GACD;AACH;;;;;"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useState, useRef, useCallback, useEffect } from 'react';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Hook for debouncing values with extended functionality
|
|
7
|
+
*
|
|
8
|
+
* @param value - Value to debounce
|
|
9
|
+
* @param options - Configuration options
|
|
10
|
+
* @returns Object with debounced value and control methods
|
|
11
|
+
*/
|
|
12
|
+
const useDebounce = (value, options) => {
|
|
13
|
+
// Backward compatibility support - if number is passed, it's delay
|
|
14
|
+
const config = typeof options === 'number' ? {
|
|
15
|
+
delay: options
|
|
16
|
+
} : options;
|
|
17
|
+
const {
|
|
18
|
+
delay,
|
|
19
|
+
callback,
|
|
20
|
+
immediate = false
|
|
21
|
+
} = config;
|
|
22
|
+
const [debouncedValue, setDebouncedValue] = useState(value);
|
|
23
|
+
const [isPending, setIsPending] = useState(false);
|
|
24
|
+
const timeoutRef = useRef(null);
|
|
25
|
+
const isFirstRun = useRef(true);
|
|
26
|
+
const cancel = useCallback(() => {
|
|
27
|
+
if (timeoutRef.current) {
|
|
28
|
+
clearTimeout(timeoutRef.current);
|
|
29
|
+
timeoutRef.current = null;
|
|
30
|
+
setIsPending(false);
|
|
31
|
+
}
|
|
32
|
+
}, []);
|
|
33
|
+
const flush = useCallback(() => {
|
|
34
|
+
cancel();
|
|
35
|
+
setDebouncedValue(value);
|
|
36
|
+
callback === null || callback === void 0 ? void 0 : callback(value);
|
|
37
|
+
setIsPending(false);
|
|
38
|
+
}, [value, callback, cancel]);
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
// If immediate === true and this is first run, set value immediately
|
|
41
|
+
if (immediate && isFirstRun.current) {
|
|
42
|
+
setDebouncedValue(value);
|
|
43
|
+
callback === null || callback === void 0 ? void 0 : callback(value);
|
|
44
|
+
isFirstRun.current = false;
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
isFirstRun.current = false;
|
|
48
|
+
setIsPending(true);
|
|
49
|
+
const handler = setTimeout(() => {
|
|
50
|
+
setDebouncedValue(value);
|
|
51
|
+
callback === null || callback === void 0 ? void 0 : callback(value);
|
|
52
|
+
setIsPending(false);
|
|
53
|
+
timeoutRef.current = null;
|
|
54
|
+
}, delay);
|
|
55
|
+
timeoutRef.current = handler;
|
|
56
|
+
return () => {
|
|
57
|
+
clearTimeout(handler);
|
|
58
|
+
setIsPending(false);
|
|
59
|
+
};
|
|
60
|
+
}, [value, delay, callback, immediate]);
|
|
61
|
+
// Cleanup on unmount
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
return () => {
|
|
64
|
+
if (timeoutRef.current) {
|
|
65
|
+
clearTimeout(timeoutRef.current);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
}, []);
|
|
69
|
+
return {
|
|
70
|
+
debouncedValue,
|
|
71
|
+
flush,
|
|
72
|
+
cancel,
|
|
73
|
+
isPending
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export { useDebounce as default, useDebounce };
|
|
78
|
+
//# sourceMappingURL=useDebounce.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDebounce.esm.js","sources":["../../../src/react/hooks/useDebounce.ts"],"sourcesContent":["import { useState, useEffect, useRef, useCallback } from 'react'\n\ninterface UseDebounceOptions<T> {\n /**\n * Delay in milliseconds\n */\n delay: number\n /**\n * Callback called when debounced value changes\n */\n callback?: (value: T) => void\n /**\n * If true, the value will be set immediately on first call\n */\n immediate?: boolean\n}\n\ninterface UseDebounceReturn<T> {\n /**\n * Current debounced value\n */\n debouncedValue: T\n /**\n * Function to force flush debounce and immediately update value\n */\n flush: () => void\n /**\n * Function to cancel current pending debounce\n */\n cancel: () => void\n /**\n * Whether there's a pending change\n */\n isPending: boolean\n}\n\n/**\n * Hook for debouncing values with extended functionality\n * \n * @param value - Value to debounce\n * @param options - Configuration options\n * @returns Object with debounced value and control methods\n */\nconst useDebounce = <T>(\n value: T, \n options: UseDebounceOptions<T> | number\n): UseDebounceReturn<T> => {\n // Backward compatibility support - if number is passed, it's delay\n const config: UseDebounceOptions<T> = typeof options === 'number' \n ? { delay: options }\n : options\n\n const { delay, callback, immediate = false } = config\n \n const [debouncedValue, setDebouncedValue] = useState<T>(value)\n const [isPending, setIsPending] = useState(false)\n const timeoutRef = useRef<NodeJS.Timeout | null>(null)\n const isFirstRun = useRef(true)\n\n const cancel = useCallback(() => {\n if (timeoutRef.current) {\n clearTimeout(timeoutRef.current)\n timeoutRef.current = null\n setIsPending(false)\n }\n }, [])\n\n const flush = useCallback(() => {\n cancel()\n setDebouncedValue(value)\n callback?.(value)\n setIsPending(false)\n }, [value, callback, cancel])\n\n useEffect(() => {\n // If immediate === true and this is first run, set value immediately\n if (immediate && isFirstRun.current) {\n setDebouncedValue(value)\n callback?.(value)\n isFirstRun.current = false\n return\n }\n\n isFirstRun.current = false\n setIsPending(true)\n\n const handler = setTimeout(() => {\n setDebouncedValue(value)\n callback?.(value)\n setIsPending(false)\n timeoutRef.current = null\n }, delay)\n\n timeoutRef.current = handler\n\n return () => {\n clearTimeout(handler)\n setIsPending(false)\n }\n }, [value, delay, callback, immediate])\n\n // Cleanup on unmount\n useEffect(() => {\n return () => {\n if (timeoutRef.current) {\n clearTimeout(timeoutRef.current)\n }\n }\n }, [])\n\n return {\n debouncedValue,\n flush,\n cancel,\n isPending\n }\n}\n\n// Export for backward compatibility\nexport default useDebounce\n\n// Named export for new API\nexport { useDebounce } "],"names":["useDebounce","value","options","config","delay","callback","immediate","debouncedValue","setDebouncedValue","useState","isPending","setIsPending","timeoutRef","useRef","isFirstRun","cancel","useCallback","current","clearTimeout","flush","useEffect","handler","setTimeout"],"mappings":";;;;AAoCA;;;;;;AAMG;AACH,MAAMA,WAAW,GAAGA,CAClBC,KAAQ,EACRC,OAAuC,KACf;AACxB;AACA,EAAA,MAAMC,MAAM,GAA0B,OAAOD,OAAO,KAAK,QAAQ,GAC7D;AAAEE,IAAAA,KAAK,EAAEF;AAAS,GAAA,GAClBA,OAAO;EAEX,MAAM;IAAEE,KAAK;IAAEC,QAAQ;AAAEC,IAAAA,SAAS,GAAG;AAAK,GAAE,GAAGH,MAAM;EAErD,MAAM,CAACI,cAAc,EAAEC,iBAAiB,CAAC,GAAGC,QAAQ,CAAIR,KAAK,CAAC;EAC9D,MAAM,CAACS,SAAS,EAAEC,YAAY,CAAC,GAAGF,QAAQ,CAAC,KAAK,CAAC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