@molecule/app-svelte 3.3.0 → 3.5.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.
Files changed (164) hide show
  1. package/README.md +2393 -220
  2. package/dist/agent-identity.js +0 -1
  3. package/dist/context.d.ts +101 -1
  4. package/dist/context.d.ts.map +1 -1
  5. package/dist/context.js +199 -3
  6. package/dist/hooks/external-store.d.ts +28 -0
  7. package/dist/hooks/external-store.d.ts.map +1 -0
  8. package/dist/hooks/external-store.js +95 -0
  9. package/dist/hooks/index.d.ts +37 -0
  10. package/dist/hooks/index.d.ts.map +1 -0
  11. package/dist/hooks/index.js +36 -0
  12. package/dist/hooks/live-store.d.ts +68 -0
  13. package/dist/hooks/live-store.d.ts.map +1 -0
  14. package/dist/hooks/live-store.js +76 -0
  15. package/dist/hooks/useAIModels.d.ts +58 -0
  16. package/dist/hooks/useAIModels.d.ts.map +1 -0
  17. package/dist/hooks/useAIModels.js +155 -0
  18. package/dist/hooks/useAsyncState.d.ts +52 -0
  19. package/dist/hooks/useAsyncState.d.ts.map +1 -0
  20. package/dist/hooks/useAsyncState.js +80 -0
  21. package/dist/hooks/useAuth.d.ts +99 -0
  22. package/dist/hooks/useAuth.d.ts.map +1 -0
  23. package/dist/hooks/useAuth.js +88 -0
  24. package/dist/hooks/useCapacitorApp.d.ts +51 -0
  25. package/dist/hooks/useCapacitorApp.d.ts.map +1 -0
  26. package/dist/hooks/useCapacitorApp.js +57 -0
  27. package/dist/hooks/useChangePassword.d.ts +41 -0
  28. package/dist/hooks/useChangePassword.d.ts.map +1 -0
  29. package/dist/hooks/useChangePassword.js +40 -0
  30. package/dist/hooks/useDevice.d.ts +48 -0
  31. package/dist/hooks/useDevice.d.ts.map +1 -0
  32. package/dist/hooks/useDevice.js +37 -0
  33. package/dist/hooks/useEditor.d.ts +65 -0
  34. package/dist/hooks/useEditor.d.ts.map +1 -0
  35. package/dist/hooks/useEditor.js +86 -0
  36. package/dist/hooks/useForm.d.ts +137 -0
  37. package/dist/hooks/useForm.d.ts.map +1 -0
  38. package/dist/hooks/useForm.js +221 -0
  39. package/dist/hooks/useHttp.d.ts +122 -0
  40. package/dist/hooks/useHttp.d.ts.map +1 -0
  41. package/dist/hooks/useHttp.js +220 -0
  42. package/dist/hooks/useLogger.d.ts +52 -0
  43. package/dist/hooks/useLogger.d.ts.map +1 -0
  44. package/dist/hooks/useLogger.js +64 -0
  45. package/dist/hooks/useLogin.d.ts +47 -0
  46. package/dist/hooks/useLogin.d.ts.map +1 -0
  47. package/dist/hooks/useLogin.js +44 -0
  48. package/dist/hooks/useOAuth.d.ts +96 -0
  49. package/dist/hooks/useOAuth.d.ts.map +1 -0
  50. package/dist/hooks/useOAuth.js +363 -0
  51. package/dist/hooks/useOptimisticValue.d.ts +58 -0
  52. package/dist/hooks/useOptimisticValue.d.ts.map +1 -0
  53. package/dist/hooks/useOptimisticValue.js +98 -0
  54. package/dist/hooks/usePasswordReset.d.ts +50 -0
  55. package/dist/hooks/usePasswordReset.d.ts.map +1 -0
  56. package/dist/hooks/usePasswordReset.js +50 -0
  57. package/dist/hooks/usePlatform.d.ts +45 -0
  58. package/dist/hooks/usePlatform.d.ts.map +1 -0
  59. package/dist/hooks/usePlatform.js +35 -0
  60. package/dist/hooks/usePreview.d.ts +54 -0
  61. package/dist/hooks/usePreview.d.ts.map +1 -0
  62. package/dist/hooks/usePreview.js +61 -0
  63. package/dist/hooks/useProfileForm.d.ts +100 -0
  64. package/dist/hooks/useProfileForm.d.ts.map +1 -0
  65. package/dist/hooks/useProfileForm.js +267 -0
  66. package/dist/hooks/usePromise.d.ts +62 -0
  67. package/dist/hooks/usePromise.d.ts.map +1 -0
  68. package/dist/hooks/usePromise.js +105 -0
  69. package/dist/hooks/usePush.d.ts +68 -0
  70. package/dist/hooks/usePush.d.ts.map +1 -0
  71. package/dist/hooks/usePush.js +79 -0
  72. package/dist/hooks/useRouter.d.ts +169 -0
  73. package/dist/hooks/useRouter.d.ts.map +1 -0
  74. package/dist/hooks/useRouter.js +212 -0
  75. package/dist/hooks/useSignup.d.ts +52 -0
  76. package/dist/hooks/useSignup.d.ts.map +1 -0
  77. package/dist/hooks/useSignup.js +42 -0
  78. package/dist/hooks/useStorage.d.ts +133 -0
  79. package/dist/hooks/useStorage.d.ts.map +1 -0
  80. package/dist/hooks/useStorage.js +228 -0
  81. package/dist/hooks/useStore.d.ts +69 -0
  82. package/dist/hooks/useStore.d.ts.map +1 -0
  83. package/dist/hooks/useStore.js +82 -0
  84. package/dist/hooks/useTheme.d.ts +55 -0
  85. package/dist/hooks/useTheme.d.ts.map +1 -0
  86. package/dist/hooks/useTheme.js +102 -0
  87. package/dist/hooks/useTranslation.d.ts +106 -0
  88. package/dist/hooks/useTranslation.d.ts.map +1 -0
  89. package/dist/hooks/useTranslation.js +159 -0
  90. package/dist/hooks/useVerifyPaymentReturn.d.ts +91 -0
  91. package/dist/hooks/useVerifyPaymentReturn.d.ts.map +1 -0
  92. package/dist/hooks/useVerifyPaymentReturn.js +189 -0
  93. package/dist/hooks/useVersion.d.ts +53 -0
  94. package/dist/hooks/useVersion.d.ts.map +1 -0
  95. package/dist/hooks/useVersion.js +70 -0
  96. package/dist/hooks/useWorkspace.d.ts +47 -0
  97. package/dist/hooks/useWorkspace.d.ts.map +1 -0
  98. package/dist/hooks/useWorkspace.js +68 -0
  99. package/dist/index.d.ts +2 -0
  100. package/dist/index.d.ts.map +1 -1
  101. package/dist/index.js +2 -1
  102. package/dist/session-ready/SessionReady.svelte +20 -0
  103. package/dist/session-ready/SessionReady.svelte.d.ts +5 -0
  104. package/dist/session-ready/SessionReady.svelte.d.ts.map +1 -0
  105. package/dist/session-ready/index.d.ts +7 -0
  106. package/dist/session-ready/index.d.ts.map +1 -0
  107. package/dist/session-ready/index.js +6 -0
  108. package/dist/stores/async-state.js +0 -1
  109. package/dist/stores/auth.js +0 -1
  110. package/dist/stores/capacitor-app.js +0 -1
  111. package/dist/stores/change-password.js +0 -1
  112. package/dist/stores/chat.d.ts.map +1 -1
  113. package/dist/stores/chat.js +3 -10
  114. package/dist/stores/device.js +0 -1
  115. package/dist/stores/forms.js +0 -1
  116. package/dist/stores/http.js +0 -1
  117. package/dist/stores/i18n.d.ts +3 -2
  118. package/dist/stores/i18n.d.ts.map +1 -1
  119. package/dist/stores/i18n.js +2 -3
  120. package/dist/stores/index.js +0 -1
  121. package/dist/stores/logger.js +0 -1
  122. package/dist/stores/login.js +0 -1
  123. package/dist/stores/oauth.js +0 -1
  124. package/dist/stores/password-reset.js +0 -1
  125. package/dist/stores/platform.js +0 -1
  126. package/dist/stores/promise.js +0 -1
  127. package/dist/stores/push.js +0 -1
  128. package/dist/stores/router.js +0 -1
  129. package/dist/stores/signup.js +0 -1
  130. package/dist/stores/state.js +0 -1
  131. package/dist/stores/storage.js +0 -1
  132. package/dist/stores/theme.js +0 -1
  133. package/dist/stores/version.js +0 -1
  134. package/dist/types.d.ts +20 -0
  135. package/dist/types.d.ts.map +1 -1
  136. package/dist/types.js +0 -1
  137. package/package.json +13 -8
  138. package/dist/agent-identity.js.map +0 -1
  139. package/dist/context.js.map +0 -1
  140. package/dist/index.js.map +0 -1
  141. package/dist/stores/async-state.js.map +0 -1
  142. package/dist/stores/auth.js.map +0 -1
  143. package/dist/stores/capacitor-app.js.map +0 -1
  144. package/dist/stores/change-password.js.map +0 -1
  145. package/dist/stores/chat.js.map +0 -1
  146. package/dist/stores/device.js.map +0 -1
  147. package/dist/stores/forms.js.map +0 -1
  148. package/dist/stores/http.js.map +0 -1
  149. package/dist/stores/i18n.js.map +0 -1
  150. package/dist/stores/index.js.map +0 -1
  151. package/dist/stores/logger.js.map +0 -1
  152. package/dist/stores/login.js.map +0 -1
  153. package/dist/stores/oauth.js.map +0 -1
  154. package/dist/stores/password-reset.js.map +0 -1
  155. package/dist/stores/platform.js.map +0 -1
  156. package/dist/stores/promise.js.map +0 -1
  157. package/dist/stores/push.js.map +0 -1
  158. package/dist/stores/router.js.map +0 -1
  159. package/dist/stores/signup.js.map +0 -1
  160. package/dist/stores/state.js.map +0 -1
  161. package/dist/stores/storage.js.map +0 -1
  162. package/dist/stores/theme.js.map +0 -1
  163. package/dist/stores/version.js.map +0 -1
  164. package/dist/types.js.map +0 -1
@@ -0,0 +1,228 @@
1
+ /**
2
+ * Svelte hooks for storage, porting React's `useStorage` family.
3
+ *
4
+ * @module
5
+ */
6
+ import { onDestroy } from 'svelte';
7
+ import { writable } from 'svelte/store';
8
+ import { getPreferenceStore } from '@molecule/app-storage';
9
+ import { useStorageProvider } from '../context.js';
10
+ /**
11
+ * One storage key as Svelte stores, React's `useStorageValue` in Svelte's
12
+ * idiom: call it during component init under `setMoleculeContext({ storage })`.
13
+ *
14
+ * A read that started before a newer local write keeps that write's value (it
15
+ * still ends `loading`), and with `sync` an outside write reaches the hook
16
+ * through the provider's own subscription — so a non-browser backend
17
+ * (native, encrypted, memory) propagates the same way, not just the browser
18
+ * `storage` event.
19
+ *
20
+ * @param key - Storage key
21
+ * @param options - Hook options
22
+ * @returns Storage value state (value, loading, error) and mutators (setValue, removeValue).
23
+ * @example
24
+ * ```svelte
25
+ * <script>
26
+ * import { useStorageValue } from '@molecule/app-svelte'
27
+ * const { value: theme, setValue: setTheme, loading } = useStorageValue('theme', {
28
+ * defaultValue: 'light',
29
+ * })
30
+ * </script>
31
+ *
32
+ * {#if $loading}<Spinner />
33
+ * {:else}<select onchange={(e) => setTheme(e.currentTarget.value)}>…</select>{/if}
34
+ * ```
35
+ */
36
+ export function useStorageValue(key, options) {
37
+ const storage = useStorageProvider();
38
+ const { defaultValue, sync = false } = options ?? {};
39
+ const value = writable(defaultValue);
40
+ const loading = writable(true);
41
+ const error = writable(null);
42
+ // Incremented by every local write. A read that started before it must not
43
+ // replace the newer value — the user editing before the first read lands is
44
+ // the common case on a slow (native, encrypted) backend.
45
+ let writeVersion = 0;
46
+ // The latest `defaultValue`, held like React's ref so a later options object
47
+ // (an inline default) keeps working without re-reading storage.
48
+ const currentDefault = defaultValue;
49
+ let mounted = true;
50
+ onDestroy(() => {
51
+ mounted = false;
52
+ });
53
+ // Load the key's value (React's mount effect).
54
+ const loadValue = async () => {
55
+ const version = writeVersion;
56
+ try {
57
+ const stored = await storage.get(key);
58
+ if (!mounted)
59
+ return;
60
+ // A write made after this read started holds the newer value; the read still ends loading.
61
+ if (writeVersion === version)
62
+ value.set((stored ?? currentDefault));
63
+ loading.set(false);
64
+ }
65
+ catch (err) {
66
+ if (!mounted)
67
+ return;
68
+ if (writeVersion === version) {
69
+ error.set(err instanceof Error ? err : new Error(String(err)));
70
+ }
71
+ loading.set(false);
72
+ }
73
+ };
74
+ void loadValue();
75
+ // Sync across tabs if supported — through the PROVIDER's own subscription, so
76
+ // a non-browser backend (native, encrypted, memory) propagates the same way
77
+ // instead of being limited to the browser `storage` event.
78
+ let stopSync = null;
79
+ if (sync && storage.subscribe) {
80
+ stopSync = storage.subscribe(key, (next) => {
81
+ writeVersion += 1;
82
+ value.set(next === null ? currentDefault : next);
83
+ });
84
+ onDestroy(() => {
85
+ stopSync?.();
86
+ stopSync = null;
87
+ });
88
+ }
89
+ const setValue = async (newValue) => {
90
+ writeVersion += 1;
91
+ try {
92
+ await storage.set(key, newValue);
93
+ value.set(newValue);
94
+ error.set(null);
95
+ }
96
+ catch (err) {
97
+ error.set(err instanceof Error ? err : new Error(String(err)));
98
+ throw err;
99
+ }
100
+ };
101
+ const removeValue = async () => {
102
+ writeVersion += 1;
103
+ try {
104
+ await storage.remove(key);
105
+ value.set(currentDefault);
106
+ error.set(null);
107
+ }
108
+ catch (err) {
109
+ error.set(err instanceof Error ? err : new Error(String(err)));
110
+ throw err;
111
+ }
112
+ };
113
+ return {
114
+ value: { subscribe: value.subscribe },
115
+ setValue,
116
+ removeValue,
117
+ loading: { subscribe: loading.subscribe },
118
+ error: { subscribe: error.subscribe },
119
+ };
120
+ }
121
+ /**
122
+ * Subscribes Svelte stores to ONE persisted preference, React's
123
+ * `useStoragePreference` in Svelte's idiom.
124
+ *
125
+ * This is the component-side half of `getPreferenceStore()`: an init-time
126
+ * value is available immediately when the bonded provider can answer
127
+ * synchronously, and an asynchronous provider's hydration CONVERGES onto the
128
+ * same component without a remount. Ordering is the store's: a read that
129
+ * started before a newer write, remove or external change is discarded, so a
130
+ * slow hydration never overwrites an edit the user already made.
131
+ *
132
+ * Pass `null` for `key` while the real key is unknown (no project selected
133
+ * yet): the hook then behaves as local state around `defaultValue` and
134
+ * persists nothing.
135
+ *
136
+ * @param key - The storage key, or `null` when there is nothing to persist.
137
+ * @param options - `defaultValue`, reported until the provider supplies one.
138
+ * @returns The current value, its mutators, and whether it is definite yet.
139
+ * @example
140
+ * ```svelte
141
+ * <script>
142
+ * import { useStoragePreference } from '@molecule/app-svelte'
143
+ * const { value: width, setValue: setWidth, ready } = useStoragePreference(key, {
144
+ * defaultValue: 280,
145
+ * })
146
+ * </script>
147
+ * ```
148
+ */
149
+ export function useStoragePreference(key, options) {
150
+ const store = getPreferenceStore();
151
+ const defaultValue = options?.defaultValue;
152
+ const read = () => {
153
+ if (!key)
154
+ return defaultValue;
155
+ const stored = store.get(key);
156
+ return stored === null ? defaultValue : stored;
157
+ };
158
+ const initial = read();
159
+ const value = writable(initial);
160
+ const ready = writable(!key);
161
+ // The value `setValue`'s functional form receives, kept beside the store.
162
+ let currentValue = initial;
163
+ if (!key) {
164
+ ready.set(true);
165
+ value.set(defaultValue);
166
+ currentValue = defaultValue;
167
+ }
168
+ else {
169
+ let cancelled = false;
170
+ const sync = () => {
171
+ if (!cancelled) {
172
+ const next = read();
173
+ value.set(next);
174
+ currentValue = next;
175
+ }
176
+ };
177
+ // Seed from whatever the store already knows, then let hydration converge
178
+ // it. With a synchronous provider this is a no-op change.
179
+ sync();
180
+ ready.set(false);
181
+ const stop = store.subscribe(key, sync);
182
+ onDestroy(() => {
183
+ cancelled = true;
184
+ stop();
185
+ });
186
+ void store.hydrate([key]).then(() => {
187
+ if (cancelled)
188
+ return;
189
+ sync();
190
+ ready.set(true);
191
+ });
192
+ }
193
+ const setValue = (next) => {
194
+ const resolved = typeof next === 'function' ? next(currentValue) : next;
195
+ currentValue = resolved;
196
+ value.set(resolved);
197
+ if (key)
198
+ store.set(key, resolved);
199
+ };
200
+ const removeValue = () => {
201
+ currentValue = defaultValue;
202
+ value.set(defaultValue);
203
+ if (key)
204
+ store.remove(key);
205
+ };
206
+ return {
207
+ value: { subscribe: value.subscribe },
208
+ setValue,
209
+ removeValue,
210
+ ready: { subscribe: ready.subscribe },
211
+ };
212
+ }
213
+ /**
214
+ * Storage operations without local state sync, React's `useStorage` in
215
+ * Svelte's idiom: plain passthroughs of the context provider.
216
+ *
217
+ * @returns Storage operation methods
218
+ */
219
+ export function useStorage() {
220
+ const storage = useStorageProvider();
221
+ return {
222
+ get: (key) => storage.get(key),
223
+ set: (key, value) => storage.set(key, value),
224
+ remove: (key) => storage.remove(key),
225
+ clear: () => storage.clear(),
226
+ keys: () => storage.keys(),
227
+ };
228
+ }
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Svelte hooks for state stores, porting React's `useStore` family.
3
+ *
4
+ * @module
5
+ */
6
+ import type { Readable } from 'svelte/store';
7
+ import type { Store } from '@molecule/app-state';
8
+ /** Options for {@link useStore}. */
9
+ export interface UseStoreOptions<T, S> {
10
+ /** Selects part of the state. */
11
+ selector?: (state: T) => S;
12
+ /** Compares two selected values; equal keeps the previous result. */
13
+ equalityFn?: (a: S, b: S) => boolean;
14
+ }
15
+ /**
16
+ * Subscribes a store with an optional selector and equality function, React's
17
+ * `useStore` in Svelte's idiom: a readable store of the selected value.
18
+ *
19
+ * The store is the argument, so this hook needs NO state provider: it reads
20
+ * `store.getState()` and subscribes with `store.subscribe()`. When
21
+ * `equalityFn` says a new selected value equals the previous one, the previous
22
+ * result is kept (the same value, not a fresh object).
23
+ *
24
+ * The selector and equality function run with the same cadence as React's
25
+ * `useSyncExternalStore` binding, because React re-reads its snapshot around
26
+ * every render; {@link useSyncExternalStore} documents the cadence.
27
+ *
28
+ * @param store - The store to subscribe to.
29
+ * @param options - Hook options (selector, equalityFn).
30
+ * @returns A readable store of the selected state.
31
+ * @example
32
+ * ```svelte
33
+ * <script>
34
+ * import { useStore } from '@molecule/app-svelte'
35
+ * const count = useStore(counterStore, { selector: (s) => s.count })
36
+ * const doubled = useStore(counterStore, { selector: (s) => s.count * 2 })
37
+ * </script>
38
+ *
39
+ * <p>{$count} / {$doubled}</p>
40
+ * ```
41
+ */
42
+ export declare function useStore<T, S = T>(store: Store<T>, options?: UseStoreOptions<T, S>): Readable<S>;
43
+ /**
44
+ * The store's setState function, React's `useSetStore` in Svelte's idiom.
45
+ *
46
+ * @param store - The store to get setState from.
47
+ * @returns The store's setState function.
48
+ */
49
+ export declare function useSetStore<T>(store: Store<T>): Store<T>['setState'];
50
+ /**
51
+ * A bound action for a store, React's `useStoreAction` in Svelte's idiom.
52
+ *
53
+ * @param store - The store to bind to.
54
+ * @param action - The action function that receives setState and getState.
55
+ * @returns A bound action function.
56
+ * @example
57
+ * ```svelte
58
+ * <script>
59
+ * import { useStoreAction } from '@molecule/app-svelte'
60
+ * const increment = useStoreAction(counterStore, (setState, getState) => () => {
61
+ * setState({ count: getState().count + 1 })
62
+ * })
63
+ * </script>
64
+ *
65
+ * <button onclick={increment}>+1</button>
66
+ * ```
67
+ */
68
+ export declare function useStoreAction<T, Args extends unknown[], R>(store: Store<T>, action: (setState: Store<T>['setState'], getState: Store<T>['getState']) => (...args: Args) => R): (...args: Args) => R;
69
+ //# sourceMappingURL=useStore.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useStore.d.ts","sourceRoot":"","sources":["../../src/hooks/useStore.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AAE5C,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAA;AAIhD,oCAAoC;AACpC,MAAM,WAAW,eAAe,CAAC,CAAC,EAAE,CAAC;IACnC,iCAAiC;IACjC,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,CAAA;IAC1B,qEAAqE;IACrE,UAAU,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,OAAO,CAAA;CACrC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,eAAe,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAqBhG;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAEpE;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,IAAI,SAAS,OAAO,EAAE,EAAE,CAAC,EACzD,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,EACf,MAAM,EAAE,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,KAAK,CAAC,GAAG,IAAI,EAAE,IAAI,KAAK,CAAC,GAC/F,CAAC,GAAG,IAAI,EAAE,IAAI,KAAK,CAAC,CAGtB"}
@@ -0,0 +1,82 @@
1
+ /**
2
+ * Svelte hooks for state stores, porting React's `useStore` family.
3
+ *
4
+ * @module
5
+ */
6
+ import { useSyncExternalStore } from './external-store.js';
7
+ /**
8
+ * Subscribes a store with an optional selector and equality function, React's
9
+ * `useStore` in Svelte's idiom: a readable store of the selected value.
10
+ *
11
+ * The store is the argument, so this hook needs NO state provider: it reads
12
+ * `store.getState()` and subscribes with `store.subscribe()`. When
13
+ * `equalityFn` says a new selected value equals the previous one, the previous
14
+ * result is kept (the same value, not a fresh object).
15
+ *
16
+ * The selector and equality function run with the same cadence as React's
17
+ * `useSyncExternalStore` binding, because React re-reads its snapshot around
18
+ * every render; {@link useSyncExternalStore} documents the cadence.
19
+ *
20
+ * @param store - The store to subscribe to.
21
+ * @param options - Hook options (selector, equalityFn).
22
+ * @returns A readable store of the selected state.
23
+ * @example
24
+ * ```svelte
25
+ * <script>
26
+ * import { useStore } from '@molecule/app-svelte'
27
+ * const count = useStore(counterStore, { selector: (s) => s.count })
28
+ * const doubled = useStore(counterStore, { selector: (s) => s.count * 2 })
29
+ * </script>
30
+ *
31
+ * <p>{$count} / {$doubled}</p>
32
+ * ```
33
+ */
34
+ export function useStore(store, options) {
35
+ const { selector, equalityFn } = options ?? {};
36
+ // The previous selected value (React's prevSelectedRef): while an equality
37
+ // function accepts it, it stays the result, so its identity is stable.
38
+ let previous;
39
+ return useSyncExternalStore((onChange) => store.subscribe(onChange), () => {
40
+ const state = store.getState();
41
+ const selected = selector ? selector(state) : state;
42
+ // With an equality function and a previous value, an equal selection
43
+ // keeps the previous result — the same value, not a fresh object.
44
+ if (equalityFn && previous !== undefined) {
45
+ if (equalityFn(previous, selected))
46
+ return previous;
47
+ }
48
+ previous = selected;
49
+ return selected;
50
+ });
51
+ }
52
+ /**
53
+ * The store's setState function, React's `useSetStore` in Svelte's idiom.
54
+ *
55
+ * @param store - The store to get setState from.
56
+ * @returns The store's setState function.
57
+ */
58
+ export function useSetStore(store) {
59
+ return store.setState;
60
+ }
61
+ /**
62
+ * A bound action for a store, React's `useStoreAction` in Svelte's idiom.
63
+ *
64
+ * @param store - The store to bind to.
65
+ * @param action - The action function that receives setState and getState.
66
+ * @returns A bound action function.
67
+ * @example
68
+ * ```svelte
69
+ * <script>
70
+ * import { useStoreAction } from '@molecule/app-svelte'
71
+ * const increment = useStoreAction(counterStore, (setState, getState) => () => {
72
+ * setState({ count: getState().count + 1 })
73
+ * })
74
+ * </script>
75
+ *
76
+ * <button onclick={increment}>+1</button>
77
+ * ```
78
+ */
79
+ export function useStoreAction(store, action) {
80
+ const bound = action(store.setState, store.getState);
81
+ return (...args) => bound(...args);
82
+ }
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Svelte hook for theming, porting React's `useTheme` family.
3
+ *
4
+ * @module
5
+ */
6
+ import { type Readable } from 'svelte/store';
7
+ import type { Theme } from '@molecule/app-theme';
8
+ /** What {@link useTheme} returns. */
9
+ export interface UseThemeResult {
10
+ /** The current theme object. */
11
+ theme: Readable<Theme>;
12
+ /** The current theme's name. */
13
+ themeName: Readable<string>;
14
+ /** Switches the active theme by name. */
15
+ setTheme: (name: string) => void;
16
+ /** Toggles light/dark (the provider's `toggleMode`, or an index cycle). */
17
+ toggleTheme: () => void;
18
+ /** The current mode: 'light' or 'dark'. */
19
+ mode: Readable<'light' | 'dark'>;
20
+ }
21
+ /**
22
+ * Theme state and actions, React's `useTheme` in Svelte's idiom: call it
23
+ * during component init under `setMoleculeContext({ theme })`.
24
+ *
25
+ * @returns Theme state as stores and the actions as functions.
26
+ * @example
27
+ * ```svelte
28
+ * <script>
29
+ * import { useTheme } from '@molecule/app-svelte'
30
+ * const { themeName, toggleTheme } = useTheme()
31
+ * </script>
32
+ *
33
+ * <button onclick={toggleTheme}>Current theme: {$themeName}</button>
34
+ * ```
35
+ */
36
+ export declare function useTheme(): UseThemeResult;
37
+ /**
38
+ * The current theme object, React's `useCurrentTheme` in Svelte's idiom.
39
+ *
40
+ * @returns A readable store of the current theme.
41
+ */
42
+ export declare function useCurrentTheme(): Readable<Theme>;
43
+ /**
44
+ * The current theme mode, React's `useThemeMode` in Svelte's idiom.
45
+ *
46
+ * @returns A readable store of 'light' or 'dark'.
47
+ */
48
+ export declare function useThemeMode(): Readable<'light' | 'dark'>;
49
+ /**
50
+ * The current theme's colors, React's `useThemeColors` in Svelte's idiom.
51
+ *
52
+ * @returns A readable store of the current theme's colors.
53
+ */
54
+ export declare function useThemeColors(): Readable<Theme['colors']>;
55
+ //# sourceMappingURL=useTheme.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useTheme.d.ts","sourceRoot":"","sources":["../../src/hooks/useTheme.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAW,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAA;AAErD,OAAO,KAAK,EAAE,KAAK,EAAiB,MAAM,qBAAqB,CAAA;AAK/D,qCAAqC;AACrC,MAAM,WAAW,cAAc;IAC7B,gCAAgC;IAChC,KAAK,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAA;IACtB,gCAAgC;IAChC,SAAS,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAA;IAC3B,yCAAyC;IACzC,QAAQ,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAA;IAChC,2EAA2E;IAC3E,WAAW,EAAE,MAAM,IAAI,CAAA;IACvB,2CAA2C;IAC3C,IAAI,EAAE,QAAQ,CAAC,OAAO,GAAG,MAAM,CAAC,CAAA;CACjC;AAqBD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,QAAQ,IAAI,cAAc,CAwCzC;AAED;;;;GAIG;AACH,wBAAgB,eAAe,IAAI,QAAQ,CAAC,KAAK,CAAC,CAGjD;AAED;;;;GAIG;AACH,wBAAgB,YAAY,IAAI,QAAQ,CAAC,OAAO,GAAG,MAAM,CAAC,CAGzD;AAED;;;;GAIG;AACH,wBAAgB,cAAc,IAAI,QAAQ,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAG1D"}
@@ -0,0 +1,102 @@
1
+ /**
2
+ * Svelte hook for theming, porting React's `useTheme` family.
3
+ *
4
+ * @module
5
+ */
6
+ import { derived } from 'svelte/store';
7
+ import { useThemeProvider } from '../context.js';
8
+ import { fieldStore, liveStore } from './live-store.js';
9
+ /**
10
+ * Subscribe to a theme provider, supporting both `subscribe` and the legacy
11
+ * `onThemeChange` methods.
12
+ *
13
+ * @param provider - The theme provider to subscribe to.
14
+ * @param callback - Listener invoked whenever the active theme changes.
15
+ * @returns An unsubscribe function that removes the listener.
16
+ */
17
+ function subscribeToThemeProvider(provider, callback) {
18
+ if (typeof provider.subscribe === 'function') {
19
+ return provider.subscribe(callback);
20
+ }
21
+ const legacy = provider;
22
+ if (typeof legacy.onThemeChange === 'function') {
23
+ return legacy.onThemeChange(callback);
24
+ }
25
+ return () => { };
26
+ }
27
+ /**
28
+ * Theme state and actions, React's `useTheme` in Svelte's idiom: call it
29
+ * during component init under `setMoleculeContext({ theme })`.
30
+ *
31
+ * @returns Theme state as stores and the actions as functions.
32
+ * @example
33
+ * ```svelte
34
+ * <script>
35
+ * import { useTheme } from '@molecule/app-svelte'
36
+ * const { themeName, toggleTheme } = useTheme()
37
+ * </script>
38
+ *
39
+ * <button onclick={toggleTheme}>Current theme: {$themeName}</button>
40
+ * ```
41
+ */
42
+ export function useTheme() {
43
+ const provider = useThemeProvider();
44
+ // `getThemeName()` is not part of `ThemeProvider`; probe it the way React
45
+ // does, so a provider that predates the theme object's `name` still works.
46
+ const providerWithName = provider;
47
+ const readName = () => typeof providerWithName.getThemeName === 'function'
48
+ ? providerWithName.getThemeName()
49
+ : provider.getTheme().name;
50
+ const state = liveStore(() => ({ theme: provider.getTheme(), name: readName() }), (notify) => subscribeToThemeProvider(provider, notify));
51
+ const toggleTheme = () => {
52
+ // Prefer the provider's purpose-built light/dark toggle — unlike
53
+ // `getThemes()`, which is optional, so the index-cycle below silently
54
+ // no-ops for providers that don't implement it. The index-cycle is kept
55
+ // only as a fallback for non-conforming providers that predate it.
56
+ if (typeof provider.toggleMode === 'function') {
57
+ provider.toggleMode();
58
+ return;
59
+ }
60
+ const themes = provider.getThemes?.() ?? [];
61
+ if (themes.length === 0)
62
+ return;
63
+ const currentName = readName();
64
+ const currentIndex = themes.findIndex((entry) => typeof entry === 'string' ? entry === currentName : entry.name === currentName);
65
+ const next = themes[(currentIndex + 1) % themes.length];
66
+ provider.setTheme(typeof next === 'string' ? next : next.name);
67
+ };
68
+ return {
69
+ theme: fieldStore(state, 'theme'),
70
+ themeName: fieldStore(state, 'name'),
71
+ setTheme: (name) => provider.setTheme(name),
72
+ toggleTheme,
73
+ mode: derived(state, (current) => current.theme.mode),
74
+ };
75
+ }
76
+ /**
77
+ * The current theme object, React's `useCurrentTheme` in Svelte's idiom.
78
+ *
79
+ * @returns A readable store of the current theme.
80
+ */
81
+ export function useCurrentTheme() {
82
+ const { theme } = useTheme();
83
+ return theme;
84
+ }
85
+ /**
86
+ * The current theme mode, React's `useThemeMode` in Svelte's idiom.
87
+ *
88
+ * @returns A readable store of 'light' or 'dark'.
89
+ */
90
+ export function useThemeMode() {
91
+ const { mode } = useTheme();
92
+ return mode;
93
+ }
94
+ /**
95
+ * The current theme's colors, React's `useThemeColors` in Svelte's idiom.
96
+ *
97
+ * @returns A readable store of the current theme's colors.
98
+ */
99
+ export function useThemeColors() {
100
+ const { theme } = useTheme();
101
+ return derived(theme, (current) => current.colors);
102
+ }
@@ -0,0 +1,106 @@
1
+ /**
2
+ * Svelte hook for internationalization, porting React's `useTranslation`
3
+ * family: the locale-reactive values as readable stores, the translation and
4
+ * formatting functions as plain functions.
5
+ *
6
+ * @module
7
+ */
8
+ import { type Readable } from 'svelte/store';
9
+ import type { I18nProvider, TranslateOptions } from '@molecule/app-i18n';
10
+ /**
11
+ * Options for the shorthand form of {@link UseTFunction}: the fallback text
12
+ * is passed positionally, so only `count` remains here.
13
+ */
14
+ export type UseTShorthandOptions = Omit<TranslateOptions, 'defaultValue'>;
15
+ /**
16
+ * Translation function returned by `useT()`.
17
+ *
18
+ * Two compatible call forms:
19
+ * - `t(key, values?, options?)` — the provider's signature.
20
+ * - `t(key, fallback, options?)` — a string second argument is the inline
21
+ * English `defaultValue`, with optional `count` for pluralization.
22
+ */
23
+ export type UseTFunction = I18nProvider['t'] & {
24
+ (key: string, fallback: string, options?: UseTShorthandOptions): string;
25
+ };
26
+ /**
27
+ * What {@link useTranslation} returns: the locale-reactive values as readable
28
+ * stores and the provider's functions as plain functions.
29
+ */
30
+ export interface UseTranslationResult {
31
+ /**
32
+ * Translates a key in the active locale. A call made while markup, `$derived` or an
33
+ * effect evaluates re-runs on every locale switch (like React's `t`, which is
34
+ * rebuilt on every locale change), so `{t('key', {}, { defaultValue })}` needs no
35
+ * store read beside it.
36
+ */
37
+ t: I18nProvider['t'];
38
+ /** The active locale code. */
39
+ locale: Readable<string>;
40
+ /** Switches the active locale. */
41
+ setLocale: I18nProvider['setLocale'];
42
+ /** The registered locales. */
43
+ locales: Readable<ReturnType<I18nProvider['getLocales']>>;
44
+ /** Formats a number for the active locale. */
45
+ formatNumber: I18nProvider['formatNumber'];
46
+ /** Formats a date for the active locale. */
47
+ formatDate: I18nProvider['formatDate'];
48
+ /** The active locale's text direction. */
49
+ direction: Readable<'ltr' | 'rtl'>;
50
+ }
51
+ /**
52
+ * Internationalization, React's `useTranslation` in Svelte's idiom: call it
53
+ * during component init under `setMoleculeContext({ i18n })`.
54
+ *
55
+ * @returns The translation function and the locale values as stores.
56
+ * @example
57
+ * ```svelte
58
+ * <script>
59
+ * import { useTranslation } from '@molecule/app-svelte'
60
+ * const { t, locale } = useTranslation()
61
+ * </script>
62
+ *
63
+ * <h1>{t('welcome.title', { name: 'User' })}</h1>
64
+ * <select value={$locale} onchange={(e) => setLocale(e.currentTarget.value)}>
65
+ * ```
66
+ */
67
+ export declare function useTranslation(): UseTranslationResult;
68
+ /**
69
+ * The translation function alone, React's `useT` in Svelte's idiom.
70
+ *
71
+ * Call it in either of two compatible forms:
72
+ * - `t(key, values?, options?)` — the provider signature.
73
+ * - `t(key, fallback, options?)` — a string second argument is the inline
74
+ * English `defaultValue`.
75
+ *
76
+ * @returns The translation function.
77
+ */
78
+ export declare function useT(): UseTFunction;
79
+ /**
80
+ * The current locale, React's `useLocale` in Svelte's idiom.
81
+ *
82
+ * @returns A readable store of the locale code.
83
+ */
84
+ export declare function useLocale(): Readable<string>;
85
+ /**
86
+ * The text direction, React's `useDirection` in Svelte's idiom.
87
+ *
88
+ * @returns A readable store of 'ltr' or 'rtl'.
89
+ */
90
+ export declare function useDirection(): Readable<'ltr' | 'rtl'>;
91
+ /**
92
+ * Translates an error at render time so the displayed message updates
93
+ * automatically when the locale changes, React's `useI18nError` in Svelte's
94
+ * idiom: a readable store of the translated message.
95
+ *
96
+ * **Always use this hook to display errors** — reading `error.message`
97
+ * directly leaves stale text after a locale switch. If `error` is an
98
+ * `I18nError` (thrown via `throw new I18nError(key)`), its key is translated
99
+ * using the current locale. For plain `Error` instances, `error.message` is
100
+ * returned unchanged.
101
+ *
102
+ * @param error - The error to translate, or `null`/`undefined`.
103
+ * @returns A readable store of the translated error string, or null.
104
+ */
105
+ export declare function useI18nError(error: Error | null | undefined): Readable<string | null>;
106
+ //# sourceMappingURL=useTranslation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useTranslation.d.ts","sourceRoot":"","sources":["../../src/hooks/useTranslation.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,EAAsB,KAAK,QAAQ,EAAY,MAAM,cAAc,CAAA;AAE1E,OAAO,KAAK,EAEV,YAAY,EAGZ,gBAAgB,EACjB,MAAM,oBAAoB,CAAA;AAK3B;;;GAGG;AACH,MAAM,MAAM,oBAAoB,GAAG,IAAI,CAAC,gBAAgB,EAAE,cAAc,CAAC,CAAA;AAEzE;;;;;;;GAOG;AACH,MAAM,MAAM,YAAY,GAAG,YAAY,CAAC,GAAG,CAAC,GAAG;IAC7C,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,oBAAoB,GAAG,MAAM,CAAA;CACxE,CAAA;AAED;;;GAGG;AACH,MAAM,WAAW,oBAAoB;IACnC;;;;;OAKG;IACH,CAAC,EAAE,YAAY,CAAC,GAAG,CAAC,CAAA;IACpB,8BAA8B;IAC9B,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAA;IACxB,kCAAkC;IAClC,SAAS,EAAE,YAAY,CAAC,WAAW,CAAC,CAAA;IACpC,8BAA8B;IAC9B,OAAO,EAAE,QAAQ,CAAC,UAAU,CAAC,YAAY,CAAC,YAAY,CAAC,CAAC,CAAC,CAAA;IACzD,8CAA8C;IAC9C,YAAY,EAAE,YAAY,CAAC,cAAc,CAAC,CAAA;IAC1C,4CAA4C;IAC5C,UAAU,EAAE,YAAY,CAAC,YAAY,CAAC,CAAA;IACtC,0CAA0C;IAC1C,SAAS,EAAE,QAAQ,CAAC,KAAK,GAAG,KAAK,CAAC,CAAA;CACnC;AAqDD;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,cAAc,IAAI,oBAAoB,CAmBrD;AAED;;;;;;;;;GASG;AACH,wBAAgB,IAAI,IAAI,YAAY,CAenC;AAED;;;;GAIG;AACH,wBAAgB,SAAS,IAAI,QAAQ,CAAC,MAAM,CAAC,CAG5C;AAED;;;;GAIG;AACH,wBAAgB,YAAY,IAAI,QAAQ,CAAC,KAAK,GAAG,KAAK,CAAC,CAGtD;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,GAAG,SAAS,GAAG,QAAQ,CAAC,MAAM,GAAG,IAAI,CAAC,CAYrF"}