@triggery/zustand 0.1.0

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 ADDED
@@ -0,0 +1,9 @@
1
+ # @triggery/zustand
2
+
3
+ ## 0.1.0
4
+
5
+ First public preview release.
6
+
7
+ Zustand adapter for Triggery — `useZustandCondition` hook wiring a Zustand store into a trigger
8
+
9
+ See the [repository-level CHANGELOG](../../CHANGELOG.md#010--2026-05-16) for the full set of packages and the umbrella feature list. Future entries on this file are appended automatically by changesets.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Aleksey Skhomenko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # @triggery/zustand
2
+
3
+ Read a [Zustand](https://github.com/pmndrs/zustand) store from a Triggery condition.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ pnpm add @triggery/core @triggery/react @triggery/zustand zustand
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```tsx
14
+ import { create } from 'zustand';
15
+ import { createTrigger } from '@triggery/core';
16
+ import { useEvent, useAction } from '@triggery/react';
17
+ import { useZustandCondition } from '@triggery/zustand';
18
+
19
+ type Settings = { sound: boolean; notifications: boolean };
20
+
21
+ const useSettings = create<Settings>(() => ({ sound: true, notifications: true }));
22
+
23
+ const messageTrigger = createTrigger<{
24
+ events: { 'new-message': { text: string } };
25
+ conditions: { settings: Settings };
26
+ actions: { showToast: { body: string } };
27
+ }>({
28
+ id: 'message-received',
29
+ events: ['new-message'],
30
+ required: ['settings'],
31
+ handler({ event, conditions, actions }) {
32
+ if (!conditions.settings.notifications) return;
33
+ actions.showToast?.({ body: event.payload.text });
34
+ },
35
+ });
36
+
37
+ function SettingsBridge() {
38
+ useZustandCondition(messageTrigger, 'settings', useSettings, (s) => s);
39
+ return null;
40
+ }
41
+ ```
42
+
43
+ ## How it works
44
+
45
+ The runtime is pull-only: `selector(store.getState())` is called **only** when a trigger fires, not when the store changes. That means:
46
+
47
+ 1. Nothing in your React tree re-renders because of this hook. If a component also needs the same slice, call Zustand's own `useStore(store, selector)` alongside.
48
+ 2. The trigger always sees the latest state at fire-time — no subscription, no possibility of a stale snapshot.
49
+
50
+ ## API
51
+
52
+ ```ts
53
+ useZustandCondition<T, S, K>(
54
+ trigger: Trigger<S>,
55
+ name: K,
56
+ store: { getState(): T },
57
+ selector: (state: T) => ConditionMap<S>[K],
58
+ ): void
59
+ ```
60
+
61
+ Works with both vanilla stores (`createStore`) and hook stores (`create`).
62
+
63
+ ## License
64
+
65
+ MIT
@@ -0,0 +1,38 @@
1
+ import { TriggerSchema, ConditionKey, Trigger, ConditionMap } from '@triggery/core';
2
+
3
+ /**
4
+ * Minimal Zustand store shape we depend on. Both vanilla stores (`createStore`)
5
+ * and the React hook stores (`create`) expose this contract, so the adapter
6
+ * works with either one without importing Zustand itself.
7
+ */
8
+ interface ZustandStoreLike<T> {
9
+ getState(): T;
10
+ }
11
+ /**
12
+ * Wire a Zustand store into a Triggery condition.
13
+ *
14
+ * The runtime is pull-only — `selector(store.getState())` is called when a
15
+ * trigger fires, not when the store changes. That means:
16
+ *
17
+ * 1. Nothing in the React tree re-renders because of this hook. If a
18
+ * component also needs to read the same slice, call Zustand's own hook
19
+ * (`useStore(store, selector)`) alongside.
20
+ * 2. The trigger always sees the latest state at fire-time, with no
21
+ * subscription overhead and no possibility of a stale snapshot.
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * import { create } from 'zustand';
26
+ * import { useZustandCondition } from '@triggery/zustand';
27
+ *
28
+ * const useSettings = create(() => ({ sound: true, notifications: true }));
29
+ *
30
+ * function SettingsBridge() {
31
+ * useZustandCondition(messageTrigger, 'settings', useSettings, (s) => s);
32
+ * return null;
33
+ * }
34
+ * ```
35
+ */
36
+ declare function useZustandCondition<T, S extends TriggerSchema, K extends ConditionKey<S>>(trigger: Trigger<S>, name: K, store: ZustandStoreLike<T>, selector: (state: T) => ConditionMap<S>[K]): void;
37
+
38
+ export { type ZustandStoreLike, useZustandCondition };
package/dist/index.js ADDED
@@ -0,0 +1,10 @@
1
+ import { useCondition } from '@triggery/react';
2
+
3
+ // src/index.ts
4
+ function useZustandCondition(trigger, name, store, selector) {
5
+ useCondition(trigger, name, () => selector(store.getState()), [store, selector]);
6
+ }
7
+
8
+ export { useZustandCondition };
9
+ //# sourceMappingURL=index.js.map
10
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;AAqCO,SAAS,mBAAA,CACd,OAAA,EACA,IAAA,EACA,KAAA,EACA,QAAA,EACM;AACN,EAAA,YAAA,CAAa,OAAA,EAAS,IAAA,EAAM,MAAM,QAAA,CAAS,KAAA,CAAM,QAAA,EAAU,CAAA,EAAG,CAAC,KAAA,EAAO,QAAQ,CAAC,CAAA;AACjF","file":"index.js","sourcesContent":["import type { ConditionKey, ConditionMap, Trigger, TriggerSchema } from '@triggery/core';\nimport { useCondition } from '@triggery/react';\n\n/**\n * Minimal Zustand store shape we depend on. Both vanilla stores (`createStore`)\n * and the React hook stores (`create`) expose this contract, so the adapter\n * works with either one without importing Zustand itself.\n */\nexport interface ZustandStoreLike<T> {\n getState(): T;\n}\n\n/**\n * Wire a Zustand store into a Triggery condition.\n *\n * The runtime is pull-only — `selector(store.getState())` is called when a\n * trigger fires, not when the store changes. That means:\n *\n * 1. Nothing in the React tree re-renders because of this hook. If a\n * component also needs to read the same slice, call Zustand's own hook\n * (`useStore(store, selector)`) alongside.\n * 2. The trigger always sees the latest state at fire-time, with no\n * subscription overhead and no possibility of a stale snapshot.\n *\n * @example\n * ```ts\n * import { create } from 'zustand';\n * import { useZustandCondition } from '@triggery/zustand';\n *\n * const useSettings = create(() => ({ sound: true, notifications: true }));\n *\n * function SettingsBridge() {\n * useZustandCondition(messageTrigger, 'settings', useSettings, (s) => s);\n * return null;\n * }\n * ```\n */\nexport function useZustandCondition<T, S extends TriggerSchema, K extends ConditionKey<S>>(\n trigger: Trigger<S>,\n name: K,\n store: ZustandStoreLike<T>,\n selector: (state: T) => ConditionMap<S>[K],\n): void {\n useCondition(trigger, name, () => selector(store.getState()), [store, selector]);\n}\n"]}
package/package.json ADDED
@@ -0,0 +1,81 @@
1
+ {
2
+ "name": "@triggery/zustand",
3
+ "version": "0.1.0",
4
+ "description": "Zustand adapter for Triggery — `useZustandCondition` hook wiring a Zustand store into a trigger",
5
+ "license": "MIT",
6
+ "author": "Aleksey Skhomenko",
7
+ "homepage": "https://triggeryjs.github.io/triggery",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/triggeryjs/triggery.git",
11
+ "directory": "packages/zustand"
12
+ },
13
+ "bugs": "https://github.com/triggeryjs/triggery/issues",
14
+ "funding": [
15
+ {
16
+ "type": "patreon",
17
+ "url": "https://www.patreon.com/triggery"
18
+ },
19
+ {
20
+ "type": "boosty",
21
+ "url": "https://boosty.to/triggery"
22
+ }
23
+ ],
24
+ "keywords": [
25
+ "triggery",
26
+ "zustand",
27
+ "adapter",
28
+ "react",
29
+ "state"
30
+ ],
31
+ "type": "module",
32
+ "main": "./dist/index.js",
33
+ "module": "./dist/index.js",
34
+ "types": "./dist/index.d.ts",
35
+ "exports": {
36
+ ".": {
37
+ "source": "./src/index.ts",
38
+ "types": "./dist/index.d.ts",
39
+ "import": "./dist/index.js",
40
+ "default": "./dist/index.js"
41
+ },
42
+ "./package.json": "./package.json"
43
+ },
44
+ "files": [
45
+ "dist",
46
+ "README.md",
47
+ "LICENSE",
48
+ "CHANGELOG.md"
49
+ ],
50
+ "sideEffects": false,
51
+ "publishConfig": {
52
+ "access": "public"
53
+ },
54
+ "peerDependencies": {
55
+ "react": ">=18.0.0",
56
+ "zustand": "^4.0.0 || ^5.0.0",
57
+ "@triggery/core": "0.1.0",
58
+ "@triggery/react": "0.1.0"
59
+ },
60
+ "devDependencies": {
61
+ "@testing-library/react": "^16.3.2",
62
+ "@types/react": "^19.2.14",
63
+ "happy-dom": "^20.9.0",
64
+ "react": "^19.2.6",
65
+ "react-dom": "^19.2.6",
66
+ "tsup": "^8.5.1",
67
+ "typescript": "^6.0.3",
68
+ "vitest": "^4.1.6",
69
+ "zustand": "^5.0.0",
70
+ "@triggery/react": "0.1.0",
71
+ "@triggery/core": "0.1.0"
72
+ },
73
+ "scripts": {
74
+ "build": "tsup",
75
+ "dev": "tsup --watch",
76
+ "test": "vitest run",
77
+ "test:watch": "vitest",
78
+ "test:coverage": "vitest run --coverage",
79
+ "clean": "rm -rf dist *.tsbuildinfo"
80
+ }
81
+ }