@but212/atom-effect-jquery 0.22.1 → 0.23.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/dist/index.d.ts CHANGED
@@ -17,9 +17,10 @@ export { batch }
17
17
 
18
18
  /**
19
19
  * Configuration options for `atomBind`.
20
+ * @template T Type of the value for two-way binding (`val` field).
20
21
  */
21
- export declare interface BindingOptions {
22
- /** Binds textContent to any reactive source (usually string/number). */
22
+ export declare interface BindingOptions<T = unknown> {
23
+ /** Binds textContent to any reactive source. */
23
24
  text?: ReactiveValue<unknown>;
24
25
  /** Binds innerHTML to a reactive string source (sanitized). */
25
26
  html?: ReactiveValue<string>;
@@ -37,9 +38,9 @@ export declare interface BindingOptions {
37
38
  hide?: ReactiveValue<boolean>;
38
39
  /**
39
40
  * Two-way binding for input values.
40
- * Pass a bare atom or a `[atom, options]` tuple to customise parse/format/debounce.
41
+ * Pass an atom or a `[atom, options]` tuple.
41
42
  */
42
- val?: WritableAtom<unknown> | [atom: WritableAtom<unknown>, options: ValOptions<unknown>];
43
+ val?: WritableAtom<T> | [atom: WritableAtom<T>, options: ValOptions<T>];
43
44
  /** Two-way binding for checkboxes and radio buttons. */
44
45
  checked?: WritableAtom<boolean>;
45
46
  /** Event listeners with automatic batched execution and lifecycle-bound cleanup. */
@@ -67,18 +68,17 @@ declare class BindingRegistry {
67
68
  trackCleanup(el: Element, fn: () => void): void;
68
69
  setComponentCleanup(el: Element, fn: (() => void) | undefined): void;
69
70
  hasBind(el: Element): boolean;
70
- cleanup(el: Element): void;
71
- cleanupDescendants(el: Element): void;
72
- cleanupTree(el: Element): void;
71
+ cleanup(el: Element | Node): void;
72
+ cleanupDescendants(el: Element | DocumentFragment | ShadowRoot): void;
73
+ cleanupTree(el: Element | Node): void;
73
74
  }
74
75
 
75
76
  /**
76
- * Functional Component type.
77
77
  * A function that initializes logic on a jQuery element and returns an optional cleanup function.
78
- * `P` defaults to `object` (empty props) use `P = Record<string, never>` for strictly no-props
79
- * components.
78
+ * `P` defaults to `Record<string, unknown>` for convenience. Use `P = Record<string, never>`
79
+ * for strictly no-props components.
80
80
  */
81
- export declare type ComponentFn<P = object> = ($el: JQuery, props: P) => EffectResult;
81
+ export declare type ComponentFn<P = Record<string, unknown>> = ($el: JQuery, props: P) => EffectResult;
82
82
 
83
83
  export { computed }
84
84
 
@@ -91,18 +91,13 @@ export declare type CssBindings = Record<string, CssValue>;
91
91
 
92
92
  /**
93
93
  * CSS value: either a direct reactive value or a numeric tuple of [source, unit].
94
- *
95
- * The tuple form `[source, unit]` only accepts numeric sources because appending
96
- * a unit suffix to a string value (e.g. `"100%" + "px"`) is semantically
97
- * meaningless. Use `ReactiveValue<string>` directly when the full CSS value is
98
- * already a string (e.g. `fontFamilyAtom`).
99
94
  */
100
95
  export declare type CssValue = ReactiveValue<string | number> | [source: ReactiveValue<number>, unit: string];
101
96
 
102
97
  export default default_2;
103
98
 
104
99
  /**
105
- * Stops the MutationObserver started by `enableAutoCleanup`.
100
+ * Stops all MutationObservers started by `enableAutoCleanup`.
106
101
  */
107
102
  export declare function disableAutoCleanup(): void;
108
103
 
@@ -121,6 +116,7 @@ export declare type EffectCleanup = () => void;
121
116
 
122
117
  /**
123
118
  * Result of a reactive factory or component mount.
119
+ * Returns `void` (no cleanup) or an `EffectCleanup` function.
124
120
  */
125
121
  export declare type EffectResult = undefined | EffectCleanup;
126
122
 
@@ -128,13 +124,11 @@ export declare type EffectResult = undefined | EffectCleanup;
128
124
  * Starts observing `root` for removed elements and automatically disposes
129
125
  * their reactive bindings when they leave the DOM.
130
126
  *
127
+ * Multiple roots can be observed concurrently (e.g. for Micro-Frontends).
131
128
  * The `root` parameter is required (no default) to make the caller explicit
132
- * about which subtree is being observed — `document.body` can be null if the
133
- * script runs before the body is parsed.
129
+ * about which subtree is being observed.
134
130
  *
135
- * Idempotent: calling more than once with the same root before
136
- * `disableAutoCleanup` has no effect. Calling with a different root while
137
- * already active emits a warning and returns without re-observing.
131
+ * Idempotent: calling more than once with the same root has no effect.
138
132
  */
139
133
  export declare function enableAutoCleanup(root: Element): void;
140
134
 
@@ -160,48 +154,19 @@ export declare type EqualFn<T> = (a: T, b: T) => boolean;
160
154
  * Configuration options for `atomFetch`.
161
155
  */
162
156
  export declare interface FetchOptions<T> {
163
- /**
164
- * Value exposed by the atom before the first fetch resolves.
165
- * Also returned while a subsequent fetch is in flight.
166
- */
157
+ /** Initial value before the first fetch resolves. */
167
158
  defaultValue: T;
168
- /**
169
- * HTTP method forwarded to `$.ajax` (default: `'GET'`).
170
- * Takes precedence over the same field in `ajaxOptions`.
171
- * Accepts any string for non-standard methods; common values are
172
- * auto-completed: `'GET'`, `'POST'`, `'PUT'`, `'PATCH'`, `'DELETE'`.
173
- */
159
+ /** HTTP method (default: 'GET'). */
174
160
  method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS' | (string & {});
175
- /**
176
- * HTTP headers forwarded to `$.ajax`.
177
- * Takes precedence over the same field in `ajaxOptions`.
178
- */
161
+ /** HTTP headers. */
179
162
  headers?: Record<string, string>;
180
- /**
181
- * Transforms the raw `$.ajax` response into `T`.
182
- *
183
- * When omitted the raw response is cast to `T` with no runtime validation.
184
- * Provide this function whenever the server response shape is not
185
- * guaranteed to match `T` at runtime.
186
- */
163
+ /** Transforms the raw response into T. */
187
164
  transform?: (raw: unknown) => T;
188
- /**
189
- * Additional `$.ajax` settings.
190
- * Top-level fields (`url`, `method`, `headers`) always override the same
191
- * fields here, so avoid duplicating them to prevent silent conflicts.
192
- */
193
- ajaxOptions?: JQuery.AjaxSettings;
194
- /**
195
- * Called when the fetch fails with a non-abort error.
196
- * Receives the raw rejection value from `$.ajax`.
197
- * Does not suppress the error — the computed atom still enters its error
198
- * state and `hasError` becomes true.
199
- */
165
+ /** Additional `$.ajax` settings. Can be a getter function for reactive data tracking. */
166
+ ajaxOptions?: JQuery.AjaxSettings | (() => JQuery.AjaxSettings);
167
+ /** Error callback. */
200
168
  onError?: (err: unknown) => void;
201
- /**
202
- * When `true` (default), the first fetch starts immediately on creation.
203
- * When `false`, the fetch is deferred until `atom.value` is first accessed.
204
- */
169
+ /** Whether to fetch immediately (default: true). */
205
170
  eager?: boolean;
206
171
  }
207
172
 
@@ -218,26 +183,48 @@ export { isComputed }
218
183
  */
219
184
  export declare function isReactive(value: unknown): value is ReadonlyAtom<unknown>;
220
185
 
186
+ /**
187
+ * Helper to extract keys of T whose values extend V.
188
+ * Used to ensure `key` property refers to valid ID-like values.
189
+ */
190
+ declare type KeysOfType<T, V> = {
191
+ [K in keyof T]: T[K] extends V ? K : never;
192
+ }[keyof T];
193
+
194
+ /** Key type for Map/Set inside list.ts */
195
+ declare type ListKey = string | number;
196
+
197
+ /** Key extractor function signature. */
198
+ declare type ListKeyFn<T> = (item: T, index: number) => ListKey;
199
+
221
200
  /**
222
201
  * Configuration options for `atomList`.
223
202
  */
224
203
  export declare interface ListOptions<T> {
225
- /** Key to track items (property name or extractor function). */
226
- key: keyof T | ((item: T, index: number) => string | number);
204
+ /**
205
+ * Key to track items. Must be a property name whose value is a string|number,
206
+ * or a key extractor function.
207
+ */
208
+ key: KeysOfType<T, ListKey> | ListKeyFn<T>;
227
209
  /** Render function for each item. */
228
- render: (item: T, index: number) => string | Element | DocumentFragment | JQuery;
210
+ render: (item: T, index: number) => ListRenderResult;
229
211
  /** Optional post-render binding logic. */
230
212
  bind?: ($el: JQuery, item: T, index: number) => void;
231
213
  /** Optional update logic when item data changes but DOM is reused. */
232
214
  update?: ($el: JQuery, item: T, index: number) => void;
233
215
  /** Lifecycle hook: called when an element is added to the list. */
234
216
  onAdd?: ($el: JQuery) => void;
235
- /** Lifecycle hook: called when an element is about to be removed. Supports async transitions. */
217
+ /** Lifecycle hook: called when an element is about to be removed. */
236
218
  onRemove?: ($el: JQuery) => Promise<void> | void;
237
219
  /** Content to show when the list is empty. */
238
- empty?: string | Element | DocumentFragment | JQuery;
220
+ empty?: ListRenderResult;
221
+ /** Delegated event handlers attached to the container. */
222
+ events?: Record<string, (item: T, index: number, e: JQuery.TriggeredEvent) => void>;
239
223
  }
240
224
 
225
+ /** Possible return types for render() / empty */
226
+ declare type ListRenderResult = string | Element | DocumentFragment | JQuery;
227
+
241
228
  /**
242
229
  * Resolves after all pending microtask-scheduled reactive effects have flushed.
243
230
  *
@@ -267,8 +254,7 @@ export declare type PrimitiveValue = string | number | boolean | null | undefine
267
254
  * or a plain static value of the same type.
268
255
  *
269
256
  * `ComputedAtom<T>` is a structural sub-type of `ReadonlyAtom<T>`, so it is
270
- * already covered by `ReadonlyAtom<T>` — listing it separately would be
271
- * redundant and misleading.
257
+ * already covered by `ReadonlyAtom<T>`.
272
258
  */
273
259
  export declare type ReactiveValue<T> = T | ReadonlyAtom<T>;
274
260
 
@@ -276,123 +262,43 @@ export { ReadonlyAtom }
276
262
 
277
263
  export declare const registry: BindingRegistry;
278
264
 
279
- /**
280
- * Route that renders content via a custom function.
281
- */
282
265
  export declare interface RenderRoute extends RouteLifecycle {
283
- /** Custom render function providing full control over the container DOM. */
284
266
  render: (container: HTMLElement, route: string, params: Record<string, string>) => void;
285
267
  template?: never;
286
268
  }
287
269
 
288
- /**
289
- * Configuration for `$.route()`.
290
- */
291
270
  export declare interface RouteConfig {
292
- /** CSS selector of the element into which route content is rendered. */
293
271
  target: string;
294
- /** Route name used when the URL has no explicit route segment. */
295
272
  default: string;
296
- /** Map of route names to their definitions. */
297
273
  routes: Record<string, RouteDefinition>;
298
- /**
299
- * Routing strategy. Default: `'hash'`.
300
- * - `'hash'` — reads/writes `location.hash` (`#routeName`).
301
- * - `'history'` — reads/writes `location.pathname` via `history.pushState`.
302
- */
303
274
  mode?: 'hash' | 'history';
304
- /**
305
- * Path prefix stripped from `location.pathname` in history mode.
306
- * A trailing slash is normalized away internally.
307
- * Has no effect in hash mode.
308
- */
309
275
  basePath?: string;
310
- /** Route name to render when the requested route is not found (404 fallback). */
311
276
  notFound?: string;
312
- /**
313
- * When `true`, clicks on `[data-route]` elements are intercepted and
314
- * handled via `navigate()` instead of triggering a full page load.
315
- * Default: `false`.
316
- */
317
277
  autoBindLinks?: boolean;
318
- /**
319
- * CSS class added to `[data-route]` links that match the current route.
320
- * Also sets `aria-current="page"` on the active link.
321
- * Default: `'active'`.
322
- */
323
278
  activeClass?: string;
324
- /**
325
- * Called before each route transition.
326
- * `from` is `''` on the very first render (no previous route).
327
- */
328
279
  beforeTransition?: (from: string, to: string) => void;
329
- /**
330
- * Called after each route transition completes.
331
- * `from` is `''` on the very first render (no previous route).
332
- */
333
280
  afterTransition?: (from: string, to: string) => void;
334
281
  }
335
282
 
336
- /**
337
- * Route definition for a single route.
338
- * Exactly one of `template` or `render` must be provided.
339
- *
340
- * Use `isTemplateRoute` / `isRenderRoute` from `utils.ts` for safe narrowing
341
- * instead of direct property access.
342
- */
343
283
  export declare type RouteDefinition = TemplateRoute | RenderRoute;
344
284
 
345
- /**
346
- * Shared route lifecycle hooks available on every route definition.
347
- */
285
+ /** Shared route lifecycle hooks. */
348
286
  export declare interface RouteLifecycle {
349
- /**
350
- * Called when entering this route. May return additional params to merge
351
- * into the params object passed to `render` / `onMount`.
352
- */
353
287
  onEnter?: (params: Record<string, string>) => Record<string, string> | undefined;
354
- /**
355
- * Called when leaving this route.
356
- * Return `false` to block navigation; returning `void` (or nothing) allows it.
357
- */
358
288
  onLeave?: () => boolean | undefined;
359
- /** Called when the same route is re-activated with new query parameters. */
360
289
  onParamsChange?: (params: Record<string, string>) => void;
361
290
  }
362
291
 
363
- /**
364
- * Router instance returned by `$.route()`.
365
- *
366
- * `currentRoute` and `queryParams` reflect the current URL state reactively:
367
- * - In `'hash'` mode, `queryParams` is parsed from the query string after `?`
368
- * in the hash fragment (e.g., `#home?page=2` → `{ page: '2' }`).
369
- * - In `'history'` mode, `queryParams` is parsed from `location.search`.
370
- */
371
292
  export declare interface Router {
372
- /**
373
- * Reactive atom containing the current route name.
374
- * Read-only — use `navigate()` to change routes so that the URL stays in sync.
375
- */
376
293
  currentRoute: ReadonlyAtom<string>;
377
- /**
378
- * Reactive atom containing the current query parameters as a plain object.
379
- * Updated automatically on URL changes; reset to `{}` on programmatic navigation.
380
- */
381
294
  queryParams: ReadonlyAtom<Record<string, string>>;
382
- /** Navigate programmatically to the named route. */
383
295
  navigate: (route: string) => void;
384
- /** Destroy the router, removing all event listeners and reactive effects. */
385
296
  destroy: () => void;
386
297
  }
387
298
 
388
- /**
389
- * Route that renders content by cloning a `<template>` element.
390
- */
391
299
  export declare interface TemplateRoute extends RouteLifecycle {
392
- /** CSS selector for a `<template>` element (e.g., `'#tmpl-home'`). */
393
300
  template: string;
394
301
  render?: never;
395
- /** Called after template content is appended to the container. */
396
302
  onMount?: ($content: JQuery) => void;
397
303
  }
398
304