@hamur/adapter-angular 0.0.1 → 0.0.3

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/src/indicators.ts CHANGED
@@ -1,251 +1,220 @@
1
- import {
2
- Component,
3
- DestroyRef,
4
- ElementRef,
5
- HostAttributeToken,
6
- Injector,
7
- afterNextRender,
8
- afterRenderEffect,
9
- assertInInjectionContext,
10
- computed,
11
- effect,
12
- inject,
13
- input,
14
- runInInjectionContext,
15
- signal,
16
- untracked,
17
- viewChild,
18
- type OnInit,
19
- type Signal,
20
- } from "@angular/core"
21
- import {
22
- ANNOUNCER_ASSERTIVE_ID,
23
- ANNOUNCER_POLITE_ID,
24
- ROUTE_ANNOUNCER_ID,
25
- announcerActions,
26
- announcerRole,
27
- createAnnouncer,
28
- createLoadingIndicator,
29
- createRouteAnnouncer,
30
- hideAnnouncer,
31
- loadingIndicatorOptions,
32
- paintLoadingIndicator,
33
- } from "hamur/adapter"
34
- import type {
35
- AnnouncerActions,
36
- AnnouncerPoliteness,
37
- LoadingIndicatorActions,
38
- LoadingIndicatorOptions,
39
- RouteAnnouncerActions,
40
- RouteAnnouncerPoliteness,
41
- } from "hamur/adapter"
42
- import { HAMUR_APP, HAMUR_NAVIGATION, required } from "./context.ts"
43
-
1
+ import { Component, DestroyRef, ElementRef, HostAttributeToken, Injector, afterNextRender, afterRenderEffect, assertInInjectionContext, computed, effect, inject, input, runInInjectionContext, signal, untracked, viewChild, } from "@angular/core";
2
+ import { ANNOUNCER_ASSERTIVE_ID, ANNOUNCER_POLITE_ID, ROUTE_ANNOUNCER_ID, announcerActions, announcerRole, createAnnouncer, createLoadingIndicator, createRouteAnnouncer, hideAnnouncer, loadingIndicatorOptions, paintLoadingIndicator, } from "hamur/adapter";
3
+ import { HAMUR_APP, HAMUR_NAVIGATION, required } from "./context.ts";
4
+ import * as i0 from "@angular/core";
44
5
  /** The bar's state as signals, and its `start`, `finish`, `set` and `clear`; it follows the navigation while a component that called it is alive. */
45
- export function injectLoadingIndicator(options?: LoadingIndicatorOptions): {
46
- progress: Signal<number>
47
- isLoading: Signal<boolean>
48
- error: Signal<boolean>
49
- } & LoadingIndicatorActions {
50
- assertInInjectionContext(injectLoadingIndicator)
51
- const store = createLoadingIndicator(required(HAMUR_APP, "injectLoadingIndicator"), options)
52
- const navigation = required(HAMUR_NAVIGATION, "injectLoadingIndicator")
53
- const state = signal(store.get())
54
- const injector = inject(Injector)
55
- const destroy = inject(DestroyRef)
56
- afterNextRender(() => {
57
- state.set(store.get())
58
- const stopState = store.subscribe(() => state.set(store.get()))
59
- const listeners = new Set<() => void>()
60
- const watcher = effect(
61
- () => {
62
- navigation()
63
- untracked(() => {
64
- for (const listener of [...listeners]) listener()
65
- })
66
- },
67
- { injector },
68
- )
69
- const stopFollow = store.follow({
70
- get: () => untracked(navigation),
71
- subscribe(listener) {
72
- listeners.add(listener)
73
- return () => void listeners.delete(listener)
74
- },
75
- })
76
- destroy.onDestroy(() => {
77
- stopState()
78
- stopFollow()
79
- watcher.destroy()
80
- })
81
- })
82
- return {
83
- progress: computed(() => state().progress),
84
- isLoading: computed(() => state().isLoading),
85
- error: computed(() => state().error),
86
- start: store.start,
87
- finish: store.finish,
88
- set: store.set,
89
- clear: store.clear,
90
- }
6
+ export function injectLoadingIndicator(options) {
7
+ assertInInjectionContext(injectLoadingIndicator);
8
+ const store = createLoadingIndicator(required(HAMUR_APP, "injectLoadingIndicator"), options);
9
+ const navigation = required(HAMUR_NAVIGATION, "injectLoadingIndicator");
10
+ const state = signal(store.get(), /* @ts-ignore */
11
+ ...(ngDevMode ? [{ debugName: "state" }] : /* istanbul ignore next */ []));
12
+ const injector = inject(Injector);
13
+ const destroy = inject(DestroyRef);
14
+ afterNextRender(() => {
15
+ state.set(store.get());
16
+ const stopState = store.subscribe(() => state.set(store.get()));
17
+ const listeners = new Set();
18
+ const watcher = effect(() => {
19
+ navigation();
20
+ untracked(() => {
21
+ for (const listener of [...listeners])
22
+ listener();
23
+ });
24
+ }, { ...(ngDevMode ? { debugName: "watcher" } : /* istanbul ignore next */ {}), injector });
25
+ const stopFollow = store.follow({
26
+ get: () => untracked(navigation),
27
+ subscribe(listener) {
28
+ listeners.add(listener);
29
+ return () => void listeners.delete(listener);
30
+ },
31
+ });
32
+ destroy.onDestroy(() => {
33
+ stopState();
34
+ stopFollow();
35
+ watcher.destroy();
36
+ });
37
+ });
38
+ return {
39
+ progress: computed(() => state().progress),
40
+ isLoading: computed(() => state().isLoading),
41
+ error: computed(() => state().error),
42
+ start: store.start,
43
+ finish: store.finish,
44
+ set: store.set,
45
+ clear: store.clear,
46
+ };
91
47
  }
92
-
93
48
  /**
94
49
  * `<hamur-loading-indicator />`, the bar at the top of the page that follows `injectNavigation().state`. Its look is
95
50
  * written through `element.style` after the first render, so the server's element has no `style` attribute.
96
51
  */
97
- @Component({
98
- selector: "hamur-loading-indicator",
99
- template: "",
100
- host: { "data-hamur-loading-indicator": "", "aria-hidden": "true" },
101
- })
102
- export class LoadingIndicator implements OnInit {
103
- readonly color = input<string>()
104
- readonly errorColor = input<string>()
105
- readonly height = input<number>()
106
- readonly duration = input<number>()
107
- readonly throttle = input<number>()
108
- readonly estimatedProgress = input<(duration: number, elapsed: number) => number>()
109
- private readonly injector = inject(Injector)
110
- private readonly element = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement
111
-
112
- ngOnInit(): void {
113
- runInInjectionContext(this.injector, () => {
114
- const { progress, isLoading, error } = injectLoadingIndicator(
115
- loadingIndicatorOptions({
116
- duration: this.duration(),
117
- throttle: this.throttle(),
118
- estimatedProgress: this.estimatedProgress(),
119
- }),
120
- )
121
- afterRenderEffect(() =>
122
- paintLoadingIndicator(
123
- this.element,
124
- { progress: progress(), isLoading: isLoading(), error: error() },
125
- { color: this.color(), errorColor: this.errorColor(), height: this.height() },
126
- ),
127
- )
128
- })
129
- }
52
+ export class LoadingIndicator {
53
+ color = input(/* @ts-ignore */
54
+ ...(ngDevMode ? [undefined, { debugName: "color" }] : /* istanbul ignore next */ []));
55
+ errorColor = input(/* @ts-ignore */
56
+ ...(ngDevMode ? [undefined, { debugName: "errorColor" }] : /* istanbul ignore next */ []));
57
+ height = input(/* @ts-ignore */
58
+ ...(ngDevMode ? [undefined, { debugName: "height" }] : /* istanbul ignore next */ []));
59
+ duration = input(/* @ts-ignore */
60
+ ...(ngDevMode ? [undefined, { debugName: "duration" }] : /* istanbul ignore next */ []));
61
+ throttle = input(/* @ts-ignore */
62
+ ...(ngDevMode ? [undefined, { debugName: "throttle" }] : /* istanbul ignore next */ []));
63
+ estimatedProgress = input(/* @ts-ignore */
64
+ ...(ngDevMode ? [undefined, { debugName: "estimatedProgress" }] : /* istanbul ignore next */ []));
65
+ injector = inject(Injector);
66
+ element = inject(ElementRef).nativeElement;
67
+ ngOnInit() {
68
+ runInInjectionContext(this.injector, () => {
69
+ const { progress, isLoading, error } = injectLoadingIndicator(loadingIndicatorOptions({
70
+ duration: this.duration(),
71
+ throttle: this.throttle(),
72
+ estimatedProgress: this.estimatedProgress(),
73
+ }));
74
+ afterRenderEffect(() => paintLoadingIndicator(this.element, { progress: progress(), isLoading: isLoading(), error: error() }, { color: this.color(), errorColor: this.errorColor(), height: this.height() }));
75
+ });
76
+ }
77
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: LoadingIndicator, deps: [], target: i0.ɵɵFactoryTarget.Component });
78
+ static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.1.0", version: "22.3.2", type: LoadingIndicator, isStandalone: true, selector: "hamur-loading-indicator", inputs: { color: { classPropertyName: "color", publicName: "color", isSignal: true, isRequired: false, transformFunction: null }, errorColor: { classPropertyName: "errorColor", publicName: "errorColor", isSignal: true, isRequired: false, transformFunction: null }, height: { classPropertyName: "height", publicName: "height", isSignal: true, isRequired: false, transformFunction: null }, duration: { classPropertyName: "duration", publicName: "duration", isSignal: true, isRequired: false, transformFunction: null }, throttle: { classPropertyName: "throttle", publicName: "throttle", isSignal: true, isRequired: false, transformFunction: null }, estimatedProgress: { classPropertyName: "estimatedProgress", publicName: "estimatedProgress", isSignal: true, isRequired: false, transformFunction: null } }, host: { attributes: { "data-hamur-loading-indicator": "", "aria-hidden": "true" } }, ngImport: i0, template: "", isInline: true });
130
79
  }
131
-
80
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: LoadingIndicator, decorators: [{
81
+ type: Component,
82
+ args: [{
83
+ selector: "hamur-loading-indicator",
84
+ template: "",
85
+ host: { "data-hamur-loading-indicator": "", "aria-hidden": "true" },
86
+ }]
87
+ }], propDecorators: { color: [{ type: i0.Input, args: [{ isSignal: true, alias: "color", required: false }] }], errorColor: [{ type: i0.Input, args: [{ isSignal: true, alias: "errorColor", required: false }] }], height: [{ type: i0.Input, args: [{ isSignal: true, alias: "height", required: false }] }], duration: [{ type: i0.Input, args: [{ isSignal: true, alias: "duration", required: false }] }], throttle: [{ type: i0.Input, args: [{ isSignal: true, alias: "throttle", required: false }] }], estimatedProgress: [{ type: i0.Input, args: [{ isSignal: true, alias: "estimatedProgress", required: false }] }] } });
132
88
  /** The page's route announcer as signals, and `set`, `polite` and `assertive`. */
133
- export function injectRouteAnnouncer(): {
134
- message: Signal<string>
135
- politeness: Signal<RouteAnnouncerPoliteness>
136
- } & RouteAnnouncerActions {
137
- assertInInjectionContext(injectRouteAnnouncer)
138
- const store = createRouteAnnouncer(required(HAMUR_APP, "injectRouteAnnouncer"))
139
- const state = signal(store.get())
140
- const destroy = inject(DestroyRef)
141
- afterNextRender(() => {
142
- state.set(store.get())
143
- destroy.onDestroy(store.subscribe(() => state.set(store.get())))
144
- })
145
- return {
146
- message: computed(() => state().message),
147
- politeness: computed(() => state().politeness),
148
- set: store.set,
149
- polite: store.polite,
150
- assertive: store.assertive,
151
- }
89
+ export function injectRouteAnnouncer() {
90
+ assertInInjectionContext(injectRouteAnnouncer);
91
+ const store = createRouteAnnouncer(required(HAMUR_APP, "injectRouteAnnouncer"));
92
+ const state = signal(store.get(), /* @ts-ignore */
93
+ ...(ngDevMode ? [{ debugName: "state" }] : /* istanbul ignore next */ []));
94
+ const destroy = inject(DestroyRef);
95
+ afterNextRender(() => {
96
+ state.set(store.get());
97
+ destroy.onDestroy(store.subscribe(() => state.set(store.get())));
98
+ });
99
+ return {
100
+ message: computed(() => state().message),
101
+ politeness: computed(() => state().politeness),
102
+ set: store.set,
103
+ polite: store.polite,
104
+ assertive: store.assertive,
105
+ };
152
106
  }
153
-
154
107
  /**
155
108
  * `<hamur-route-announcer />` is the region that speaks each navigation; the first one mounted holds `#__hamur_announcer`
156
109
  * and the navigator's own region goes away while it is. A `politeness` pins `aria-live` and `role`; a `class` or `style` on the
157
110
  * element keeps it visible.
158
111
  */
159
- @Component({
160
- selector: "hamur-route-announcer",
161
- template: "",
162
- host: {
163
- "[attr.id]": "owner() ? id : null",
164
- "[attr.aria-live]": "owner() ? live() : 'off'",
165
- "aria-atomic": "true",
166
- "[attr.role]": "owner() ? role() : null",
167
- "[textContent]": "owner() ? message() : ''",
168
- },
169
- })
170
112
  export class RouteAnnouncer {
171
- readonly politeness = input<RouteAnnouncerPoliteness>()
172
- protected readonly id = ROUTE_ANNOUNCER_ID
173
- private readonly store = createRouteAnnouncer(required(HAMUR_APP, "RouteAnnouncer"))
174
- private readonly state = injectRouteAnnouncer()
175
- private readonly element = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement
176
- private readonly styled =
177
- inject(new HostAttributeToken("class"), { optional: true }) !== null ||
178
- inject(new HostAttributeToken("style"), { optional: true }) !== null
179
- private readonly key = {}
180
- private readonly tick = signal(0)
181
-
182
- protected readonly message = this.state.message
183
- protected readonly live = computed(() => this.politeness() ?? this.state.politeness())
184
- protected readonly role = computed(() => announcerRole(this.live()))
185
- protected readonly owner = computed(() => {
186
- this.tick()
187
- return this.store.claim(this.key, () => this.tick.update((count) => count + 1))
188
- })
189
-
190
- constructor() {
191
- afterRenderEffect((onCleanup) => {
192
- if (!this.owner()) return
193
- onCleanup(this.store.mount())
194
- if (!this.styled) hideAnnouncer(this.element)
195
- })
196
- inject(DestroyRef).onDestroy(() => this.store.release(this.key))
197
- }
113
+ politeness = input(/* @ts-ignore */
114
+ ...(ngDevMode ? [undefined, { debugName: "politeness" }] : /* istanbul ignore next */ []));
115
+ id = ROUTE_ANNOUNCER_ID;
116
+ store = createRouteAnnouncer(required(HAMUR_APP, "RouteAnnouncer"));
117
+ state = injectRouteAnnouncer();
118
+ element = inject(ElementRef).nativeElement;
119
+ styled = inject(new HostAttributeToken("class"), { optional: true }) !== null ||
120
+ inject(new HostAttributeToken("style"), { optional: true }) !== null;
121
+ key = {};
122
+ tick = signal(0, /* @ts-ignore */
123
+ ...(ngDevMode ? [{ debugName: "tick" }] : /* istanbul ignore next */ []));
124
+ message = this.state.message;
125
+ live = computed(() => this.politeness() ?? this.state.politeness(), /* @ts-ignore */
126
+ ...(ngDevMode ? [{ debugName: "live" }] : /* istanbul ignore next */ []));
127
+ role = computed(() => announcerRole(this.live()), /* @ts-ignore */
128
+ ...(ngDevMode ? [{ debugName: "role" }] : /* istanbul ignore next */ []));
129
+ owner = computed(() => {
130
+ this.tick();
131
+ return this.store.claim(this.key, () => this.tick.update((count) => count + 1));
132
+ }, /* @ts-ignore */
133
+ ...(ngDevMode ? [{ debugName: "owner" }] : /* istanbul ignore next */ []));
134
+ constructor() {
135
+ afterRenderEffect((onCleanup) => {
136
+ if (!this.owner())
137
+ return;
138
+ onCleanup(this.store.mount());
139
+ if (!this.styled)
140
+ hideAnnouncer(this.element);
141
+ });
142
+ inject(DestroyRef).onDestroy(() => this.store.release(this.key));
143
+ }
144
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: RouteAnnouncer, deps: [], target: i0.ɵɵFactoryTarget.Component });
145
+ static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.1.0", version: "22.3.2", type: RouteAnnouncer, isStandalone: true, selector: "hamur-route-announcer", inputs: { politeness: { classPropertyName: "politeness", publicName: "politeness", isSignal: true, isRequired: false, transformFunction: null } }, host: { attributes: { "aria-atomic": "true" }, properties: { "attr.id": "owner() ? id : null", "attr.aria-live": "owner() ? live() : 'off'", "attr.role": "owner() ? role() : null", "textContent": "owner() ? message() : ''" } }, ngImport: i0, template: "", isInline: true });
198
146
  }
199
-
147
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: RouteAnnouncer, decorators: [{
148
+ type: Component,
149
+ args: [{
150
+ selector: "hamur-route-announcer",
151
+ template: "",
152
+ host: {
153
+ "[attr.id]": "owner() ? id : null",
154
+ "[attr.aria-live]": "owner() ? live() : 'off'",
155
+ "aria-atomic": "true",
156
+ "[attr.role]": "owner() ? role() : null",
157
+ "[textContent]": "owner() ? message() : ''",
158
+ },
159
+ }]
160
+ }], ctorParameters: () => [], propDecorators: { politeness: [{ type: i0.Input, args: [{ isSignal: true, alias: "politeness", required: false }] }] } });
200
161
  /** The page's announcer as signals, and `set`, `polite` and `assertive` (`polite` unless `politeness` says otherwise). */
201
- export function injectAnnouncer({ politeness = "polite" }: { politeness?: AnnouncerPoliteness } = {}): {
202
- message: Signal<string>
203
- politeness: Signal<AnnouncerPoliteness>
204
- } & AnnouncerActions {
205
- assertInInjectionContext(injectAnnouncer)
206
- const store = createAnnouncer(required(HAMUR_APP, "injectAnnouncer"))
207
- const state = signal(store.get())
208
- const destroy = inject(DestroyRef)
209
- afterNextRender(() => {
210
- state.set(store.get())
211
- destroy.onDestroy(store.subscribe(() => state.set(store.get())))
212
- })
213
- return {
214
- message: computed(() => state().message),
215
- politeness: computed(() => state().politeness),
216
- ...announcerActions(store, politeness),
217
- }
162
+ export function injectAnnouncer({ politeness = "polite" } = {}) {
163
+ assertInInjectionContext(injectAnnouncer);
164
+ const store = createAnnouncer(required(HAMUR_APP, "injectAnnouncer"));
165
+ const state = signal(store.get(), /* @ts-ignore */
166
+ ...(ngDevMode ? [{ debugName: "state" }] : /* istanbul ignore next */ []));
167
+ const destroy = inject(DestroyRef);
168
+ afterNextRender(() => {
169
+ state.set(store.get());
170
+ destroy.onDestroy(store.subscribe(() => state.set(store.get())));
171
+ });
172
+ return {
173
+ message: computed(() => state().message),
174
+ politeness: computed(() => state().politeness),
175
+ ...announcerActions(store, politeness),
176
+ };
218
177
  }
219
-
220
178
  /** `<hamur-announcer />` holds the two regions `injectAnnouncer()` writes to: `#__hamur_announcer_polite` and `#__hamur_announcer_assertive`. */
221
- @Component({
222
- selector: "hamur-announcer",
223
- template: `<div #polite [attr.id]="politeId" aria-live="polite" aria-atomic="true" role="status">{{ regions().polite }}</div
179
+ export class Announcer {
180
+ politeId = ANNOUNCER_POLITE_ID;
181
+ assertiveId = ANNOUNCER_ASSERTIVE_ID;
182
+ store = createAnnouncer(required(HAMUR_APP, "Announcer"));
183
+ state = signal(this.store.get(), /* @ts-ignore */
184
+ ...(ngDevMode ? [{ debugName: "state" }] : /* istanbul ignore next */ []));
185
+ regions = computed(() => this.state().regions, /* @ts-ignore */
186
+ ...(ngDevMode ? [{ debugName: "regions" }] : /* istanbul ignore next */ []));
187
+ polite = viewChild.required("polite", /* @ts-ignore */
188
+ ...(ngDevMode ? [{ debugName: "polite" }] : /* istanbul ignore next */ []));
189
+ assertive = viewChild.required("assertive", /* @ts-ignore */
190
+ ...(ngDevMode ? [{ debugName: "assertive" }] : /* istanbul ignore next */ []));
191
+ constructor() {
192
+ const destroy = inject(DestroyRef);
193
+ afterNextRender(() => {
194
+ this.state.set(this.store.get());
195
+ hideAnnouncer(this.polite().nativeElement);
196
+ hideAnnouncer(this.assertive().nativeElement);
197
+ const stop = this.store.subscribe(() => this.state.set(this.store.get()));
198
+ const unmount = this.store.mount();
199
+ destroy.onDestroy(() => {
200
+ stop();
201
+ unmount();
202
+ });
203
+ });
204
+ }
205
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: Announcer, deps: [], target: i0.ɵɵFactoryTarget.Component });
206
+ static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.2.0", version: "22.3.2", type: Announcer, isStandalone: true, selector: "hamur-announcer", viewQueries: [{ propertyName: "polite", first: true, predicate: ["polite"], descendants: true, isSignal: true }, { propertyName: "assertive", first: true, predicate: ["assertive"], descendants: true, isSignal: true }], ngImport: i0, template: `<div #polite [attr.id]="politeId" aria-live="polite" aria-atomic="true" role="status">{{ regions().polite }}</div
224
207
  ><div #assertive [attr.id]="assertiveId" aria-live="assertive" aria-atomic="true" role="alert">{{
225
208
  regions().assertive
226
- }}</div>`,
227
- })
228
- export class Announcer {
229
- protected readonly politeId = ANNOUNCER_POLITE_ID
230
- protected readonly assertiveId = ANNOUNCER_ASSERTIVE_ID
231
- private readonly store = createAnnouncer(required(HAMUR_APP, "Announcer"))
232
- private readonly state = signal(this.store.get())
233
- protected readonly regions = computed(() => this.state().regions)
234
- private readonly polite = viewChild.required<ElementRef<HTMLElement>>("polite")
235
- private readonly assertive = viewChild.required<ElementRef<HTMLElement>>("assertive")
236
-
237
- constructor() {
238
- const destroy = inject(DestroyRef)
239
- afterNextRender(() => {
240
- this.state.set(this.store.get())
241
- hideAnnouncer(this.polite().nativeElement)
242
- hideAnnouncer(this.assertive().nativeElement)
243
- const stop = this.store.subscribe(() => this.state.set(this.store.get()))
244
- const unmount = this.store.mount()
245
- destroy.onDestroy(() => {
246
- stop()
247
- unmount()
248
- })
249
- })
250
- }
209
+ }}</div>`, isInline: true });
251
210
  }
211
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: Announcer, decorators: [{
212
+ type: Component,
213
+ args: [{
214
+ selector: "hamur-announcer",
215
+ template: `<div #polite [attr.id]="politeId" aria-live="polite" aria-atomic="true" role="status">{{ regions().polite }}</div
216
+ ><div #assertive [attr.id]="assertiveId" aria-live="assertive" aria-atomic="true" role="alert">{{
217
+ regions().assertive
218
+ }}</div>`,
219
+ }]
220
+ }], ctorParameters: () => [], propDecorators: { polite: [{ type: i0.ViewChild, args: ["polite", { isSignal: true }] }], assertive: [{ type: i0.ViewChild, args: ["assertive", { isSignal: true }] }] } });
package/src/island.ts CHANGED
@@ -1,95 +1,89 @@
1
- import {
2
- ApplicationRef,
3
- Component,
4
- DestroyRef,
5
- ElementRef,
6
- InjectionToken,
7
- Injector,
8
- PendingTasks,
9
- Renderer2,
10
- booleanAttribute,
11
- inject,
12
- input,
13
- type OnInit,
14
- type TemplateRef,
15
- type Type,
16
- } from "@angular/core"
17
- import { ISLAND_ELEMENT, renderIsland } from "hamur/adapter"
18
- import type { IslandOn } from "hamur/adapter"
19
- import { injectId } from "./client-parts.ts"
20
- import { HAMUR_APP, HAMUR_FAIL, onServer } from "./context.ts"
21
-
1
+ import { ApplicationRef, Component, DestroyRef, ElementRef, InjectionToken, Injector, PendingTasks, Renderer2, booleanAttribute, inject, input, } from "@angular/core";
2
+ import { ISLAND_ELEMENT, renderIsland } from "hamur/adapter";
3
+ import { injectId } from "./client-parts.ts";
4
+ import { HAMUR_APP, HAMUR_FAIL, onServer } from "./context.ts";
5
+ import * as i0 from "@angular/core";
22
6
  /** A component that writes its own nodes below its host: a template it renders there is a view the application checks. */
23
- export abstract class HostWriter {
24
- protected readonly host = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement
25
- protected readonly renderer = inject(Renderer2)
26
- private readonly appRef = inject(ApplicationRef)
27
- private readonly injector = inject(Injector)
28
- private readonly destroyRef = inject(DestroyRef)
29
-
30
- protected append(template: TemplateRef<unknown>, into: Element = this.host): void {
31
- const view = template.createEmbeddedView({}, this.injector)
32
- this.appRef.attachView(view)
33
- for (const node of view.rootNodes) this.renderer.appendChild(into, node)
34
- this.destroyRef.onDestroy(() => view.destroy())
35
- }
7
+ export class HostWriter {
8
+ host = inject(ElementRef).nativeElement;
9
+ renderer = inject(Renderer2);
10
+ appRef = inject(ApplicationRef);
11
+ injector = inject(Injector);
12
+ destroyRef = inject(DestroyRef);
13
+ append(template, into = this.host) {
14
+ const view = template.createEmbeddedView({}, this.injector);
15
+ this.appRef.attachView(view);
16
+ for (const node of view.rootNodes)
17
+ this.renderer.appendChild(into, node);
18
+ this.destroyRef.onDestroy(() => view.destroy());
19
+ }
36
20
  }
37
-
38
21
  /** The element the core's islands runtime reads, `<appId>-island` of the build; a token, so a test gives it another app id. */
39
- export const HAMUR_ISLAND_ELEMENT = new InjectionToken<string>("hamur island element", {
40
- providedIn: "root",
41
- factory: () => ISLAND_ELEMENT,
42
- })
43
-
22
+ export const HAMUR_ISLAND_ELEMENT = new InjectionToken("hamur island element", {
23
+ providedIn: "root",
24
+ factory: () => ISLAND_ELEMENT,
25
+ });
44
26
  /**
45
27
  * On an island page, `component` rendered on the server as an application of its own inside `<hamur-island>`,
46
28
  * hydrated on its own in the browser; `fallback` is what the server renders under `on="only"`. Its template is empty: the island's
47
29
  * HTML is written below its host on the server and left as it is in the browser. The host is the core's element when the app id is
48
30
  * `hamur`; under another app id the host holds `<appId>-island`, which the runtime reads.
49
31
  */
50
- @Component({ selector: "hamur-island", template: "" })
51
- export class Island extends HostWriter implements OnInit {
52
- /** The default export of a file under `app/islands/`. */
53
- readonly component = input.required<Type<unknown>>()
54
- readonly props = input<Record<string, unknown>>()
55
- /** The island directive: when the island hydrates, `"only"` to mount it in the browser alone, or a directive a module adds; default `"load"`. */
56
- readonly on = input<IslandOn>()
57
- readonly fallback = input<TemplateRef<unknown>>()
58
- /** The island's id across swaps; a swap to a page with the same id keeps its element and state. */
59
- readonly persist = input<string>()
60
- /** With `persist`, a swap keeps the island's old props. */
61
- readonly persistProps = input(false, { transform: booleanAttribute })
62
- private readonly app = inject(HAMUR_APP, { optional: true })
63
- private readonly id = injectId()
64
- private readonly tasks = inject(PendingTasks)
65
- private readonly fail = inject(HAMUR_FAIL, { optional: true })
66
- private readonly server = onServer()
67
- private readonly element = inject(HAMUR_ISLAND_ELEMENT)
68
-
69
- constructor() {
70
- super()
71
- if (!this.app) throw new Error("Island must be rendered inside a Hamur app")
72
- }
73
-
74
- ngOnInit(): void {
75
- if (!this.server) return
76
- const island = renderIsland(this.app!, this.id, this.component(), this.props() ?? {}, this.on(), undefined, {
77
- persist: this.persist(),
78
- persistProps: this.persistProps(),
79
- })
80
- const element: Element =
81
- this.host.localName === this.element ? this.host : this.renderer.createElement(this.element)
82
- if (element !== this.host) this.renderer.appendChild(this.host, element)
83
- for (const [name, value] of Object.entries(island.attributes)) this.renderer.setAttribute(element, name, value)
84
- if (!island.html) {
85
- const fallback = this.fallback()
86
- if (fallback) this.append(fallback, element)
87
- return
32
+ export class Island extends HostWriter {
33
+ /** The default export of a file under `app/islands/`. */
34
+ component = input.required(/* @ts-ignore */
35
+ ...(ngDevMode ? [{ debugName: "component" }] : /* istanbul ignore next */ []));
36
+ props = input(/* @ts-ignore */
37
+ ...(ngDevMode ? [undefined, { debugName: "props" }] : /* istanbul ignore next */ []));
38
+ /** The island directive: when the island hydrates, `"only"` to mount it in the browser alone, or a directive a module adds; default `"load"`. */
39
+ on = input(/* @ts-ignore */
40
+ ...(ngDevMode ? [undefined, { debugName: "on" }] : /* istanbul ignore next */ []));
41
+ fallback = input(/* @ts-ignore */
42
+ ...(ngDevMode ? [undefined, { debugName: "fallback" }] : /* istanbul ignore next */ []));
43
+ /** The island's id across swaps; a swap to a page with the same id keeps its element and state. */
44
+ persist = input(/* @ts-ignore */
45
+ ...(ngDevMode ? [undefined, { debugName: "persist" }] : /* istanbul ignore next */ []));
46
+ /** With `persist`, a swap keeps the island's old props. */
47
+ persistProps = input(false, { ...(ngDevMode ? { debugName: "persistProps" } : /* istanbul ignore next */ {}), transform: booleanAttribute });
48
+ app = inject(HAMUR_APP, { optional: true });
49
+ id = injectId();
50
+ tasks = inject(PendingTasks);
51
+ fail = inject(HAMUR_FAIL, { optional: true });
52
+ server = onServer();
53
+ element = inject(HAMUR_ISLAND_ELEMENT);
54
+ constructor() {
55
+ super();
56
+ if (!this.app)
57
+ throw new Error("Island must be rendered inside a Hamur app");
58
+ }
59
+ ngOnInit() {
60
+ if (!this.server)
61
+ return;
62
+ const island = renderIsland(this.app, this.id, this.component(), this.props() ?? {}, this.on(), undefined, {
63
+ persist: this.persist(),
64
+ persistProps: this.persistProps(),
65
+ });
66
+ const element = this.host.localName === this.element ? this.host : this.renderer.createElement(this.element);
67
+ if (element !== this.host)
68
+ this.renderer.appendChild(this.host, element);
69
+ for (const [name, value] of Object.entries(island.attributes))
70
+ this.renderer.setAttribute(element, name, value);
71
+ if (!island.html) {
72
+ const fallback = this.fallback();
73
+ if (fallback)
74
+ this.append(fallback, element);
75
+ return;
76
+ }
77
+ const done = this.tasks.add();
78
+ island.html
79
+ .then((html) => this.renderer.setProperty(element, "innerHTML", html))
80
+ .catch((error) => this.fail?.(error))
81
+ .finally(done);
88
82
  }
89
- const done = this.tasks.add()
90
- island.html
91
- .then((html) => this.renderer.setProperty(element, "innerHTML", html))
92
- .catch((error: unknown) => this.fail?.(error))
93
- .finally(done)
94
- }
83
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: Island, deps: [], target: i0.ɵɵFactoryTarget.Component });
84
+ static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.1.0", version: "22.3.2", type: Island, isStandalone: true, selector: "hamur-island", inputs: { component: { classPropertyName: "component", publicName: "component", isSignal: true, isRequired: true, transformFunction: null }, props: { classPropertyName: "props", publicName: "props", isSignal: true, isRequired: false, transformFunction: null }, on: { classPropertyName: "on", publicName: "on", isSignal: true, isRequired: false, transformFunction: null }, fallback: { classPropertyName: "fallback", publicName: "fallback", isSignal: true, isRequired: false, transformFunction: null }, persist: { classPropertyName: "persist", publicName: "persist", isSignal: true, isRequired: false, transformFunction: null }, persistProps: { classPropertyName: "persistProps", publicName: "persistProps", isSignal: true, isRequired: false, transformFunction: null } }, usesInheritance: true, ngImport: i0, template: "", isInline: true });
95
85
  }
86
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: Island, decorators: [{
87
+ type: Component,
88
+ args: [{ selector: "hamur-island", template: "" }]
89
+ }], ctorParameters: () => [], propDecorators: { component: [{ type: i0.Input, args: [{ isSignal: true, alias: "component", required: true }] }], props: [{ type: i0.Input, args: [{ isSignal: true, alias: "props", required: false }] }], on: [{ type: i0.Input, args: [{ isSignal: true, alias: "on", required: false }] }], fallback: [{ type: i0.Input, args: [{ isSignal: true, alias: "fallback", required: false }] }], persist: [{ type: i0.Input, args: [{ isSignal: true, alias: "persist", required: false }] }], persistProps: [{ type: i0.Input, args: [{ isSignal: true, alias: "persistProps", required: false }] }] } });