@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,215 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var _a;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.ThaRouterOutlet = exports.PARAMS = exports.RENDER_ROUTE = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
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 noop_1 = require("@telperion/js-utils/function/noop");
|
|
10
|
+
const signal_watcher_lit_element_1 = require("./signal-watcher-lit-element");
|
|
11
|
+
const history_managed_1 = require("./history-managed");
|
|
12
|
+
const defer_1 = require("@telperion/js-utils/function/defer");
|
|
13
|
+
/**
|
|
14
|
+
* Symbol used by the router to call the internal render method on the outlet.
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
exports.RENDER_ROUTE = Symbol('ThaRouterOutlet:renderRoute');
|
|
18
|
+
exports.PARAMS = Symbol('ThaRouterOutlet:params');
|
|
19
|
+
/**
|
|
20
|
+
* A custom element that serves as a rendering target for router content.
|
|
21
|
+
*
|
|
22
|
+
* The outlet automatically binds to its parent tha-router or to a specific router
|
|
23
|
+
* identified by the `for` attribute. When the router's active route changes,
|
|
24
|
+
* the outlet clones and renders the route's template content.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```html
|
|
28
|
+
* <tha-router>
|
|
29
|
+
* <tha-route path="/"><template><h1>Home</h1></template></tha-route>
|
|
30
|
+
* <tha-router-outlet></tha-router-outlet>
|
|
31
|
+
* </tha-router>
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```html
|
|
36
|
+
* <!-- Bind to a specific router by ID -->
|
|
37
|
+
* <tha-router id="main-router">
|
|
38
|
+
* <tha-route path="/"><template><h1>Home</h1></template></tha-route>
|
|
39
|
+
* </tha-router>
|
|
40
|
+
* <tha-router-outlet for="main-router"></tha-router-outlet>
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
let ThaRouterOutlet = class ThaRouterOutlet extends signal_watcher_lit_element_1.SignalWatcherLitElement {
|
|
44
|
+
constructor() {
|
|
45
|
+
super(...arguments);
|
|
46
|
+
/** Weak reference to the bound router instance */
|
|
47
|
+
this.#boundRouter = (0, signals_1.signal)(undefined);
|
|
48
|
+
/** Signal containing weak reference to the route to render */
|
|
49
|
+
this.#routeToRender = (0, signals_1.signal)(null);
|
|
50
|
+
/** Computed signal that extracts the fragment from the route to render */
|
|
51
|
+
this.#fragmentToRender = (0, signals_1.computed)(() => this.#routeToRender.get()?.deref()?.[tha_route_1.FRAGMENT].get() ?? null);
|
|
52
|
+
this.#ownParams = (0, signals_1.computed)(() => {
|
|
53
|
+
const router = this.#boundRouter.get()?.deref();
|
|
54
|
+
const route = this.#routeToRender.get()?.deref();
|
|
55
|
+
const pattern = route?.[tha_route_1.PATH_PATTERN];
|
|
56
|
+
if (!router || !pattern)
|
|
57
|
+
return null;
|
|
58
|
+
const { location: { pathname } } = router[history_managed_1.HISTORY].get();
|
|
59
|
+
return pattern.exec(pathname, 'http://dummy.base')?.pathname.groups ?? null;
|
|
60
|
+
});
|
|
61
|
+
this[_a] = (0, signals_1.computed)(() => {
|
|
62
|
+
const ownParams = this.#ownParams.get() ?? {};
|
|
63
|
+
const parent = this.parentElement?.closest('tha-router-outlet');
|
|
64
|
+
const parentParams = parent?.[exports.PARAMS].get() ?? {};
|
|
65
|
+
return Object.freeze({ ...parentParams, ...ownParams });
|
|
66
|
+
});
|
|
67
|
+
this.params = {};
|
|
68
|
+
/** Cleanup function for the fragment update effect */
|
|
69
|
+
this.#fragmentUpdateCleaner = noop_1.noop;
|
|
70
|
+
/** Cleanup function for the attribute update effect */
|
|
71
|
+
this.#attrUpdateCleaner = noop_1.noop;
|
|
72
|
+
this.#paramsUpdateCleaner = noop_1.noop;
|
|
73
|
+
/**
|
|
74
|
+
* The ID of a specific tha-router element to bind to.
|
|
75
|
+
* If not set, binds to the nearest parent tha-router.
|
|
76
|
+
*
|
|
77
|
+
* @attr for
|
|
78
|
+
*/
|
|
79
|
+
this.for = null;
|
|
80
|
+
/**
|
|
81
|
+
* Reflects the path attribute of the currently active route.
|
|
82
|
+
* Automatically updated when the active route changes.
|
|
83
|
+
*
|
|
84
|
+
* @attr routepath
|
|
85
|
+
* @readonly
|
|
86
|
+
*/
|
|
87
|
+
this.routePath = null;
|
|
88
|
+
/**
|
|
89
|
+
* Reflects the ID of the currently active route.
|
|
90
|
+
* Automatically updated when the active route changes.
|
|
91
|
+
*
|
|
92
|
+
* @attr routeid
|
|
93
|
+
* @readonly
|
|
94
|
+
*/
|
|
95
|
+
this.routeID = undefined;
|
|
96
|
+
}
|
|
97
|
+
/** Weak reference to the bound router instance */
|
|
98
|
+
#boundRouter;
|
|
99
|
+
/** Signal containing weak reference to the route to render */
|
|
100
|
+
#routeToRender;
|
|
101
|
+
/** Computed signal that extracts the fragment from the route to render */
|
|
102
|
+
#fragmentToRender;
|
|
103
|
+
#ownParams;
|
|
104
|
+
/** Cleanup function for the fragment update effect */
|
|
105
|
+
#fragmentUpdateCleaner;
|
|
106
|
+
/** Cleanup function for the attribute update effect */
|
|
107
|
+
#attrUpdateCleaner;
|
|
108
|
+
#paramsUpdateCleaner;
|
|
109
|
+
/**
|
|
110
|
+
* Lifecycle callback when the element is connected to the DOM.
|
|
111
|
+
* Initializes the outlet and binds to a router.
|
|
112
|
+
*/
|
|
113
|
+
connectedCallback() {
|
|
114
|
+
super.connectedCallback();
|
|
115
|
+
(0, defer_1.defer)(() => {
|
|
116
|
+
this.bindToRouter();
|
|
117
|
+
});
|
|
118
|
+
this.#fragmentUpdateCleaner = this.updateEffect(() => {
|
|
119
|
+
Array.from(this.renderRoot.children).forEach(child => child.remove());
|
|
120
|
+
const fragment = this.#fragmentToRender.get()?.deref() ?? null;
|
|
121
|
+
if (!fragment)
|
|
122
|
+
return;
|
|
123
|
+
this.renderRoot.appendChild(fragment.cloneNode(true));
|
|
124
|
+
});
|
|
125
|
+
this.#attrUpdateCleaner = this.updateEffect(() => {
|
|
126
|
+
const route = this.#routeToRender.get()?.deref() ?? null;
|
|
127
|
+
this.routePath = route?.getAttribute('path') ?? null;
|
|
128
|
+
this.routeID = route?.id ? route?.id : undefined;
|
|
129
|
+
});
|
|
130
|
+
this.#paramsUpdateCleaner = this.updateEffect(() => {
|
|
131
|
+
const params = this[exports.PARAMS].get();
|
|
132
|
+
Array.from(this.attributes)
|
|
133
|
+
.filter(attr => attr.name.startsWith('param-'))
|
|
134
|
+
.filter(attr => !(attr.name.slice(6) in params))
|
|
135
|
+
.forEach(attr => {
|
|
136
|
+
this.removeAttribute(attr.name);
|
|
137
|
+
});
|
|
138
|
+
for (const [key, value] of Object.entries(params)) {
|
|
139
|
+
this.setAttribute(`param-${key}`, value ?? '');
|
|
140
|
+
}
|
|
141
|
+
this.params = params;
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
bindToRouter(router) {
|
|
145
|
+
if (!router) {
|
|
146
|
+
if (this.for) {
|
|
147
|
+
const root = this.getRootNode();
|
|
148
|
+
router = root.querySelector(`tha-router#${this.for}`) ?? undefined;
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
router = this.closest('tha-router') ?? undefined;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
const prevRouter = this.#boundRouter?.get()?.deref() ?? null;
|
|
155
|
+
if (prevRouter === router)
|
|
156
|
+
return prevRouter;
|
|
157
|
+
prevRouter?.assignOutlet(null);
|
|
158
|
+
router?.assignOutlet(this);
|
|
159
|
+
this.#boundRouter.set(router ? new WeakRef(router) : undefined);
|
|
160
|
+
return router ?? null;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Internal method called by the router to set the active route.
|
|
164
|
+
* Updates the routeToRender signal which triggers re-rendering.
|
|
165
|
+
*
|
|
166
|
+
* @param route - The route to render, or null to clear
|
|
167
|
+
* @internal
|
|
168
|
+
*/
|
|
169
|
+
[(_a = exports.PARAMS, exports.RENDER_ROUTE)](route) {
|
|
170
|
+
this.#routeToRender.set(route ? new WeakRef(route) : null);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Lifecycle callback when the element is disconnected from the DOM.
|
|
174
|
+
* Unbinds from the router and cleans up effects.
|
|
175
|
+
*/
|
|
176
|
+
disconnectedCallback() {
|
|
177
|
+
super.disconnectedCallback();
|
|
178
|
+
this.#boundRouter?.get()?.deref()?.assignOutlet(null);
|
|
179
|
+
try {
|
|
180
|
+
this.#fragmentUpdateCleaner();
|
|
181
|
+
this.#attrUpdateCleaner();
|
|
182
|
+
this.#paramsUpdateCleaner();
|
|
183
|
+
}
|
|
184
|
+
catch { /* no-op */ }
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Creates the render root for the element.
|
|
188
|
+
* Returns the element itself to render content in light DOM.
|
|
189
|
+
*
|
|
190
|
+
* @returns The element itself (not a shadow root)
|
|
191
|
+
*/
|
|
192
|
+
createRenderRoot() {
|
|
193
|
+
return this;
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
exports.ThaRouterOutlet = ThaRouterOutlet;
|
|
197
|
+
tslib_1.__decorate([
|
|
198
|
+
(0, decorators_js_1.state)(),
|
|
199
|
+
tslib_1.__metadata("design:type", Object)
|
|
200
|
+
], ThaRouterOutlet.prototype, "params", void 0);
|
|
201
|
+
tslib_1.__decorate([
|
|
202
|
+
(0, decorators_js_1.property)({ type: String }),
|
|
203
|
+
tslib_1.__metadata("design:type", Object)
|
|
204
|
+
], ThaRouterOutlet.prototype, "for", void 0);
|
|
205
|
+
tslib_1.__decorate([
|
|
206
|
+
(0, decorators_js_1.property)({ type: String, reflect: true }),
|
|
207
|
+
tslib_1.__metadata("design:type", Object)
|
|
208
|
+
], ThaRouterOutlet.prototype, "routePath", void 0);
|
|
209
|
+
tslib_1.__decorate([
|
|
210
|
+
(0, decorators_js_1.property)({ type: String, reflect: true }),
|
|
211
|
+
tslib_1.__metadata("design:type", Object)
|
|
212
|
+
], ThaRouterOutlet.prototype, "routeID", void 0);
|
|
213
|
+
exports.ThaRouterOutlet = ThaRouterOutlet = tslib_1.__decorate([
|
|
214
|
+
(0, decorators_js_1.customElement)("tha-router-outlet")
|
|
215
|
+
], ThaRouterOutlet);
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import type { ThaRouter } from "./tha-router";
|
|
2
|
+
import { ThaRoute } from "./tha-route";
|
|
3
|
+
import { SignalWatcherLitElement } from "./signal-watcher-lit-element";
|
|
4
|
+
declare global {
|
|
5
|
+
interface HTMLElementTagNameMap {
|
|
6
|
+
/**
|
|
7
|
+
* An outlet element that renders the active route's content from a tha-router.
|
|
8
|
+
* It automatically binds to the nearest parent tha-router or to a specific router via the `for` attribute.
|
|
9
|
+
* The outlet clones and renders the template content from the active tha-route element.
|
|
10
|
+
* The `routePath` and `routeID` attributes reflect the current route's path and ID.
|
|
11
|
+
*/
|
|
12
|
+
"tha-router-outlet": ThaRouterOutlet;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Symbol used by the router to call the internal render method on the outlet.
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
export declare const RENDER_ROUTE: unique symbol;
|
|
20
|
+
export declare const PARAMS: unique symbol;
|
|
21
|
+
/**
|
|
22
|
+
* A custom element that serves as a rendering target for router content.
|
|
23
|
+
*
|
|
24
|
+
* The outlet automatically binds to its parent tha-router or to a specific router
|
|
25
|
+
* identified by the `for` attribute. When the router's active route changes,
|
|
26
|
+
* the outlet clones and renders the route's template content.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```html
|
|
30
|
+
* <tha-router>
|
|
31
|
+
* <tha-route path="/"><template><h1>Home</h1></template></tha-route>
|
|
32
|
+
* <tha-router-outlet></tha-router-outlet>
|
|
33
|
+
* </tha-router>
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```html
|
|
38
|
+
* <!-- Bind to a specific router by ID -->
|
|
39
|
+
* <tha-router id="main-router">
|
|
40
|
+
* <tha-route path="/"><template><h1>Home</h1></template></tha-route>
|
|
41
|
+
* </tha-router>
|
|
42
|
+
* <tha-router-outlet for="main-router"></tha-router-outlet>
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export declare class ThaRouterOutlet extends SignalWatcherLitElement {
|
|
46
|
+
#private;
|
|
47
|
+
[PARAMS]: import("@lit-labs/signals").Signal.Computed<Readonly<{
|
|
48
|
+
[x: string]: string | undefined;
|
|
49
|
+
}>>;
|
|
50
|
+
params: Record<string, string | undefined>;
|
|
51
|
+
/**
|
|
52
|
+
* The ID of a specific tha-router element to bind to.
|
|
53
|
+
* If not set, binds to the nearest parent tha-router.
|
|
54
|
+
*
|
|
55
|
+
* @attr for
|
|
56
|
+
*/
|
|
57
|
+
for: string | null;
|
|
58
|
+
/**
|
|
59
|
+
* Reflects the path attribute of the currently active route.
|
|
60
|
+
* Automatically updated when the active route changes.
|
|
61
|
+
*
|
|
62
|
+
* @attr routepath
|
|
63
|
+
* @readonly
|
|
64
|
+
*/
|
|
65
|
+
routePath: string | null;
|
|
66
|
+
/**
|
|
67
|
+
* Reflects the ID of the currently active route.
|
|
68
|
+
* Automatically updated when the active route changes.
|
|
69
|
+
*
|
|
70
|
+
* @attr routeid
|
|
71
|
+
* @readonly
|
|
72
|
+
*/
|
|
73
|
+
routeID: string | undefined;
|
|
74
|
+
/**
|
|
75
|
+
* Lifecycle callback when the element is connected to the DOM.
|
|
76
|
+
* Initializes the outlet and binds to a router.
|
|
77
|
+
*/
|
|
78
|
+
connectedCallback(): void;
|
|
79
|
+
/**
|
|
80
|
+
* Binds this outlet to a tha-router element.
|
|
81
|
+
*
|
|
82
|
+
* If no router is provided, automatically searches for one:
|
|
83
|
+
* - If `for` attribute is set, finds router by ID in the same root
|
|
84
|
+
* - Otherwise, finds the nearest parent tha-router
|
|
85
|
+
*
|
|
86
|
+
* Unbinds from the previous router if already bound to a different one.
|
|
87
|
+
*
|
|
88
|
+
* @param router - Optional router to bind to
|
|
89
|
+
* @returns The bound router, or null if no router found
|
|
90
|
+
*/
|
|
91
|
+
bindToRouter(): ThaRouter | null;
|
|
92
|
+
/**
|
|
93
|
+
* Internal method called by the router to set the active route.
|
|
94
|
+
* Updates the routeToRender signal which triggers re-rendering.
|
|
95
|
+
*
|
|
96
|
+
* @param route - The route to render, or null to clear
|
|
97
|
+
* @internal
|
|
98
|
+
*/
|
|
99
|
+
[RENDER_ROUTE](route: ThaRoute | null): void;
|
|
100
|
+
/**
|
|
101
|
+
* Lifecycle callback when the element is disconnected from the DOM.
|
|
102
|
+
* Unbinds from the router and cleans up effects.
|
|
103
|
+
*/
|
|
104
|
+
disconnectedCallback(): void;
|
|
105
|
+
/**
|
|
106
|
+
* Creates the render root for the element.
|
|
107
|
+
* Returns the element itself to render content in light DOM.
|
|
108
|
+
*
|
|
109
|
+
* @returns The element itself (not a shadow root)
|
|
110
|
+
*/
|
|
111
|
+
protected createRenderRoot(): this;
|
|
112
|
+
}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
var _a;
|
|
2
|
+
import { __decorate, __metadata } from "tslib";
|
|
3
|
+
import { customElement, property, state } from "lit/decorators.js";
|
|
4
|
+
import { FRAGMENT, PATH_PATTERN } from "./tha-route";
|
|
5
|
+
import { computed, signal } from "@lit-labs/signals";
|
|
6
|
+
import { noop } from "@telperion/js-utils/function/noop";
|
|
7
|
+
import { SignalWatcherLitElement } from "./signal-watcher-lit-element";
|
|
8
|
+
import { HISTORY } from "./history-managed";
|
|
9
|
+
import { defer } from "@telperion/js-utils/function/defer";
|
|
10
|
+
/**
|
|
11
|
+
* Symbol used by the router to call the internal render method on the outlet.
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
export const RENDER_ROUTE = Symbol('ThaRouterOutlet:renderRoute');
|
|
15
|
+
export const PARAMS = Symbol('ThaRouterOutlet:params');
|
|
16
|
+
/**
|
|
17
|
+
* A custom element that serves as a rendering target for router content.
|
|
18
|
+
*
|
|
19
|
+
* The outlet automatically binds to its parent tha-router or to a specific router
|
|
20
|
+
* identified by the `for` attribute. When the router's active route changes,
|
|
21
|
+
* the outlet clones and renders the route's template content.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```html
|
|
25
|
+
* <tha-router>
|
|
26
|
+
* <tha-route path="/"><template><h1>Home</h1></template></tha-route>
|
|
27
|
+
* <tha-router-outlet></tha-router-outlet>
|
|
28
|
+
* </tha-router>
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```html
|
|
33
|
+
* <!-- Bind to a specific router by ID -->
|
|
34
|
+
* <tha-router id="main-router">
|
|
35
|
+
* <tha-route path="/"><template><h1>Home</h1></template></tha-route>
|
|
36
|
+
* </tha-router>
|
|
37
|
+
* <tha-router-outlet for="main-router"></tha-router-outlet>
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
let ThaRouterOutlet = class ThaRouterOutlet extends SignalWatcherLitElement {
|
|
41
|
+
constructor() {
|
|
42
|
+
super(...arguments);
|
|
43
|
+
/** Weak reference to the bound router instance */
|
|
44
|
+
this.#boundRouter = signal(undefined);
|
|
45
|
+
/** Signal containing weak reference to the route to render */
|
|
46
|
+
this.#routeToRender = signal(null);
|
|
47
|
+
/** Computed signal that extracts the fragment from the route to render */
|
|
48
|
+
this.#fragmentToRender = computed(() => this.#routeToRender.get()?.deref()?.[FRAGMENT].get() ?? null);
|
|
49
|
+
this.#ownParams = computed(() => {
|
|
50
|
+
const router = this.#boundRouter.get()?.deref();
|
|
51
|
+
const route = this.#routeToRender.get()?.deref();
|
|
52
|
+
const pattern = route?.[PATH_PATTERN];
|
|
53
|
+
if (!router || !pattern)
|
|
54
|
+
return null;
|
|
55
|
+
const { location: { pathname } } = router[HISTORY].get();
|
|
56
|
+
return pattern.exec(pathname, 'http://dummy.base')?.pathname.groups ?? null;
|
|
57
|
+
});
|
|
58
|
+
this[_a] = computed(() => {
|
|
59
|
+
const ownParams = this.#ownParams.get() ?? {};
|
|
60
|
+
const parent = this.parentElement?.closest('tha-router-outlet');
|
|
61
|
+
const parentParams = parent?.[PARAMS].get() ?? {};
|
|
62
|
+
return Object.freeze({ ...parentParams, ...ownParams });
|
|
63
|
+
});
|
|
64
|
+
this.params = {};
|
|
65
|
+
/** Cleanup function for the fragment update effect */
|
|
66
|
+
this.#fragmentUpdateCleaner = noop;
|
|
67
|
+
/** Cleanup function for the attribute update effect */
|
|
68
|
+
this.#attrUpdateCleaner = noop;
|
|
69
|
+
this.#paramsUpdateCleaner = noop;
|
|
70
|
+
/**
|
|
71
|
+
* The ID of a specific tha-router element to bind to.
|
|
72
|
+
* If not set, binds to the nearest parent tha-router.
|
|
73
|
+
*
|
|
74
|
+
* @attr for
|
|
75
|
+
*/
|
|
76
|
+
this.for = null;
|
|
77
|
+
/**
|
|
78
|
+
* Reflects the path attribute of the currently active route.
|
|
79
|
+
* Automatically updated when the active route changes.
|
|
80
|
+
*
|
|
81
|
+
* @attr routepath
|
|
82
|
+
* @readonly
|
|
83
|
+
*/
|
|
84
|
+
this.routePath = null;
|
|
85
|
+
/**
|
|
86
|
+
* Reflects the ID of the currently active route.
|
|
87
|
+
* Automatically updated when the active route changes.
|
|
88
|
+
*
|
|
89
|
+
* @attr routeid
|
|
90
|
+
* @readonly
|
|
91
|
+
*/
|
|
92
|
+
this.routeID = undefined;
|
|
93
|
+
}
|
|
94
|
+
/** Weak reference to the bound router instance */
|
|
95
|
+
#boundRouter;
|
|
96
|
+
/** Signal containing weak reference to the route to render */
|
|
97
|
+
#routeToRender;
|
|
98
|
+
/** Computed signal that extracts the fragment from the route to render */
|
|
99
|
+
#fragmentToRender;
|
|
100
|
+
#ownParams;
|
|
101
|
+
/** Cleanup function for the fragment update effect */
|
|
102
|
+
#fragmentUpdateCleaner;
|
|
103
|
+
/** Cleanup function for the attribute update effect */
|
|
104
|
+
#attrUpdateCleaner;
|
|
105
|
+
#paramsUpdateCleaner;
|
|
106
|
+
/**
|
|
107
|
+
* Lifecycle callback when the element is connected to the DOM.
|
|
108
|
+
* Initializes the outlet and binds to a router.
|
|
109
|
+
*/
|
|
110
|
+
connectedCallback() {
|
|
111
|
+
super.connectedCallback();
|
|
112
|
+
defer(() => {
|
|
113
|
+
this.bindToRouter();
|
|
114
|
+
});
|
|
115
|
+
this.#fragmentUpdateCleaner = this.updateEffect(() => {
|
|
116
|
+
Array.from(this.renderRoot.children).forEach(child => child.remove());
|
|
117
|
+
const fragment = this.#fragmentToRender.get()?.deref() ?? null;
|
|
118
|
+
if (!fragment)
|
|
119
|
+
return;
|
|
120
|
+
this.renderRoot.appendChild(fragment.cloneNode(true));
|
|
121
|
+
});
|
|
122
|
+
this.#attrUpdateCleaner = this.updateEffect(() => {
|
|
123
|
+
const route = this.#routeToRender.get()?.deref() ?? null;
|
|
124
|
+
this.routePath = route?.getAttribute('path') ?? null;
|
|
125
|
+
this.routeID = route?.id ? route?.id : undefined;
|
|
126
|
+
});
|
|
127
|
+
this.#paramsUpdateCleaner = this.updateEffect(() => {
|
|
128
|
+
const params = this[PARAMS].get();
|
|
129
|
+
Array.from(this.attributes)
|
|
130
|
+
.filter(attr => attr.name.startsWith('param-'))
|
|
131
|
+
.filter(attr => !(attr.name.slice(6) in params))
|
|
132
|
+
.forEach(attr => {
|
|
133
|
+
this.removeAttribute(attr.name);
|
|
134
|
+
});
|
|
135
|
+
for (const [key, value] of Object.entries(params)) {
|
|
136
|
+
this.setAttribute(`param-${key}`, value ?? '');
|
|
137
|
+
}
|
|
138
|
+
this.params = params;
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
bindToRouter(router) {
|
|
142
|
+
if (!router) {
|
|
143
|
+
if (this.for) {
|
|
144
|
+
const root = this.getRootNode();
|
|
145
|
+
router = root.querySelector(`tha-router#${this.for}`) ?? undefined;
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
router = this.closest('tha-router') ?? undefined;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
const prevRouter = this.#boundRouter?.get()?.deref() ?? null;
|
|
152
|
+
if (prevRouter === router)
|
|
153
|
+
return prevRouter;
|
|
154
|
+
prevRouter?.assignOutlet(null);
|
|
155
|
+
router?.assignOutlet(this);
|
|
156
|
+
this.#boundRouter.set(router ? new WeakRef(router) : undefined);
|
|
157
|
+
return router ?? null;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Internal method called by the router to set the active route.
|
|
161
|
+
* Updates the routeToRender signal which triggers re-rendering.
|
|
162
|
+
*
|
|
163
|
+
* @param route - The route to render, or null to clear
|
|
164
|
+
* @internal
|
|
165
|
+
*/
|
|
166
|
+
[(_a = PARAMS, RENDER_ROUTE)](route) {
|
|
167
|
+
this.#routeToRender.set(route ? new WeakRef(route) : null);
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Lifecycle callback when the element is disconnected from the DOM.
|
|
171
|
+
* Unbinds from the router and cleans up effects.
|
|
172
|
+
*/
|
|
173
|
+
disconnectedCallback() {
|
|
174
|
+
super.disconnectedCallback();
|
|
175
|
+
this.#boundRouter?.get()?.deref()?.assignOutlet(null);
|
|
176
|
+
try {
|
|
177
|
+
this.#fragmentUpdateCleaner();
|
|
178
|
+
this.#attrUpdateCleaner();
|
|
179
|
+
this.#paramsUpdateCleaner();
|
|
180
|
+
}
|
|
181
|
+
catch { /* no-op */ }
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Creates the render root for the element.
|
|
185
|
+
* Returns the element itself to render content in light DOM.
|
|
186
|
+
*
|
|
187
|
+
* @returns The element itself (not a shadow root)
|
|
188
|
+
*/
|
|
189
|
+
createRenderRoot() {
|
|
190
|
+
return this;
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
__decorate([
|
|
194
|
+
state(),
|
|
195
|
+
__metadata("design:type", Object)
|
|
196
|
+
], ThaRouterOutlet.prototype, "params", void 0);
|
|
197
|
+
__decorate([
|
|
198
|
+
property({ type: String }),
|
|
199
|
+
__metadata("design:type", Object)
|
|
200
|
+
], ThaRouterOutlet.prototype, "for", void 0);
|
|
201
|
+
__decorate([
|
|
202
|
+
property({ type: String, reflect: true }),
|
|
203
|
+
__metadata("design:type", Object)
|
|
204
|
+
], ThaRouterOutlet.prototype, "routePath", void 0);
|
|
205
|
+
__decorate([
|
|
206
|
+
property({ type: String, reflect: true }),
|
|
207
|
+
__metadata("design:type", Object)
|
|
208
|
+
], ThaRouterOutlet.prototype, "routeID", void 0);
|
|
209
|
+
ThaRouterOutlet = __decorate([
|
|
210
|
+
customElement("tha-router-outlet")
|
|
211
|
+
], ThaRouterOutlet);
|
|
212
|
+
export { ThaRouterOutlet };
|
|
213
|
+
|
|
214
|
+
//# sourceMappingURL=tha-router-outlet.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../libs/elements/src/router/tha-router-outlet.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAEnE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAY,MAAM,aAAa,CAAC;AAC/D,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AACrD,OAAO,EAAE,IAAI,EAAE,MAAM,mCAAmC,CAAC;AACzD,OAAO,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AACvE,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAE,KAAK,EAAE,MAAM,oCAAoC,CAAC;AAc3D;;;GAGG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,MAAM,CAAC,6BAA6B,CAAC,CAAC;AAClE,MAAM,CAAC,MAAM,MAAM,GAAG,MAAM,CAAC,wBAAwB,CAAC,CAAC;AAEvD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEI,IAAM,eAAe,GAArB,MAAM,eAAgB,SAAQ,uBAAuB;IAArD;;QACL,kDAAkD;QAClD,iBAAY,GAAG,MAAM,CAAiC,SAAS,CAAC,CAAC;QACjE,8DAA8D;QAC9D,mBAAc,GAAG,MAAM,CAA2B,IAAI,CAAC,CAAC;QACxD,0EAA0E;QAC1E,sBAAiB,GAAG,QAAQ,CAAC,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G;IACgB,gBAAgB;QACjC,OAAO,IAAI,CAAC;IACd,CAAC;CACF,CAAA;AAvJC;IADC,KAAK,EAAE;;+CACwC;AAehD;IADC,QAAQ,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;;4CACD;AAU1B;IADC,QAAQ,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;;kDACV;AAUhC;IADC,QAAQ,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;;gDACF;AAhE7B,eAAe;IAD3B,aAAa,CAAC,mBAAmB,CAAC;GACtB,eAAe,CAoL3B","file":"tha-router-outlet.js","sourcesContent":["import { customElement, property, state } from \"lit/decorators.js\";\nimport type { ThaRouter } from \"./tha-router\";\nimport { FRAGMENT, PATH_PATTERN, ThaRoute } from \"./tha-route\";\nimport { computed, signal } from \"@lit-labs/signals\";\nimport { noop } from \"@telperion/js-utils/function/noop\";\nimport { SignalWatcherLitElement } from \"./signal-watcher-lit-element\";\nimport { HISTORY } from \"./history-managed\";\nimport { defer } from \"@telperion/js-utils/function/defer\";\n\ndeclare global {\n interface HTMLElementTagNameMap {\n /**\n * An outlet element that renders the active route's content from a tha-router.\n * It automatically binds to the nearest parent tha-router or to a specific router via the `for` attribute.\n * The outlet clones and renders the template content from the active tha-route element.\n * The `routePath` and `routeID` attributes reflect the current route's path and ID.\n */\n \"tha-router-outlet\": ThaRouterOutlet;\n }\n}\n\n/**\n * Symbol used by the router to call the internal render method on the outlet.\n * @internal\n */\nexport const RENDER_ROUTE = Symbol('ThaRouterOutlet:renderRoute');\nexport const PARAMS = Symbol('ThaRouterOutlet:params');\n\n/**\n * A custom element that serves as a rendering target for router content.\n *\n * The outlet automatically binds to its parent tha-router or to a specific router\n * identified by the `for` attribute. When the router's active route changes,\n * the outlet clones and renders the route's template content.\n *\n * @example\n * ```html\n * <tha-router>\n * <tha-route path=\"/\"><template><h1>Home</h1></template></tha-route>\n * <tha-router-outlet></tha-router-outlet>\n * </tha-router>\n * ```\n *\n * @example\n * ```html\n * <!-- Bind to a specific router by ID -->\n * <tha-router id=\"main-router\">\n * <tha-route path=\"/\"><template><h1>Home</h1></template></tha-route>\n * </tha-router>\n * <tha-router-outlet for=\"main-router\"></tha-router-outlet>\n * ```\n */\n@customElement(\"tha-router-outlet\")\nexport class ThaRouterOutlet extends SignalWatcherLitElement {\n /** Weak reference to the bound router instance */\n #boundRouter = signal<WeakRef<ThaRouter> | undefined>(undefined);\n /** Signal containing weak reference to the route to render */\n #routeToRender = signal<WeakRef<ThaRoute> | null>(null);\n /** Computed signal that extracts the fragment from the route to render */\n #fragmentToRender = computed(() => this.#routeToRender.get()?.deref()?.[FRAGMENT].get() ?? null);\n\n #ownParams = computed(() => {\n const router = this.#boundRouter.get()?.deref();\n const route = this.#routeToRender.get()?.deref();\n const pattern = route?.[PATH_PATTERN];\n\n if (!router || !pattern) return null;\n\n const {location: {pathname}} = router[HISTORY].get();\n\n return pattern.exec(pathname, 'http://dummy.base')?.pathname.groups ?? null;\n });\n\n [PARAMS] = computed(() => {\n const ownParams = this.#ownParams.get() ?? {};\n const parent = this.parentElement?.closest('tha-router-outlet') as ThaRouterOutlet;\n const parentParams: Record<string, string | undefined> = parent?.[PARAMS].get() ?? {};\n\n return Object.freeze({...parentParams, ...ownParams});\n });\n\n @state()\n params: Record<string, string | undefined> = {};\n\n /** Cleanup function for the fragment update effect */\n #fragmentUpdateCleaner = noop;\n /** Cleanup function for the attribute update effect */\n #attrUpdateCleaner = noop;\n #paramsUpdateCleaner = noop;\n\n /**\n * The ID of a specific tha-router element to bind to.\n * If not set, binds to the nearest parent tha-router.\n *\n * @attr for\n */\n @property({ type: String })\n for: string | null = null;\n\n /**\n * Reflects the path attribute of the currently active route.\n * Automatically updated when the active route changes.\n *\n * @attr routepath\n * @readonly\n */\n @property({ type: String, reflect: true })\n routePath: string | null = null;\n\n /**\n * Reflects the ID of the currently active route.\n * Automatically updated when the active route changes.\n *\n * @attr routeid\n * @readonly\n */\n @property({ type: String, reflect: true })\n routeID: string | undefined = undefined;\n\n /**\n * Lifecycle callback when the element is connected to the DOM.\n * Initializes the outlet and binds to a router.\n */\n override connectedCallback(): void {\n super.connectedCallback();\n\n defer(() => {\n this.bindToRouter();\n });\n\n this.#fragmentUpdateCleaner = this.updateEffect(() => {\n Array.from(this.renderRoot.children).forEach(child => child.remove());\n\n const fragment = this.#fragmentToRender.get()?.deref() ?? null;\n if (!fragment) return;\n\n this.renderRoot.appendChild(fragment.cloneNode(true));\n });\n\n this.#attrUpdateCleaner = this.updateEffect(() => {\n const route = this.#routeToRender.get()?.deref() ?? null;\n this.routePath = route?.getAttribute('path') ?? null;\n this.routeID = route?.id ? route?.id : undefined;\n });\n\n this.#paramsUpdateCleaner = this.updateEffect(() => {\n const params = this[PARAMS].get();\n\n Array.from(this.attributes)\n .filter(attr => attr.name.startsWith('param-'))\n .filter(attr => !(attr.name.slice(6) in params))\n .forEach(attr => {\n this.removeAttribute(attr.name);\n });\n\n for (const [key, value] of Object.entries(params)) {\n this.setAttribute(`param-${key}`, value ?? '');\n }\n\n this.params = params;\n });\n }\n\n /**\n * Binds this outlet to a tha-router element.\n *\n * If no router is provided, automatically searches for one:\n * - If `for` attribute is set, finds router by ID in the same root\n * - Otherwise, finds the nearest parent tha-router\n *\n * Unbinds from the previous router if already bound to a different one.\n *\n * @param router - Optional router to bind to\n * @returns The bound router, or null if no router found\n */\n bindToRouter(): ThaRouter | null;\n bindToRouter(router?: ThaRouter): ThaRouter | null {\n if (!router) {\n if (this.for) {\n const root = this.getRootNode() as Document | ShadowRoot;\n router = root.querySelector(`tha-router#${this.for}`) as ThaRouter ?? undefined;\n } else {\n router = this.closest('tha-router') ?? undefined;\n }\n }\n\n const prevRouter = this.#boundRouter?.get()?.deref() ?? null;\n\n if (prevRouter === router) return prevRouter;\n\n prevRouter?.assignOutlet(null);\n router?.assignOutlet(this);\n this.#boundRouter.set(router ? new WeakRef(router) : undefined);\n\n return router ?? null;\n }\n\n /**\n * Internal method called by the router to set the active route.\n * Updates the routeToRender signal which triggers re-rendering.\n *\n * @param route - The route to render, or null to clear\n * @internal\n */\n [RENDER_ROUTE](route: ThaRoute | null) {\n this.#routeToRender.set(route ? new WeakRef(route) : null);\n }\n\n /**\n * Lifecycle callback when the element is disconnected from the DOM.\n * Unbinds from the router and cleans up effects.\n */\n override disconnectedCallback(): void {\n super.disconnectedCallback();\n\n this.#boundRouter?.get()?.deref()?.assignOutlet(null);\n\n try {\n this.#fragmentUpdateCleaner();\n this.#attrUpdateCleaner();\n this.#paramsUpdateCleaner();\n } catch { /* no-op */ }\n }\n\n /**\n * Creates the render root for the element.\n * Returns the element itself to render content in light DOM.\n *\n * @returns The element itself (not a shadow root)\n */\n protected override createRenderRoot() {\n return this;\n }\n}\n"]}
|