@rivinia/modal 0.5.1 → 0.5.2

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.
Files changed (2) hide show
  1. package/README.md +235 -171
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,213 +1,277 @@
1
1
  # @rivinia/modal
2
2
 
3
- [![npm version](https://img.shields.io/npm/v/@rivinia/modal.svg)](https://www.npmjs.com/package/@rivinia/modal)
4
- [![license](https://img.shields.io/npm/l/@rivinia/modal.svg)](https://www.npmjs.com/package/@rivinia/modal)
3
+ An Angular modal manager with typed results, stack/queue admission, dismissal
4
+ policies, async guards, and optional ready-made UI. The core package owns
5
+ modal state and lifecycle but does not render or style dialogs.
5
6
 
6
- Headless modal manager for Angular 22+. Stack/Queue strategies, typed
7
- `Promise` results, dismissal policies, async guards, ownership — zero DOM,
8
- zero styles, zero router dependency. A future `@rivinia/modal/ui` entry point
9
- will render this state; Core never imports UI.
7
+ - Install: `npm install @rivinia/modal`
8
+ - Prepared patch: **0.5.2** (latest published: **0.5.1**)
9
+ - Peer dependencies: `@angular/core ^22.0.0` and `@angular/common ^22.0.0`
10
+ - `@rivinia/modal` is the headless manager.
11
+ - `@rivinia/modal/ui` contains standalone dialog and host components.
10
12
 
11
- - Version: **0.5.0**
12
- - Requires: **Angular `^22.0.0`**, `tslib`
13
+ ## Configure the manager
13
14
 
14
- ## Install
15
-
16
- ```bash
17
- npm install @rivinia/modal
18
- ng add @rivinia/modal # wires provideRiviniaModal() into app.config.ts
19
- ```
15
+ Register the provider once in the application's root environment providers.
16
+ The defaults use a stack with up to five visible modals and queue overflow:
20
17
 
21
18
  ```ts
22
19
  // app.config.ts
20
+ import type { ApplicationConfig } from '@angular/core';
23
21
  import { provideRiviniaModal } from '@rivinia/modal';
24
22
 
25
- providers: [provideRiviniaModal({ strategy: 'stack', maxVisible: 3 })];
23
+ export const appConfig: ApplicationConfig = {
24
+ providers: [
25
+ provideRiviniaModal({
26
+ strategy: 'stack',
27
+ maxVisible: 3,
28
+ closingTimeout: 300,
29
+ }),
30
+ ],
31
+ };
26
32
  ```
27
33
 
28
- ## Quick start
34
+ `ng add @rivinia/modal` can insert `provideRiviniaModal()` into a detected
35
+ standalone app configuration. It does not add a rendering host; add one as
36
+ described below. You can also configure the provider manually as above.
29
37
 
30
- ```ts
31
- export class Users {
32
- private modals = injectRiviniaModal();
38
+ ## Open a typed modal
33
39
 
34
- async remove(user: User) {
35
- const handle = this.modals.openModal<DeleteData, boolean>(confirmTemplate, {
36
- data: { name: user.name },
37
- ref: 'confirm-delete',
38
- });
39
- const result = await handle.result;
40
- if (result.status === 'confirmed' && result.value) {
41
- this.delete(user);
42
- }
43
- // 'cancelled' | 'dismissed' | 'queued-cancelled' | 'owner-destroyed' | 'rejected'
44
- }
45
- }
46
- ```
40
+ This standalone component uses a typed template, mounts one shared modal host,
41
+ and waits for the result before changing application state:
47
42
 
48
- ## API
49
-
50
- | Export | Kind | Purpose |
51
- | --------------------------------- | ------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
52
- | `provideRiviniaModal(config?)` | provider | `{ strategy?, dismiss?, maxVisible?, maxQueue?, overflow?, descendants?, presets?, closingTimeout?, diagnostics? }` |
53
- | `RiviniaModal` | `@Service()` | `openModal/close/dismiss/cancel/requestDismiss/update/confirmClosed/dismissAll/findByRef/useStrategy/debugState` + `active/queued/closing/visible/top` signals |
54
- | `injectRiviniaModal()` | function | shorthand for `inject(RiviniaModal)` |
55
- | `RIVINIA_MODAL_CONTEXT` | token | per-instance context for dynamic components |
56
- | `StackStrategy` / `QueueStrategy` | classes | built-in ordering policies (`ModalStrategy` for custom) |
57
- | types | types | `ModalHandle`, `ModalResult`, `ModalContent`, `ModalOpenOptions`, `ModalDismissPolicy`, `ModalGuard`, … |
43
+ ```ts
44
+ import { Component, TemplateRef, ViewChild, signal } from '@angular/core';
45
+ import type { ModalHandle, ModalTemplateContext } from '@rivinia/modal';
46
+ import { injectRiviniaModal } from '@rivinia/modal';
47
+ import { RiviniaModalHost } from '@rivinia/modal/ui';
48
+
49
+ interface User {
50
+ readonly id: string;
51
+ readonly name: string;
52
+ }
58
53
 
59
- Results settle exactly once, at dismissal initiation — exit animations never
60
- delay callers. `close/dismiss/cancel` are guard-free primitives (booleans);
61
- `requestDismiss(id, origin)` runs async guards for user intents. Forced paths
62
- (owner destroy, overflow eviction, `dismissAll`) bypass guards. Render failures
63
- reject the promise instead of becoming variants.
54
+ interface DeleteData {
55
+ readonly id: string;
56
+ readonly name: string;
57
+ }
64
58
 
65
- `openModal` accepts a raw `TemplateRef`, a component `Type`, or a full
66
- `ModalContent` descriptor (component inputs via `options.inputs`). Dismissal
67
- policy merges field-by-field: built-in defaults < global `dismiss` < named
68
- preset < per-call options — absent keys never clobber.
59
+ @Component({
60
+ standalone: true,
61
+ imports: [RiviniaModalHost],
62
+ template: `
63
+ @for (user of users(); track user.id) {
64
+ <button type="button" (click)="askToDelete(user)">Delete {{ user.name }}</button>
65
+ }
69
66
 
70
- ## Ownership (who cleans up)
67
+ <ng-template #deleteConfirmation let-data>
68
+ <p>Delete {{ data.name }}?</p>
69
+ <button type="button" (click)="confirmDelete()">Delete user</button>
70
+ <button type="button" (click)="cancelDelete()">Cancel</button>
71
+ </ng-template>
71
72
 
72
- ```ts
73
- // Owned by a short-lived component: dies with it (active → owner-destroyed)
74
- this.modals.openModal(tpl, { owner: this.destroyRef });
73
+ <rv-modal-host [modal]="modals" size="sm" />
74
+ `,
75
+ })
76
+ export class UsersPage {
77
+ private readonly manager = injectRiviniaModal();
78
+ protected readonly modals = this.manager;
79
+ readonly users = signal<User[]>([{ id: '42', name: 'Ada Lovelace' }]);
75
80
 
76
- // Independent (default): lives until closed — for services and app flows
77
- this.modals.openModal(tpl);
78
- ```
81
+ @ViewChild('deleteConfirmation', { static: true })
82
+ private deleteTemplate!: TemplateRef<ModalTemplateContext<DeleteData>>;
79
83
 
80
- Owner teardown frees capacity like any other removal: queued requests promote
81
- normally (a queued request sharing the dead owner is still torn down by its own
82
- callback, settling `owner-destroyed`). Opening with an already-destroyed owner
83
- settles `owner-destroyed` immediately without adding anything.
84
+ private pendingDelete: ModalHandle<boolean> | null = null;
84
85
 
85
- Closing a parent cascades to descendants (`dismissed: parent-closed`) unless
86
- `descendants: 'prevent-parent-close'` blocks it. Swapping strategies with live
87
- modals throws — close everything first.
86
+ async askToDelete(user: User): Promise<void> {
87
+ if (this.pendingDelete) return;
88
88
 
89
- ## Ready-made UI (`@rivinia/modal/ui`)
89
+ const handle = this.manager.openModal<DeleteData, boolean>(this.deleteTemplate, {
90
+ data: { id: user.id, name: user.name },
91
+ ref: 'confirm-delete',
92
+ metadata: { title: 'Delete user' },
93
+ dismiss: { backdrop: 'static' },
94
+ });
95
+ this.pendingDelete = handle;
96
+
97
+ try {
98
+ const result = await handle.result;
99
+ if (result.status === 'confirmed' && result.value) {
100
+ this.users.update((users) => users.filter((entry) => entry.id !== user.id));
101
+ }
102
+ } finally {
103
+ if (this.pendingDelete === handle) this.pendingDelete = null;
104
+ }
105
+ }
90
106
 
91
- Optional secondary entry point. Zero new runtime dependencies (platform
92
- `<dialog>` + Angular only), no design system, no CSS framework. Headless
93
- consumers never load it (proven: core FESM contains zero UI markers).
107
+ confirmDelete(): void {
108
+ // ModalHandle<boolean> checks that the confirmed value is a boolean.
109
+ this.pendingDelete?.close(true);
110
+ }
94
111
 
95
- ```ts
96
- import { RiviniaModalDialog } from '@rivinia/modal/ui'; // UI-only
97
- import { RiviniaModalHost } from '@rivinia/modal/ui'; // managed adapter
112
+ cancelDelete(): void {
113
+ this.pendingDelete?.cancel('user-cancelled');
114
+ }
115
+ }
98
116
  ```
99
117
 
100
- **Mode A — UI-only** (no controller; owns its `open` state):
118
+ The UI host uses `metadata.title` by default, so the example renders
119
+ “Delete user” without custom title logic. Import `RiviniaModalHost` into the
120
+ standalone component that renders it. For an NgModule-based app, import this
121
+ standalone component in the module's `imports` array, not `declarations`.
122
+ Mount a single host for the manager you pass to it.
101
123
 
102
- ```html
103
- <rv-modal-dialog [(open)]="open" [title]="'Delete?'" [size]="'sm'" (confirmed)="remove()">
104
- <p>Delete {{ user.name }}?</p>
105
- </rv-modal-dialog>
106
- ```
124
+ `openModal<TData, TResult>()` accepts a `TemplateRef`, a component type, or a
125
+ `ModalContent<TData>` descriptor. Template content receives `data` as
126
+ `let-data` and the per-modal operations as `let-modal="modal"`. For component
127
+ content, pass `inputs` in the options; those keys are `Record<string, unknown>`
128
+ and are not checked against the component's inputs at compile time. The
129
+ optional `RIVINIA_MODAL_CONTEXT` injection token supplies the same per-modal
130
+ context to a dynamically created component.
107
131
 
108
- **Mode B — headless + custom UI:** keep using `@rivinia/modal` alone.
109
- Nothing here loads, no styles leak.
132
+ The result is a discriminated `ModalResult<TResult>`:
110
133
 
111
- **Mode C — headless + ready-made UI** (single source of truth stays Core):
134
+ - `confirmed` carries the typed `value`.
135
+ - `cancelled` and `dismissed` may carry a `reason`.
136
+ - `owner-destroyed` and `queued-cancelled` identify lifecycle cleanup.
137
+ - `rejected` identifies a request refused by the configured admission or
138
+ overflow policy.
112
139
 
113
- ```html
114
- <rv-modal-host [modal]="injectRiviniaModal()" />
115
- ```
140
+ Narrow on `result.status` before reading `result.value`. Results settle once
141
+ when closing starts; callers do not wait for the exit animation. A component
142
+ that owns a handle may use `close(value)`, `cancel(reason)`, or
143
+ `dismiss(reason)`. `ModalHandle.close()` enforces `TResult`; the `close()`
144
+ method on template/component context accepts `unknown`, so prefer the handle
145
+ when the result type matters.
116
146
 
117
- Intents (X, Escape, backdrop) route through `requestDismiss` (guards run);
118
- exits play, then the host calls `confirmClosed()` (match `closingTimeout`
119
- to the animation duration; default 300 ms covers the 150 ms default).
147
+ ## Dismissal, guards, and lifecycle
120
148
 
121
- Custom chrome (no X button, custom header/footer, own actions in content):
149
+ The default dismissal policy allows Escape and backdrop click. Configure
150
+ global defaults, named presets, or per-modal overrides:
122
151
 
123
- ```html
124
- <rv-modal-dialog [(open)]="open">
125
- <div rv-modal-header rvModalHeader>My header, no X</div>
126
- <p>Body.</p>
127
- <div rv-modal-footer rvModalFooter><button (click)="save()">Save</button></div>
128
- </rv-modal-dialog>
152
+ ```ts
153
+ provideRiviniaModal({
154
+ dismiss: { closeOnEscape: true, backdrop: 'static' },
155
+ presets: {
156
+ critical: { dismiss: { closeOnEscape: false, backdrop: 'static' } },
157
+ },
158
+ });
129
159
  ```
130
160
 
131
- No header at all: omit the title (no custom header either). Header rules:
132
- title + optional `description` (wired as `aria-describedby`), top-aligned
133
- with a shrink-proof X; an empty heading collapses (no dead space); a custom
134
- header renders inside the same padded row. Focus goes to the title when
135
- there is no close button, to the dialog otherwise. No footer:
136
- omit the footer slot (renders nothing). Positions: `center` (default),
137
- `top/bottom`, `-start/-end` variants (RTL-aware), `side-start/side-end`
138
- full-height panels, `fullscreen`. Under 640px viewports, center/top/bottom
139
- shapes become bottom sheets automatically (top-only radius, slide-up
140
- entrance); panels and fullscreen keep their shape.
141
- Sizes `sm/md/lg/xl/full/auto` are ceilings via `--rv-modal-width-*` vars.
142
-
143
- Theming (no theme service; consumer owns it):
144
-
145
- ```css
146
- :root {
147
- --rv-modal-bg: #1c1c1e;
148
- --rv-modal-fg: #f5f5f7;
149
- }
150
- @media (prefers-color-scheme: dark) {
151
- .rv-modal {
152
- /* built-in dark defaults already apply; override vars to brand it */
153
- }
154
- }
155
- ```
161
+ Dismissal policy fields merge independently in this order: built-in defaults,
162
+ global config, named preset, and per-open options. A missing key at a later
163
+ layer does not erase an earlier setting. `backdrop` may be `close`, `static`,
164
+ or `disabled`.
165
+
166
+ `RiviniaModalHost` routes Escape, backdrop, and close-button intents through
167
+ `requestDismiss()`, which runs any async `guards` supplied when opening. A
168
+ guard that returns `false` or throws keeps the modal open. The direct
169
+ `close()`, `dismiss()`, and `cancel()` methods on a handle are guard-free;
170
+ use `requestDismiss(id, origin)` when programmatic code should also be checked.
171
+ Forced cleanup such as owner destruction and `dismissAll()` bypasses guards.
172
+
173
+ The manager supports `strategy: 'stack' | 'queue'` (or a custom
174
+ `ModalStrategy`). Stack allows up to `maxVisible` active modals, with the
175
+ latest on top; queue activates one at a time in FIFO order. A modal in its
176
+ exit animation normally occupies a visible slot until the host confirms
177
+ teardown or the timeout finalizes it; `dismiss-oldest` preempts an active
178
+ instance to make room while its exit runs. When capacity is full, `overflow`
179
+ may queue (default), dismiss the oldest active modal, or reject the new request.
180
+ `maxQueue` bounds queued requests when queue overflow is enabled.
181
+
182
+ An optional `owner: DestroyRef` ties a modal to a component/service lifetime.
183
+ Destroying the owner settles an active request as `owner-destroyed`; a queued
184
+ request settles as `queued-cancelled`. Without an owner, the modal is
185
+ independent of the caller's lifetime. Set `parentId` to associate a child
186
+ modal with a parent; by default, closing a parent closes its descendants.
187
+ `descendants: 'prevent-parent-close'` instead blocks parent closure while a
188
+ descendant remains open.
189
+
190
+ When a modal enters `closing`, its result has already settled but it remains
191
+ visible while the UI exits. The UI host calls `confirmClosed()` after its exit
192
+ animation. The default `closingTimeout` is 300 ms; set it to cover the
193
+ renderer’s exit duration, or set it to `0` when a custom renderer will always
194
+ acknowledge closure manually. Changing strategy with active, queued, or
195
+ closing modals throws; close them first.
196
+
197
+ ## Ready-made UI
198
+
199
+ `@rivinia/modal/ui` is optional. It provides a native `<dialog>` component
200
+ (`RiviniaModalDialog`) for UI-only use and `RiviniaModalHost` to render
201
+ manager-owned modals. The host passes dismissal intents to the manager before
202
+ closing, so guards and promise results remain coordinated.
203
+
204
+ For a UI-only dialog, import the standalone component and bind its `open`
205
+ model. This mode has no manager, guards, or `ModalHandle` promise:
156
206
 
157
- Plus: `prefers-reduced-motion` disables animation; `animation: 'none'`
158
- opt-out; `backdrop: 'close' | 'static' | 'disabled'`; Escape/backdrop/X
159
- policies stay independent (no `disableClose` bundle); long content scrolls
160
- inside (`rv-modal-body`); `scrollbar-gutter: stable` while open;
161
- reference-counted scroll lock restores previous styles; focus trap + focus
162
- restore + `aria-labelledby`/`aria-label`; `dir=rtl` works via logical
163
- properties; SSR renders closed and opens after hydration; lazy-load the
164
- entry (`loadComponent`) to split it into its own chunk (~16.7 kB raw,
165
- ~4.9 kB transfer, measured).
166
-
167
- ## Toasts above an open modal (measured pattern)
168
-
169
- A toast host outside the dialog can never paint above it — neither a high
170
- `z-index` nor `popover="manual"` escapes the modal top layer (verified in
171
- Chrome via `elementFromPoint`). When an error toast must appear over an open
172
- modal (e.g. a data error inside the form), host the list **inside** the
173
- dialog content instead: as a descendant of the top-layer element it paints
174
- above the dialog background with zero extra code and zero coupling:
175
-
176
- ```html
177
- <rv-modal-dialog [(open)]="open" [title]="'Edit user'">
178
- <!-- paints above this dialog; positions are still viewport-anchored -->
179
- <rv-toast-list [manager]="injectRiviniaToast()" [position]="'top-end'" />
180
- <form>…</form>
181
- </rv-modal-dialog>
207
+ ```ts
208
+ import { Component } from '@angular/core';
209
+ import { RiviniaModalDialog } from '@rivinia/modal/ui';
210
+
211
+ @Component({
212
+ standalone: true,
213
+ imports: [RiviniaModalDialog],
214
+ template: `
215
+ <button type="button" (click)="open = true">Preview</button>
216
+ <rv-modal-dialog #dialog [(open)]="open" title="Preview">
217
+ <p>Dialog content.</p>
218
+ <button type="button" (click)="dialog.confirm(true)">Continue</button>
219
+ <button type="button" (click)="dialog.cancel('cancelled')">Cancel</button>
220
+ </rv-modal-dialog>
221
+ `,
222
+ })
223
+ export class PreviewDialog {
224
+ open = false;
225
+ }
182
226
  ```
183
227
 
184
- Notes: positions stay viewport-anchored (`fixed`), so `top-end` means the
185
- viewport corner in both LTR and RTL (logical properties). Toasts fired
186
- while no dialog is open still need a page-level list — the two lists share
187
- the same manager, so nothing is lost or duplicated. Toasts: MEASURED (Chrome, `elementFromPoint`) —
188
- neither a high `z-index` nor `popover="manual"` paints above an open modal
189
- dialog; toasts queue behind it. No modal→toast dependency by design.
190
-
191
- ## Type holes (honest, by design)
192
-
193
- Core checks `ModalHandle<TResult>.close(result?: TResult)` and
194
- `ModalResult`/`ModalContext.data` typing (see `modal-type-safety.spec.ts`
195
- `@ts-expect-error` tests). It does NOT check:
196
-
197
- - `ModalContext.close(result?: unknown)` — any payload compiles; the mismatch
198
- reaches the opener's awaited `ModalResult` unchecked.
199
- - `inputs?: Record<string, unknown>` (options + component content) — any shape
200
- compiles; misspelled keys fail silently at render. The UI owns binding.
201
- - The internal deferred-promise cast to `Promise<ModalResult<TResult>>` —
202
- `close()` settles whatever it receives, never validated.
203
-
204
- ## Browser back button (non-goal)
205
-
206
- Native `<dialog>.showModal()` does not integrate with history: the back
207
- button neither closes the dialog nor is blocked by it (platform behavior,
208
- not a Rivinia choice). If your app needs back-to-dismiss, wire it at the
209
- app layer (router guard / `popstate` → `dismiss()`); the manager's
210
- `dismissRequested` + guards flow stays the single teardown path.
228
+ `RiviniaModalDialog` accepts `open`, `title`, `description`, `ariaLabel`,
229
+ `size`, `position`, `animation`, `closeOnEscape`, `backdrop`, and
230
+ `showCloseButton`; its `open` model supports `[(open)]`. In UI-only mode it
231
+ emits `confirmed`, `dismissed`, and `cancelled`. In managed use, the host
232
+ accepts the required `modal` manager input plus `size`, `showCloseButton`,
233
+ and `titleOf` overrides; observe the manager's `ModalHandle.result` instead
234
+ of the dialog outputs.
235
+ `RvModalHeader` and `RvModalFooter` are marker directives for custom header
236
+ and footer projection; import the marker used by the template and apply it
237
+ alongside `rv-modal-header` or `rv-modal-footer`. A custom header replaces the
238
+ default title and close button. Without a footer slot, no footer region is
239
+ rendered.
240
+
241
+ The native dialog provides focus containment and restores focus to its
242
+ invoker. The UI includes scroll locking, reduced-motion support, RTL-aware
243
+ logical positions, and responsive bottom-sheet styling below 640px for
244
+ center/top/bottom placements. The UI exposes size, position, animation,
245
+ backdrop, Escape, and close-button inputs. During server rendering it remains
246
+ closed and opens on the client after hydration. Theme it with CSS custom
247
+ properties such as `--rv-modal-bg`, `--rv-modal-fg`, `--rv-modal-border`,
248
+ `--rv-modal-radius`, and `--rv-modal-width-md`.
249
+
250
+ ## Public core exports
251
+
252
+ The core entry point exports:
253
+
254
+ - `provideRiviniaModal()` and `injectRiviniaModal()` for configuration and
255
+ injection, plus `RiviniaModal` for direct manager APIs.
256
+ - `StackStrategy`, `QueueStrategy`, and the `ModalStrategy` contract.
257
+ - `RiviniaModalConfig`, `ModalPreset`, `ModalHandle`, `ModalResult`,
258
+ `ModalContent`, `ModalOpenOptions`, `ModalUpdate`, `ModalInstance`,
259
+ `ModalTemplateContext`, `ModalContext`, `ModalGuard`,
260
+ `ModalDismissPolicy`, `ModalDismissOrigin`, `ModalOverflowPolicy`,
261
+ `ModalStatus`, `ModalDiagnostic`, and `DescendantPolicy`.
262
+ - `RIVINIA_MODAL_CONTEXT` for dynamic modal components.
263
+
264
+ `RiviniaModal` exposes readonly `active`, `queued`, `closing`, `visible`, and
265
+ `top` signals, along with `openModal`, `close`, `dismiss`, `cancel`,
266
+ `requestDismiss`, `update`, `confirmClosed`, `dismissAll`, `findByRef`,
267
+ `useStrategy`, and diagnostics helpers. Inspect the signal values by calling
268
+ them, for example `manager.top()`.
269
+
270
+ The `@rivinia/modal/ui` entry point exports `RiviniaModalDialog`,
271
+ `RiviniaModalHost`, `RiviniaModalHostManager`, `RiviniaModalScrollLock`,
272
+ `RvModalHeader`, and `RvModalFooter`, plus the `RiviniaModalAnimation`,
273
+ `RiviniaModalBackdrop`, `RiviniaModalPosition`, `RiviniaModalSize`, and
274
+ `RiviniaModalUiOptions` types.
211
275
 
212
276
  ## License
213
277
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rivinia/modal",
3
- "version": "0.5.1",
3
+ "version": "0.5.2",
4
4
  "description": "Headless modal manager for Angular: stack/queue strategies, typed results, guards, ownership.",
5
5
  "keywords": [
6
6
  "angular",