qstd 0.2.5 → 0.2.7

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/CHANGELOG.md CHANGED
@@ -7,6 +7,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.2.7] - 2025-11-25
11
+
12
+ ### Changed
13
+
14
+ - Added console logging to `useTheme` toggle function for debugging
15
+
16
+ ## [0.2.6] - 2025-11-25
17
+
18
+ ### Fixed
19
+
20
+ - Improved `useTheme` hook reliability with refs to prevent stale closures
21
+ - Added `storeRef` to always access current state
22
+ - Added `isInternalUpdateRef` to distinguish internal vs external updates
23
+ - Fixed localStorage parsing to use correct `parsed.value` property
24
+ - Prevents duplicate state updates from custom events
25
+
10
26
  ## [0.2.5] - 2025-11-25
11
27
 
12
28
  ### Fixed
@@ -3563,7 +3563,7 @@ var getInitialStore = () => {
3563
3563
  if (stored) {
3564
3564
  const parsed = JSON.parse(stored);
3565
3565
  return {
3566
- value: parsed.theme ?? getInitialTheme(),
3566
+ value: parsed.value ?? getInitialTheme(),
3567
3567
  isManual: parsed.isManual ?? false
3568
3568
  };
3569
3569
  }
@@ -3588,16 +3588,14 @@ var saveStore = (store) => {
3588
3588
  // src/react/use-theme/index.ts
3589
3589
  function useTheme() {
3590
3590
  const [store, setStore] = React11__namespace.default.useState(getInitialStore);
3591
- const [shouldSave, setShouldSave] = React11__namespace.default.useState(false);
3591
+ const storeRef = React11__namespace.default.useRef(store);
3592
+ const isInternalUpdateRef = React11__namespace.default.useRef(false);
3593
+ React11__namespace.default.useEffect(() => {
3594
+ storeRef.current = store;
3595
+ }, [store]);
3592
3596
  React11__namespace.default.useEffect(() => {
3593
3597
  document.documentElement.setAttribute("data-theme", store.value);
3594
3598
  }, [store.value]);
3595
- React11__namespace.default.useEffect(() => {
3596
- if (shouldSave) {
3597
- saveStore(store);
3598
- setShouldSave(false);
3599
- }
3600
- }, [store, shouldSave]);
3601
3599
  React11__namespace.default.useEffect(() => {
3602
3600
  const handleStorageChange = (e) => {
3603
3601
  if (e.key === THEME_STORAGE_KEY || e.key === THEME_STORE_STORAGE_KEY) {
@@ -3605,6 +3603,10 @@ function useTheme() {
3605
3603
  }
3606
3604
  };
3607
3605
  const handleThemeChange = (e) => {
3606
+ if (isInternalUpdateRef.current) {
3607
+ isInternalUpdateRef.current = false;
3608
+ return;
3609
+ }
3608
3610
  const customEvent = e;
3609
3611
  if (customEvent.detail) {
3610
3612
  setStore(customEvent.detail);
@@ -3618,15 +3620,20 @@ function useTheme() {
3618
3620
  };
3619
3621
  }, []);
3620
3622
  const update = (updates) => {
3621
- setStore((prev) => ({ ...prev, ...updates }));
3622
- setShouldSave(true);
3623
+ const newStore = { ...storeRef.current, ...updates };
3624
+ setStore(newStore);
3625
+ isInternalUpdateRef.current = true;
3626
+ saveStore(newStore);
3623
3627
  };
3624
3628
  const toggle = (theme) => {
3625
- setStore((prev) => ({
3626
- value: theme ?? (prev.value === "light" ? "dark" : "light"),
3629
+ console.log("toggle theme");
3630
+ const newStore = {
3631
+ value: theme ?? (storeRef.current.value === "light" ? "dark" : "light"),
3627
3632
  isManual: true
3628
- }));
3629
- setShouldSave(true);
3633
+ };
3634
+ setStore(newStore);
3635
+ isInternalUpdateRef.current = true;
3636
+ saveStore(newStore);
3630
3637
  };
3631
3638
  return { ...store, update, toggle };
3632
3639
  }
@@ -3540,7 +3540,7 @@ var getInitialStore = () => {
3540
3540
  if (stored) {
3541
3541
  const parsed = JSON.parse(stored);
3542
3542
  return {
3543
- value: parsed.theme ?? getInitialTheme(),
3543
+ value: parsed.value ?? getInitialTheme(),
3544
3544
  isManual: parsed.isManual ?? false
3545
3545
  };
3546
3546
  }
@@ -3565,16 +3565,14 @@ var saveStore = (store) => {
3565
3565
  // src/react/use-theme/index.ts
3566
3566
  function useTheme() {
3567
3567
  const [store, setStore] = React11__default.useState(getInitialStore);
3568
- const [shouldSave, setShouldSave] = React11__default.useState(false);
3568
+ const storeRef = React11__default.useRef(store);
3569
+ const isInternalUpdateRef = React11__default.useRef(false);
3570
+ React11__default.useEffect(() => {
3571
+ storeRef.current = store;
3572
+ }, [store]);
3569
3573
  React11__default.useEffect(() => {
3570
3574
  document.documentElement.setAttribute("data-theme", store.value);
3571
3575
  }, [store.value]);
3572
- React11__default.useEffect(() => {
3573
- if (shouldSave) {
3574
- saveStore(store);
3575
- setShouldSave(false);
3576
- }
3577
- }, [store, shouldSave]);
3578
3576
  React11__default.useEffect(() => {
3579
3577
  const handleStorageChange = (e) => {
3580
3578
  if (e.key === THEME_STORAGE_KEY || e.key === THEME_STORE_STORAGE_KEY) {
@@ -3582,6 +3580,10 @@ function useTheme() {
3582
3580
  }
3583
3581
  };
3584
3582
  const handleThemeChange = (e) => {
3583
+ if (isInternalUpdateRef.current) {
3584
+ isInternalUpdateRef.current = false;
3585
+ return;
3586
+ }
3585
3587
  const customEvent = e;
3586
3588
  if (customEvent.detail) {
3587
3589
  setStore(customEvent.detail);
@@ -3595,15 +3597,20 @@ function useTheme() {
3595
3597
  };
3596
3598
  }, []);
3597
3599
  const update = (updates) => {
3598
- setStore((prev) => ({ ...prev, ...updates }));
3599
- setShouldSave(true);
3600
+ const newStore = { ...storeRef.current, ...updates };
3601
+ setStore(newStore);
3602
+ isInternalUpdateRef.current = true;
3603
+ saveStore(newStore);
3600
3604
  };
3601
3605
  const toggle = (theme) => {
3602
- setStore((prev) => ({
3603
- value: theme ?? (prev.value === "light" ? "dark" : "light"),
3606
+ console.log("toggle theme");
3607
+ const newStore = {
3608
+ value: theme ?? (storeRef.current.value === "light" ? "dark" : "light"),
3604
3609
  isManual: true
3605
- }));
3606
- setShouldSave(true);
3610
+ };
3611
+ setStore(newStore);
3612
+ isInternalUpdateRef.current = true;
3613
+ saveStore(newStore);
3607
3614
  };
3608
3615
  return { ...store, update, toggle };
3609
3616
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qstd",
3
- "version": "0.2.5",
3
+ "version": "0.2.7",
4
4
  "description": "Standard Block component and utilities library with Panda CSS",
5
5
  "author": "malin1",
6
6
  "license": "MIT",