@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.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
2
- import React16, { forwardRef, useRef, useEffect, createContext, useState, useImperativeHandle, useContext, useReducer, useCallback } from 'react';
2
+ import React16, { forwardRef, useRef, useEffect, createContext, useState, useImperativeHandle, useContext, useReducer, useCallback, useMemo } from 'react';
3
3
  import { createPortal } from 'react-dom';
4
4
  import { CSSTransition } from 'react-transition-group';
5
5
 
@@ -336,7 +336,7 @@ function useModalContext() {
336
336
  }
337
337
  function ModalProvider({ children }) {
338
338
  const [currentState, setCurrentState] = useState(0);
339
- function reducer(state, action) {
339
+ function reducer2(state, action) {
340
340
  const updatedValues = { ...state };
341
341
  if ("modalShowing" in action) {
342
342
  updatedValues.modalShowing = action.modalShowing;
@@ -349,7 +349,7 @@ function ModalProvider({ children }) {
349
349
  }
350
350
  return updatedValues;
351
351
  }
352
- const [values, dispatch] = useReducer(reducer, {
352
+ const [values, dispatch] = useReducer(reducer2, {
353
353
  modalShowing: false,
354
354
  drawerShowing: false
355
355
  });
@@ -1555,7 +1555,7 @@ function TagInput({
1555
1555
  }) {
1556
1556
  const [currentState, setCurrentState] = useState(0);
1557
1557
  const tagInputRef = useRef(null);
1558
- function reducer(state, action) {
1558
+ function reducer2(state, action) {
1559
1559
  const updatedValues = { ...state };
1560
1560
  if ("tags" in action) {
1561
1561
  updatedValues.tags = Array.from(new Set(action.tags));
@@ -1568,7 +1568,7 @@ function TagInput({
1568
1568
  }
1569
1569
  return updatedValues;
1570
1570
  }
1571
- const [values, dispatch] = useReducer(reducer, {
1571
+ const [values, dispatch] = useReducer(reducer2, {
1572
1572
  tags: []
1573
1573
  });
1574
1574
  const { tags } = values;
@@ -1660,6 +1660,60 @@ function NoData({ className }) {
1660
1660
  ] });
1661
1661
  }
1662
1662
  var ThemeContext = createContext(void 0);
1663
+ var currentIsDark = false;
1664
+ var matchMediaListeners = /* @__PURE__ */ new Set();
1665
+ var originalMatchMedia = null;
1666
+ if (typeof window !== "undefined") {
1667
+ originalMatchMedia = window.matchMedia;
1668
+ try {
1669
+ const saved = localStorage.getItem("theme");
1670
+ const prefersDark = originalMatchMedia.call(window, "(prefers-color-scheme: dark)").matches;
1671
+ const isSystemOrNone = !saved || saved === "system";
1672
+ currentIsDark = saved === "dark" || isSystemOrNone && prefersDark;
1673
+ } catch (e) {
1674
+ }
1675
+ window.matchMedia = function(query) {
1676
+ const normalized = query.replace(/\s+/g, "").toLowerCase();
1677
+ const isDarkQuery = normalized.includes("prefers-color-scheme:dark");
1678
+ const isLightQuery = normalized.includes("prefers-color-scheme:light");
1679
+ if (isDarkQuery || isLightQuery) {
1680
+ const matches = isDarkQuery ? currentIsDark : !currentIsDark;
1681
+ return {
1682
+ matches,
1683
+ media: query,
1684
+ onchange: null,
1685
+ addListener(listener) {
1686
+ matchMediaListeners.add({ listener, query });
1687
+ },
1688
+ removeListener(listener) {
1689
+ for (const item of matchMediaListeners) {
1690
+ if (item.listener === listener) {
1691
+ matchMediaListeners.delete(item);
1692
+ }
1693
+ }
1694
+ },
1695
+ addEventListener(type, listener) {
1696
+ if (type === "change") {
1697
+ matchMediaListeners.add({ listener, query });
1698
+ }
1699
+ },
1700
+ removeEventListener(type, listener) {
1701
+ if (type === "change") {
1702
+ for (const item of matchMediaListeners) {
1703
+ if (item.listener === listener) {
1704
+ matchMediaListeners.delete(item);
1705
+ }
1706
+ }
1707
+ }
1708
+ },
1709
+ dispatchEvent(event) {
1710
+ return true;
1711
+ }
1712
+ };
1713
+ }
1714
+ return originalMatchMedia.call(window, query);
1715
+ };
1716
+ }
1663
1717
  function ThemeProvider({ children, defaultTheme = "system" }) {
1664
1718
  const [mode, setMode] = useState(defaultTheme);
1665
1719
  const [isDark, setIsDark] = useState(false);
@@ -1671,27 +1725,47 @@ function ThemeProvider({ children, defaultTheme = "system" }) {
1671
1725
  }, [defaultTheme]);
1672
1726
  useEffect(() => {
1673
1727
  if (!mounted) return;
1674
- const media = window.matchMedia("(prefers-color-scheme: dark)");
1728
+ const media = originalMatchMedia ? originalMatchMedia.call(window, "(prefers-color-scheme: dark)") : null;
1675
1729
  const updateTheme = () => {
1676
- const prefersDark = media.matches;
1677
- const currentIsDark = mode === "dark" || mode === "system" && prefersDark;
1678
- if (currentIsDark) {
1730
+ const prefersDark = media ? media.matches : false;
1731
+ const currentIsDarkVal = mode === "dark" || mode === "system" && prefersDark;
1732
+ if (currentIsDarkVal) {
1679
1733
  document.documentElement.classList.add("dark");
1680
1734
  document.documentElement.classList.remove("light");
1681
1735
  } else {
1682
1736
  document.documentElement.classList.remove("dark");
1683
1737
  document.documentElement.classList.add("light");
1684
1738
  }
1685
- setIsDark(currentIsDark);
1739
+ currentIsDark = currentIsDarkVal;
1740
+ setIsDark(currentIsDarkVal);
1741
+ matchMediaListeners.forEach(({ listener, query }) => {
1742
+ try {
1743
+ const normalized = query.replace(/\s+/g, "").toLowerCase();
1744
+ const isDarkQuery = normalized.includes("prefers-color-scheme:dark");
1745
+ const matches = isDarkQuery ? currentIsDarkVal : !currentIsDarkVal;
1746
+ const event = { matches, media: query };
1747
+ if (typeof listener === "function") {
1748
+ listener(event);
1749
+ } else if (listener && typeof listener.handleEvent === "function") {
1750
+ listener.handleEvent(event);
1751
+ }
1752
+ } catch (e) {
1753
+ console.error(e);
1754
+ }
1755
+ });
1686
1756
  };
1687
1757
  const timeoutId = setTimeout(updateTheme, 0);
1688
1758
  const handleMediaChange = () => {
1689
1759
  updateTheme();
1690
1760
  };
1691
- media.addEventListener("change", handleMediaChange);
1761
+ if (media) {
1762
+ media.addEventListener("change", handleMediaChange);
1763
+ }
1692
1764
  return () => {
1693
1765
  clearTimeout(timeoutId);
1694
- media.removeEventListener("change", handleMediaChange);
1766
+ if (media) {
1767
+ media.removeEventListener("change", handleMediaChange);
1768
+ }
1695
1769
  };
1696
1770
  }, [mode, mounted]);
1697
1771
  function handleToggleTheme(triggerMode) {
@@ -1947,71 +2021,65 @@ var resetValues = {
1947
2021
  description: null,
1948
2022
  actions: null
1949
2023
  };
1950
- function PopoverProvider({ children }) {
1951
- const [currentState, setCurrentState] = useState(0);
1952
- const [values, dispatch] = useReducer(reducer, resetValues);
1953
- const { show, style, tooltipStyle, position, description, actions } = values;
1954
- function reducer(state, action) {
1955
- const updatedValues = { ...state };
1956
- if ("show" in action) {
1957
- updatedValues.show = action.show;
1958
- }
1959
- if ("style" in action) {
1960
- updatedValues.style = action.style;
1961
- }
1962
- if ("tooltipStyle" in action) {
1963
- updatedValues.tooltipStyle = action.tooltipStyle;
1964
- }
1965
- if ("position" in action) {
1966
- updatedValues.position = action.position;
1967
- }
1968
- if ("description" in action) {
1969
- updatedValues.description = action.description;
1970
- }
1971
- if ("actions" in action) {
1972
- updatedValues.actions = action.actions;
1973
- }
1974
- if ("render" in action && !!action.render) {
1975
- setCurrentState((prev) => 1 - prev);
2024
+ function reducer(state, action) {
2025
+ let changed = false;
2026
+ for (const k in action) {
2027
+ const key = k;
2028
+ if (action[key] !== state[key]) {
2029
+ changed = true;
2030
+ break;
1976
2031
  }
1977
- return updatedValues;
1978
2032
  }
1979
- function handleShowPopover(params) {
1980
- dispatch({
1981
- ...params,
1982
- render: true
1983
- });
2033
+ if (!changed) return state;
2034
+ const updatedValues = { ...state };
2035
+ if ("show" in action) {
2036
+ updatedValues.show = action.show;
1984
2037
  }
1985
- function handleClosePopover() {
1986
- dispatch({
1987
- ...resetValues,
1988
- render: true
1989
- });
2038
+ if ("style" in action) {
2039
+ updatedValues.style = action.style;
1990
2040
  }
1991
- return /* @__PURE__ */ jsxs(
1992
- PopoverContext.Provider,
1993
- {
1994
- value: {
1995
- showPopover: show,
1996
- setPopover: handleShowPopover
1997
- },
1998
- children: [
1999
- children,
2000
- !!show && /* @__PURE__ */ jsx(
2001
- PopoverCard,
2002
- {
2003
- show,
2004
- style,
2005
- tooltipStyle,
2006
- position,
2007
- description,
2008
- actions,
2009
- onClose: handleClosePopover
2010
- }
2011
- )
2012
- ]
2013
- }
2014
- );
2041
+ if ("tooltipStyle" in action) {
2042
+ updatedValues.tooltipStyle = action.tooltipStyle;
2043
+ }
2044
+ if ("position" in action) {
2045
+ updatedValues.position = action.position;
2046
+ }
2047
+ if ("description" in action) {
2048
+ updatedValues.description = action.description;
2049
+ }
2050
+ if ("actions" in action) {
2051
+ updatedValues.actions = action.actions;
2052
+ }
2053
+ return updatedValues;
2054
+ }
2055
+ function PopoverProvider({ children }) {
2056
+ const [values, dispatch] = useReducer(reducer, resetValues);
2057
+ const { show, style, tooltipStyle, position, description, actions } = values;
2058
+ const handleShowPopover = useCallback((params) => {
2059
+ dispatch(params);
2060
+ }, []);
2061
+ const handleClosePopover = useCallback(() => {
2062
+ dispatch(resetValues);
2063
+ }, []);
2064
+ const contextValue = useMemo(() => ({
2065
+ showPopover: show,
2066
+ setPopover: handleShowPopover
2067
+ }), [show, handleShowPopover]);
2068
+ return /* @__PURE__ */ jsxs(PopoverContext.Provider, { value: contextValue, children: [
2069
+ children,
2070
+ !!show && /* @__PURE__ */ jsx(
2071
+ PopoverCard,
2072
+ {
2073
+ show,
2074
+ style,
2075
+ tooltipStyle,
2076
+ position,
2077
+ description,
2078
+ actions,
2079
+ onClose: handleClosePopover
2080
+ }
2081
+ )
2082
+ ] });
2015
2083
  }
2016
2084
  function usePopoverContext() {
2017
2085
  const context = useContext(PopoverContext);
@@ -2026,8 +2094,15 @@ function Popover({ className, description, actions, children, opts }) {
2026
2094
  const { screenWidth, screenHeight } = useResizeListener();
2027
2095
  const [menuData, setMenuData] = useState({ showMenu: false, x: 0, y: 0, left: 0, right: 0, top: 0, bottom: 0 });
2028
2096
  const parentRef = useRef(null);
2097
+ const prevShowMenuRef = useRef(false);
2098
+ const prevShowPopoverRef = useRef(false);
2029
2099
  (actions || []).some((action) => (action.items || []).length > 0);
2030
2100
  useEffect(() => {
2101
+ const wasOpen = prevShowMenuRef.current;
2102
+ prevShowMenuRef.current = menuData.showMenu;
2103
+ if (!menuData.showMenu && !wasOpen) {
2104
+ return;
2105
+ }
2031
2106
  const stickyTop = menuData.showMenu && screenHeight - (menuData.y + 14) < 300;
2032
2107
  setPopover({
2033
2108
  show: menuData.showMenu,
@@ -2051,13 +2126,14 @@ function Popover({ className, description, actions, children, opts }) {
2051
2126
  });
2052
2127
  }, [menuData, screenWidth, screenHeight, description, actions, setPopover]);
2053
2128
  useEffect(() => {
2054
- if (!showPopover && !!menuData.showMenu) {
2129
+ if (prevShowPopoverRef.current && !showPopover) {
2055
2130
  setMenuData((prev) => ({
2056
2131
  ...prev,
2057
2132
  showMenu: false
2058
2133
  }));
2059
2134
  }
2060
- }, [showPopover, menuData.showMenu]);
2135
+ prevShowPopoverRef.current = showPopover;
2136
+ }, [showPopover]);
2061
2137
  function handleToggleMenu(e) {
2062
2138
  e.stopPropagation();
2063
2139
  if (menuData.showMenu) {
@@ -2180,7 +2256,7 @@ function Dragger({
2180
2256
  onChange
2181
2257
  }) {
2182
2258
  const [, setCurrentState] = useState(0);
2183
- const [values, dispatch] = useReducer(reducer, {
2259
+ const [values, dispatch] = useReducer(reducer2, {
2184
2260
  items: null,
2185
2261
  displayItems: null,
2186
2262
  dragging: false,
@@ -2219,7 +2295,7 @@ function Dragger({
2219
2295
  }
2220
2296
  }
2221
2297
  }, [grandChildren, updated]);
2222
- function reducer(state, action) {
2298
+ function reducer2(state, action) {
2223
2299
  const updatedValues = { ...state };
2224
2300
  if ("items" in action) {
2225
2301
  updatedValues.items = action.items;