@streetui/context 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 StreetUI contributors
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,112 @@
1
+ # @streetui/context
2
+
3
+ Minimal build-time provider/consumer scoping for the StreetUI DSL. This is **not**
4
+ a re-implementation of React Context and it is **not** a second reactive system —
5
+ it is a tiny synchronous value stack that mirrors how StreetUI builds its
6
+ semantic tree.
7
+
8
+ ```bash
9
+ # part of the StreetUI monorepo — no separate install
10
+ ```
11
+
12
+ ---
13
+
14
+ ## Why it exists
15
+
16
+ StreetUI builds its semantic tree synchronously, top-down, when the DSL builders
17
+ run. A `Context` mirrors that shape exactly: `provide(value, run)` pushes a value
18
+ for the duration of the synchronous `run()` — during which the child DSL builders
19
+ execute and may `consume()` — then pops it again. This lets deeply nested field
20
+ helpers reach a shared form, an i18n instance, a theme, or any other ambient
21
+ value **without prop drilling**, while staying entirely inside the existing
22
+ build pass.
23
+
24
+ Because `provide()` pops its value as soon as `run()` returns (even if it
25
+ throws), the context holds no subscriptions and leaves no references behind. If
26
+ you need reactivity, put a **signal** into the context — reactivity then belongs
27
+ to that signal and is torn down by the normal node lifecycle when the consuming
28
+ subtree unmounts.
29
+
30
+ ---
31
+
32
+ ## API
33
+
34
+ ```ts
35
+ import { createContext, type Context } from '@streetui/context';
36
+
37
+ const context = createContext<T>(defaultValue, 'optional.debug.description');
38
+ ```
39
+
40
+ | Member | Description |
41
+ |---|---|
42
+ | `context.provide(value, run)` | Provide `value` to any `consume()` made synchronously inside `run`; returns `run`'s result. |
43
+ | `context.consume()` | Read the **nearest** active provider's value, or `defaultValue` when none is active. |
44
+ | `context.hasProvider()` | True while at least one provider is active. |
45
+ | `context.defaultValue` | The value `consume()` returns with no active provider. |
46
+ | `context.id` | A unique `symbol` identity (handy for debugging/inspection). |
47
+
48
+ The default value is **required**, so `consume()` always returns a `T` — never
49
+ `undefined` unless `T` itself permits it.
50
+
51
+ ---
52
+
53
+ ## Example — sharing a form down to field helpers
54
+
55
+ ```ts
56
+ import { createContext } from '@streetui/context';
57
+
58
+ interface FormScope { form: Form<SignupValues>; i18n: AccountI18n; }
59
+ const FormContext = createContext<FormScope | null>(null, 'streetui.account.form');
60
+
61
+ function textField(scope: ContainerDSL, name: keyof SignupValues & string) {
62
+ const scoped = FormContext.consume();
63
+ if (scoped === null) throw new Error('textField must run inside a FormContext provider');
64
+ const { form, i18n } = scoped;
65
+ const field = form.field(name);
66
+ scope.input({ bind: field.value });
67
+ }
68
+
69
+ // Provider wraps the synchronous builder run:
70
+ page.form('signup', (fb) => {
71
+ FormContext.provide({ form, i18n }, () => {
72
+ textField(fb, 'name');
73
+ textField(fb, 'email');
74
+ textField(fb, 'password');
75
+ });
76
+ });
77
+ ```
78
+
79
+ ## Nested providers resolve nearest
80
+
81
+ ```ts
82
+ const Theme = createContext('light');
83
+
84
+ Theme.provide('dark', () => {
85
+ Theme.consume(); // 'dark'
86
+ Theme.provide('high-contrast', () => {
87
+ Theme.consume(); // 'high-contrast' (nearest wins)
88
+ });
89
+ Theme.consume(); // 'dark' again — inner value popped
90
+ });
91
+ Theme.consume(); // 'light' (the default)
92
+ ```
93
+
94
+ ---
95
+
96
+ ## Reactive context
97
+
98
+ The value can be a signal (or an object holding signals). The context stores the
99
+ reference; the signal owns the reactivity:
100
+
101
+ ```ts
102
+ const Locale = createContext(signal('en'));
103
+
104
+ Locale.provide(signal('fr'), () => {
105
+ const locale = Locale.consume(); // Signal<string>
106
+ page.text(locale); // reactive — re-renders when the signal changes
107
+ });
108
+ ```
109
+
110
+ Consume only inside a provider's synchronous `run()`. Consuming later (e.g. from
111
+ an async callback that runs after `provide()` has returned) resolves to the
112
+ default, by design — the value has already been popped.
package/dist/index.cjs ADDED
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ createContext: () => createContext
24
+ });
25
+ module.exports = __toCommonJS(index_exports);
26
+
27
+ // src/context.ts
28
+ function createContext(defaultValue, description) {
29
+ const id = Symbol(description ?? "streetui.context");
30
+ const stack = [];
31
+ return {
32
+ id,
33
+ defaultValue,
34
+ provide(value, run) {
35
+ stack.push(value);
36
+ try {
37
+ return run();
38
+ } finally {
39
+ stack.pop();
40
+ }
41
+ },
42
+ consume() {
43
+ return stack.length > 0 ? stack[stack.length - 1] : defaultValue;
44
+ },
45
+ hasProvider() {
46
+ return stack.length > 0;
47
+ }
48
+ };
49
+ }
50
+ // Annotate the CommonJS export names for ESM import in node:
51
+ 0 && (module.exports = {
52
+ createContext
53
+ });
54
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/context.ts"],"sourcesContent":["export * from './context.js';\n","/**\n * @streetui/context — build-time provider/consumer scoping.\n *\n * StreetUI builds its semantic tree synchronously, top-down, when the DSL\n * builders run. A `Context` mirrors that shape: `provide(value, run)` pushes a\n * value for the duration of the synchronous `run()` (during which the child\n * DSL builders execute and may `consume()`), then pops it. Consumers resolve\n * the *nearest* enclosing provider, falling back to the context default.\n *\n * This is deliberately NOT a second reactive system. A context value can be a\n * signal (see @streetui/state); reactivity then belongs to that signal and is\n * torn down by the normal node lifecycle when the consuming subtree unmounts —\n * the context itself holds no subscriptions and leaves no refs behind after a\n * `provide()` call returns.\n */\n\nexport interface Context<T> {\n /** Unique identity for this context (useful for debugging/inspection). */\n readonly id: symbol;\n /** The value returned by {@link consume} when no provider is active. */\n readonly defaultValue: T;\n /**\n * Provide `value` to any `consume()` calls made synchronously inside `run`.\n * The value is popped again as soon as `run` returns (even if it throws),\n * so nesting resolves to the nearest active provider.\n */\n provide<R>(value: T, run: () => R): R;\n /** Read the nearest active provider's value, or {@link defaultValue}. */\n consume(): T;\n /** True while at least one provider is active for this context. */\n hasProvider(): boolean;\n}\n\n/**\n * Create a typed context with a required default value, so `consume()` always\n * returns a `T` (never `undefined` unless `T` itself permits it).\n */\nexport function createContext<T>(defaultValue: T, description?: string): Context<T> {\n const id = Symbol(description ?? 'streetui.context');\n const stack: T[] = [];\n\n return {\n id,\n defaultValue,\n provide<R>(value: T, run: () => R): R {\n stack.push(value);\n try {\n return run();\n } finally {\n stack.pop();\n }\n },\n consume(): T {\n return stack.length > 0 ? (stack[stack.length - 1] as T) : defaultValue;\n },\n hasProvider(): boolean {\n return stack.length > 0;\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACqCO,SAAS,cAAiB,cAAiB,aAAkC;AAClF,QAAM,KAAK,OAAO,eAAe,kBAAkB;AACnD,QAAM,QAAa,CAAC;AAEpB,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA,QAAW,OAAU,KAAiB;AACpC,YAAM,KAAK,KAAK;AAChB,UAAI;AACF,eAAO,IAAI;AAAA,MACb,UAAE;AACA,cAAM,IAAI;AAAA,MACZ;AAAA,IACF;AAAA,IACA,UAAa;AACX,aAAO,MAAM,SAAS,IAAK,MAAM,MAAM,SAAS,CAAC,IAAU;AAAA,IAC7D;AAAA,IACA,cAAuB;AACrB,aAAO,MAAM,SAAS;AAAA,IACxB;AAAA,EACF;AACF;","names":[]}
@@ -0,0 +1,38 @@
1
+ /**
2
+ * @streetui/context — build-time provider/consumer scoping.
3
+ *
4
+ * StreetUI builds its semantic tree synchronously, top-down, when the DSL
5
+ * builders run. A `Context` mirrors that shape: `provide(value, run)` pushes a
6
+ * value for the duration of the synchronous `run()` (during which the child
7
+ * DSL builders execute and may `consume()`), then pops it. Consumers resolve
8
+ * the *nearest* enclosing provider, falling back to the context default.
9
+ *
10
+ * This is deliberately NOT a second reactive system. A context value can be a
11
+ * signal (see @streetui/state); reactivity then belongs to that signal and is
12
+ * torn down by the normal node lifecycle when the consuming subtree unmounts —
13
+ * the context itself holds no subscriptions and leaves no refs behind after a
14
+ * `provide()` call returns.
15
+ */
16
+ interface Context<T> {
17
+ /** Unique identity for this context (useful for debugging/inspection). */
18
+ readonly id: symbol;
19
+ /** The value returned by {@link consume} when no provider is active. */
20
+ readonly defaultValue: T;
21
+ /**
22
+ * Provide `value` to any `consume()` calls made synchronously inside `run`.
23
+ * The value is popped again as soon as `run` returns (even if it throws),
24
+ * so nesting resolves to the nearest active provider.
25
+ */
26
+ provide<R>(value: T, run: () => R): R;
27
+ /** Read the nearest active provider's value, or {@link defaultValue}. */
28
+ consume(): T;
29
+ /** True while at least one provider is active for this context. */
30
+ hasProvider(): boolean;
31
+ }
32
+ /**
33
+ * Create a typed context with a required default value, so `consume()` always
34
+ * returns a `T` (never `undefined` unless `T` itself permits it).
35
+ */
36
+ declare function createContext<T>(defaultValue: T, description?: string): Context<T>;
37
+
38
+ export { type Context, createContext };
@@ -0,0 +1,38 @@
1
+ /**
2
+ * @streetui/context — build-time provider/consumer scoping.
3
+ *
4
+ * StreetUI builds its semantic tree synchronously, top-down, when the DSL
5
+ * builders run. A `Context` mirrors that shape: `provide(value, run)` pushes a
6
+ * value for the duration of the synchronous `run()` (during which the child
7
+ * DSL builders execute and may `consume()`), then pops it. Consumers resolve
8
+ * the *nearest* enclosing provider, falling back to the context default.
9
+ *
10
+ * This is deliberately NOT a second reactive system. A context value can be a
11
+ * signal (see @streetui/state); reactivity then belongs to that signal and is
12
+ * torn down by the normal node lifecycle when the consuming subtree unmounts —
13
+ * the context itself holds no subscriptions and leaves no refs behind after a
14
+ * `provide()` call returns.
15
+ */
16
+ interface Context<T> {
17
+ /** Unique identity for this context (useful for debugging/inspection). */
18
+ readonly id: symbol;
19
+ /** The value returned by {@link consume} when no provider is active. */
20
+ readonly defaultValue: T;
21
+ /**
22
+ * Provide `value` to any `consume()` calls made synchronously inside `run`.
23
+ * The value is popped again as soon as `run` returns (even if it throws),
24
+ * so nesting resolves to the nearest active provider.
25
+ */
26
+ provide<R>(value: T, run: () => R): R;
27
+ /** Read the nearest active provider's value, or {@link defaultValue}. */
28
+ consume(): T;
29
+ /** True while at least one provider is active for this context. */
30
+ hasProvider(): boolean;
31
+ }
32
+ /**
33
+ * Create a typed context with a required default value, so `consume()` always
34
+ * returns a `T` (never `undefined` unless `T` itself permits it).
35
+ */
36
+ declare function createContext<T>(defaultValue: T, description?: string): Context<T>;
37
+
38
+ export { type Context, createContext };
package/dist/index.js ADDED
@@ -0,0 +1,27 @@
1
+ // src/context.ts
2
+ function createContext(defaultValue, description) {
3
+ const id = Symbol(description ?? "streetui.context");
4
+ const stack = [];
5
+ return {
6
+ id,
7
+ defaultValue,
8
+ provide(value, run) {
9
+ stack.push(value);
10
+ try {
11
+ return run();
12
+ } finally {
13
+ stack.pop();
14
+ }
15
+ },
16
+ consume() {
17
+ return stack.length > 0 ? stack[stack.length - 1] : defaultValue;
18
+ },
19
+ hasProvider() {
20
+ return stack.length > 0;
21
+ }
22
+ };
23
+ }
24
+ export {
25
+ createContext
26
+ };
27
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/context.ts"],"sourcesContent":["/**\n * @streetui/context — build-time provider/consumer scoping.\n *\n * StreetUI builds its semantic tree synchronously, top-down, when the DSL\n * builders run. A `Context` mirrors that shape: `provide(value, run)` pushes a\n * value for the duration of the synchronous `run()` (during which the child\n * DSL builders execute and may `consume()`), then pops it. Consumers resolve\n * the *nearest* enclosing provider, falling back to the context default.\n *\n * This is deliberately NOT a second reactive system. A context value can be a\n * signal (see @streetui/state); reactivity then belongs to that signal and is\n * torn down by the normal node lifecycle when the consuming subtree unmounts —\n * the context itself holds no subscriptions and leaves no refs behind after a\n * `provide()` call returns.\n */\n\nexport interface Context<T> {\n /** Unique identity for this context (useful for debugging/inspection). */\n readonly id: symbol;\n /** The value returned by {@link consume} when no provider is active. */\n readonly defaultValue: T;\n /**\n * Provide `value` to any `consume()` calls made synchronously inside `run`.\n * The value is popped again as soon as `run` returns (even if it throws),\n * so nesting resolves to the nearest active provider.\n */\n provide<R>(value: T, run: () => R): R;\n /** Read the nearest active provider's value, or {@link defaultValue}. */\n consume(): T;\n /** True while at least one provider is active for this context. */\n hasProvider(): boolean;\n}\n\n/**\n * Create a typed context with a required default value, so `consume()` always\n * returns a `T` (never `undefined` unless `T` itself permits it).\n */\nexport function createContext<T>(defaultValue: T, description?: string): Context<T> {\n const id = Symbol(description ?? 'streetui.context');\n const stack: T[] = [];\n\n return {\n id,\n defaultValue,\n provide<R>(value: T, run: () => R): R {\n stack.push(value);\n try {\n return run();\n } finally {\n stack.pop();\n }\n },\n consume(): T {\n return stack.length > 0 ? (stack[stack.length - 1] as T) : defaultValue;\n },\n hasProvider(): boolean {\n return stack.length > 0;\n },\n };\n}\n"],"mappings":";AAqCO,SAAS,cAAiB,cAAiB,aAAkC;AAClF,QAAM,KAAK,OAAO,eAAe,kBAAkB;AACnD,QAAM,QAAa,CAAC;AAEpB,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA,QAAW,OAAU,KAAiB;AACpC,YAAM,KAAK,KAAK;AAChB,UAAI;AACF,eAAO,IAAI;AAAA,MACb,UAAE;AACA,cAAM,IAAI;AAAA,MACZ;AAAA,IACF;AAAA,IACA,UAAa;AACX,aAAO,MAAM,SAAS,IAAK,MAAM,MAAM,SAAS,CAAC,IAAU;AAAA,IAC7D;AAAA,IACA,cAAuB;AACrB,aAAO,MAAM,SAAS;AAAA,IACxB;AAAA,EACF;AACF;","names":[]}
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@streetui/context",
3
+ "version": "1.0.0",
4
+ "description": "StreetUI context — build-time provider/consumer scoping with nearest-provider resolution",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": {
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/index.js"
14
+ },
15
+ "require": {
16
+ "types": "./dist/index.d.cts",
17
+ "default": "./dist/index.cjs"
18
+ }
19
+ }
20
+ },
21
+ "scripts": {
22
+ "build": "tsup",
23
+ "typecheck": "tsc --noEmit",
24
+ "test": "vitest run",
25
+ "clean": "rm -rf dist"
26
+ },
27
+ "devDependencies": {
28
+ "typescript": "*",
29
+ "tsup": "*",
30
+ "vitest": "*"
31
+ },
32
+ "license": "MIT",
33
+ "sideEffects": false,
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
37
+ "files": [
38
+ "dist",
39
+ "README.md",
40
+ "LICENSE"
41
+ ]
42
+ }