@woobee/ui 0.2.1 → 0.2.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/dist/index.js CHANGED
@@ -342,7 +342,7 @@ function useModalContext() {
342
342
  }
343
343
  function ModalProvider({ children }) {
344
344
  const [currentState, setCurrentState] = React16.useState(0);
345
- function reducer(state, action) {
345
+ function reducer2(state, action) {
346
346
  const updatedValues = { ...state };
347
347
  if ("modalShowing" in action) {
348
348
  updatedValues.modalShowing = action.modalShowing;
@@ -355,7 +355,7 @@ function ModalProvider({ children }) {
355
355
  }
356
356
  return updatedValues;
357
357
  }
358
- const [values, dispatch] = React16.useReducer(reducer, {
358
+ const [values, dispatch] = React16.useReducer(reducer2, {
359
359
  modalShowing: false,
360
360
  drawerShowing: false
361
361
  });
@@ -1561,7 +1561,7 @@ function TagInput({
1561
1561
  }) {
1562
1562
  const [currentState, setCurrentState] = React16.useState(0);
1563
1563
  const tagInputRef = React16.useRef(null);
1564
- function reducer(state, action) {
1564
+ function reducer2(state, action) {
1565
1565
  const updatedValues = { ...state };
1566
1566
  if ("tags" in action) {
1567
1567
  updatedValues.tags = Array.from(new Set(action.tags));
@@ -1574,7 +1574,7 @@ function TagInput({
1574
1574
  }
1575
1575
  return updatedValues;
1576
1576
  }
1577
- const [values, dispatch] = React16.useReducer(reducer, {
1577
+ const [values, dispatch] = React16.useReducer(reducer2, {
1578
1578
  tags: []
1579
1579
  });
1580
1580
  const { tags } = values;
@@ -1666,6 +1666,60 @@ function NoData({ className }) {
1666
1666
  ] });
1667
1667
  }
1668
1668
  var ThemeContext = React16.createContext(void 0);
1669
+ var currentIsDark = false;
1670
+ var matchMediaListeners = /* @__PURE__ */ new Set();
1671
+ var originalMatchMedia = null;
1672
+ if (typeof window !== "undefined") {
1673
+ originalMatchMedia = window.matchMedia;
1674
+ try {
1675
+ const saved = localStorage.getItem("theme");
1676
+ const prefersDark = originalMatchMedia.call(window, "(prefers-color-scheme: dark)").matches;
1677
+ const isSystemOrNone = !saved || saved === "system";
1678
+ currentIsDark = saved === "dark" || isSystemOrNone && prefersDark;
1679
+ } catch (e) {
1680
+ }
1681
+ window.matchMedia = function(query) {
1682
+ const normalized = query.replace(/\s+/g, "").toLowerCase();
1683
+ const isDarkQuery = normalized.includes("prefers-color-scheme:dark");
1684
+ const isLightQuery = normalized.includes("prefers-color-scheme:light");
1685
+ if (isDarkQuery || isLightQuery) {
1686
+ const matches = isDarkQuery ? currentIsDark : !currentIsDark;
1687
+ return {
1688
+ matches,
1689
+ media: query,
1690
+ onchange: null,
1691
+ addListener(listener) {
1692
+ matchMediaListeners.add({ listener, query });
1693
+ },
1694
+ removeListener(listener) {
1695
+ for (const item of matchMediaListeners) {
1696
+ if (item.listener === listener) {
1697
+ matchMediaListeners.delete(item);
1698
+ }
1699
+ }
1700
+ },
1701
+ addEventListener(type, listener) {
1702
+ if (type === "change") {
1703
+ matchMediaListeners.add({ listener, query });
1704
+ }
1705
+ },
1706
+ removeEventListener(type, listener) {
1707
+ if (type === "change") {
1708
+ for (const item of matchMediaListeners) {
1709
+ if (item.listener === listener) {
1710
+ matchMediaListeners.delete(item);
1711
+ }
1712
+ }
1713
+ }
1714
+ },
1715
+ dispatchEvent(event) {
1716
+ return true;
1717
+ }
1718
+ };
1719
+ }
1720
+ return originalMatchMedia.call(window, query);
1721
+ };
1722
+ }
1669
1723
  function ThemeProvider({ children, defaultTheme = "system" }) {
1670
1724
  const [mode, setMode] = React16.useState(defaultTheme);
1671
1725
  const [isDark, setIsDark] = React16.useState(false);
@@ -1677,27 +1731,47 @@ function ThemeProvider({ children, defaultTheme = "system" }) {
1677
1731
  }, [defaultTheme]);
1678
1732
  React16.useEffect(() => {
1679
1733
  if (!mounted) return;
1680
- const media = window.matchMedia("(prefers-color-scheme: dark)");
1734
+ const media = originalMatchMedia ? originalMatchMedia.call(window, "(prefers-color-scheme: dark)") : null;
1681
1735
  const updateTheme = () => {
1682
- const prefersDark = media.matches;
1683
- const currentIsDark = mode === "dark" || mode === "system" && prefersDark;
1684
- if (currentIsDark) {
1736
+ const prefersDark = media ? media.matches : false;
1737
+ const currentIsDarkVal = mode === "dark" || mode === "system" && prefersDark;
1738
+ if (currentIsDarkVal) {
1685
1739
  document.documentElement.classList.add("dark");
1686
1740
  document.documentElement.classList.remove("light");
1687
1741
  } else {
1688
1742
  document.documentElement.classList.remove("dark");
1689
1743
  document.documentElement.classList.add("light");
1690
1744
  }
1691
- setIsDark(currentIsDark);
1745
+ currentIsDark = currentIsDarkVal;
1746
+ setIsDark(currentIsDarkVal);
1747
+ matchMediaListeners.forEach(({ listener, query }) => {
1748
+ try {
1749
+ const normalized = query.replace(/\s+/g, "").toLowerCase();
1750
+ const isDarkQuery = normalized.includes("prefers-color-scheme:dark");
1751
+ const matches = isDarkQuery ? currentIsDarkVal : !currentIsDarkVal;
1752
+ const event = { matches, media: query };
1753
+ if (typeof listener === "function") {
1754
+ listener(event);
1755
+ } else if (listener && typeof listener.handleEvent === "function") {
1756
+ listener.handleEvent(event);
1757
+ }
1758
+ } catch (e) {
1759
+ console.error(e);
1760
+ }
1761
+ });
1692
1762
  };
1693
1763
  const timeoutId = setTimeout(updateTheme, 0);
1694
1764
  const handleMediaChange = () => {
1695
1765
  updateTheme();
1696
1766
  };
1697
- media.addEventListener("change", handleMediaChange);
1767
+ if (media) {
1768
+ media.addEventListener("change", handleMediaChange);
1769
+ }
1698
1770
  return () => {
1699
1771
  clearTimeout(timeoutId);
1700
- media.removeEventListener("change", handleMediaChange);
1772
+ if (media) {
1773
+ media.removeEventListener("change", handleMediaChange);
1774
+ }
1701
1775
  };
1702
1776
  }, [mode, mounted]);
1703
1777
  function handleToggleTheme(triggerMode) {
@@ -1953,71 +2027,65 @@ var resetValues = {
1953
2027
  description: null,
1954
2028
  actions: null
1955
2029
  };
1956
- function PopoverProvider({ children }) {
1957
- const [currentState, setCurrentState] = React16.useState(0);
1958
- const [values, dispatch] = React16.useReducer(reducer, resetValues);
1959
- const { show, style, tooltipStyle, position, description, actions } = values;
1960
- function reducer(state, action) {
1961
- const updatedValues = { ...state };
1962
- if ("show" in action) {
1963
- updatedValues.show = action.show;
1964
- }
1965
- if ("style" in action) {
1966
- updatedValues.style = action.style;
1967
- }
1968
- if ("tooltipStyle" in action) {
1969
- updatedValues.tooltipStyle = action.tooltipStyle;
1970
- }
1971
- if ("position" in action) {
1972
- updatedValues.position = action.position;
1973
- }
1974
- if ("description" in action) {
1975
- updatedValues.description = action.description;
1976
- }
1977
- if ("actions" in action) {
1978
- updatedValues.actions = action.actions;
1979
- }
1980
- if ("render" in action && !!action.render) {
1981
- setCurrentState((prev) => 1 - prev);
2030
+ function reducer(state, action) {
2031
+ let changed = false;
2032
+ for (const k in action) {
2033
+ const key = k;
2034
+ if (action[key] !== state[key]) {
2035
+ changed = true;
2036
+ break;
1982
2037
  }
1983
- return updatedValues;
1984
2038
  }
1985
- function handleShowPopover(params) {
1986
- dispatch({
1987
- ...params,
1988
- render: true
1989
- });
2039
+ if (!changed) return state;
2040
+ const updatedValues = { ...state };
2041
+ if ("show" in action) {
2042
+ updatedValues.show = action.show;
1990
2043
  }
1991
- function handleClosePopover() {
1992
- dispatch({
1993
- ...resetValues,
1994
- render: true
1995
- });
2044
+ if ("style" in action) {
2045
+ updatedValues.style = action.style;
1996
2046
  }
1997
- return /* @__PURE__ */ jsxRuntime.jsxs(
1998
- PopoverContext.Provider,
1999
- {
2000
- value: {
2001
- showPopover: show,
2002
- setPopover: handleShowPopover
2003
- },
2004
- children: [
2005
- children,
2006
- !!show && /* @__PURE__ */ jsxRuntime.jsx(
2007
- PopoverCard,
2008
- {
2009
- show,
2010
- style,
2011
- tooltipStyle,
2012
- position,
2013
- description,
2014
- actions,
2015
- onClose: handleClosePopover
2016
- }
2017
- )
2018
- ]
2019
- }
2020
- );
2047
+ if ("tooltipStyle" in action) {
2048
+ updatedValues.tooltipStyle = action.tooltipStyle;
2049
+ }
2050
+ if ("position" in action) {
2051
+ updatedValues.position = action.position;
2052
+ }
2053
+ if ("description" in action) {
2054
+ updatedValues.description = action.description;
2055
+ }
2056
+ if ("actions" in action) {
2057
+ updatedValues.actions = action.actions;
2058
+ }
2059
+ return updatedValues;
2060
+ }
2061
+ function PopoverProvider({ children }) {
2062
+ const [values, dispatch] = React16.useReducer(reducer, resetValues);
2063
+ const { show, style, tooltipStyle, position, description, actions } = values;
2064
+ const handleShowPopover = React16.useCallback((params) => {
2065
+ dispatch(params);
2066
+ }, []);
2067
+ const handleClosePopover = React16.useCallback(() => {
2068
+ dispatch(resetValues);
2069
+ }, []);
2070
+ const contextValue = React16.useMemo(() => ({
2071
+ showPopover: show,
2072
+ setPopover: handleShowPopover
2073
+ }), [show, handleShowPopover]);
2074
+ return /* @__PURE__ */ jsxRuntime.jsxs(PopoverContext.Provider, { value: contextValue, children: [
2075
+ children,
2076
+ !!show && /* @__PURE__ */ jsxRuntime.jsx(
2077
+ PopoverCard,
2078
+ {
2079
+ show,
2080
+ style,
2081
+ tooltipStyle,
2082
+ position,
2083
+ description,
2084
+ actions,
2085
+ onClose: handleClosePopover
2086
+ }
2087
+ )
2088
+ ] });
2021
2089
  }
2022
2090
  function usePopoverContext() {
2023
2091
  const context = React16.useContext(PopoverContext);
@@ -2032,8 +2100,15 @@ function Popover({ className, description, actions, children, opts }) {
2032
2100
  const { screenWidth, screenHeight } = useResizeListener();
2033
2101
  const [menuData, setMenuData] = React16.useState({ showMenu: false, x: 0, y: 0, left: 0, right: 0, top: 0, bottom: 0 });
2034
2102
  const parentRef = React16.useRef(null);
2103
+ const prevShowMenuRef = React16.useRef(false);
2104
+ const prevShowPopoverRef = React16.useRef(false);
2035
2105
  (actions || []).some((action) => (action.items || []).length > 0);
2036
2106
  React16.useEffect(() => {
2107
+ const wasOpen = prevShowMenuRef.current;
2108
+ prevShowMenuRef.current = menuData.showMenu;
2109
+ if (!menuData.showMenu && !wasOpen) {
2110
+ return;
2111
+ }
2037
2112
  const stickyTop = menuData.showMenu && screenHeight - (menuData.y + 14) < 300;
2038
2113
  setPopover({
2039
2114
  show: menuData.showMenu,
@@ -2057,13 +2132,14 @@ function Popover({ className, description, actions, children, opts }) {
2057
2132
  });
2058
2133
  }, [menuData, screenWidth, screenHeight, description, actions, setPopover]);
2059
2134
  React16.useEffect(() => {
2060
- if (!showPopover && !!menuData.showMenu) {
2135
+ if (prevShowPopoverRef.current && !showPopover) {
2061
2136
  setMenuData((prev) => ({
2062
2137
  ...prev,
2063
2138
  showMenu: false
2064
2139
  }));
2065
2140
  }
2066
- }, [showPopover, menuData.showMenu]);
2141
+ prevShowPopoverRef.current = showPopover;
2142
+ }, [showPopover]);
2067
2143
  function handleToggleMenu(e) {
2068
2144
  e.stopPropagation();
2069
2145
  if (menuData.showMenu) {
@@ -2186,7 +2262,7 @@ function Dragger({
2186
2262
  onChange
2187
2263
  }) {
2188
2264
  const [, setCurrentState] = React16.useState(0);
2189
- const [values, dispatch] = React16.useReducer(reducer, {
2265
+ const [values, dispatch] = React16.useReducer(reducer2, {
2190
2266
  items: null,
2191
2267
  displayItems: null,
2192
2268
  dragging: false,
@@ -2225,7 +2301,7 @@ function Dragger({
2225
2301
  }
2226
2302
  }
2227
2303
  }, [grandChildren, updated]);
2228
- function reducer(state, action) {
2304
+ function reducer2(state, action) {
2229
2305
  const updatedValues = { ...state };
2230
2306
  if ("items" in action) {
2231
2307
  updatedValues.items = action.items;