@peachy/hooks 0.0.8 → 0.0.10

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
@@ -1,5 +1,13 @@
1
1
  # @peachy/hooks
2
2
 
3
+ ## 0.0.10
4
+
5
+ ### Patch Changes
6
+
7
+ - b74728b: Add useDarkMode and useAccentColor hook
8
+
9
+ ## 0.0.9
10
+
3
11
  ## 0.0.8
4
12
 
5
13
  ### Patch Changes
package/README.md CHANGED
@@ -92,3 +92,27 @@ const App = () => {
92
92
  );
93
93
  };
94
94
  ```
95
+
96
+ ### Adwaita
97
+
98
+ #### `useDarkMode`
99
+
100
+ Allows checking the current dark mode state and setting it.
101
+
102
+ ```ts
103
+ import { useDarkMode } from "@peachy/hooks";
104
+
105
+ const [darkMode, setDarkMode] = useDarkMode();
106
+ ```
107
+
108
+ #### `useAccentColor`
109
+
110
+ Allows checking the current accent color if set.
111
+
112
+ ```ts
113
+ import { useAccentColor } from "@peachy/hooks";
114
+
115
+ const [accentColor] = useAccentColor();
116
+
117
+ // NOTE: returns `null` if accent colors are unsupported
118
+ ```
@@ -0,0 +1,3 @@
1
+ import { useAccentColor } from "./use-accent-color.mjs";
2
+ import { useDarkMode } from "./use-dark-mode.mjs";
3
+ export { useAccentColor, useDarkMode };
@@ -0,0 +1,4 @@
1
+ import { useAccentColor } from "./use-accent-color.mjs";
2
+ import { useDarkMode } from "./use-dark-mode.mjs";
3
+
4
+ export { useAccentColor, useDarkMode };
@@ -0,0 +1,8 @@
1
+ //#region src/adw/use-accent-color.d.ts
2
+ /**
3
+ * Query the system's accent color if supported
4
+ * @returns [accentColor] - accent color or null
5
+ */
6
+ declare function useAccentColor(): any[];
7
+ //#endregion
8
+ export { useAccentColor };
@@ -0,0 +1,19 @@
1
+ import { useBinding } from "../gobject/use-binding.mjs";
2
+ import "../gobject/index.mjs";
3
+ import { useRef } from "react";
4
+ import Adw from "gi://Adw?version=1";
5
+
6
+ //#region src/adw/use-accent-color.ts
7
+ /**
8
+ * Query the system's accent color if supported
9
+ * @returns [accentColor] - accent color or null
10
+ */
11
+ function useAccentColor() {
12
+ const styleManager = useRef(Adw.StyleManager.get_default());
13
+ const [accentColor] = useBinding(styleManager, "accentColor");
14
+ const [systemSupportsAccentColors] = useBinding(styleManager, "systemSupportsAccentColors");
15
+ return [systemSupportsAccentColors ? accentColor : null];
16
+ }
17
+
18
+ //#endregion
19
+ export { useAccentColor };
@@ -0,0 +1,4 @@
1
+ //#region src/adw/use-dark-mode.d.ts
2
+ declare function useDarkMode(): any[];
3
+ //#endregion
4
+ export { useDarkMode };
@@ -0,0 +1,16 @@
1
+ import { useBinding } from "../gobject/use-binding.mjs";
2
+ import "../gobject/index.mjs";
3
+ import { useCallback, useRef } from "react";
4
+ import Adw from "gi://Adw?version=1";
5
+
6
+ //#region src/adw/use-dark-mode.ts
7
+ function useDarkMode() {
8
+ const styleManager = useRef(Adw.StyleManager.get_default());
9
+ const [isDark] = useBinding(styleManager, "dark", false);
10
+ return [isDark, useCallback((value) => {
11
+ styleManager.current.set_color_scheme(value ? Adw.ColorScheme.PREFER_DARK : Adw.ColorScheme.PREFER_LIGHT);
12
+ }, [styleManager])];
13
+ }
14
+
15
+ //#endregion
16
+ export { useDarkMode };
@@ -3,7 +3,7 @@ import GObject from "gi://GObject?version=2.0";
3
3
 
4
4
  //#region src/gobject/use-binding.d.ts
5
5
  type NonFunctionKeys<T> = { [K in keyof T]: T[K] extends Function ? never : K extends Symbol ? never : K extends number ? never : K }[keyof T];
6
- declare function useBinding<T extends GObject.Object, Prop extends NonFunctionKeys<T>>(object: T | RefObject<T>, property: Prop, defaultValue?: T[Prop], converter?: undefined): [T[Prop]];
6
+ declare function useBinding<T extends GObject.Object, Prop extends NonFunctionKeys<T>>(object: T | RefObject<T | null>, property: Prop, defaultValue?: T[Prop], converter?: undefined): [T[Prop]];
7
7
  declare function useBinding<T extends GObject.Object, Prop extends NonFunctionKeys<T>, Converter extends (value: T[Prop]) => any>(object: T | RefObject<T>, property: Prop, defaultValue: T[Prop] | undefined, converter: Converter): [ReturnType<Converter>];
8
8
  //#endregion
9
9
  export { useBinding };
@@ -12,17 +12,14 @@ function useBinding(object, _property, defaultValue, converter) {
12
12
  return converter ? converter(initialResult) : initialResult;
13
13
  });
14
14
  useEffect(() => {
15
- if (!value) {
16
- console.error(`Object is null or undefined`);
17
- return;
18
- }
15
+ if (!value) return;
19
16
  const id = value.connect(`notify::${property}`, () => {
20
17
  setResult(value[property]);
21
18
  });
22
19
  return () => {
23
20
  value.disconnect(id);
24
21
  };
25
- }, []);
22
+ }, [value]);
26
23
  return [result];
27
24
  }
28
25
  function toKebabCase(str) {
package/dist/index.d.mts CHANGED
@@ -1,5 +1,7 @@
1
+ import { useAccentColor } from "./adw/use-accent-color.mjs";
2
+ import { useDarkMode } from "./adw/use-dark-mode.mjs";
1
3
  import { useReference } from "./base/use-reference.mjs";
2
4
  import "./base/index.mjs";
3
5
  import { useBinding } from "./gobject/use-binding.mjs";
4
6
  import "./gobject/index.mjs";
5
- export { useBinding, useReference };
7
+ export { useAccentColor, useBinding, useDarkMode, useReference };
package/dist/index.mjs CHANGED
@@ -2,5 +2,8 @@ import { useReference } from "./base/use-reference.mjs";
2
2
  import "./base/index.mjs";
3
3
  import { useBinding } from "./gobject/use-binding.mjs";
4
4
  import "./gobject/index.mjs";
5
+ import { useAccentColor } from "./adw/use-accent-color.mjs";
6
+ import { useDarkMode } from "./adw/use-dark-mode.mjs";
7
+ import "./adw/index.mjs";
5
8
 
6
- export { useBinding, useReference };
9
+ export { useAccentColor, useBinding, useDarkMode, useReference };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peachy/hooks",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
4
4
  "description": "",
5
5
  "main": "./dist/index.mjs",
6
6
  "exports": {
@@ -16,11 +16,11 @@
16
16
  "author": "",
17
17
  "license": "MIT",
18
18
  "devDependencies": {
19
- "@types/react": "^19.2.8",
20
- "react": "^19.2.3",
21
- "tsdown": "0.20.0-beta.4",
19
+ "@types/react": "^19.2.13",
20
+ "react": "^19.2.4",
21
+ "tsdown": "0.20.3",
22
22
  "typescript": "^5.9.3",
23
- "@peachy/react": "0.0.8"
23
+ "@peachy/react": "0.0.10"
24
24
  },
25
25
  "peerDependencies": {
26
26
  "react": "^19.2.0"
@@ -0,0 +1,2 @@
1
+ export * from "./use-accent-color";
2
+ export * from "./use-dark-mode";
@@ -0,0 +1,21 @@
1
+ import Adw from "gi://Adw?version=1";
2
+
3
+ import { useRef } from "react";
4
+
5
+ import { useBinding } from "../gobject";
6
+
7
+ /**
8
+ * Query the system's accent color if supported
9
+ * @returns [accentColor] - accent color or null
10
+ */
11
+ export function useAccentColor() {
12
+ const styleManager = useRef(Adw.StyleManager.get_default());
13
+
14
+ const [accentColor] = useBinding(styleManager, "accentColor");
15
+ const [systemSupportsAccentColors] = useBinding(
16
+ styleManager,
17
+ "systemSupportsAccentColors",
18
+ );
19
+
20
+ return [systemSupportsAccentColors ? accentColor : null];
21
+ }
@@ -0,0 +1,21 @@
1
+ import Adw from "gi://Adw?version=1";
2
+
3
+ import { useCallback, useRef } from "react";
4
+
5
+ import { useBinding } from "../gobject";
6
+
7
+ export function useDarkMode() {
8
+ const styleManager = useRef(Adw.StyleManager.get_default());
9
+
10
+ const [isDark] = useBinding(styleManager, "dark", false);
11
+ const setDarkMode = useCallback(
12
+ (value: boolean) => {
13
+ styleManager.current.set_color_scheme(
14
+ value ? Adw.ColorScheme.PREFER_DARK : Adw.ColorScheme.PREFER_LIGHT,
15
+ );
16
+ },
17
+ [styleManager],
18
+ );
19
+
20
+ return [isDark, setDarkMode];
21
+ }
@@ -16,7 +16,7 @@ export function useBinding<
16
16
  T extends GObject.Object,
17
17
  Prop extends NonFunctionKeys<T>,
18
18
  >(
19
- object: T | RefObject<T>,
19
+ object: T | RefObject<T | null>,
20
20
  property: Prop,
21
21
  defaultValue?: T[Prop],
22
22
  converter?: undefined,
@@ -46,25 +46,24 @@ export function useBinding<
46
46
 
47
47
  const [result, setResult] = useState(() => {
48
48
  if (!value) return defaultValue;
49
- const initialResult = value[property];
49
+ const initialResult = value[property as Prop];
50
50
  return converter ? converter(initialResult) : initialResult;
51
51
  });
52
52
 
53
53
  // run this on initial render
54
54
  useEffect(() => {
55
55
  if (!value) {
56
- console.error(`Object is null or undefined`);
57
56
  return;
58
57
  }
59
58
 
60
59
  const id = value.connect(`notify::${property}`, () => {
61
- setResult(value[property]);
60
+ setResult(value[property as Prop]);
62
61
  });
63
62
 
64
63
  return () => {
65
64
  value.disconnect(id);
66
65
  };
67
- }, []);
66
+ }, [value]);
68
67
 
69
68
  return [result] as const;
70
69
  }
package/src/index.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from "./base";
2
2
  export * from "./gobject";
3
+ export * from "./adw";