@telperion/elements 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 +21 -0
- package/README.md +102 -0
- package/bundle.js +164 -0
- package/bundle.mjs +2630 -0
- package/drag-drop/drag-element.cjs +438 -0
- package/drag-drop/drag-element.d.ts +120 -0
- package/drag-drop/drag-element.js +437 -0
- package/drag-drop/drag-element.js.map +1 -0
- package/drag-drop/drag-handle-element.cjs +35 -0
- package/drag-drop/drag-handle-element.d.ts +12 -0
- package/drag-drop/drag-handle-element.js +34 -0
- package/drag-drop/drag-handle-element.js.map +1 -0
- package/drag-drop/dropzone-element.cjs +117 -0
- package/drag-drop/dropzone-element.d.ts +17 -0
- package/drag-drop/dropzone-element.js +116 -0
- package/drag-drop/dropzone-element.js.map +1 -0
- package/drag-drop/index.cjs +11 -0
- package/drag-drop/index.d.ts +22 -0
- package/drag-drop/index.js +6 -0
- package/drag-drop/index.js.map +1 -0
- package/drag-drop/private-props.cjs +6 -0
- package/drag-drop/private-props.d.ts +3 -0
- package/drag-drop/private-props.js +5 -0
- package/drag-drop/private-props.js.map +1 -0
- package/drag-drop/tha-drag-event.cjs +34 -0
- package/drag-drop/tha-drag-event.d.ts +19 -0
- package/drag-drop/tha-drag-event.js +32 -0
- package/drag-drop/tha-drag-event.js.map +1 -0
- package/icon/icon-element.cjs +114 -0
- package/icon/icon-element.d.ts +40 -0
- package/icon/icon-element.js +113 -0
- package/icon/icon-element.js.map +1 -0
- package/icon/index.cjs +5 -0
- package/icon/index.d.ts +1 -0
- package/icon/index.js +3 -0
- package/icon/index.js.map +1 -0
- package/icon/material-symbols.css +22 -0
- package/index.cjs +11 -0
- package/index.d.ts +6 -0
- package/index.js +10 -0
- package/index.js.map +1 -0
- package/package.json +127 -0
- package/router/history-managed.cjs +97 -0
- package/router/history-managed.d.ts +76 -0
- package/router/history-managed.js +95 -0
- package/router/history-managed.js.map +1 -0
- package/router/history.cjs +65 -0
- package/router/history.d.ts +48 -0
- package/router/history.js +63 -0
- package/router/history.js.map +1 -0
- package/router/index.cjs +15 -0
- package/router/index.d.ts +6 -0
- package/router/index.js +8 -0
- package/router/index.js.map +1 -0
- package/router/signal-watcher-lit-element.cjs +11 -0
- package/router/signal-watcher-lit-element.d.ts +25 -0
- package/router/signal-watcher-lit-element.js +10 -0
- package/router/signal-watcher-lit-element.js.map +1 -0
- package/router/tha-route.cjs +125 -0
- package/router/tha-route.d.ts +90 -0
- package/router/tha-route.js +124 -0
- package/router/tha-route.js.map +1 -0
- package/router/tha-router-config.cjs +241 -0
- package/router/tha-router-config.d.ts +223 -0
- package/router/tha-router-config.js +240 -0
- package/router/tha-router-config.js.map +1 -0
- package/router/tha-router-link.cjs +209 -0
- package/router/tha-router-link.d.ts +95 -0
- package/router/tha-router-link.js +208 -0
- package/router/tha-router-link.js.map +1 -0
- package/router/tha-router-outlet.cjs +215 -0
- package/router/tha-router-outlet.d.ts +112 -0
- package/router/tha-router-outlet.js +214 -0
- package/router/tha-router-outlet.js.map +1 -0
- package/router/tha-router-param.cjs +278 -0
- package/router/tha-router-param.d.ts +226 -0
- package/router/tha-router-param.js +277 -0
- package/router/tha-router-param.js.map +1 -0
- package/router/tha-router.cjs +163 -0
- package/router/tha-router.d.ts +69 -0
- package/router/tha-router.js +162 -0
- package/router/tha-router.js.map +1 -0
- package/tha-url-output.cjs +79 -0
- package/tha-url-output.d.ts +41 -0
- package/tha-url-output.js +78 -0
- package/tha-url-output.js.map +1 -0
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Router parameter display component
|
|
3
|
+
*
|
|
4
|
+
* Provides a declarative way to display dynamic route parameter values
|
|
5
|
+
* directly in HTML templates without requiring JavaScript.
|
|
6
|
+
*
|
|
7
|
+
* @module router/tha-router-param
|
|
8
|
+
*/
|
|
9
|
+
import { __decorate, __metadata } from "tslib";
|
|
10
|
+
import { customElement, property } from "lit/decorators.js";
|
|
11
|
+
import { SignalWatcherLitElement } from "./signal-watcher-lit-element";
|
|
12
|
+
import { computed, html, signal } from "@lit-labs/signals";
|
|
13
|
+
import { PARAMS } from "./tha-router-outlet";
|
|
14
|
+
/**
|
|
15
|
+
* Custom element for displaying dynamic route parameter values.
|
|
16
|
+
*
|
|
17
|
+
* This component provides a declarative way to access and display URL
|
|
18
|
+
* parameters extracted from dynamic route segments without writing JavaScript.
|
|
19
|
+
* It must be used inside a `<tha-router-outlet>` element.
|
|
20
|
+
*
|
|
21
|
+
* ## Features
|
|
22
|
+
*
|
|
23
|
+
* - **Declarative**: Access params directly in HTML
|
|
24
|
+
* - **Reactive**: Automatically updates when route changes
|
|
25
|
+
* - **Hierarchical**: Inherits params from parent outlets
|
|
26
|
+
* - **Type-safe**: Integrated with TypeScript definitions
|
|
27
|
+
*
|
|
28
|
+
* ## Usage
|
|
29
|
+
*
|
|
30
|
+
* ### Basic Parameter Display
|
|
31
|
+
*
|
|
32
|
+
* ```html
|
|
33
|
+
* <tha-router>
|
|
34
|
+
* <tha-route path="/users/:id">
|
|
35
|
+
* <template>
|
|
36
|
+
* <h1>User Profile</h1>
|
|
37
|
+
* <p>User ID: <tha-router-param name="id"></tha-router-param></p>
|
|
38
|
+
* </template>
|
|
39
|
+
* </tha-route>
|
|
40
|
+
* <tha-router-outlet></tha-router-outlet>
|
|
41
|
+
* </tha-router>
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* When navigating to `/users/123`, the component renders: `User ID: 123`
|
|
45
|
+
*
|
|
46
|
+
* ### Multiple Parameters
|
|
47
|
+
*
|
|
48
|
+
* ```html
|
|
49
|
+
* <tha-route path="/blog/:year/:month/:slug">
|
|
50
|
+
* <template>
|
|
51
|
+
* <article>
|
|
52
|
+
* <header>
|
|
53
|
+
* <time>
|
|
54
|
+
* <tha-router-param name="year"></tha-router-param>-
|
|
55
|
+
* <tha-router-param name="month"></tha-router-param>
|
|
56
|
+
* </time>
|
|
57
|
+
* <h1><tha-router-param name="slug"></tha-router-param></h1>
|
|
58
|
+
* </header>
|
|
59
|
+
* </article>
|
|
60
|
+
* </template>
|
|
61
|
+
* </tha-route>
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* For `/blog/2024/12/hello-world`, renders:
|
|
65
|
+
* ```html
|
|
66
|
+
* <time>2024-12</time>
|
|
67
|
+
* <h1>hello-world</h1>
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* ### Nested Routes with Parameter Inheritance
|
|
71
|
+
*
|
|
72
|
+
* ```html
|
|
73
|
+
* <tha-router>
|
|
74
|
+
* <tha-route path="/projects/:projectId">
|
|
75
|
+
* <template>
|
|
76
|
+
* <h1>Project <tha-router-param name="projectId"></tha-router-param></h1>
|
|
77
|
+
*
|
|
78
|
+
* <!-- Nested outlet inherits projectId parameter -->
|
|
79
|
+
* <tha-router-outlet>
|
|
80
|
+
* <tha-route path="/tasks/:taskId">
|
|
81
|
+
* <template>
|
|
82
|
+
* <!-- Both params are available -->
|
|
83
|
+
* <h2>Task Details</h2>
|
|
84
|
+
* <p>Project: <tha-router-param name="projectId"></tha-router-param></p>
|
|
85
|
+
* <p>Task: <tha-router-param name="taskId"></tha-router-param></p>
|
|
86
|
+
* </template>
|
|
87
|
+
* </tha-route>
|
|
88
|
+
* </tha-router-outlet>
|
|
89
|
+
* </template>
|
|
90
|
+
* </tha-route>
|
|
91
|
+
* <tha-router-outlet></tha-router-outlet>
|
|
92
|
+
* </tha-router>
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* ### With Styling
|
|
96
|
+
*
|
|
97
|
+
* ```html
|
|
98
|
+
* <style>
|
|
99
|
+
* tha-router-param {
|
|
100
|
+
* font-weight: bold;
|
|
101
|
+
* color: var(--primary-color);
|
|
102
|
+
* }
|
|
103
|
+
*
|
|
104
|
+
* tha-router-param[name="id"] {
|
|
105
|
+
* font-family: monospace;
|
|
106
|
+
* }
|
|
107
|
+
* </style>
|
|
108
|
+
*
|
|
109
|
+
* <tha-route path="/users/:id">
|
|
110
|
+
* <template>
|
|
111
|
+
* <span>User: <tha-router-param name="id"></tha-router-param></span>
|
|
112
|
+
* </template>
|
|
113
|
+
* </tha-route>
|
|
114
|
+
* ```
|
|
115
|
+
*
|
|
116
|
+
* ## How It Works
|
|
117
|
+
*
|
|
118
|
+
* 1. On connection, finds the nearest parent `<tha-router-outlet>`
|
|
119
|
+
* 2. Accesses the outlet's PARAMS signal containing all route parameters
|
|
120
|
+
* 3. Computes and displays the value for the specified parameter name
|
|
121
|
+
* 4. Automatically re-renders when the route (and thus params) change
|
|
122
|
+
*
|
|
123
|
+
* ## Error Handling
|
|
124
|
+
*
|
|
125
|
+
* Throws an error if not placed within a `<tha-router-outlet>`:
|
|
126
|
+
*
|
|
127
|
+
* ```javascript
|
|
128
|
+
* // ✗ Wrong - not inside outlet
|
|
129
|
+
* <tha-router-param name="id"></tha-router-param>
|
|
130
|
+
*
|
|
131
|
+
* // ✓ Correct - inside outlet
|
|
132
|
+
* <tha-router-outlet>
|
|
133
|
+
* <tha-router-param name="id"></tha-router-param>
|
|
134
|
+
* </tha-router-outlet>
|
|
135
|
+
* ```
|
|
136
|
+
*
|
|
137
|
+
* ## Alternative: Using Attributes
|
|
138
|
+
*
|
|
139
|
+
* The parent `<tha-router-outlet>` also exposes parameters as `param-*` attributes,
|
|
140
|
+
* which can be used as an alternative approach:
|
|
141
|
+
*
|
|
142
|
+
* ```html
|
|
143
|
+
* <tha-router-outlet id="outlet">
|
|
144
|
+
* <script>
|
|
145
|
+
* const outlet = document.getElementById('outlet');
|
|
146
|
+
* // Params available as: outlet.getAttribute('param-id')
|
|
147
|
+
* </script>
|
|
148
|
+
* </tha-router-outlet>
|
|
149
|
+
* ```
|
|
150
|
+
*
|
|
151
|
+
* However, `<tha-router-param>` is preferred for declarative templates.
|
|
152
|
+
*
|
|
153
|
+
* @example Basic usage
|
|
154
|
+
* ```html
|
|
155
|
+
* <tha-router>
|
|
156
|
+
* <tha-route path="/product/:sku">
|
|
157
|
+
* <template>
|
|
158
|
+
* <h1>Product: <tha-router-param name="sku"></tha-router-param></h1>
|
|
159
|
+
* </template>
|
|
160
|
+
* </tha-route>
|
|
161
|
+
* <tha-router-outlet></tha-router-outlet>
|
|
162
|
+
* </tha-router>
|
|
163
|
+
* ```
|
|
164
|
+
*
|
|
165
|
+
* @example Multiple params with formatting
|
|
166
|
+
* ```html
|
|
167
|
+
* <tha-route path="/user/:username/post/:postId">
|
|
168
|
+
* <template>
|
|
169
|
+
* <article>
|
|
170
|
+
* <p>@<tha-router-param name="username"></tha-router-param></p>
|
|
171
|
+
* <p>Post #<tha-router-param name="postId"></tha-router-param></p>
|
|
172
|
+
* </article>
|
|
173
|
+
* </template>
|
|
174
|
+
* </tha-route>
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
177
|
+
let ThaRouterParam = class ThaRouterParam extends SignalWatcherLitElement {
|
|
178
|
+
constructor() {
|
|
179
|
+
super(...arguments);
|
|
180
|
+
/**
|
|
181
|
+
* Signal holding reference to the parent outlet.
|
|
182
|
+
* Set during connectedCallback.
|
|
183
|
+
*/
|
|
184
|
+
this.#outlet = signal(null);
|
|
185
|
+
/**
|
|
186
|
+
* Signal tracking the parameter name to display.
|
|
187
|
+
* Updated when the 'name' attribute changes.
|
|
188
|
+
*/
|
|
189
|
+
this.#name = signal('');
|
|
190
|
+
/**
|
|
191
|
+
* Computed signal that extracts the parameter value.
|
|
192
|
+
* Reactively updates when the outlet's params or name changes.
|
|
193
|
+
*/
|
|
194
|
+
this.#value = computed(() => this.#outlet.get()?.[PARAMS]?.get()[this.#name.get()]);
|
|
195
|
+
/**
|
|
196
|
+
* The name of the route parameter to display.
|
|
197
|
+
*
|
|
198
|
+
* Must match a parameter name defined in the route's path pattern.
|
|
199
|
+
* For example, for path `/users/:id`, use `name="id"`.
|
|
200
|
+
*
|
|
201
|
+
* @attr name
|
|
202
|
+
* @type {string}
|
|
203
|
+
*/
|
|
204
|
+
this.name = '';
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Signal holding reference to the parent outlet.
|
|
208
|
+
* Set during connectedCallback.
|
|
209
|
+
*/
|
|
210
|
+
#outlet;
|
|
211
|
+
/**
|
|
212
|
+
* Signal tracking the parameter name to display.
|
|
213
|
+
* Updated when the 'name' attribute changes.
|
|
214
|
+
*/
|
|
215
|
+
#name;
|
|
216
|
+
/**
|
|
217
|
+
* Computed signal that extracts the parameter value.
|
|
218
|
+
* Reactively updates when the outlet's params or name changes.
|
|
219
|
+
*/
|
|
220
|
+
#value;
|
|
221
|
+
/**
|
|
222
|
+
* Lifecycle callback when element is connected to the DOM.
|
|
223
|
+
*
|
|
224
|
+
* Finds the parent `<tha-router-outlet>` and stores a reference.
|
|
225
|
+
* Throws an error if not used within an outlet.
|
|
226
|
+
*
|
|
227
|
+
* @throws {Error} If no parent tha-router-outlet is found
|
|
228
|
+
*/
|
|
229
|
+
connectedCallback() {
|
|
230
|
+
super.connectedCallback();
|
|
231
|
+
const outlet = this.closest('tha-router-outlet');
|
|
232
|
+
if (!outlet) {
|
|
233
|
+
throw new Error('<tha-router-param> must be used within a <tha-router-outlet>.');
|
|
234
|
+
}
|
|
235
|
+
this.#outlet.set(outlet);
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Lifecycle callback when observed attributes change.
|
|
239
|
+
*
|
|
240
|
+
* Updates the internal name signal when the 'name' attribute changes,
|
|
241
|
+
* which triggers the computed value signal to recalculate and re-render.
|
|
242
|
+
*
|
|
243
|
+
* @param name - The attribute name that changed
|
|
244
|
+
* @param _old - Previous attribute value (unused)
|
|
245
|
+
* @param value - New attribute value
|
|
246
|
+
*/
|
|
247
|
+
attributeChangedCallback(name, _old, value) {
|
|
248
|
+
super.attributeChangedCallback(name, _old, value);
|
|
249
|
+
switch (name) {
|
|
250
|
+
case 'name':
|
|
251
|
+
this.#name.set(value ?? '');
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Renders the parameter value.
|
|
257
|
+
*
|
|
258
|
+
* The value is extracted from the outlet's params signal and
|
|
259
|
+
* displayed as text content. Automatically updates when the
|
|
260
|
+
* route changes.
|
|
261
|
+
*
|
|
262
|
+
* @returns The parameter value as a template result
|
|
263
|
+
*/
|
|
264
|
+
render() {
|
|
265
|
+
return html `${this.#value}`;
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
__decorate([
|
|
269
|
+
property({ type: String }),
|
|
270
|
+
__metadata("design:type", String)
|
|
271
|
+
], ThaRouterParam.prototype, "name", void 0);
|
|
272
|
+
ThaRouterParam = __decorate([
|
|
273
|
+
customElement('tha-router-param')
|
|
274
|
+
], ThaRouterParam);
|
|
275
|
+
export { ThaRouterParam };
|
|
276
|
+
|
|
277
|
+
//# sourceMappingURL=tha-router-param.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/elements/src/router/tha-router-param.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;;AAEH,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AACvE,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC3D,OAAO,EAAE,MAAM,EAAwB,MAAM,qBAAqB,CAAC;AAcnE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkKG;AAEI,IAAM,cAAc,GAApB,MAAM,cAAe,SAAQ,uBAAuB;IAApD;;QACL;;;WAGG;QACH,YAAO,GAAG,MAAM,CAAkB,IAAK,CAAC,CAAC;QAEzC;;;WAGG;QACH,UAAK,GAAG,MAAM,CAAS,EAAE,CAAC,CAAC;QAE3B;;;WAGG;QACH,WAAM,GAAG,QAAQ,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,MAAM,CAAC,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;QAE/E;;;;;;;;WAQG;QAEH,SAAI,GAAW,EAAE,CAAC;IAsDpB,CAAC;IAlFC;;;OAGG;IACH,OAAO,CAAkC;IAEzC;;;OAGG;IACH,KAAK,CAAsB;IAE3B;;;OAGG;IACH,MAAM,CAAyE;IAc/E;;;;;;;OAOG;IACM,iBAAiB;QACxB,KAAK,CAAC,iBAAiB,EAAE,CAAC;QAE1B,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,mBAAmB,CAAC,CAAC;QAEjD,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,MAAM,IAAI,KAAK,CAAC,+DAA+D,CAAC,CAAC;QACnF,CAAC;QAED,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAC3B,CAAC;IAED;;;;;;;;;OASG;IACM,wBAAwB,CAAC,IAAY,EAAE,IAAmB,EAAE,KAAoB;QACvF,KAAK,CAAC,wBAAwB,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;QAElD,QAAQ,IAAI,EAAE,CAAC;YACb,KAAK,MAAM;gBACT,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC;gBAC5B,MAAM;QACV,CAAC;IACH,CAAC;IAED;;;;;;;;OAQG;IACgB,MAAM;QACvB,OAAO,IAAI,CAAA,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;IAC9B,CAAC;CACF,CAAA;AAtDC;IADC,QAAQ,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;;4CACT;AA7BP,cAAc;IAD1B,aAAa,CAAC,kBAAkB,CAAC;GACrB,cAAc,CAmF1B","file":"tha-router-param.js","sourcesContent":["/**\n * @fileoverview Router parameter display component\n *\n * Provides a declarative way to display dynamic route parameter values\n * directly in HTML templates without requiring JavaScript.\n *\n * @module router/tha-router-param\n */\n\nimport { customElement, property } from \"lit/decorators.js\";\nimport { SignalWatcherLitElement } from \"./signal-watcher-lit-element\";\nimport { computed, html, signal } from \"@lit-labs/signals\";\nimport { PARAMS, type ThaRouterOutlet } from \"./tha-router-outlet\";\n\ndeclare global {\n interface HTMLElementTagNameMap {\n /**\n * A component that displays the value of a route parameter.\n *\n * Must be used within a tha-router-outlet element. Automatically\n * updates when route parameters change.\n */\n 'tha-router-param': ThaRouterParam;\n }\n}\n\n/**\n * Custom element for displaying dynamic route parameter values.\n *\n * This component provides a declarative way to access and display URL\n * parameters extracted from dynamic route segments without writing JavaScript.\n * It must be used inside a `<tha-router-outlet>` element.\n *\n * ## Features\n *\n * - **Declarative**: Access params directly in HTML\n * - **Reactive**: Automatically updates when route changes\n * - **Hierarchical**: Inherits params from parent outlets\n * - **Type-safe**: Integrated with TypeScript definitions\n *\n * ## Usage\n *\n * ### Basic Parameter Display\n *\n * ```html\n * <tha-router>\n * <tha-route path=\"/users/:id\">\n * <template>\n * <h1>User Profile</h1>\n * <p>User ID: <tha-router-param name=\"id\"></tha-router-param></p>\n * </template>\n * </tha-route>\n * <tha-router-outlet></tha-router-outlet>\n * </tha-router>\n * ```\n *\n * When navigating to `/users/123`, the component renders: `User ID: 123`\n *\n * ### Multiple Parameters\n *\n * ```html\n * <tha-route path=\"/blog/:year/:month/:slug\">\n * <template>\n * <article>\n * <header>\n * <time>\n * <tha-router-param name=\"year\"></tha-router-param>-\n * <tha-router-param name=\"month\"></tha-router-param>\n * </time>\n * <h1><tha-router-param name=\"slug\"></tha-router-param></h1>\n * </header>\n * </article>\n * </template>\n * </tha-route>\n * ```\n *\n * For `/blog/2024/12/hello-world`, renders:\n * ```html\n * <time>2024-12</time>\n * <h1>hello-world</h1>\n * ```\n *\n * ### Nested Routes with Parameter Inheritance\n *\n * ```html\n * <tha-router>\n * <tha-route path=\"/projects/:projectId\">\n * <template>\n * <h1>Project <tha-router-param name=\"projectId\"></tha-router-param></h1>\n *\n * <!-- Nested outlet inherits projectId parameter -->\n * <tha-router-outlet>\n * <tha-route path=\"/tasks/:taskId\">\n * <template>\n * <!-- Both params are available -->\n * <h2>Task Details</h2>\n * <p>Project: <tha-router-param name=\"projectId\"></tha-router-param></p>\n * <p>Task: <tha-router-param name=\"taskId\"></tha-router-param></p>\n * </template>\n * </tha-route>\n * </tha-router-outlet>\n * </template>\n * </tha-route>\n * <tha-router-outlet></tha-router-outlet>\n * </tha-router>\n * ```\n *\n * ### With Styling\n *\n * ```html\n * <style>\n * tha-router-param {\n * font-weight: bold;\n * color: var(--primary-color);\n * }\n *\n * tha-router-param[name=\"id\"] {\n * font-family: monospace;\n * }\n * </style>\n *\n * <tha-route path=\"/users/:id\">\n * <template>\n * <span>User: <tha-router-param name=\"id\"></tha-router-param></span>\n * </template>\n * </tha-route>\n * ```\n *\n * ## How It Works\n *\n * 1. On connection, finds the nearest parent `<tha-router-outlet>`\n * 2. Accesses the outlet's PARAMS signal containing all route parameters\n * 3. Computes and displays the value for the specified parameter name\n * 4. Automatically re-renders when the route (and thus params) change\n *\n * ## Error Handling\n *\n * Throws an error if not placed within a `<tha-router-outlet>`:\n *\n * ```javascript\n * // ✗ Wrong - not inside outlet\n * <tha-router-param name=\"id\"></tha-router-param>\n *\n * // ✓ Correct - inside outlet\n * <tha-router-outlet>\n * <tha-router-param name=\"id\"></tha-router-param>\n * </tha-router-outlet>\n * ```\n *\n * ## Alternative: Using Attributes\n *\n * The parent `<tha-router-outlet>` also exposes parameters as `param-*` attributes,\n * which can be used as an alternative approach:\n *\n * ```html\n * <tha-router-outlet id=\"outlet\">\n * <script>\n * const outlet = document.getElementById('outlet');\n * // Params available as: outlet.getAttribute('param-id')\n * </script>\n * </tha-router-outlet>\n * ```\n *\n * However, `<tha-router-param>` is preferred for declarative templates.\n *\n * @example Basic usage\n * ```html\n * <tha-router>\n * <tha-route path=\"/product/:sku\">\n * <template>\n * <h1>Product: <tha-router-param name=\"sku\"></tha-router-param></h1>\n * </template>\n * </tha-route>\n * <tha-router-outlet></tha-router-outlet>\n * </tha-router>\n * ```\n *\n * @example Multiple params with formatting\n * ```html\n * <tha-route path=\"/user/:username/post/:postId\">\n * <template>\n * <article>\n * <p>@<tha-router-param name=\"username\"></tha-router-param></p>\n * <p>Post #<tha-router-param name=\"postId\"></tha-router-param></p>\n * </article>\n * </template>\n * </tha-route>\n * ```\n */\n@customElement('tha-router-param')\nexport class ThaRouterParam extends SignalWatcherLitElement {\n /**\n * Signal holding reference to the parent outlet.\n * Set during connectedCallback.\n */\n #outlet = signal<ThaRouterOutlet>(null!);\n\n /**\n * Signal tracking the parameter name to display.\n * Updated when the 'name' attribute changes.\n */\n #name = signal<string>('');\n\n /**\n * Computed signal that extracts the parameter value.\n * Reactively updates when the outlet's params or name changes.\n */\n #value = computed(() => this.#outlet.get()?.[PARAMS]?.get()[this.#name.get()]);\n\n /**\n * The name of the route parameter to display.\n *\n * Must match a parameter name defined in the route's path pattern.\n * For example, for path `/users/:id`, use `name=\"id\"`.\n *\n * @attr name\n * @type {string}\n */\n @property({ type: String })\n name: string = '';\n\n /**\n * Lifecycle callback when element is connected to the DOM.\n *\n * Finds the parent `<tha-router-outlet>` and stores a reference.\n * Throws an error if not used within an outlet.\n *\n * @throws {Error} If no parent tha-router-outlet is found\n */\n override connectedCallback(): void {\n super.connectedCallback();\n\n const outlet = this.closest('tha-router-outlet');\n\n if (!outlet) {\n throw new Error('<tha-router-param> must be used within a <tha-router-outlet>.');\n }\n\n this.#outlet.set(outlet);\n }\n\n /**\n * Lifecycle callback when observed attributes change.\n *\n * Updates the internal name signal when the 'name' attribute changes,\n * which triggers the computed value signal to recalculate and re-render.\n *\n * @param name - The attribute name that changed\n * @param _old - Previous attribute value (unused)\n * @param value - New attribute value\n */\n override attributeChangedCallback(name: string, _old: string | null, value: string | null): void {\n super.attributeChangedCallback(name, _old, value);\n\n switch (name) {\n case 'name':\n this.#name.set(value ?? '');\n break;\n }\n }\n\n /**\n * Renders the parameter value.\n *\n * The value is extracted from the outlet's params signal and\n * displayed as text content. Automatically updates when the\n * route changes.\n *\n * @returns The parameter value as a template result\n */\n protected override render() {\n return html`${this.#value}`;\n }\n}\n"]}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ThaRouter = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const lit_1 = require("lit");
|
|
6
|
+
const decorators_js_1 = require("lit/decorators.js");
|
|
7
|
+
const tha_route_1 = require("./tha-route");
|
|
8
|
+
const signals_1 = require("@lit-labs/signals");
|
|
9
|
+
const tha_router_outlet_1 = require("./tha-router-outlet");
|
|
10
|
+
const noop_1 = require("@telperion/js-utils/function/noop");
|
|
11
|
+
const defer_1 = require("@telperion/js-utils/function/defer");
|
|
12
|
+
const signal_watcher_lit_element_1 = require("./signal-watcher-lit-element");
|
|
13
|
+
const history_managed_1 = require("./history-managed");
|
|
14
|
+
/**
|
|
15
|
+
* A custom element that manages client-side routing for single-page applications.
|
|
16
|
+
*
|
|
17
|
+
* The router watches for navigation events and matches the current URL against
|
|
18
|
+
* registered routes (tha-route elements), rendering the matching route's content
|
|
19
|
+
* in a tha-router-outlet element.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```html
|
|
23
|
+
* <tha-router history="browser">
|
|
24
|
+
* <tha-route path="/">
|
|
25
|
+
* <template><h1>Home</h1></template>
|
|
26
|
+
* </tha-route>
|
|
27
|
+
* <tha-route path="/about">
|
|
28
|
+
* <template><h1>About</h1></template>
|
|
29
|
+
* </tha-route>
|
|
30
|
+
* <tha-router-outlet></tha-router-outlet>
|
|
31
|
+
* </tha-router>
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
let ThaRouter = class ThaRouter extends (0, history_managed_1.HistoryManaged)(signal_watcher_lit_element_1.SignalWatcherLitElement) {
|
|
35
|
+
/**
|
|
36
|
+
* Injects global styles for tha-router elements to hide non-outlet children.
|
|
37
|
+
* Ensures that only tha-router-outlet elements are visible within the router.
|
|
38
|
+
*/
|
|
39
|
+
static {
|
|
40
|
+
const styleEl = document.createElement("style");
|
|
41
|
+
styleEl.setAttribute("type", "text/css");
|
|
42
|
+
styleEl.setAttribute("data-id", "tha-router-styles");
|
|
43
|
+
styleEl.textContent = (0, lit_1.css) `
|
|
44
|
+
tha-router {
|
|
45
|
+
& > :not(tha-router-outlet) {
|
|
46
|
+
display: none;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
`.toString();
|
|
50
|
+
document.head.appendChild(styleEl);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Observes changes to child elements to detect when routes are added or removed.
|
|
54
|
+
* Updates the #routeEls signal whenever tha-route children change.
|
|
55
|
+
*/
|
|
56
|
+
#routeChildObserver = new MutationObserver((mutations) => {
|
|
57
|
+
const childListMutations = mutations.filter(mutation => mutation.type === 'childList');
|
|
58
|
+
if (!childListMutations.length)
|
|
59
|
+
return;
|
|
60
|
+
this.#routeEls.set(Array.from(this.children).filter(child => child instanceof tha_route_1.ThaRoute).map(routeEl => new WeakRef(routeEl)));
|
|
61
|
+
});
|
|
62
|
+
/** Cleanup function for the route children rendering effect */
|
|
63
|
+
#routeChildrenEffectCleaner = noop_1.noop;
|
|
64
|
+
/** Cleanup function for the history type change effect */
|
|
65
|
+
#historyTypeEffectCleaner = noop_1.noop;
|
|
66
|
+
/** Cleanup function to remove history listener */
|
|
67
|
+
#historyListenerRemover = noop_1.noop;
|
|
68
|
+
/** Signal containing weak references to all tha-route children */
|
|
69
|
+
#routeEls = (0, signals_1.signal)([]);
|
|
70
|
+
/** Signal containing weak reference to the currently active route */
|
|
71
|
+
#activeRoute = (0, signals_1.signal)(null);
|
|
72
|
+
/** Signal containing weak reference to the assigned outlet element */
|
|
73
|
+
#outlet = (0, signals_1.signal)(null);
|
|
74
|
+
/**
|
|
75
|
+
* Lifecycle callback when the element is connected to the DOM.
|
|
76
|
+
* Sets up observers, effects, and history listeners.
|
|
77
|
+
*/
|
|
78
|
+
connectedCallback() {
|
|
79
|
+
super.connectedCallback();
|
|
80
|
+
// Start observing child route elements
|
|
81
|
+
this.#routeChildObserver.observe(this, { childList: true });
|
|
82
|
+
// Effect to render the active route in the assigned outlet
|
|
83
|
+
this.#routeChildrenEffectCleaner = this.updateEffect(() => {
|
|
84
|
+
const activeRoute = this.#activeRoute.get()?.deref() ?? null;
|
|
85
|
+
const outlet = this.#outlet.get()?.deref() ?? null;
|
|
86
|
+
outlet?.[tha_router_outlet_1.RENDER_ROUTE](activeRoute);
|
|
87
|
+
});
|
|
88
|
+
// Effect to handle history type changes
|
|
89
|
+
this.#historyTypeEffectCleaner = this.updateEffect(() => {
|
|
90
|
+
const history = this[history_managed_1.HISTORY].get();
|
|
91
|
+
this.#historyListenerRemover();
|
|
92
|
+
this.#handleRouteChange({
|
|
93
|
+
location: this[history_managed_1.HISTORY].get().location,
|
|
94
|
+
action: 'POP',
|
|
95
|
+
});
|
|
96
|
+
// Set up listener for navigation events
|
|
97
|
+
this.#historyListenerRemover = history ? history.listen(this.#handleRouteChange) : noop_1.noop;
|
|
98
|
+
});
|
|
99
|
+
// Initial route matching on connect
|
|
100
|
+
(0, defer_1.defer)(() => {
|
|
101
|
+
this.#handleRouteChange({
|
|
102
|
+
location: this[history_managed_1.HISTORY].get().location,
|
|
103
|
+
action: 'POP',
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Assigns a router outlet to this router.
|
|
109
|
+
* The outlet will be used to render the active route's content.
|
|
110
|
+
*
|
|
111
|
+
* @param outlet - The outlet element to assign, or null to unassign
|
|
112
|
+
* @internal
|
|
113
|
+
*/
|
|
114
|
+
assignOutlet(outlet) {
|
|
115
|
+
this.#outlet.set(outlet ? new WeakRef(outlet) : null);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Handles navigation events by matching the new URL against registered routes.
|
|
119
|
+
* Updates the active route signal when a match is found.
|
|
120
|
+
*
|
|
121
|
+
* @param update - The history update containing the new location
|
|
122
|
+
*/
|
|
123
|
+
#handleRouteChange = (update) => {
|
|
124
|
+
const routes = this.#routeEls.get()
|
|
125
|
+
.map(weakRef => weakRef.deref())
|
|
126
|
+
.filter(Boolean);
|
|
127
|
+
for (const route of routes) {
|
|
128
|
+
const pathPattern = route[tha_route_1.PATH_PATTERN];
|
|
129
|
+
if (pathPattern?.test({ pathname: update.location.pathname })) {
|
|
130
|
+
this.#activeRoute.set(new WeakRef(route));
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
this.#activeRoute.set(null);
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* Lifecycle callback when the element is disconnected from the DOM.
|
|
138
|
+
* Cleans up all observers, effects, and listeners.
|
|
139
|
+
*/
|
|
140
|
+
disconnectedCallback() {
|
|
141
|
+
super.disconnectedCallback();
|
|
142
|
+
try {
|
|
143
|
+
this.#routeChildrenEffectCleaner();
|
|
144
|
+
this.#historyTypeEffectCleaner();
|
|
145
|
+
}
|
|
146
|
+
catch { /* no-op */ }
|
|
147
|
+
this.#routeChildObserver.disconnect();
|
|
148
|
+
this.#historyListenerRemover();
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Creates the render root for the element.
|
|
152
|
+
* Returns the element itself to render children in light DOM.
|
|
153
|
+
*
|
|
154
|
+
* @returns The element itself (not a shadow root)
|
|
155
|
+
*/
|
|
156
|
+
createRenderRoot() {
|
|
157
|
+
return this;
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
exports.ThaRouter = ThaRouter;
|
|
161
|
+
exports.ThaRouter = ThaRouter = tslib_1.__decorate([
|
|
162
|
+
(0, decorators_js_1.customElement)("tha-router")
|
|
163
|
+
], ThaRouter);
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { LitElement } from "lit";
|
|
2
|
+
import { ThaRouterOutlet } from "./tha-router-outlet";
|
|
3
|
+
declare global {
|
|
4
|
+
interface HTMLElementTagNameMap {
|
|
5
|
+
/**
|
|
6
|
+
* A router element that manages client-side routing for single-page applications.
|
|
7
|
+
* It watches for navigation events and matches the current URL against registered routes
|
|
8
|
+
* (tha-route elements), rendering the matching route's content in a tha-router-outlet element.
|
|
9
|
+
* The `history` attribute specifies the history management strategy:
|
|
10
|
+
* - 'browser' for HTML5 History API (default)
|
|
11
|
+
* - 'hash' for hash-based routing
|
|
12
|
+
* - 'memory' for in-memory history (testing)
|
|
13
|
+
* - 'memory:name' for named memory history instances
|
|
14
|
+
*/
|
|
15
|
+
"tha-router": ThaRouter;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
declare const ThaRouter_base: (new (...args: any[]) => import("./history-managed").HistoryManagedLitElementType) & (new () => LitElement & {
|
|
19
|
+
updateEffect(fn: () => void, options?: import("./signal-watcher-lit-element").EffectOptions): () => void;
|
|
20
|
+
});
|
|
21
|
+
/**
|
|
22
|
+
* A custom element that manages client-side routing for single-page applications.
|
|
23
|
+
*
|
|
24
|
+
* The router watches for navigation events and matches the current URL against
|
|
25
|
+
* registered routes (tha-route elements), rendering the matching route's content
|
|
26
|
+
* in a tha-router-outlet element.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```html
|
|
30
|
+
* <tha-router history="browser">
|
|
31
|
+
* <tha-route path="/">
|
|
32
|
+
* <template><h1>Home</h1></template>
|
|
33
|
+
* </tha-route>
|
|
34
|
+
* <tha-route path="/about">
|
|
35
|
+
* <template><h1>About</h1></template>
|
|
36
|
+
* </tha-route>
|
|
37
|
+
* <tha-router-outlet></tha-router-outlet>
|
|
38
|
+
* </tha-router>
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export declare class ThaRouter extends ThaRouter_base {
|
|
42
|
+
#private;
|
|
43
|
+
/**
|
|
44
|
+
* Lifecycle callback when the element is connected to the DOM.
|
|
45
|
+
* Sets up observers, effects, and history listeners.
|
|
46
|
+
*/
|
|
47
|
+
connectedCallback(): void;
|
|
48
|
+
/**
|
|
49
|
+
* Assigns a router outlet to this router.
|
|
50
|
+
* The outlet will be used to render the active route's content.
|
|
51
|
+
*
|
|
52
|
+
* @param outlet - The outlet element to assign, or null to unassign
|
|
53
|
+
* @internal
|
|
54
|
+
*/
|
|
55
|
+
assignOutlet(outlet: ThaRouterOutlet | null): void;
|
|
56
|
+
/**
|
|
57
|
+
* Lifecycle callback when the element is disconnected from the DOM.
|
|
58
|
+
* Cleans up all observers, effects, and listeners.
|
|
59
|
+
*/
|
|
60
|
+
disconnectedCallback(): void;
|
|
61
|
+
/**
|
|
62
|
+
* Creates the render root for the element.
|
|
63
|
+
* Returns the element itself to render children in light DOM.
|
|
64
|
+
*
|
|
65
|
+
* @returns The element itself (not a shadow root)
|
|
66
|
+
*/
|
|
67
|
+
protected createRenderRoot(): this;
|
|
68
|
+
}
|
|
69
|
+
export {};
|