@faber1999/axon.js 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.
@@ -0,0 +1,463 @@
1
+ import { S as Signal, G as Getter, C as ComponentFn, J as JSXChild } from './jsx-KOQXGMp1.js';
2
+ export { F as Fragment, a as Setter, h } from './jsx-KOQXGMp1.js';
3
+
4
+ /**
5
+ * axon.js - Reactivity: signal
6
+ *
7
+ * A signal is a reactive value container.
8
+ * Reading it inside an effect creates a subscription.
9
+ * Writing it notifies all subscribers.
10
+ */
11
+
12
+ /**
13
+ * Creates a reactive signal.
14
+ *
15
+ * @param initialValue - The starting value.
16
+ * @returns A [getter, setter] tuple. Reading the getter inside an effect
17
+ * creates a subscription. Writing the setter notifies all subscribers.
18
+ *
19
+ * @example
20
+ * const [count, setCount] = signal(0)
21
+ * effect(() => console.log(count())) // logs 0
22
+ * setCount(1) // logs 1
23
+ */
24
+ declare function signal<T>(initialValue: T): Signal<T>;
25
+ /**
26
+ * Batches multiple signal writes into a single flush of effects.
27
+ * Prevents effects from running multiple times when several signals change together.
28
+ *
29
+ * @example
30
+ * batch(() => {
31
+ * setFirstName('Juan')
32
+ * setLastName('García')
33
+ * }) // effects run once, not twice
34
+ */
35
+ declare function batch(fn: () => void): void;
36
+
37
+ /**
38
+ * axon.js - Reactivity: effect
39
+ *
40
+ * A stack-based dependency tracking system.
41
+ * The currently executing effect is always at the top of the stack.
42
+ */
43
+
44
+ /**
45
+ * Creates a reactive effect that re-runs whenever any signal it reads changes.
46
+ * Returns a dispose function to stop tracking.
47
+ *
48
+ * @param fn - Effect function. May return a cleanup function.
49
+ * @returns dispose — call to stop the effect and unsubscribe from all signals.
50
+ */
51
+ declare function effect(fn: () => void | (() => void)): () => void;
52
+ /**
53
+ * Runs a function without tracking any signals.
54
+ * Reads inside will not subscribe the current effect.
55
+ */
56
+ declare function untrack<T>(fn: () => T): T;
57
+
58
+ /**
59
+ * axon.js - Reactivity: computed
60
+ *
61
+ * A derived, read-only reactive value.
62
+ * Like a signal, but its value is calculated from other signals.
63
+ */
64
+
65
+ /**
66
+ * Creates a computed (derived) signal.
67
+ * Re-evaluates automatically when its dependencies change.
68
+ * Returns a read-only getter — computed values cannot be set directly.
69
+ *
70
+ * @param fn - Pure derivation function. Should have no side effects.
71
+ * @returns A getter function. Reading it inside an effect creates a subscription.
72
+ *
73
+ * @example
74
+ * const [count, setCount] = signal(0)
75
+ * const double = computed(() => count() * 2)
76
+ * effect(() => console.log(double())) // logs 0
77
+ * setCount(5) // logs 10
78
+ */
79
+ declare function computed<T>(fn: () => T): Getter<T>;
80
+
81
+ /**
82
+ * axon.js - DOM Renderer: mount() and createApp()
83
+ */
84
+
85
+ /**
86
+ * Mounts a component into a DOM container.
87
+ * Clears the container first.
88
+ *
89
+ * @param component - Component function or a pre-built DOM node.
90
+ * @param container - Target DOM element.
91
+ * @param props - Props to pass to the component (if it's a function).
92
+ */
93
+ declare function mount(component: ComponentFn | Node, container: Element, props?: Record<string, unknown>): void;
94
+ /**
95
+ * Creates an application entry point.
96
+ *
97
+ * @example
98
+ * createApp(App).mount('#app')
99
+ * createApp(App).mount(document.getElementById('root')!)
100
+ */
101
+ declare function createApp(RootComponent: ComponentFn): {
102
+ mount(selector: string | Element): void;
103
+ };
104
+
105
+ /**
106
+ * axon.js - DOM Helpers: Show, For, Dynamic, Portal
107
+ *
108
+ * Control-flow components for use in JSX.
109
+ * These are the idiomatic way to handle conditionals and lists
110
+ * with fine-grained reactivity (no re-render of the whole tree).
111
+ */
112
+
113
+ interface ShowProps<T = unknown> {
114
+ /** Reactive condition. If a function, it's read as a signal getter. */
115
+ when: (() => T) | T;
116
+ /** Rendered when `when` is falsy. */
117
+ fallback?: JSXChild;
118
+ /** Rendered when `when` is truthy. */
119
+ children?: JSXChild | JSXChild[];
120
+ }
121
+ /**
122
+ * Conditionally renders children or a fallback.
123
+ *
124
+ * @example
125
+ * <Show when={isLoggedIn} fallback={<Login />}>
126
+ * <Dashboard />
127
+ * </Show>
128
+ */
129
+ declare function Show<T = unknown>({ when, fallback, children }: ShowProps<T>): DocumentFragment;
130
+ interface ForProps<T> {
131
+ /** Reactive array. If a function, it's read as a signal getter. */
132
+ each: (() => T[]) | T[];
133
+ /** Render function called for each item. */
134
+ children: (item: T, index: () => number) => JSXChild;
135
+ }
136
+ /**
137
+ * Reactively renders a list.
138
+ *
139
+ * @example
140
+ * <For each={items}>
141
+ * {(item, index) => <li>{item.name}</li>}
142
+ * </For>
143
+ */
144
+ declare function For<T>({ each, children: renderItem }: ForProps<T>): DocumentFragment;
145
+ interface DynamicProps {
146
+ /** Reactive component getter. */
147
+ component: (() => ComponentFn) | ComponentFn;
148
+ [key: string]: unknown;
149
+ }
150
+ /**
151
+ * Reactively renders a dynamic component.
152
+ * Useful when the component itself needs to change based on state.
153
+ *
154
+ * @example
155
+ * const [currentView, setCurrentView] = signal(HomeView)
156
+ * <Dynamic component={currentView} title="Hello" />
157
+ */
158
+ declare function Dynamic({ component: getComponent, ...props }: DynamicProps): DocumentFragment;
159
+ interface PortalProps {
160
+ /** Target DOM element to render into. */
161
+ mount: Element;
162
+ children?: JSXChild | JSXChild[];
163
+ }
164
+ /**
165
+ * Renders children into a different part of the DOM.
166
+ * Useful for modals, tooltips, and overlays.
167
+ *
168
+ * @example
169
+ * <Portal mount={document.body}>
170
+ * <Modal />
171
+ * </Portal>
172
+ */
173
+ declare function Portal({ mount: target, children }: PortalProps): Comment;
174
+
175
+ /**
176
+ * axon.js — View Transitions
177
+ *
178
+ * Wraps DOM updates in the browser's View Transitions API for smooth animations.
179
+ * Gracefully falls back to a direct call if the browser doesn't support the API.
180
+ *
181
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/View_Transitions_API
182
+ */
183
+ /**
184
+ * Wraps a DOM update function in a View Transition.
185
+ *
186
+ * If the browser supports `document.startViewTransition`, the update is
187
+ * animated. Otherwise it runs immediately with no transition.
188
+ *
189
+ * @param fn - The function that updates the DOM (e.g. a navigate call).
190
+ *
191
+ * @example
192
+ * // Manually trigger a transition on any state change:
193
+ * withViewTransition(() => navigate('/about'))
194
+ * withViewTransition(() => setCurrentTab('settings'))
195
+ *
196
+ * @example
197
+ * // Custom CSS for the transition (add to your stylesheet):
198
+ * // ::view-transition-old(root) { animation: 150ms ease-out fade-out; }
199
+ * // ::view-transition-new(root) { animation: 150ms ease-in fade-in; }
200
+ */
201
+ declare function withViewTransition(fn: () => void): void;
202
+
203
+ /**
204
+ * axon.js - Component Lifecycle
205
+ *
206
+ * Each component gets an "owner" context that tracks:
207
+ * - onMount callbacks (run after the component's DOM is inserted)
208
+ * - onCleanup callbacks (run when the component is destroyed)
209
+ * - child owners (for propagating cleanup down the tree)
210
+ */
211
+
212
+ /**
213
+ * Registers a callback to run after the component mounts (DOM is inserted).
214
+ * Must be called synchronously during component setup.
215
+ */
216
+ declare function onMount(fn: () => void): void;
217
+ /**
218
+ * Registers a cleanup callback to run when the component is destroyed.
219
+ * Must be called synchronously during component setup.
220
+ */
221
+ declare function onCleanup(fn: () => void): void;
222
+
223
+ /**
224
+ * axon.js - Context API
225
+ *
226
+ * Allows passing data down the component tree without prop drilling.
227
+ * Uses a stack-based approach during the synchronous render phase.
228
+ */
229
+
230
+ interface Context<T> {
231
+ /** Wrap children with this to provide a value down the tree. */
232
+ Provider: ComponentFn<{
233
+ value: T;
234
+ children?: JSXChild | JSXChild[];
235
+ }>;
236
+ /** Read the nearest Provider's value. Returns defaultValue if no Provider found. */
237
+ use(): T;
238
+ }
239
+ /**
240
+ * Creates a context object.
241
+ *
242
+ * @param defaultValue - Returned by `use()` when no Provider is found above.
243
+ * @returns `{ Provider, use }` — Provider component and reader function.
244
+ *
245
+ * @example
246
+ * const ThemeCtx = createContext('dark')
247
+ *
248
+ * function App() {
249
+ * return <ThemeCtx.Provider value="light"><Child /></ThemeCtx.Provider>
250
+ * }
251
+ * function Child() {
252
+ * const theme = ThemeCtx.use() // 'light'
253
+ * return <div class={theme}>...</div>
254
+ * }
255
+ */
256
+ declare function createContext<T>(defaultValue: T): Context<T>;
257
+
258
+ /**
259
+ * axon.js - Router
260
+ *
261
+ * Client-side router using the History API.
262
+ * Built entirely on signals — the current URL is a signal,
263
+ * so route matching is reactive automatically.
264
+ */
265
+
266
+ interface RouteConfig {
267
+ path: string;
268
+ component: ComponentFn;
269
+ }
270
+ /**
271
+ * A group of routes that share a layout and/or an access guard.
272
+ *
273
+ * @example
274
+ * {
275
+ * layout: DashboardLayout,
276
+ * guard: () => isLoggedIn() || '/login',
277
+ * children: [
278
+ * { path: '/dashboard', component: Dashboard },
279
+ * { path: '/settings', component: Settings },
280
+ * ],
281
+ * }
282
+ */
283
+ interface RouteGroup {
284
+ layout?: ComponentFn<{
285
+ children?: JSXChild;
286
+ }>;
287
+ /**
288
+ * Called before rendering any child route.
289
+ * - Return `true` → allow access.
290
+ * - Return `false` → deny: go back to the previous route, or to `fallbackPath`
291
+ * if there is no previous route (e.g. direct URL access).
292
+ * - Return a string → redirect to that path.
293
+ *
294
+ * Reactive: if the guard reads a signal (e.g. `isLoggedIn()`),
295
+ * RouterView re-evaluates it whenever that signal changes.
296
+ */
297
+ guard?: () => boolean | string;
298
+ /**
299
+ * Path to redirect to when the guard returns `false` and there is no
300
+ * previous route to go back to (e.g. the user typed the URL directly).
301
+ * If omitted and there is no previous route, nothing is rendered.
302
+ */
303
+ fallbackPath?: string;
304
+ children: RouteConfig[];
305
+ }
306
+ /** Union of a flat route and a route group. Pass this to `createRouter`. */
307
+ type RouteDefinition = RouteConfig | RouteGroup;
308
+ interface CompiledRoute extends RouteConfig {
309
+ regex: RegExp;
310
+ paramNames: string[];
311
+ layout?: ComponentFn<{
312
+ children?: JSXChild;
313
+ }>;
314
+ guard?: () => boolean | string;
315
+ fallbackPath?: string;
316
+ }
317
+ interface NavigateOptions {
318
+ replace?: boolean;
319
+ }
320
+ interface RouterOptions {
321
+ /** Wrap route transitions in the View Transitions API for animated navigation. */
322
+ viewTransitions?: boolean;
323
+ }
324
+ interface Router {
325
+ /** Reactive getter for the current pathname (e.g. '/user/42'). */
326
+ pathname: Getter<string>;
327
+ /** Reactive getter for the current query string (e.g. '?tab=posts'). */
328
+ search: Getter<string>;
329
+ /** Reactive getter for the current route params (e.g. { id: '42' }). */
330
+ params: Getter<Record<string, string>>;
331
+ /** Navigate to a new path programmatically. */
332
+ navigate(to: string, options?: NavigateOptions): void;
333
+ /** Compiled route definitions. */
334
+ routes: CompiledRoute[];
335
+ /** Returns the route matching the current path, or null. */
336
+ currentRoute(): CompiledRoute | null;
337
+ }
338
+ /**
339
+ * Creates and registers the global router instance.
340
+ * Call this once at app startup, before mounting.
341
+ *
342
+ * Accepts flat routes and/or route groups (with shared layout and guard).
343
+ *
344
+ * @example
345
+ * createRouter([
346
+ * { path: '/login', component: Login },
347
+ * {
348
+ * layout: DashboardLayout,
349
+ * guard: () => isLoggedIn() || '/login',
350
+ * children: [
351
+ * { path: '/dashboard', component: Dashboard },
352
+ * ],
353
+ * },
354
+ * ], { viewTransitions: true })
355
+ */
356
+ declare function createRouter(routes: RouteDefinition[], options?: RouterOptions): Router;
357
+ /**
358
+ * Returns the active router instance.
359
+ * Must be called after `createRouter()`.
360
+ */
361
+ declare function useRouter(): Router;
362
+ /**
363
+ * Returns the current route params as a plain object.
364
+ * Reactive: re-reads when the route changes.
365
+ */
366
+ declare function useParams(): Record<string, string>;
367
+ /**
368
+ * Returns the `navigate` function from the active router.
369
+ */
370
+ declare function useNavigate(): Router['navigate'];
371
+
372
+ /**
373
+ * axon.js - Router Components: RouterView, Link
374
+ */
375
+
376
+ /**
377
+ * Renders the component matching the current route.
378
+ * Handles route guards and optional group layouts automatically.
379
+ * Place this where you want the page content to appear.
380
+ *
381
+ * @example
382
+ * function App() {
383
+ * return (
384
+ * <div>
385
+ * <Nav />
386
+ * <main><RouterView /></main>
387
+ * </div>
388
+ * )
389
+ * }
390
+ */
391
+ declare function RouterView(): DocumentFragment;
392
+ interface LinkProps {
393
+ /** Target path for navigation. */
394
+ href: string;
395
+ /** Use replaceState instead of pushState. Default: false. */
396
+ replace?: boolean;
397
+ /** CSS class always applied to the anchor. */
398
+ class?: string;
399
+ /** CSS class applied when `href` matches the current pathname. */
400
+ activeClass?: string;
401
+ children?: JSXChild | JSXChild[];
402
+ }
403
+ /**
404
+ * A client-side navigation link. Prevents full page reload.
405
+ *
406
+ * @example
407
+ * <Link href="/about">About</Link>
408
+ * <Link href="/profile" activeClass="active">Profile</Link>
409
+ */
410
+ declare function Link({ href, replace, class: cls, activeClass, children }: LinkProps): HTMLAnchorElement;
411
+
412
+ /**
413
+ * axon.js - Store
414
+ *
415
+ * Global reactive state built on signals.
416
+ * Each property is a signal internally; reading via the Proxy creates subscriptions.
417
+ *
418
+ * @example
419
+ * const [store, setStore] = createStore({ count: 0, theme: 'dark' })
420
+ *
421
+ * // Read (reactive inside effects/components):
422
+ * store.count // 0
423
+ *
424
+ * // Write:
425
+ * setStore('count', 42)
426
+ * setStore('count', prev => prev + 1)
427
+ * setStore({ count: 10, theme: 'light' }) // merge update
428
+ */
429
+
430
+ /**
431
+ * Updater function or direct value — mirrors signal's setter API.
432
+ */
433
+ type ValueOrUpdater<T> = T | ((prev: T) => T);
434
+ /**
435
+ * The setStore function with 3 call signatures:
436
+ * setStore(key, value) — set one property
437
+ * setStore(key, fn) — update one property with a function
438
+ * setStore(partialObject) — merge-update multiple properties
439
+ */
440
+ interface SetStore<T extends object> {
441
+ <K extends keyof T>(key: K, valueOrUpdater: ValueOrUpdater<T[K]>): void;
442
+ (partial: Partial<T>): void;
443
+ }
444
+ /**
445
+ * Creates a reactive store from an initial state object.
446
+ * Each top-level property becomes an independent signal.
447
+ *
448
+ * @returns A `[store, setStore]` tuple.
449
+ * - `store` — read-only Proxy; reading a property is reactive.
450
+ * - `setStore` — write function with 3 overloads.
451
+ */
452
+ declare function createStore<T extends object>(initialState: T): [T, SetStore<T>];
453
+ /**
454
+ * Creates a computed value derived from a store.
455
+ * Syntactic sugar for `computed(() => selector(store))`.
456
+ *
457
+ * @example
458
+ * const fullName = select(store, s => `${s.firstName} ${s.lastName}`)
459
+ * effect(() => console.log(fullName())) // reactive
460
+ */
461
+ declare function select<T extends object, R>(store: T, selector: (store: T) => R): Getter<R>;
462
+
463
+ export { ComponentFn, type Context, Dynamic, type DynamicProps, For, type ForProps, Getter, JSXChild, Link, type LinkProps, type NavigateOptions, Portal, type PortalProps, type RouteConfig, type RouteDefinition, type RouteGroup, type Router, type RouterOptions, RouterView, type SetStore, Show, type ShowProps, Signal, batch, computed, createApp, createContext, createRouter, createStore, effect, mount, onCleanup, onMount, select, signal, untrack, useNavigate, useParams, useRouter, withViewTransition };