@raystack/apsara 0.10.5 → 0.10.6

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/dist/index.cjs CHANGED
@@ -15385,7 +15385,7 @@ const Table = Object.assign(TableRoot, {
15385
15385
  Caption: TableCaption,
15386
15386
  });
15387
15387
 
15388
- function DataTableRoot({ columns, data, emptyState, children, ...props }) {
15388
+ function DataTableRoot({ columns, data, emptyState, children, parentStyle, ...props }) {
15389
15389
  const convertedChildren = React.Children.toArray(children);
15390
15390
  const header = convertedChildren.find((child) => child.type === DataTableToolbar) || jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, {});
15391
15391
  const footer = convertedChildren.find((child) => child.type === DataTableFooter) || jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, {});
@@ -15429,7 +15429,7 @@ function DataTableRoot({ columns, data, emptyState, children, ...props }) {
15429
15429
  resetColumns,
15430
15430
  onGlobalFilterChange: setGlobalFilter,
15431
15431
  onChange: () => ({}),
15432
- }, children: [jsxRuntimeExports.jsxs(Flex, { direction: "column", className: styles$4.datatable, children: [header, jsxRuntimeExports.jsxs(Flex, { children: [jsxRuntimeExports.jsxs(Table, { ...props, children: [jsxRuntimeExports.jsx(Table.Header, { children: table.getHeaderGroups().map((headerGroup) => (jsxRuntimeExports.jsx(Table.Row, { children: headerGroup.headers.map((header) => {
15432
+ }, children: [jsxRuntimeExports.jsxs(Flex, { direction: "column", className: styles$4.datatable, children: [header, jsxRuntimeExports.jsxs(Flex, { style: parentStyle, children: [jsxRuntimeExports.jsxs(Table, { ...props, children: [jsxRuntimeExports.jsx(Table.Header, { children: table.getHeaderGroups().map((headerGroup) => (jsxRuntimeExports.jsx(Table.Row, { children: headerGroup.headers.map((header) => {
15433
15433
  return (jsxRuntimeExports.jsx(Table.Head, { children: jsxRuntimeExports.jsxs(Text, { className: styles$4.head, children: [header.isPlaceholder
15434
15434
  ? null
15435
15435
  : flexRender(header.column.columnDef.header, header.getContext()), {
@@ -15622,136 +15622,228 @@ const Tabs = Object.assign(TabsRoot, {
15622
15622
  List: TabsList,
15623
15623
  });
15624
15624
 
15625
- const isBrowser = () => typeof window !== "undefined";
15626
- const getMediaTheme = () => {
15627
- if (!isBrowser())
15628
- return null;
15629
- // If they haven't been explicitly set, let's check the media query
15630
- const mqlDark = window.matchMedia("(prefers-color-scheme: dark)");
15631
- const hasMediaQueryPreference = typeof mqlDark.matches === "boolean";
15632
- if (hasMediaQueryPreference)
15633
- return mqlDark.matches ? "dark" : "light";
15634
- return null;
15635
- };
15636
- const listenForOSPreferenceChanges = (preferenceChanged) => {
15637
- if (!isBrowser())
15638
- return null;
15639
- // Listen out for if a user changes operating system mode,
15640
- // but don't save the change in local storage.
15641
- // The only two options here are dark or light.
15642
- window
15643
- .matchMedia("(prefers-color-scheme: dark)")
15644
- .addEventListener("change", (e) => {
15645
- preferenceChanged(e.matches ? "dark" : "light");
15646
- });
15647
- window
15648
- .matchMedia("(prefers-color-scheme: light)")
15649
- .addEventListener("change", (e) => {
15650
- preferenceChanged(e.matches ? "light" : "dark");
15651
- });
15652
- };
15653
-
15654
- const themeLocalStorageKey = "theme";
15655
- function isValidThemePreference(theme) {
15656
- return theme == "auto" || theme == "dark" || theme == "light";
15657
- }
15658
- const saveThemePreference = (newTheme) => {
15659
- try {
15660
- if (typeof newTheme === "string")
15661
- window.localStorage.setItem(themeLocalStorageKey, newTheme);
15662
- }
15663
- catch (e) {
15664
- console.warn(e);
15665
- }
15666
- };
15667
- const getSavedThemePreference = () => {
15668
- try {
15669
- const savedMode = window.localStorage.getItem(themeLocalStorageKey);
15670
- // If the user has explicitly chosen a colour mode,
15671
- // let's use it. Otherwise, this value will be null.
15672
- return isValidThemePreference(savedMode) ? savedMode : null;
15673
- }
15674
- catch (e) {
15675
- // When Chrome in incognito, localStorage cannot be accessed
15676
- console.warn(e);
15677
- return null;
15678
- }
15679
- };
15680
-
15681
- const { createContext, useState, useEffect } = React;
15682
- const defaultThemeName = "dark";
15683
- const initialValues = {
15684
- themeName: defaultThemeName,
15685
- themePreference: defaultThemeName,
15686
- setTheme: () => { },
15687
- };
15688
- const ApsaraThemeContext = createContext(initialValues);
15689
- ApsaraThemeContext.displayName = "ApsaraThemeContext ";
15690
- const useTheme = () => {
15691
- const [themePreference, setThemePreference] = useState(defaultThemeName);
15692
- const [themeName, setThemeName] = useState(defaultThemeName);
15693
- const [osTheme, setOsTheme] = useState(getMediaTheme());
15694
- // in the future this should prefer auto if no saved
15695
- useEffect(() => {
15696
- const initialTheme = getSavedThemePreference();
15697
- if (isValidThemePreference(initialTheme)) {
15698
- setThemePreference(initialTheme);
15625
+ const colorSchemes = ["light", "dark"];
15626
+ const MEDIA = "(prefers-color-scheme: dark)";
15627
+ const isServer = typeof window === "undefined";
15628
+ const ThemeContext = React.createContext(undefined);
15629
+ const defaultContext = { setTheme: (_) => { }, themes: [] };
15630
+ const useTheme = () => React.useContext(ThemeContext) ?? defaultContext;
15631
+ const ThemeProvider = (props) => {
15632
+ const context = React.useContext(ThemeContext);
15633
+ // Ignore nested context providers, just passthrough children
15634
+ if (context)
15635
+ return jsxRuntimeExports.jsx(React.Fragment, { children: props.children });
15636
+ return jsxRuntimeExports.jsx(Theme$1, { ...props });
15637
+ };
15638
+ const defaultThemes = ["light", "dark"];
15639
+ const Theme$1 = ({ forcedTheme, disableTransitionOnChange = false, enableSystem = true, enableColorScheme = true, storageKey = "theme", themes = defaultThemes, defaultTheme = enableSystem ? "system" : "light", attribute = "data-theme", value, children, nonce, }) => {
15640
+ const [theme, setThemeState] = React.useState(() => getTheme(storageKey, defaultTheme));
15641
+ const [resolvedTheme, setResolvedTheme] = React.useState(() => getTheme(storageKey));
15642
+ const attrs = !value ? themes : Object.values(value);
15643
+ const applyTheme = React.useCallback((theme) => {
15644
+ let resolved = theme;
15645
+ if (!resolved)
15646
+ return;
15647
+ // If theme is system, resolve it before setting theme
15648
+ if (theme === "system" && enableSystem) {
15649
+ resolved = getSystemTheme();
15650
+ }
15651
+ const name = value ? value[resolved] : resolved;
15652
+ const enable = disableTransitionOnChange ? disableAnimation() : null;
15653
+ const d = document.documentElement;
15654
+ if (attribute === "class") {
15655
+ d.classList.remove(...attrs);
15656
+ if (name)
15657
+ d.classList.add(name);
15699
15658
  }
15700
15659
  else {
15701
- setThemePreference(defaultThemeName);
15660
+ if (name) {
15661
+ d.setAttribute(attribute, name);
15662
+ }
15663
+ else {
15664
+ d.removeAttribute(attribute);
15665
+ }
15666
+ }
15667
+ if (enableColorScheme) {
15668
+ const fallback = colorSchemes.includes(defaultTheme)
15669
+ ? defaultTheme
15670
+ : null;
15671
+ const colorScheme = colorSchemes.includes(resolved) ? resolved : fallback;
15672
+ // @ts-ignore
15673
+ d.style.colorScheme = colorScheme;
15702
15674
  }
15675
+ enable?.();
15703
15676
  }, []);
15704
- useEffect(() => {
15705
- if (themePreference == "auto") {
15706
- setThemeName(osTheme ?? defaultThemeName);
15677
+ const setTheme = React.useCallback((theme) => {
15678
+ setThemeState(theme);
15679
+ // Save to storage
15680
+ try {
15681
+ localStorage.setItem(storageKey, theme);
15682
+ }
15683
+ catch (e) {
15684
+ // Unsupported
15685
+ }
15686
+ }, [forcedTheme]);
15687
+ const handleMediaQuery = React.useCallback((e) => {
15688
+ const resolved = getSystemTheme(e);
15689
+ setResolvedTheme(resolved);
15690
+ if (theme === "system" && enableSystem && !forcedTheme) {
15691
+ applyTheme("system");
15692
+ }
15693
+ }, [theme, forcedTheme]);
15694
+ // Always listen to System preference
15695
+ React.useEffect(() => {
15696
+ const media = window.matchMedia(MEDIA);
15697
+ // Intentionally use deprecated listener methods to support iOS & old browsers
15698
+ media.addListener(handleMediaQuery);
15699
+ handleMediaQuery(media);
15700
+ return () => media.removeListener(handleMediaQuery);
15701
+ }, [handleMediaQuery]);
15702
+ // localStorage event handling
15703
+ React.useEffect(() => {
15704
+ const handleStorage = (e) => {
15705
+ if (e.key !== storageKey) {
15706
+ return;
15707
+ }
15708
+ // If default theme set, use it if localstorage === null (happens on local storage manual deletion)
15709
+ const theme = e.newValue || defaultTheme;
15710
+ setTheme(theme);
15711
+ };
15712
+ window.addEventListener("storage", handleStorage);
15713
+ return () => window.removeEventListener("storage", handleStorage);
15714
+ }, [setTheme]);
15715
+ // Whenever theme or forcedTheme changes, apply it
15716
+ React.useEffect(() => {
15717
+ // @ts-ignore
15718
+ applyTheme(forcedTheme ?? theme);
15719
+ }, [forcedTheme, theme]);
15720
+ const providerValue = React.useMemo(() => ({
15721
+ theme,
15722
+ setTheme,
15723
+ forcedTheme,
15724
+ resolvedTheme: theme === "system" ? resolvedTheme : theme,
15725
+ themes: enableSystem ? [...themes, "system"] : themes,
15726
+ systemTheme: (enableSystem ? resolvedTheme : undefined),
15727
+ }), [theme, setTheme, forcedTheme, resolvedTheme, enableSystem, themes]);
15728
+ return (jsxRuntimeExports.jsxs(ThemeContext.Provider, { value: providerValue, children: [jsxRuntimeExports.jsx(ThemeScript, { ...{
15729
+ forcedTheme,
15730
+ disableTransitionOnChange,
15731
+ enableSystem,
15732
+ enableColorScheme,
15733
+ storageKey,
15734
+ themes,
15735
+ defaultTheme,
15736
+ attribute,
15737
+ value,
15738
+ children,
15739
+ attrs,
15740
+ nonce,
15741
+ } }), children] }));
15742
+ };
15743
+ const ThemeScript = React.memo(({ forcedTheme, storageKey, attribute, enableSystem, enableColorScheme, defaultTheme, value, attrs, nonce, }) => {
15744
+ const defaultSystem = defaultTheme === "system";
15745
+ // Code-golfing the amount of characters in the script
15746
+ const optimization = (() => {
15747
+ if (attribute === "class") {
15748
+ const removeClasses = `c.remove(${attrs
15749
+ .map((t) => `'${t}'`)
15750
+ .join(",")})`;
15751
+ return `var d=document.documentElement,c=d.classList;${removeClasses};`;
15707
15752
  }
15708
15753
  else {
15709
- setThemeName(themePreference);
15710
- }
15711
- }, [themePreference]);
15712
- useEffect(() => {
15713
- // if os theme changes and we are in auto mode, change up
15714
- if (themePreference == "auto") {
15715
- setThemeName(osTheme ?? defaultThemeName);
15716
- }
15717
- }, [osTheme]);
15718
- useEffect(() => {
15719
- const html = document.documentElement;
15720
- for (const k of ["dark", "light"]) {
15721
- html.classList.remove(k);
15722
- }
15723
- html.classList.add(themeName);
15724
- }, [themeName]);
15725
- listenForOSPreferenceChanges((osPref) => {
15726
- if (osPref === osTheme)
15727
- return;
15728
- setOsTheme(osPref);
15729
- });
15730
- return {
15731
- themeName,
15732
- themePreference: themePreference,
15733
- setTheme: (newTheme) => {
15734
- setThemePreference(newTheme);
15735
- saveThemePreference(newTheme);
15736
- },
15754
+ return `var d=document.documentElement,n='${attribute}',s='setAttribute';`;
15755
+ }
15756
+ })();
15757
+ const fallbackColorScheme = (() => {
15758
+ if (!enableColorScheme) {
15759
+ return "";
15760
+ }
15761
+ const fallback = colorSchemes.includes(defaultTheme)
15762
+ ? defaultTheme
15763
+ : null;
15764
+ if (fallback) {
15765
+ return `if(e==='light'||e==='dark'||!e)d.style.colorScheme=e||'${defaultTheme}'`;
15766
+ }
15767
+ else {
15768
+ return `if(e==='light'||e==='dark')d.style.colorScheme=e`;
15769
+ }
15770
+ })();
15771
+ const updateDOM = (name, literal = false, setColorScheme = true) => {
15772
+ const resolvedName = value ? value[name] : name;
15773
+ const val = literal ? name + `|| ''` : `'${resolvedName}'`;
15774
+ let text = "";
15775
+ // MUCH faster to set colorScheme alongside HTML attribute/class
15776
+ // as it only incurs 1 style recalculation rather than 2
15777
+ // This can save over 250ms of work for pages with big DOM
15778
+ if (enableColorScheme &&
15779
+ setColorScheme &&
15780
+ !literal &&
15781
+ colorSchemes.includes(name)) {
15782
+ text += `d.style.colorScheme = '${name}';`;
15783
+ }
15784
+ if (attribute === "class") {
15785
+ if (literal || resolvedName) {
15786
+ text += `c.add(${val})`;
15787
+ }
15788
+ else {
15789
+ text += `null`;
15790
+ }
15791
+ }
15792
+ else {
15793
+ if (resolvedName) {
15794
+ text += `d[s](n,${val})`;
15795
+ }
15796
+ }
15797
+ return text;
15737
15798
  };
15738
- };
15739
- const ApsaraThemeProvider = ({ children }) => {
15740
- const { themePreference, setTheme, themeName } = useTheme();
15741
- return (jsxRuntimeExports.jsx(ApsaraThemeContext.Provider, { value: {
15742
- themeName,
15743
- themePreference,
15744
- setTheme,
15745
- }, children: children }));
15746
- };
15747
- function useApsaraTheme() {
15748
- const context = React.useContext(ApsaraThemeContext);
15749
- if (!context) {
15750
- throw new Error("[Apsara UI 2.0]: useApsaraTheme must be used within a ApsaraThemeProvider");
15799
+ const scriptSrc = (() => {
15800
+ if (forcedTheme) {
15801
+ return `!function(){${optimization}${updateDOM(forcedTheme)}}()`;
15802
+ }
15803
+ if (enableSystem) {
15804
+ return `!function(){try{${optimization}var e=localStorage.getItem('${storageKey}');if('system'===e||(!e&&${defaultSystem})){var t='${MEDIA}',m=window.matchMedia(t);if(m.media!==t||m.matches){${updateDOM("dark")}}else{${updateDOM("light")}}}else if(e){${value ? `var x=${JSON.stringify(value)};` : ""}${updateDOM(value ? `x[e]` : "e", true)}}${!defaultSystem
15805
+ ? `else{` + updateDOM(defaultTheme, false, false) + "}"
15806
+ : ""}${fallbackColorScheme}}catch(e){}}()`;
15807
+ }
15808
+ return `!function(){try{${optimization}var e=localStorage.getItem('${storageKey}');if(e){${value ? `var x=${JSON.stringify(value)};` : ""}${updateDOM(value ? `x[e]` : "e", true)}}else{${updateDOM(defaultTheme, false, false)};}${fallbackColorScheme}}catch(t){}}();`;
15809
+ })();
15810
+ return (jsxRuntimeExports.jsx("script", { nonce: nonce, dangerouslySetInnerHTML: { __html: scriptSrc } }));
15811
+ },
15812
+ // Never re-render this component
15813
+ () => true);
15814
+ // Helpers
15815
+ const getTheme = (key, fallback) => {
15816
+ if (isServer)
15817
+ return undefined;
15818
+ let theme;
15819
+ try {
15820
+ theme = localStorage.getItem(key) || undefined;
15751
15821
  }
15752
- const { themePreference, setTheme, themeName } = context;
15753
- return { themePreference, setTheme, themeName };
15754
- }
15822
+ catch (e) {
15823
+ // Unsupported
15824
+ }
15825
+ return theme || fallback;
15826
+ };
15827
+ const disableAnimation = () => {
15828
+ const css = document.createElement("style");
15829
+ css.appendChild(document.createTextNode(`*{-webkit-transition:none!important;-moz-transition:none!important;-o-transition:none!important;-ms-transition:none!important;transition:none!important}`));
15830
+ document.head.appendChild(css);
15831
+ return () => {
15832
+ // Force restyle
15833
+ (() => window.getComputedStyle(document.body))();
15834
+ // Wait for next tick before removing
15835
+ setTimeout(() => {
15836
+ document.head.removeChild(css);
15837
+ }, 1);
15838
+ };
15839
+ };
15840
+ const getSystemTheme = (e) => {
15841
+ if (!e)
15842
+ e = window.matchMedia(MEDIA);
15843
+ const isDark = e.matches;
15844
+ const systemTheme = isDark ? "dark" : "light";
15845
+ return systemTheme;
15846
+ };
15755
15847
 
15756
15848
  var Theme;
15757
15849
  (function (Theme) {
@@ -15759,11 +15851,11 @@ var Theme;
15759
15851
  Theme["LIGHT"] = "light";
15760
15852
  })(Theme || (Theme = {}));
15761
15853
  function ThemeSwitcher({ size = 30, ...props }) {
15762
- const { themeName, setTheme } = useApsaraTheme();
15854
+ const { theme, setTheme } = useTheme();
15763
15855
  const onClickHandler = () => {
15764
- setTheme(themeName === Theme.DARK ? Theme.LIGHT : Theme.DARK);
15856
+ setTheme(theme === Theme.DARK ? Theme.LIGHT : Theme.DARK);
15765
15857
  };
15766
- return (jsxRuntimeExports.jsx(Box, { ...props, children: themeName === Theme.DARK ? (jsxRuntimeExports.jsx(SunIcon, { width: size, height: size, onClick: onClickHandler })) : (jsxRuntimeExports.jsx(MoonIcon, { width: size, height: size, onClick: onClickHandler })) }));
15858
+ return (jsxRuntimeExports.jsx(Box, { ...props, children: theme === Theme.DARK ? (jsxRuntimeExports.jsx(SunIcon, { width: size, height: size, onClick: onClickHandler })) : (jsxRuntimeExports.jsx(MoonIcon, { width: size, height: size, onClick: onClickHandler })) }));
15767
15859
  }
15768
15860
 
15769
15861
  var styles = {"title":"title-module_title__r78X4","title-small":"title-module_title-small__aS4B3","title-medium":"title-module_title-medium__e8yQc","title-large":"title-module_title-large__TVRRL"};
@@ -15784,7 +15876,6 @@ function Title({ children, className, size, ...props }) {
15784
15876
  return (jsxRuntimeExports.jsx("span", { className: title({ size, className }), ...props, children: children }));
15785
15877
  }
15786
15878
 
15787
- exports.ApsaraThemeProvider = ApsaraThemeProvider;
15788
15879
  exports.Avatar = Avatar;
15789
15880
  exports.Badge = badge$1;
15790
15881
  exports.Body = Body;
@@ -15817,8 +15908,9 @@ exports.Table = Table;
15817
15908
  exports.Tabs = Tabs;
15818
15909
  exports.Text = Text;
15819
15910
  exports.TextField = TextField;
15911
+ exports.ThemeProvider = ThemeProvider;
15820
15912
  exports.ThemeSwitcher = ThemeSwitcher;
15821
15913
  exports.Title = Title;
15822
- exports.useApsaraTheme = useApsaraTheme;
15823
15914
  exports.useTable = useTable;
15915
+ exports.useTheme = useTheme;
15824
15916
  //# sourceMappingURL=index.cjs.map