@svelte-lean/core 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 +7 -0
- package/LICENSE +21 -0
- package/README.md +85 -0
- package/dist/diagnostics.d.ts +26 -0
- package/dist/diagnostics.js +97 -0
- package/dist/diagnostics.js.map +1 -0
- package/dist/dom.d.ts +19 -0
- package/dist/dom.js +38 -0
- package/dist/dom.js.map +1 -0
- package/dist/env.d.ts +1 -0
- package/dist/env.development.d.ts +1 -0
- package/dist/env.development.js +3 -0
- package/dist/env.development.js.map +1 -0
- package/dist/env.js +4 -0
- package/dist/env.js.map +1 -0
- package/dist/env.production.d.ts +1 -0
- package/dist/env.production.js +4 -0
- package/dist/env.production.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/keyboard.d.ts +27 -0
- package/dist/keyboard.js +69 -0
- package/dist/keyboard.js.map +1 -0
- package/dist/registry.d.ts +9 -0
- package/dist/registry.js +37 -0
- package/dist/registry.js.map +1 -0
- package/dist/runtime.d.ts +10 -0
- package/dist/runtime.js +131 -0
- package/dist/runtime.js.map +1 -0
- package/dist/state.d.ts +11 -0
- package/dist/state.js +20 -0
- package/dist/state.js.map +1 -0
- package/dist/types.d.ts +52 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +56 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# @svelte-lean/core
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- eb039db: Initial release: the behavior registry, the shared event router (one listener per event type on the runtime root), DOM protocol and keyboard helpers, `lazyState` and development diagnostics that production builds drop.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ömer Say
|
|
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,85 @@
|
|
|
1
|
+
# @svelte-lean/core
|
|
2
|
+
|
|
3
|
+
[Documentation](https://svelte-lean.vercel.app/docs/architecture/dom-protocol) ·
|
|
4
|
+
[Repository](https://github.com/say16/svelte-lean/tree/main/packages/core) ·
|
|
5
|
+
[Changelog](https://github.com/say16/svelte-lean/blob/main/packages/core/CHANGELOG.md)
|
|
6
|
+
|
|
7
|
+
Runtime kernel for Svelte Lean: a behavior registry, one shared event router per runtime, and the
|
|
8
|
+
DOM, keyboard and state helpers that behaviors are written with. No Svelte imports, no dependencies,
|
|
9
|
+
ESM only, SSR-safe (nothing touches `document` or `window` at import time).
|
|
10
|
+
|
|
11
|
+
Behaviors are looked up from the DOM protocol (`data-slean="<behavior>"` roots,
|
|
12
|
+
`data-slean-part="<part>"` parts, `data-slean-<option>` options) and get one listener per event type
|
|
13
|
+
they handle, attached to the runtime root. Routing walks `event.composedPath()` from the target
|
|
14
|
+
outward, so nested behaviors and open shadow roots work and no selector runs over the document.
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
`@svelte-lean/core` is a dependency of `@svelte-lean/primitives` and is installed with it. Install
|
|
19
|
+
it directly only to write your own behaviors:
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
yarn add @svelte-lean/core
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
ESM only, no dependencies, no framework dependency. The invariants every behavior follows are in
|
|
26
|
+
the repository's [`packages/core/AGENTS.md`](https://github.com/say16/svelte-lean/blob/main/packages/core/AGENTS.md).
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { registerBehavior, part, parts, nextIndex, emit } from '@svelte-lean/core';
|
|
32
|
+
|
|
33
|
+
registerBehavior('tabs', {
|
|
34
|
+
tier: 1,
|
|
35
|
+
events: {
|
|
36
|
+
click({ root, target }) {
|
|
37
|
+
const trigger = target.closest('[data-slean-part="trigger"]');
|
|
38
|
+
if (trigger) emit(root, 'change', trigger.getAttribute('data-slean-value'));
|
|
39
|
+
},
|
|
40
|
+
keydown({ root, event }) {
|
|
41
|
+
const triggers = parts(root, 'trigger');
|
|
42
|
+
const current = triggers.indexOf(document.activeElement as HTMLElement);
|
|
43
|
+
const next = nextIndex(current, triggers.length, event.key, { orientation: 'horizontal' });
|
|
44
|
+
if (next !== -1) {
|
|
45
|
+
event.preventDefault();
|
|
46
|
+
triggers[next]?.focus();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
`registerBehavior` is idempotent (re-registering replaces the definition and never adds a listener)
|
|
54
|
+
and safe on the server: without a `document` the definition waits until a browser runtime exists.
|
|
55
|
+
`createRuntime({ root })` creates a runtime for another root (a `ShadowRoot`, an element or an
|
|
56
|
+
iframe document).
|
|
57
|
+
|
|
58
|
+
## API
|
|
59
|
+
|
|
60
|
+
| Export | Purpose |
|
|
61
|
+
| ------------------------------------------------------------------ | ------------------------------------------------------------------ |
|
|
62
|
+
| `registerBehavior`, `unregisterBehavior` | Register on the document runtime; SSR-safe |
|
|
63
|
+
| `getDefaultRuntime`, `createRuntime`, `EVENT_TYPES` | Runtimes; `listeners()` reports one entry per installed event type |
|
|
64
|
+
| `parts`, `part`, `closestRoot`, `isDisabled`, `readOption`, `emit` | DOM protocol helpers |
|
|
65
|
+
| `nextIndex`, `isTypeaheadKey`, `createTypeahead` | Keyboard navigation (arrows, Home, End, RTL, loop) and typeahead |
|
|
66
|
+
| `lazyState` | `WeakMap` state created on first access (Tier 2 behaviors) |
|
|
67
|
+
| `DEV`, `warn`, `scopedControllers`, `report` | Development diagnostics; `window.__SLEAN_DEBUG__` in development |
|
|
68
|
+
|
|
69
|
+
Types: `BehaviorDefinition`, `BehaviorContext`, `BehaviorHandler`, `SleanEventType`, `Runtime`.
|
|
70
|
+
|
|
71
|
+
## Size
|
|
72
|
+
|
|
73
|
+
Measured by `scripts/size.mjs` in the repository (esbuild 0.25 bundle + minify for the entry, Vite 7
|
|
74
|
+
production build for the consumer case; gzip level 9, brotli quality 11); `artifacts/size.json`
|
|
75
|
+
holds the latest run.
|
|
76
|
+
|
|
77
|
+
| Bundle | raw | gzip | brotli |
|
|
78
|
+
| ---------------------------------------------------------- | ------ | ------ | ------ |
|
|
79
|
+
| Entry, every export, `production` condition | 4357 B | 2087 B | 1907 B |
|
|
80
|
+
| Entry, every export, runtime-detected env | 4560 B | 2169 B | 1960 B |
|
|
81
|
+
| Consumer: one Tier 1 behavior with `parts` + `emit` (Vite) | 1864 B | 965 B | 852 B |
|
|
82
|
+
|
|
83
|
+
Diagnostics (`warn`, `validate`, `__SLEAN_DEBUG__`, `report`) are gated on `DEV`, which a bundler
|
|
84
|
+
resolves through the `#env` import conditions (`development`, `production`, `default`); the consumer
|
|
85
|
+
build contains none of them.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { BehaviorDefinition, Runtime } from './types.js';
|
|
2
|
+
/** Prints a development warning as `[Svelte Lean: <behavior>] <message>`. No-op in production. */
|
|
3
|
+
export declare function warn(behavior: string, message: string): void;
|
|
4
|
+
/**
|
|
5
|
+
* Counter for Tier 2 scoped controllers. A behavior calls `add` when it creates an
|
|
6
|
+
* `AbortController`-scoped session and `remove` when the session ends. Development only.
|
|
7
|
+
*/
|
|
8
|
+
export declare const scopedControllers: {
|
|
9
|
+
add(behavior: string): void;
|
|
10
|
+
remove(behavior: string): void;
|
|
11
|
+
count(behavior?: string): number;
|
|
12
|
+
};
|
|
13
|
+
export interface SleanDebug {
|
|
14
|
+
runtimes(): Runtime[];
|
|
15
|
+
report(): string;
|
|
16
|
+
}
|
|
17
|
+
/** Human-readable summary of live runtimes, their listeners and scoped controllers. */
|
|
18
|
+
export declare function report(): string;
|
|
19
|
+
/** @internal Called by `createRuntime`; installs `window.__SLEAN_DEBUG__` once, in development. */
|
|
20
|
+
export declare function trackRuntime(runtime: Runtime): void;
|
|
21
|
+
/** @internal */
|
|
22
|
+
export declare function untrackRuntime(runtime: Runtime): void;
|
|
23
|
+
/** @internal Runs `definition.validate` once per root and warns each message. */
|
|
24
|
+
export declare function validateRoot(name: string, definition: BehaviorDefinition, root: HTMLElement): void;
|
|
25
|
+
/** @internal Forgets validated roots so a replaced definition (HMR) is validated again. */
|
|
26
|
+
export declare function resetValidation(): void;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// `#env` resolves through package.json `imports` to a literal `true`/`false` module under the
|
|
2
|
+
// `development`/`production` conditions, so everything gated on DEV is dropped from production.
|
|
3
|
+
import { DEV } from '#env';
|
|
4
|
+
// Shape rule for this file: a DEV gate is `if (DEV) helper(...)` with no declarations inside the
|
|
5
|
+
// gate. A Vite (Rollup) production build removes the gates and the helpers they referenced;
|
|
6
|
+
// scripts/size.mjs fails if a diagnostic survives that build. esbuild on its own folds the gates
|
|
7
|
+
// but keeps referenced helpers, because it tree-shakes before it inlines cross-module constants,
|
|
8
|
+
// and it drops only declaration-free dead blocks, hence the helpers.
|
|
9
|
+
/** Prints a development warning as `[Svelte Lean: <behavior>] <message>`. No-op in production. */
|
|
10
|
+
export function warn(behavior, message) {
|
|
11
|
+
if (DEV)
|
|
12
|
+
console.warn(`[Svelte Lean: ${behavior}] ${message}`);
|
|
13
|
+
}
|
|
14
|
+
const controllers = new Map();
|
|
15
|
+
function decrement(behavior) {
|
|
16
|
+
const next = (controllers.get(behavior) ?? 0) - 1;
|
|
17
|
+
if (next > 0)
|
|
18
|
+
controllers.set(behavior, next);
|
|
19
|
+
else
|
|
20
|
+
controllers.delete(behavior);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Counter for Tier 2 scoped controllers. A behavior calls `add` when it creates an
|
|
24
|
+
* `AbortController`-scoped session and `remove` when the session ends. Development only.
|
|
25
|
+
*/
|
|
26
|
+
export const scopedControllers = {
|
|
27
|
+
add(behavior) {
|
|
28
|
+
if (DEV)
|
|
29
|
+
controllers.set(behavior, (controllers.get(behavior) ?? 0) + 1);
|
|
30
|
+
},
|
|
31
|
+
remove(behavior) {
|
|
32
|
+
if (DEV)
|
|
33
|
+
decrement(behavior);
|
|
34
|
+
},
|
|
35
|
+
count(behavior) {
|
|
36
|
+
if (behavior)
|
|
37
|
+
return controllers.get(behavior) ?? 0;
|
|
38
|
+
let total = 0;
|
|
39
|
+
for (const n of controllers.values())
|
|
40
|
+
total += n;
|
|
41
|
+
return total;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
const live = new Set();
|
|
45
|
+
/** Human-readable summary of live runtimes, their listeners and scoped controllers. */
|
|
46
|
+
export function report() {
|
|
47
|
+
const lines = ['Svelte Lean Runtime', '-------------------', `runtimes: ${live.size}`];
|
|
48
|
+
let i = 0;
|
|
49
|
+
for (const runtime of live) {
|
|
50
|
+
const installed = runtime.listeners();
|
|
51
|
+
lines.push('', `[runtime ${++i}] root: ${runtime.root.nodeName}`, 'registered behaviors:');
|
|
52
|
+
for (const name of runtime.behaviors())
|
|
53
|
+
lines.push(` ${name}`);
|
|
54
|
+
lines.push('shared listeners:');
|
|
55
|
+
for (const type of Object.keys(installed))
|
|
56
|
+
lines.push(` ${type}: ${installed[type]}`);
|
|
57
|
+
}
|
|
58
|
+
lines.push('', 'scoped controllers:');
|
|
59
|
+
for (const [name, n] of controllers)
|
|
60
|
+
lines.push(` ${name}: ${n}`);
|
|
61
|
+
return lines.join('\n');
|
|
62
|
+
}
|
|
63
|
+
function install(runtime) {
|
|
64
|
+
live.add(runtime);
|
|
65
|
+
if (typeof window !== 'undefined') {
|
|
66
|
+
window.__SLEAN_DEBUG__ ??= {
|
|
67
|
+
runtimes: () => [...live],
|
|
68
|
+
report
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/** @internal Called by `createRuntime`; installs `window.__SLEAN_DEBUG__` once, in development. */
|
|
73
|
+
export function trackRuntime(runtime) {
|
|
74
|
+
if (DEV)
|
|
75
|
+
install(runtime);
|
|
76
|
+
}
|
|
77
|
+
/** @internal */
|
|
78
|
+
export function untrackRuntime(runtime) {
|
|
79
|
+
if (DEV)
|
|
80
|
+
live.delete(runtime);
|
|
81
|
+
}
|
|
82
|
+
let validated = new WeakSet();
|
|
83
|
+
/** @internal Runs `definition.validate` once per root and warns each message. */
|
|
84
|
+
export function validateRoot(name, definition, root) {
|
|
85
|
+
if (validated.has(root))
|
|
86
|
+
return;
|
|
87
|
+
validated.add(root);
|
|
88
|
+
const messages = definition.validate?.(root);
|
|
89
|
+
if (messages)
|
|
90
|
+
for (const message of messages)
|
|
91
|
+
warn(name, message);
|
|
92
|
+
}
|
|
93
|
+
/** @internal Forgets validated roots so a replaced definition (HMR) is validated again. */
|
|
94
|
+
export function resetValidation() {
|
|
95
|
+
validated = new WeakSet();
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=diagnostics.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"diagnostics.js","sourceRoot":"","sources":["../src/diagnostics.ts"],"names":[],"mappings":"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","sourcesContent":["import type { BehaviorDefinition, Runtime } from './types.js';\n// `#env` resolves through package.json `imports` to a literal `true`/`false` module under the\n// `development`/`production` conditions, so everything gated on DEV is dropped from production.\nimport { DEV } from '#env';\n\n// Shape rule for this file: a DEV gate is `if (DEV) helper(...)` with no declarations inside the\n// gate. A Vite (Rollup) production build removes the gates and the helpers they referenced;\n// scripts/size.mjs fails if a diagnostic survives that build. esbuild on its own folds the gates\n// but keeps referenced helpers, because it tree-shakes before it inlines cross-module constants,\n// and it drops only declaration-free dead blocks, hence the helpers.\n\n/** Prints a development warning as `[Svelte Lean: <behavior>] <message>`. No-op in production. */\nexport function warn(behavior: string, message: string): void {\n\tif (DEV) console.warn(`[Svelte Lean: ${behavior}] ${message}`);\n}\n\nconst controllers = new Map<string, number>();\n\nfunction decrement(behavior: string): void {\n\tconst next = (controllers.get(behavior) ?? 0) - 1;\n\tif (next > 0) controllers.set(behavior, next);\n\telse controllers.delete(behavior);\n}\n\n/**\n * Counter for Tier 2 scoped controllers. A behavior calls `add` when it creates an\n * `AbortController`-scoped session and `remove` when the session ends. Development only.\n */\nexport const scopedControllers = {\n\tadd(behavior: string): void {\n\t\tif (DEV) controllers.set(behavior, (controllers.get(behavior) ?? 0) + 1);\n\t},\n\tremove(behavior: string): void {\n\t\tif (DEV) decrement(behavior);\n\t},\n\tcount(behavior?: string): number {\n\t\tif (behavior) return controllers.get(behavior) ?? 0;\n\t\tlet total = 0;\n\t\tfor (const n of controllers.values()) total += n;\n\t\treturn total;\n\t}\n};\n\nexport interface SleanDebug {\n\truntimes(): Runtime[];\n\treport(): string;\n}\n\nconst live = new Set<Runtime>();\n\n/** Human-readable summary of live runtimes, their listeners and scoped controllers. */\nexport function report(): string {\n\tconst lines = ['Svelte Lean Runtime', '-------------------', `runtimes: ${live.size}`];\n\tlet i = 0;\n\tfor (const runtime of live) {\n\t\tconst installed = runtime.listeners();\n\t\tlines.push('', `[runtime ${++i}] root: ${runtime.root.nodeName}`, 'registered behaviors:');\n\t\tfor (const name of runtime.behaviors()) lines.push(` ${name}`);\n\t\tlines.push('shared listeners:');\n\t\tfor (const type of Object.keys(installed)) lines.push(` ${type}: ${installed[type]}`);\n\t}\n\tlines.push('', 'scoped controllers:');\n\tfor (const [name, n] of controllers) lines.push(` ${name}: ${n}`);\n\treturn lines.join('\\n');\n}\n\nfunction install(runtime: Runtime): void {\n\tlive.add(runtime);\n\tif (typeof window !== 'undefined') {\n\t\t(window as Window & { __SLEAN_DEBUG__?: SleanDebug }).__SLEAN_DEBUG__ ??= {\n\t\t\truntimes: () => [...live],\n\t\t\treport\n\t\t};\n\t}\n}\n\n/** @internal Called by `createRuntime`; installs `window.__SLEAN_DEBUG__` once, in development. */\nexport function trackRuntime(runtime: Runtime): void {\n\tif (DEV) install(runtime);\n}\n\n/** @internal */\nexport function untrackRuntime(runtime: Runtime): void {\n\tif (DEV) live.delete(runtime);\n}\n\nlet validated = new WeakSet<HTMLElement>();\n\n/** @internal Runs `definition.validate` once per root and warns each message. */\nexport function validateRoot(\n\tname: string,\n\tdefinition: BehaviorDefinition,\n\troot: HTMLElement\n): void {\n\tif (validated.has(root)) return;\n\tvalidated.add(root);\n\tconst messages = definition.validate?.(root);\n\tif (messages) for (const message of messages) warn(name, message);\n}\n\n/** @internal Forgets validated roots so a replaced definition (HMR) is validated again. */\nexport function resetValidation(): void {\n\tvalidated = new WeakSet();\n}\n"]}
|
package/dist/dom.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Elements with `data-slean-part="<part>"` inside `root`, excluding parts that belong to a nested
|
|
3
|
+
* root of the same behavior. Light DOM only; part names must be plain identifiers.
|
|
4
|
+
*/
|
|
5
|
+
export declare function parts(root: HTMLElement, part: string): HTMLElement[];
|
|
6
|
+
/** First element `parts()` would return, or null. */
|
|
7
|
+
export declare function part(root: HTMLElement, part: string): HTMLElement | null;
|
|
8
|
+
/** Nearest ancestor-or-self with `data-slean` (optionally a specific behavior). Light DOM only. */
|
|
9
|
+
export declare function closestRoot(el: Element, behavior?: string): HTMLElement | null;
|
|
10
|
+
/** `disabled` property, `aria-disabled="true"` or the `data-slean-disabled` attribute. */
|
|
11
|
+
export declare function isDisabled(el: Element): boolean;
|
|
12
|
+
/** Reads the static option `data-slean-<name>` from a root. */
|
|
13
|
+
export declare function readOption(root: HTMLElement, name: string): string | undefined;
|
|
14
|
+
export declare function readOption(root: HTMLElement, name: string, fallback: string): string;
|
|
15
|
+
/**
|
|
16
|
+
* Dispatches `slean:<name>` from `root` (bubbling, cancelable). Returns false when a listener
|
|
17
|
+
* called `preventDefault()`.
|
|
18
|
+
*/
|
|
19
|
+
export declare function emit<T = unknown>(root: HTMLElement, name: string, detail?: T): boolean;
|
package/dist/dom.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Elements with `data-slean-part="<part>"` inside `root`, excluding parts that belong to a nested
|
|
3
|
+
* root of the same behavior. Light DOM only; part names must be plain identifiers.
|
|
4
|
+
*/
|
|
5
|
+
export function parts(root, part) {
|
|
6
|
+
const behavior = root.getAttribute('data-slean');
|
|
7
|
+
const out = [];
|
|
8
|
+
for (const el of root.querySelectorAll(`[data-slean-part="${part}"]`)) {
|
|
9
|
+
if (!behavior || el.parentElement?.closest(`[data-slean="${behavior}"]`) === root)
|
|
10
|
+
out.push(el);
|
|
11
|
+
}
|
|
12
|
+
return out;
|
|
13
|
+
}
|
|
14
|
+
/** First element `parts()` would return, or null. */
|
|
15
|
+
export function part(root, part) {
|
|
16
|
+
return parts(root, part)[0] ?? null;
|
|
17
|
+
}
|
|
18
|
+
/** Nearest ancestor-or-self with `data-slean` (optionally a specific behavior). Light DOM only. */
|
|
19
|
+
export function closestRoot(el, behavior) {
|
|
20
|
+
return el.closest(behavior ? `[data-slean="${behavior}"]` : '[data-slean]');
|
|
21
|
+
}
|
|
22
|
+
/** `disabled` property, `aria-disabled="true"` or the `data-slean-disabled` attribute. */
|
|
23
|
+
export function isDisabled(el) {
|
|
24
|
+
return (el.disabled === true ||
|
|
25
|
+
el.getAttribute('aria-disabled') === 'true' ||
|
|
26
|
+
el.hasAttribute('data-slean-disabled'));
|
|
27
|
+
}
|
|
28
|
+
export function readOption(root, name, fallback) {
|
|
29
|
+
return root.getAttribute(`data-slean-${name}`) ?? fallback;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Dispatches `slean:<name>` from `root` (bubbling, cancelable). Returns false when a listener
|
|
33
|
+
* called `preventDefault()`.
|
|
34
|
+
*/
|
|
35
|
+
export function emit(root, name, detail) {
|
|
36
|
+
return root.dispatchEvent(new CustomEvent(`slean:${name}`, { bubbles: true, cancelable: true, detail }));
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=dom.js.map
|
package/dist/dom.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dom.js","sourceRoot":"","sources":["../src/dom.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,UAAU,KAAK,CAAC,IAAiB,EAAE,IAAY;IACpD,MAAM,QAAQ,GAAG,IAAI,CAAC,YAAY,CAAC,YAAY,CAAC,CAAC;IACjD,MAAM,GAAG,GAAkB,EAAE,CAAC;IAC9B,KAAK,MAAM,EAAE,IAAI,IAAI,CAAC,gBAAgB,CAAc,qBAAqB,IAAI,IAAI,CAAC,EAAE,CAAC;QACpF,IAAI,CAAC,QAAQ,IAAI,EAAE,CAAC,aAAa,EAAE,OAAO,CAAC,gBAAgB,QAAQ,IAAI,CAAC,KAAK,IAAI;YAAE,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACjG,CAAC;IACD,OAAO,GAAG,CAAC;AACZ,CAAC;AAED,qDAAqD;AACrD,MAAM,UAAU,IAAI,CAAC,IAAiB,EAAE,IAAY;IACnD,OAAO,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC;AACrC,CAAC;AAED,mGAAmG;AACnG,MAAM,UAAU,WAAW,CAAC,EAAW,EAAE,QAAiB;IACzD,OAAO,EAAE,CAAC,OAAO,CAAc,QAAQ,CAAC,CAAC,CAAC,gBAAgB,QAAQ,IAAI,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC;AAC1F,CAAC;AAED,0FAA0F;AAC1F,MAAM,UAAU,UAAU,CAAC,EAAW;IACrC,OAAO,CACL,EAAwB,CAAC,QAAQ,KAAK,IAAI;QAC3C,EAAE,CAAC,YAAY,CAAC,eAAe,CAAC,KAAK,MAAM;QAC3C,EAAE,CAAC,YAAY,CAAC,qBAAqB,CAAC,CACtC,CAAC;AACH,CAAC;AAKD,MAAM,UAAU,UAAU,CAAC,IAAiB,EAAE,IAAY,EAAE,QAAiB;IAC5E,OAAO,IAAI,CAAC,YAAY,CAAC,cAAc,IAAI,EAAE,CAAC,IAAI,QAAQ,CAAC;AAC5D,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,IAAI,CAAc,IAAiB,EAAE,IAAY,EAAE,MAAU;IAC5E,OAAO,IAAI,CAAC,aAAa,CACxB,IAAI,WAAW,CAAC,SAAS,IAAI,EAAE,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,CAC7E,CAAC;AACH,CAAC","sourcesContent":["/**\n * Elements with `data-slean-part=\"<part>\"` inside `root`, excluding parts that belong to a nested\n * root of the same behavior. Light DOM only; part names must be plain identifiers.\n */\nexport function parts(root: HTMLElement, part: string): HTMLElement[] {\n\tconst behavior = root.getAttribute('data-slean');\n\tconst out: HTMLElement[] = [];\n\tfor (const el of root.querySelectorAll<HTMLElement>(`[data-slean-part=\"${part}\"]`)) {\n\t\tif (!behavior || el.parentElement?.closest(`[data-slean=\"${behavior}\"]`) === root) out.push(el);\n\t}\n\treturn out;\n}\n\n/** First element `parts()` would return, or null. */\nexport function part(root: HTMLElement, part: string): HTMLElement | null {\n\treturn parts(root, part)[0] ?? null;\n}\n\n/** Nearest ancestor-or-self with `data-slean` (optionally a specific behavior). Light DOM only. */\nexport function closestRoot(el: Element, behavior?: string): HTMLElement | null {\n\treturn el.closest<HTMLElement>(behavior ? `[data-slean=\"${behavior}\"]` : '[data-slean]');\n}\n\n/** `disabled` property, `aria-disabled=\"true\"` or the `data-slean-disabled` attribute. */\nexport function isDisabled(el: Element): boolean {\n\treturn (\n\t\t(el as HTMLButtonElement).disabled === true ||\n\t\tel.getAttribute('aria-disabled') === 'true' ||\n\t\tel.hasAttribute('data-slean-disabled')\n\t);\n}\n\n/** Reads the static option `data-slean-<name>` from a root. */\nexport function readOption(root: HTMLElement, name: string): string | undefined;\nexport function readOption(root: HTMLElement, name: string, fallback: string): string;\nexport function readOption(root: HTMLElement, name: string, fallback?: string): string | undefined {\n\treturn root.getAttribute(`data-slean-${name}`) ?? fallback;\n}\n\n/**\n * Dispatches `slean:<name>` from `root` (bubbling, cancelable). Returns false when a listener\n * called `preventDefault()`.\n */\nexport function emit<T = unknown>(root: HTMLElement, name: string, detail?: T): boolean {\n\treturn root.dispatchEvent(\n\t\tnew CustomEvent(`slean:${name}`, { bubbles: true, cancelable: true, detail })\n\t);\n}\n"]}
|
package/dist/env.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const DEV: boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const DEV: boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env.development.js","sourceRoot":"","sources":["../src/env.development.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,MAAM,CAAC,MAAM,GAAG,GAAY,IAAI,CAAC","sourcesContent":["// Selected by the `development` export condition (Vite dev server, vitest).\nexport const DEV: boolean = true;\n"]}
|
package/dist/env.js
ADDED
package/dist/env.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env.js","sourceRoot":"","sources":["../src/env.ts"],"names":[],"mappings":"AAWA,MAAM,CAAC,MAAM,GAAG,GACf,OAAQ,MAAM,CAAC,IAAa,CAAC,GAAG,KAAK,WAAW;IAC/C,CAAC,CAAE,MAAM,CAAC,IAAa,CAAC,GAAI,CAAC,GAAG,KAAK,IAAI;IACzC,CAAC,CAAC,OAAO,OAAO,KAAK,WAAW,IAAI,CAAC,CAAC,OAAO,CAAC,GAAG,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,CAAC","sourcesContent":["/**\n * Runtime-detected development flag. This is the `default` target of the `#env` subpath import;\n * bundlers that honour the `development`/`production` export conditions never load it and get a\n * literal from `env.development.ts` or `env.production.ts` instead, so `if (DEV)` branches fold away.\n *\n * Reading `import.meta.env` cannot throw: `import.meta` always exists in ESM and the property is\n * simply undefined outside bundlers. `process` is guarded with `typeof` for browsers. Without any\n * signal the flag is false, so a no-bundler browser behaves like production.\n */\ntype Meta = ImportMeta & { env?: { DEV?: unknown } };\n\nexport const DEV: boolean =\n\ttypeof (import.meta as Meta).env !== 'undefined'\n\t\t? (import.meta as Meta).env!.DEV === true\n\t\t: typeof process !== 'undefined' && !!process.env && process.env.NODE_ENV !== 'production';\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const DEV: boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env.production.js","sourceRoot":"","sources":["../src/env.production.ts"],"names":[],"mappings":"AAAA,0FAA0F;AAC1F,2BAA2B;AAC3B,MAAM,CAAC,MAAM,GAAG,GAAY,KAAK,CAAC","sourcesContent":["// Selected by the `production` export condition (Vite build). A literal, so bundlers drop\n// every `if (DEV)` branch.\nexport const DEV: boolean = false;\n"]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type { BehaviorContext, BehaviorDefinition, BehaviorEvents, BehaviorHandler, Runtime, RuntimeOptions, RuntimeRoot, SleanEventMap, SleanEventType } from './types.js';
|
|
2
|
+
export { createRuntime, EVENT_TYPES } from './runtime.js';
|
|
3
|
+
export { getDefaultRuntime, registerBehavior, unregisterBehavior } from './registry.js';
|
|
4
|
+
export { closestRoot, emit, isDisabled, part, parts, readOption } from './dom.js';
|
|
5
|
+
export { createTypeahead, isTypeaheadKey, nextIndex } from './keyboard.js';
|
|
6
|
+
export type { NavigationOptions, Typeahead } from './keyboard.js';
|
|
7
|
+
export { lazyState } from './state.js';
|
|
8
|
+
export type { LazyState } from './state.js';
|
|
9
|
+
export { report, scopedControllers, warn } from './diagnostics.js';
|
|
10
|
+
/**
|
|
11
|
+
* True in development builds; see `AGENTS.md` for how the `#env` conditions select it. Re-exported
|
|
12
|
+
* through a typed constant so the public declaration file never references the `#env` specifier,
|
|
13
|
+
* which legacy `moduleResolution: node` cannot follow.
|
|
14
|
+
*/
|
|
15
|
+
export declare const DEV: boolean;
|
|
16
|
+
export type { SleanDebug } from './diagnostics.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export { createRuntime, EVENT_TYPES } from './runtime.js';
|
|
2
|
+
export { getDefaultRuntime, registerBehavior, unregisterBehavior } from './registry.js';
|
|
3
|
+
export { closestRoot, emit, isDisabled, part, parts, readOption } from './dom.js';
|
|
4
|
+
export { createTypeahead, isTypeaheadKey, nextIndex } from './keyboard.js';
|
|
5
|
+
export { lazyState } from './state.js';
|
|
6
|
+
import { DEV as env } from '#env';
|
|
7
|
+
export { report, scopedControllers, warn } from './diagnostics.js';
|
|
8
|
+
/**
|
|
9
|
+
* True in development builds; see `AGENTS.md` for how the `#env` conditions select it. Re-exported
|
|
10
|
+
* through a typed constant so the public declaration file never references the `#env` specifier,
|
|
11
|
+
* which legacy `moduleResolution: node` cannot follow.
|
|
12
|
+
*/
|
|
13
|
+
export const DEV = env;
|
|
14
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC1D,OAAO,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACxF,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAClF,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAE3E,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,OAAO,EAAE,GAAG,IAAI,GAAG,EAAE,MAAM,MAAM,CAAC;AAClC,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAEnE;;;;GAIG;AACH,MAAM,CAAC,MAAM,GAAG,GAAY,GAAG,CAAC","sourcesContent":["export type {\n\tBehaviorContext,\n\tBehaviorDefinition,\n\tBehaviorEvents,\n\tBehaviorHandler,\n\tRuntime,\n\tRuntimeOptions,\n\tRuntimeRoot,\n\tSleanEventMap,\n\tSleanEventType\n} from './types.js';\nexport { createRuntime, EVENT_TYPES } from './runtime.js';\nexport { getDefaultRuntime, registerBehavior, unregisterBehavior } from './registry.js';\nexport { closestRoot, emit, isDisabled, part, parts, readOption } from './dom.js';\nexport { createTypeahead, isTypeaheadKey, nextIndex } from './keyboard.js';\nexport type { NavigationOptions, Typeahead } from './keyboard.js';\nexport { lazyState } from './state.js';\nexport type { LazyState } from './state.js';\nimport { DEV as env } from '#env';\nexport { report, scopedControllers, warn } from './diagnostics.js';\n\n/**\n * True in development builds; see `AGENTS.md` for how the `#env` conditions select it. Re-exported\n * through a typed constant so the public declaration file never references the `#env` specifier,\n * which legacy `moduleResolution: node` cannot follow.\n */\nexport const DEV: boolean = env;\nexport type { SleanDebug } from './diagnostics.js';\n"]}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export interface NavigationOptions {
|
|
2
|
+
/** Which arrow keys move. Defaults to `'horizontal'`. */
|
|
3
|
+
orientation?: 'horizontal' | 'vertical' | 'both';
|
|
4
|
+
/** Wrap at the ends. Defaults to `true`. */
|
|
5
|
+
loop?: boolean;
|
|
6
|
+
/** Swap ArrowLeft/ArrowRight. Defaults to `false`. */
|
|
7
|
+
rtl?: boolean;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Index an arrow/Home/End key moves to, or -1 when the key is not a navigation key for this
|
|
11
|
+
* orientation. With no current item (-1) a forward key selects the first and a backward key the
|
|
12
|
+
* last. Without `loop`, an edge returns the edge index itself, so the key still counts as handled.
|
|
13
|
+
*/
|
|
14
|
+
export declare function nextIndex(current: number, count: number, key: string, options?: NavigationOptions): number;
|
|
15
|
+
/** A single printable character without a modifier, excluding Space. */
|
|
16
|
+
export declare function isTypeaheadKey(event: KeyboardEvent): boolean;
|
|
17
|
+
export interface Typeahead {
|
|
18
|
+
/**
|
|
19
|
+
* Adds `key` to the buffer and returns the index of the matching label, or -1. Typing the
|
|
20
|
+
* same character repeatedly cycles through labels starting with it; a longer buffer matches
|
|
21
|
+
* from the current item so the match can stay where it is.
|
|
22
|
+
*/
|
|
23
|
+
search(key: string, items: readonly string[], current: number): number;
|
|
24
|
+
reset(): void;
|
|
25
|
+
}
|
|
26
|
+
/** Buffered typeahead matcher. The buffer resets after `timeout` ms (500 by default). */
|
|
27
|
+
export declare function createTypeahead(timeout?: number): Typeahead;
|
package/dist/keyboard.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Index an arrow/Home/End key moves to, or -1 when the key is not a navigation key for this
|
|
3
|
+
* orientation. With no current item (-1) a forward key selects the first and a backward key the
|
|
4
|
+
* last. Without `loop`, an edge returns the edge index itself, so the key still counts as handled.
|
|
5
|
+
*/
|
|
6
|
+
export function nextIndex(current, count, key, options = {}) {
|
|
7
|
+
if (count <= 0)
|
|
8
|
+
return -1;
|
|
9
|
+
if (key === 'Home')
|
|
10
|
+
return 0;
|
|
11
|
+
if (key === 'End')
|
|
12
|
+
return count - 1;
|
|
13
|
+
const { orientation = 'horizontal', loop = true, rtl = false } = options;
|
|
14
|
+
const horizontal = orientation !== 'vertical';
|
|
15
|
+
const vertical = orientation !== 'horizontal';
|
|
16
|
+
let delta;
|
|
17
|
+
if (horizontal && key === 'ArrowRight')
|
|
18
|
+
delta = rtl ? -1 : 1;
|
|
19
|
+
else if (horizontal && key === 'ArrowLeft')
|
|
20
|
+
delta = rtl ? 1 : -1;
|
|
21
|
+
else if (vertical && key === 'ArrowDown')
|
|
22
|
+
delta = 1;
|
|
23
|
+
else if (vertical && key === 'ArrowUp')
|
|
24
|
+
delta = -1;
|
|
25
|
+
else
|
|
26
|
+
return -1;
|
|
27
|
+
if (current < 0 || current >= count)
|
|
28
|
+
return delta > 0 ? 0 : count - 1;
|
|
29
|
+
const next = current + delta;
|
|
30
|
+
if (next < 0)
|
|
31
|
+
return loop ? count - 1 : 0;
|
|
32
|
+
if (next >= count)
|
|
33
|
+
return loop ? 0 : count - 1;
|
|
34
|
+
return next;
|
|
35
|
+
}
|
|
36
|
+
/** A single printable character without a modifier, excluding Space. */
|
|
37
|
+
export function isTypeaheadKey(event) {
|
|
38
|
+
const key = event.key;
|
|
39
|
+
return key.length === 1 && key !== ' ' && !event.ctrlKey && !event.metaKey && !event.altKey;
|
|
40
|
+
}
|
|
41
|
+
/** Buffered typeahead matcher. The buffer resets after `timeout` ms (500 by default). */
|
|
42
|
+
export function createTypeahead(timeout = 500) {
|
|
43
|
+
let buffer = '';
|
|
44
|
+
let last = 0;
|
|
45
|
+
return {
|
|
46
|
+
search(key, items, current) {
|
|
47
|
+
const now = Date.now();
|
|
48
|
+
const char = key.toLowerCase();
|
|
49
|
+
buffer = now - last > timeout ? char : buffer + char;
|
|
50
|
+
last = now;
|
|
51
|
+
const first = buffer[0];
|
|
52
|
+
const repeated = buffer.length > 1 && buffer === first.repeat(buffer.length);
|
|
53
|
+
const query = repeated ? first : buffer;
|
|
54
|
+
const from = Math.max(0, buffer.length > 1 && !repeated ? current : current + 1);
|
|
55
|
+
const count = items.length;
|
|
56
|
+
for (let i = 0; i < count; i++) {
|
|
57
|
+
const index = (from + i) % count;
|
|
58
|
+
if (items[index].toLowerCase().startsWith(query))
|
|
59
|
+
return index;
|
|
60
|
+
}
|
|
61
|
+
return -1;
|
|
62
|
+
},
|
|
63
|
+
reset() {
|
|
64
|
+
buffer = '';
|
|
65
|
+
last = 0;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=keyboard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keyboard.js","sourceRoot":"","sources":["../src/keyboard.ts"],"names":[],"mappings":"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","sourcesContent":["export interface NavigationOptions {\n\t/** Which arrow keys move. Defaults to `'horizontal'`. */\n\torientation?: 'horizontal' | 'vertical' | 'both';\n\t/** Wrap at the ends. Defaults to `true`. */\n\tloop?: boolean;\n\t/** Swap ArrowLeft/ArrowRight. Defaults to `false`. */\n\trtl?: boolean;\n}\n\n/**\n * Index an arrow/Home/End key moves to, or -1 when the key is not a navigation key for this\n * orientation. With no current item (-1) a forward key selects the first and a backward key the\n * last. Without `loop`, an edge returns the edge index itself, so the key still counts as handled.\n */\nexport function nextIndex(\n\tcurrent: number,\n\tcount: number,\n\tkey: string,\n\toptions: NavigationOptions = {}\n): number {\n\tif (count <= 0) return -1;\n\tif (key === 'Home') return 0;\n\tif (key === 'End') return count - 1;\n\tconst { orientation = 'horizontal', loop = true, rtl = false } = options;\n\tconst horizontal = orientation !== 'vertical';\n\tconst vertical = orientation !== 'horizontal';\n\tlet delta: number;\n\tif (horizontal && key === 'ArrowRight') delta = rtl ? -1 : 1;\n\telse if (horizontal && key === 'ArrowLeft') delta = rtl ? 1 : -1;\n\telse if (vertical && key === 'ArrowDown') delta = 1;\n\telse if (vertical && key === 'ArrowUp') delta = -1;\n\telse return -1;\n\tif (current < 0 || current >= count) return delta > 0 ? 0 : count - 1;\n\tconst next = current + delta;\n\tif (next < 0) return loop ? count - 1 : 0;\n\tif (next >= count) return loop ? 0 : count - 1;\n\treturn next;\n}\n\n/** A single printable character without a modifier, excluding Space. */\nexport function isTypeaheadKey(event: KeyboardEvent): boolean {\n\tconst key = event.key;\n\treturn key.length === 1 && key !== ' ' && !event.ctrlKey && !event.metaKey && !event.altKey;\n}\n\nexport interface Typeahead {\n\t/**\n\t * Adds `key` to the buffer and returns the index of the matching label, or -1. Typing the\n\t * same character repeatedly cycles through labels starting with it; a longer buffer matches\n\t * from the current item so the match can stay where it is.\n\t */\n\tsearch(key: string, items: readonly string[], current: number): number;\n\treset(): void;\n}\n\n/** Buffered typeahead matcher. The buffer resets after `timeout` ms (500 by default). */\nexport function createTypeahead(timeout = 500): Typeahead {\n\tlet buffer = '';\n\tlet last = 0;\n\treturn {\n\t\tsearch(key, items, current) {\n\t\t\tconst now = Date.now();\n\t\t\tconst char = key.toLowerCase();\n\t\t\tbuffer = now - last > timeout ? char : buffer + char;\n\t\t\tlast = now;\n\t\t\tconst first = buffer[0] as string;\n\t\t\tconst repeated = buffer.length > 1 && buffer === first.repeat(buffer.length);\n\t\t\tconst query = repeated ? first : buffer;\n\t\t\tconst from = Math.max(0, buffer.length > 1 && !repeated ? current : current + 1);\n\t\t\tconst count = items.length;\n\t\t\tfor (let i = 0; i < count; i++) {\n\t\t\t\tconst index = (from + i) % count;\n\t\t\t\tif ((items[index] as string).toLowerCase().startsWith(query)) return index;\n\t\t\t}\n\t\t\treturn -1;\n\t\t},\n\t\treset() {\n\t\t\tbuffer = '';\n\t\t\tlast = 0;\n\t\t}\n\t};\n}\n"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { BehaviorDefinition, Runtime } from './types.js';
|
|
2
|
+
/** The document runtime, created on first use. Throws outside a browser. */
|
|
3
|
+
export declare function getDefaultRuntime(): Runtime;
|
|
4
|
+
/**
|
|
5
|
+
* Registers a behavior on the document runtime. Safe to call during SSR: without a `document`
|
|
6
|
+
* the definition is kept until a browser runtime exists. Idempotent and HMR-safe.
|
|
7
|
+
*/
|
|
8
|
+
export declare function registerBehavior(name: string, definition: BehaviorDefinition): void;
|
|
9
|
+
export declare function unregisterBehavior(name: string): void;
|
package/dist/registry.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { createRuntimeInternal } from './runtime.js';
|
|
2
|
+
let defaultRuntime = null;
|
|
3
|
+
// Definitions registered where no `document` exists (SSR). They are installed by the first
|
|
4
|
+
// `getDefaultRuntime()` call in a browser.
|
|
5
|
+
const pending = new Map();
|
|
6
|
+
/** The document runtime, created on first use. Throws outside a browser. */
|
|
7
|
+
export function getDefaultRuntime() {
|
|
8
|
+
if (!defaultRuntime) {
|
|
9
|
+
if (typeof document === 'undefined') {
|
|
10
|
+
throw new Error('[Svelte Lean] getDefaultRuntime() needs a document. Use registerBehavior() in code that may run on the server.');
|
|
11
|
+
}
|
|
12
|
+
const runtime = createRuntimeInternal(document, () => {
|
|
13
|
+
if (defaultRuntime === runtime)
|
|
14
|
+
defaultRuntime = null;
|
|
15
|
+
});
|
|
16
|
+
defaultRuntime = runtime;
|
|
17
|
+
for (const [name, definition] of pending)
|
|
18
|
+
runtime.register(name, definition);
|
|
19
|
+
pending.clear();
|
|
20
|
+
}
|
|
21
|
+
return defaultRuntime;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Registers a behavior on the document runtime. Safe to call during SSR: without a `document`
|
|
25
|
+
* the definition is kept until a browser runtime exists. Idempotent and HMR-safe.
|
|
26
|
+
*/
|
|
27
|
+
export function registerBehavior(name, definition) {
|
|
28
|
+
if (typeof document === 'undefined')
|
|
29
|
+
pending.set(name, definition);
|
|
30
|
+
else
|
|
31
|
+
getDefaultRuntime().register(name, definition);
|
|
32
|
+
}
|
|
33
|
+
export function unregisterBehavior(name) {
|
|
34
|
+
pending.delete(name);
|
|
35
|
+
defaultRuntime?.unregister(name);
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../src/registry.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAErD,IAAI,cAAc,GAAmB,IAAI,CAAC;AAE1C,2FAA2F;AAC3F,2CAA2C;AAC3C,MAAM,OAAO,GAAG,IAAI,GAAG,EAA8B,CAAC;AAEtD,4EAA4E;AAC5E,MAAM,UAAU,iBAAiB;IAChC,IAAI,CAAC,cAAc,EAAE,CAAC;QACrB,IAAI,OAAO,QAAQ,KAAK,WAAW,EAAE,CAAC;YACrC,MAAM,IAAI,KAAK,CACd,gHAAgH,CAChH,CAAC;QACH,CAAC;QACD,MAAM,OAAO,GAAG,qBAAqB,CAAC,QAAQ,EAAE,GAAG,EAAE;YACpD,IAAI,cAAc,KAAK,OAAO;gBAAE,cAAc,GAAG,IAAI,CAAC;QACvD,CAAC,CAAC,CAAC;QACH,cAAc,GAAG,OAAO,CAAC;QACzB,KAAK,MAAM,CAAC,IAAI,EAAE,UAAU,CAAC,IAAI,OAAO;YAAE,OAAO,CAAC,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;QAC7E,OAAO,CAAC,KAAK,EAAE,CAAC;IACjB,CAAC;IACD,OAAO,cAAc,CAAC;AACvB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,gBAAgB,CAAC,IAAY,EAAE,UAA8B;IAC5E,IAAI,OAAO,QAAQ,KAAK,WAAW;QAAE,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;;QAC9D,iBAAiB,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;AACrD,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAC,IAAY;IAC9C,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACrB,cAAc,EAAE,UAAU,CAAC,IAAI,CAAC,CAAC;AAClC,CAAC","sourcesContent":["import type { BehaviorDefinition, Runtime } from './types.js';\nimport { createRuntimeInternal } from './runtime.js';\n\nlet defaultRuntime: Runtime | null = null;\n\n// Definitions registered where no `document` exists (SSR). They are installed by the first\n// `getDefaultRuntime()` call in a browser.\nconst pending = new Map<string, BehaviorDefinition>();\n\n/** The document runtime, created on first use. Throws outside a browser. */\nexport function getDefaultRuntime(): Runtime {\n\tif (!defaultRuntime) {\n\t\tif (typeof document === 'undefined') {\n\t\t\tthrow new Error(\n\t\t\t\t'[Svelte Lean] getDefaultRuntime() needs a document. Use registerBehavior() in code that may run on the server.'\n\t\t\t);\n\t\t}\n\t\tconst runtime = createRuntimeInternal(document, () => {\n\t\t\tif (defaultRuntime === runtime) defaultRuntime = null;\n\t\t});\n\t\tdefaultRuntime = runtime;\n\t\tfor (const [name, definition] of pending) runtime.register(name, definition);\n\t\tpending.clear();\n\t}\n\treturn defaultRuntime;\n}\n\n/**\n * Registers a behavior on the document runtime. Safe to call during SSR: without a `document`\n * the definition is kept until a browser runtime exists. Idempotent and HMR-safe.\n */\nexport function registerBehavior(name: string, definition: BehaviorDefinition): void {\n\tif (typeof document === 'undefined') pending.set(name, definition);\n\telse getDefaultRuntime().register(name, definition);\n}\n\nexport function unregisterBehavior(name: string): void {\n\tpending.delete(name);\n\tdefaultRuntime?.unregister(name);\n}\n"]}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Runtime, RuntimeOptions, RuntimeRoot, SleanEventType } from './types.js';
|
|
2
|
+
/** Every event type the router can install, in report order. */
|
|
3
|
+
export declare const EVENT_TYPES: readonly SleanEventType[];
|
|
4
|
+
/**
|
|
5
|
+
* Creates a runtime whose shared listeners attach to `root` (`document` by default).
|
|
6
|
+
* Nothing is installed until a behavior that needs an event type is registered.
|
|
7
|
+
*/
|
|
8
|
+
export declare function createRuntime(options?: RuntimeOptions): Runtime;
|
|
9
|
+
/** @internal `onDestroy` lets the default-runtime module forget a destroyed instance. */
|
|
10
|
+
export declare function createRuntimeInternal(root: RuntimeRoot, onDestroy?: () => void): Runtime;
|
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { DEV } from '#env';
|
|
2
|
+
import { resetValidation, trackRuntime, untrackRuntime, validateRoot, warn } from './diagnostics.js';
|
|
3
|
+
import { isDisabled } from './dom.js';
|
|
4
|
+
/** Every event type the router can install, in report order. */
|
|
5
|
+
export const EVENT_TYPES = [
|
|
6
|
+
'click',
|
|
7
|
+
'dblclick',
|
|
8
|
+
'keydown',
|
|
9
|
+
'keyup',
|
|
10
|
+
'focusin',
|
|
11
|
+
'focusout',
|
|
12
|
+
'input',
|
|
13
|
+
'change',
|
|
14
|
+
'pointerdown',
|
|
15
|
+
'pointerup',
|
|
16
|
+
'pointerover',
|
|
17
|
+
'pointerout',
|
|
18
|
+
'contextmenu',
|
|
19
|
+
'dragenter',
|
|
20
|
+
'dragleave',
|
|
21
|
+
'drop',
|
|
22
|
+
'toggle',
|
|
23
|
+
'beforetoggle'
|
|
24
|
+
];
|
|
25
|
+
// `toggle` and `beforetoggle` do not bubble; a root listener only observes them in the capture
|
|
26
|
+
// phase. Every other routed type bubbles, so it stays in the bubble phase and element-level
|
|
27
|
+
// listeners keep their normal ordering.
|
|
28
|
+
const CAPTURE = { toggle: true, beforetoggle: true };
|
|
29
|
+
/** Development check, run once per registration: every handled event type must be routable. */
|
|
30
|
+
function checkEventTypes(name, definition) {
|
|
31
|
+
for (const type in definition.events) {
|
|
32
|
+
if (!definition.events[type])
|
|
33
|
+
continue;
|
|
34
|
+
if (!EVENT_TYPES.includes(type)) {
|
|
35
|
+
warn(name, `"${type}" is not a routed event type; use a scoped listener instead.`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Creates a runtime whose shared listeners attach to `root` (`document` by default).
|
|
41
|
+
* Nothing is installed until a behavior that needs an event type is registered.
|
|
42
|
+
*/
|
|
43
|
+
export function createRuntime(options = {}) {
|
|
44
|
+
return createRuntimeInternal(options.root ?? document);
|
|
45
|
+
}
|
|
46
|
+
/** @internal `onDestroy` lets the default-runtime module forget a destroyed instance. */
|
|
47
|
+
export function createRuntimeInternal(root, onDestroy) {
|
|
48
|
+
const behaviors = new Map();
|
|
49
|
+
const installed = new Set();
|
|
50
|
+
const route = (event) => {
|
|
51
|
+
const type = event.type;
|
|
52
|
+
const path = event.composedPath();
|
|
53
|
+
const first = path[0];
|
|
54
|
+
const target = (first && first.nodeType === 1 ? first : event.target);
|
|
55
|
+
for (let i = 0; i < path.length; i++) {
|
|
56
|
+
const node = path[i];
|
|
57
|
+
if (node.nodeType !== 1)
|
|
58
|
+
continue;
|
|
59
|
+
const name = node.getAttribute('data-slean');
|
|
60
|
+
if (!name)
|
|
61
|
+
continue;
|
|
62
|
+
const definition = behaviors.get(name);
|
|
63
|
+
const handler = definition?.events?.[type];
|
|
64
|
+
if (!handler || isDisabled(node))
|
|
65
|
+
continue;
|
|
66
|
+
if (DEV)
|
|
67
|
+
validateRoot(name, definition, node);
|
|
68
|
+
handler({ root: node, target, event, runtime });
|
|
69
|
+
if (event.cancelBubble)
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
// Makes the installed set equal to the set of routed event types some registered behavior
|
|
74
|
+
// handles. Unroutable types were reported once, at registration.
|
|
75
|
+
const sync = () => {
|
|
76
|
+
const needed = new Set();
|
|
77
|
+
for (const definition of behaviors.values()) {
|
|
78
|
+
const events = definition.events;
|
|
79
|
+
for (const type in events) {
|
|
80
|
+
if (!events[type])
|
|
81
|
+
continue;
|
|
82
|
+
if (EVENT_TYPES.includes(type))
|
|
83
|
+
needed.add(type);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
for (const type of needed) {
|
|
87
|
+
if (installed.has(type))
|
|
88
|
+
continue;
|
|
89
|
+
root.addEventListener(type, route, CAPTURE[type] === true);
|
|
90
|
+
installed.add(type);
|
|
91
|
+
}
|
|
92
|
+
for (const type of installed) {
|
|
93
|
+
if (needed.has(type))
|
|
94
|
+
continue;
|
|
95
|
+
root.removeEventListener(type, route, CAPTURE[type] === true);
|
|
96
|
+
installed.delete(type);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
const runtime = {
|
|
100
|
+
root,
|
|
101
|
+
register(name, definition) {
|
|
102
|
+
behaviors.set(name, definition);
|
|
103
|
+
if (DEV)
|
|
104
|
+
checkEventTypes(name, definition);
|
|
105
|
+
if (DEV)
|
|
106
|
+
resetValidation();
|
|
107
|
+
sync();
|
|
108
|
+
},
|
|
109
|
+
unregister(name) {
|
|
110
|
+
if (behaviors.delete(name))
|
|
111
|
+
sync();
|
|
112
|
+
},
|
|
113
|
+
has: (name) => behaviors.has(name),
|
|
114
|
+
behaviors: () => [...behaviors.keys()],
|
|
115
|
+
listeners() {
|
|
116
|
+
const out = {};
|
|
117
|
+
for (const type of installed)
|
|
118
|
+
out[type] = 1;
|
|
119
|
+
return out;
|
|
120
|
+
},
|
|
121
|
+
destroy() {
|
|
122
|
+
behaviors.clear();
|
|
123
|
+
sync();
|
|
124
|
+
untrackRuntime(runtime);
|
|
125
|
+
onDestroy?.();
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
trackRuntime(runtime);
|
|
129
|
+
return runtime;
|
|
130
|
+
}
|
|
131
|
+
//# sourceMappingURL=runtime.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime.js","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"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","sourcesContent":["import type {\n\tBehaviorDefinition,\n\tBehaviorHandler,\n\tRuntime,\n\tRuntimeOptions,\n\tRuntimeRoot,\n\tSleanEventType\n} from './types.js';\nimport { DEV } from '#env';\nimport {\n\tresetValidation,\n\ttrackRuntime,\n\tuntrackRuntime,\n\tvalidateRoot,\n\twarn\n} from './diagnostics.js';\nimport { isDisabled } from './dom.js';\n\n/** Every event type the router can install, in report order. */\nexport const EVENT_TYPES: readonly SleanEventType[] = [\n\t'click',\n\t'dblclick',\n\t'keydown',\n\t'keyup',\n\t'focusin',\n\t'focusout',\n\t'input',\n\t'change',\n\t'pointerdown',\n\t'pointerup',\n\t'pointerover',\n\t'pointerout',\n\t'contextmenu',\n\t'dragenter',\n\t'dragleave',\n\t'drop',\n\t'toggle',\n\t'beforetoggle'\n];\n\n// `toggle` and `beforetoggle` do not bubble; a root listener only observes them in the capture\n// phase. Every other routed type bubbles, so it stays in the bubble phase and element-level\n// listeners keep their normal ordering.\nconst CAPTURE: Partial<Record<SleanEventType, true>> = { toggle: true, beforetoggle: true };\n\n/** Development check, run once per registration: every handled event type must be routable. */\nfunction checkEventTypes(name: string, definition: BehaviorDefinition): void {\n\tfor (const type in definition.events) {\n\t\tif (!definition.events[type as SleanEventType]) continue;\n\t\tif (!(EVENT_TYPES as readonly string[]).includes(type)) {\n\t\t\twarn(name, `\"${type}\" is not a routed event type; use a scoped listener instead.`);\n\t\t}\n\t}\n}\n\n/**\n * Creates a runtime whose shared listeners attach to `root` (`document` by default).\n * Nothing is installed until a behavior that needs an event type is registered.\n */\nexport function createRuntime(options: RuntimeOptions = {}): Runtime {\n\treturn createRuntimeInternal(options.root ?? document);\n}\n\n/** @internal `onDestroy` lets the default-runtime module forget a destroyed instance. */\nexport function createRuntimeInternal(root: RuntimeRoot, onDestroy?: () => void): Runtime {\n\tconst behaviors = new Map<string, BehaviorDefinition>();\n\tconst installed = new Set<SleanEventType>();\n\n\tconst route = (event: Event): void => {\n\t\tconst type = event.type as SleanEventType;\n\t\tconst path = event.composedPath();\n\t\tconst first = path[0] as Node | undefined;\n\t\tconst target = (first && first.nodeType === 1 ? first : event.target) as Element;\n\t\tfor (let i = 0; i < path.length; i++) {\n\t\t\tconst node = path[i] as HTMLElement;\n\t\t\tif (node.nodeType !== 1) continue;\n\t\t\tconst name = node.getAttribute('data-slean');\n\t\t\tif (!name) continue;\n\t\t\tconst definition = behaviors.get(name);\n\t\t\tconst handler = definition?.events?.[type];\n\t\t\tif (!handler || isDisabled(node)) continue;\n\t\t\tif (DEV) validateRoot(name, definition as BehaviorDefinition, node);\n\t\t\t(handler as BehaviorHandler)({ root: node, target, event, runtime });\n\t\t\tif (event.cancelBubble) return;\n\t\t}\n\t};\n\n\t// Makes the installed set equal to the set of routed event types some registered behavior\n\t// handles. Unroutable types were reported once, at registration.\n\tconst sync = (): void => {\n\t\tconst needed = new Set<SleanEventType>();\n\t\tfor (const definition of behaviors.values()) {\n\t\t\tconst events = definition.events;\n\t\t\tfor (const type in events) {\n\t\t\t\tif (!events[type as SleanEventType]) continue;\n\t\t\t\tif ((EVENT_TYPES as readonly string[]).includes(type)) needed.add(type as SleanEventType);\n\t\t\t}\n\t\t}\n\t\tfor (const type of needed) {\n\t\t\tif (installed.has(type)) continue;\n\t\t\troot.addEventListener(type, route, CAPTURE[type] === true);\n\t\t\tinstalled.add(type);\n\t\t}\n\t\tfor (const type of installed) {\n\t\t\tif (needed.has(type)) continue;\n\t\t\troot.removeEventListener(type, route, CAPTURE[type] === true);\n\t\t\tinstalled.delete(type);\n\t\t}\n\t};\n\n\tconst runtime: Runtime = {\n\t\troot,\n\t\tregister(name, definition) {\n\t\t\tbehaviors.set(name, definition);\n\t\t\tif (DEV) checkEventTypes(name, definition);\n\t\t\tif (DEV) resetValidation();\n\t\t\tsync();\n\t\t},\n\t\tunregister(name) {\n\t\t\tif (behaviors.delete(name)) sync();\n\t\t},\n\t\thas: (name) => behaviors.has(name),\n\t\tbehaviors: () => [...behaviors.keys()],\n\t\tlisteners() {\n\t\t\tconst out: Record<string, number> = {};\n\t\t\tfor (const type of installed) out[type] = 1;\n\t\t\treturn out;\n\t\t},\n\t\tdestroy() {\n\t\t\tbehaviors.clear();\n\t\t\tsync();\n\t\t\tuntrackRuntime(runtime);\n\t\t\tonDestroy?.();\n\t\t}\n\t};\n\ttrackRuntime(runtime);\n\treturn runtime;\n}\n"]}
|
package/dist/state.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface LazyState<T> {
|
|
2
|
+
/** Returns the state for `root`, creating it on first access. */
|
|
3
|
+
get(root: HTMLElement): T;
|
|
4
|
+
has(root: HTMLElement): boolean;
|
|
5
|
+
delete(root: HTMLElement): boolean;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Per-root state that is allocated on first use and released with the element (`WeakMap`).
|
|
9
|
+
* `create` must return an object; `undefined` is treated as "not created".
|
|
10
|
+
*/
|
|
11
|
+
export declare function lazyState<T extends object>(create: (root: HTMLElement) => T): LazyState<T>;
|
package/dist/state.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-root state that is allocated on first use and released with the element (`WeakMap`).
|
|
3
|
+
* `create` must return an object; `undefined` is treated as "not created".
|
|
4
|
+
*/
|
|
5
|
+
export function lazyState(create) {
|
|
6
|
+
const states = new WeakMap();
|
|
7
|
+
return {
|
|
8
|
+
get(root) {
|
|
9
|
+
let state = states.get(root);
|
|
10
|
+
if (state === undefined) {
|
|
11
|
+
state = create(root);
|
|
12
|
+
states.set(root, state);
|
|
13
|
+
}
|
|
14
|
+
return state;
|
|
15
|
+
},
|
|
16
|
+
has: (root) => states.has(root),
|
|
17
|
+
delete: (root) => states.delete(root)
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=state.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state.js","sourceRoot":"","sources":["../src/state.ts"],"names":[],"mappings":"AAOA;;;GAGG;AACH,MAAM,UAAU,SAAS,CAAmB,MAAgC;IAC3E,MAAM,MAAM,GAAG,IAAI,OAAO,EAAkB,CAAC;IAC7C,OAAO;QACN,GAAG,CAAC,IAAI;YACP,IAAI,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YAC7B,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;gBACzB,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;gBACrB,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;YACzB,CAAC;YACD,OAAO,KAAK,CAAC;QACd,CAAC;QACD,GAAG,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC;QAC/B,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC;KACrC,CAAC;AACH,CAAC","sourcesContent":["export interface LazyState<T> {\n\t/** Returns the state for `root`, creating it on first access. */\n\tget(root: HTMLElement): T;\n\thas(root: HTMLElement): boolean;\n\tdelete(root: HTMLElement): boolean;\n}\n\n/**\n * Per-root state that is allocated on first use and released with the element (`WeakMap`).\n * `create` must return an object; `undefined` is treated as \"not created\".\n */\nexport function lazyState<T extends object>(create: (root: HTMLElement) => T): LazyState<T> {\n\tconst states = new WeakMap<HTMLElement, T>();\n\treturn {\n\t\tget(root) {\n\t\t\tlet state = states.get(root);\n\t\t\tif (state === undefined) {\n\t\t\t\tstate = create(root);\n\t\t\t\tstates.set(root, state);\n\t\t\t}\n\t\t\treturn state;\n\t\t},\n\t\thas: (root) => states.has(root),\n\t\tdelete: (root) => states.delete(root)\n\t};\n}\n"]}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Event types the shared router is allowed to install. Nothing outside this list is ever
|
|
3
|
+
* installed generically; in particular `pointermove`, `scroll` and `resize` belong to scoped
|
|
4
|
+
* controllers (Tier 2) with an explicit lifetime. `pointerover` and `pointerout` are routed: they
|
|
5
|
+
* bubble and fire when the pointer crosses an element boundary, not on every movement (tooltip,
|
|
6
|
+
* toast). `contextmenu` is routed for the context menu: it bubbles and fires once per request
|
|
7
|
+
* (secondary click, long press, the context-menu key or Shift+F10). `dragenter`, `dragleave` and
|
|
8
|
+
* `drop` are routed for the file drop area; `dragover`, which fires continuously, is not.
|
|
9
|
+
*/
|
|
10
|
+
export type SleanEventType = 'click' | 'dblclick' | 'keydown' | 'keyup' | 'focusin' | 'focusout' | 'input' | 'change' | 'pointerdown' | 'pointerup' | 'pointerover' | 'pointerout' | 'contextmenu' | 'dragenter' | 'dragleave' | 'drop' | 'toggle' | 'beforetoggle';
|
|
11
|
+
/** Native event class per routed event type. */
|
|
12
|
+
export type SleanEventMap = Pick<HTMLElementEventMap, SleanEventType>;
|
|
13
|
+
/** What a behavior handler receives. `target` is the deepest element of the composed path. */
|
|
14
|
+
export interface BehaviorContext<E extends Event = Event> {
|
|
15
|
+
root: HTMLElement;
|
|
16
|
+
target: Element;
|
|
17
|
+
event: E;
|
|
18
|
+
runtime: Runtime;
|
|
19
|
+
}
|
|
20
|
+
export type BehaviorHandler<E extends Event = Event> = (context: BehaviorContext<E>) => void;
|
|
21
|
+
export type BehaviorEvents = {
|
|
22
|
+
[K in SleanEventType]?: BehaviorHandler<SleanEventMap[K]>;
|
|
23
|
+
};
|
|
24
|
+
export interface BehaviorDefinition {
|
|
25
|
+
/** Runtime tier as declared in the primitive's contract (ADR 0003). Tier 0 never registers. */
|
|
26
|
+
tier: 1 | 2;
|
|
27
|
+
/** Handlers keyed by event type. Only the listed types get a shared listener. */
|
|
28
|
+
events?: BehaviorEvents;
|
|
29
|
+
/**
|
|
30
|
+
* Development-only markup validation. Called once per root on the first routed event, in
|
|
31
|
+
* development builds only; each returned string is printed through `warn()`. Primitives may
|
|
32
|
+
* strip it from production definitions.
|
|
33
|
+
*/
|
|
34
|
+
validate?: (root: HTMLElement) => string[];
|
|
35
|
+
}
|
|
36
|
+
export type RuntimeRoot = Document | ShadowRoot | HTMLElement;
|
|
37
|
+
export interface RuntimeOptions {
|
|
38
|
+
/** Event target the shared listeners attach to. Defaults to `document`. */
|
|
39
|
+
root?: RuntimeRoot;
|
|
40
|
+
}
|
|
41
|
+
export interface Runtime {
|
|
42
|
+
readonly root: RuntimeRoot;
|
|
43
|
+
/** Idempotent: registering the same name again replaces the definition without adding listeners. */
|
|
44
|
+
register(name: string, definition: BehaviorDefinition): void;
|
|
45
|
+
unregister(name: string): void;
|
|
46
|
+
has(name: string): boolean;
|
|
47
|
+
behaviors(): string[];
|
|
48
|
+
/** Installed shared listeners per event type; every installed type maps to exactly 1. */
|
|
49
|
+
listeners(): Record<string, number>;
|
|
50
|
+
/** Removes every listener and definition. A destroyed runtime must not be reused. */
|
|
51
|
+
destroy(): void;
|
|
52
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Event types the shared router is allowed to install. Nothing outside this list is ever\n * installed generically; in particular `pointermove`, `scroll` and `resize` belong to scoped\n * controllers (Tier 2) with an explicit lifetime. `pointerover` and `pointerout` are routed: they\n * bubble and fire when the pointer crosses an element boundary, not on every movement (tooltip,\n * toast). `contextmenu` is routed for the context menu: it bubbles and fires once per request\n * (secondary click, long press, the context-menu key or Shift+F10). `dragenter`, `dragleave` and\n * `drop` are routed for the file drop area; `dragover`, which fires continuously, is not.\n */\nexport type SleanEventType =\n\t| 'click'\n\t| 'dblclick'\n\t| 'keydown'\n\t| 'keyup'\n\t| 'focusin'\n\t| 'focusout'\n\t| 'input'\n\t| 'change'\n\t| 'pointerdown'\n\t| 'pointerup'\n\t| 'pointerover'\n\t| 'pointerout'\n\t| 'contextmenu'\n\t| 'dragenter'\n\t| 'dragleave'\n\t| 'drop'\n\t| 'toggle'\n\t| 'beforetoggle';\n\n/** Native event class per routed event type. */\nexport type SleanEventMap = Pick<HTMLElementEventMap, SleanEventType>;\n\n/** What a behavior handler receives. `target` is the deepest element of the composed path. */\nexport interface BehaviorContext<E extends Event = Event> {\n\troot: HTMLElement;\n\ttarget: Element;\n\tevent: E;\n\truntime: Runtime;\n}\n\nexport type BehaviorHandler<E extends Event = Event> = (context: BehaviorContext<E>) => void;\n\nexport type BehaviorEvents = {\n\t[K in SleanEventType]?: BehaviorHandler<SleanEventMap[K]>;\n};\n\nexport interface BehaviorDefinition {\n\t/** Runtime tier as declared in the primitive's contract (ADR 0003). Tier 0 never registers. */\n\ttier: 1 | 2;\n\t/** Handlers keyed by event type. Only the listed types get a shared listener. */\n\tevents?: BehaviorEvents;\n\t/**\n\t * Development-only markup validation. Called once per root on the first routed event, in\n\t * development builds only; each returned string is printed through `warn()`. Primitives may\n\t * strip it from production definitions.\n\t */\n\tvalidate?: (root: HTMLElement) => string[];\n}\n\nexport type RuntimeRoot = Document | ShadowRoot | HTMLElement;\n\nexport interface RuntimeOptions {\n\t/** Event target the shared listeners attach to. Defaults to `document`. */\n\troot?: RuntimeRoot;\n}\n\nexport interface Runtime {\n\treadonly root: RuntimeRoot;\n\t/** Idempotent: registering the same name again replaces the definition without adding listeners. */\n\tregister(name: string, definition: BehaviorDefinition): void;\n\tunregister(name: string): void;\n\thas(name: string): boolean;\n\tbehaviors(): string[];\n\t/** Installed shared listeners per event type; every installed type maps to exactly 1. */\n\tlisteners(): Record<string, number>;\n\t/** Removes every listener and definition. A destroyed runtime must not be reused. */\n\tdestroy(): void;\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@svelte-lean/core",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Svelte Lean runtime kernel: behavior registry, shared event router, DOM and keyboard helpers.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Ömer Say",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./package.json": "./package.json"
|
|
19
|
+
},
|
|
20
|
+
"imports": {
|
|
21
|
+
"#env": {
|
|
22
|
+
"development": "./dist/env.development.js",
|
|
23
|
+
"production": "./dist/env.production.js",
|
|
24
|
+
"default": "./dist/env.js"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\" && yarn run -T tsc -p tsconfig.build.json && yarn run -T publint",
|
|
32
|
+
"check": "yarn run -T tsc -p tsconfig.json --noEmit",
|
|
33
|
+
"test": "yarn run -T vitest run",
|
|
34
|
+
"size": "node scripts/size.mjs"
|
|
35
|
+
},
|
|
36
|
+
"homepage": "https://svelte-lean.vercel.app/docs/architecture/dom-protocol",
|
|
37
|
+
"bugs": {
|
|
38
|
+
"url": "https://github.com/say16/svelte-lean/issues"
|
|
39
|
+
},
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "git+https://github.com/say16/svelte-lean.git",
|
|
43
|
+
"directory": "packages/core"
|
|
44
|
+
},
|
|
45
|
+
"keywords": [
|
|
46
|
+
"svelte",
|
|
47
|
+
"svelte5",
|
|
48
|
+
"svelte-lean",
|
|
49
|
+
"ui",
|
|
50
|
+
"runtime",
|
|
51
|
+
"event-delegation",
|
|
52
|
+
"dom",
|
|
53
|
+
"accessibility",
|
|
54
|
+
"ssr"
|
|
55
|
+
]
|
|
56
|
+
}
|