@ngbracket/a11y-devtools 0.10.1 → 0.12.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/CHANGELOG.md +68 -0
- package/README.md +4 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/inpage.global.js +585 -480
- package/dist/keyboard/focus-trap.js +2 -0
- package/dist/keyboard/tab-sequence.d.ts +18 -0
- package/dist/keyboard/tab-sequence.js +52 -1
- package/dist/overlay.js +5 -0
- package/dist/pill-menu.d.ts +35 -0
- package/dist/pill-menu.js +185 -0
- package/dist/provider.d.ts +30 -0
- package/dist/provider.js +136 -20
- package/dist/settings.d.ts +39 -0
- package/dist/settings.js +68 -0
- package/dist/toggle.d.ts +66 -0
- package/dist/toggle.js +204 -0
- package/dist/top-layer.d.ts +40 -0
- package/dist/top-layer.js +117 -0
- package/package.json +1 -1
package/dist/settings.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/** `localStorage` key for a developer's own menu choices (only what they changed). */
|
|
2
|
+
export const SETTINGS_STORAGE_KEY = 'ngbr-a11y-devtools:settings';
|
|
3
|
+
const IMPACT_RANK = { minor: 0, moderate: 1, serious: 2, critical: 3 };
|
|
4
|
+
/** Severity choices, least to most severe. */
|
|
5
|
+
export const IMPACTS = ['minor', 'moderate', 'serious', 'critical'];
|
|
6
|
+
/**
|
|
7
|
+
* Is a finding at or above `min`? A finding with no impact is kept only when
|
|
8
|
+
* everything is shown (`minor`).
|
|
9
|
+
*/
|
|
10
|
+
export function meetsMinImpact(impact, min) {
|
|
11
|
+
if (!impact)
|
|
12
|
+
return min === 'minor';
|
|
13
|
+
return IMPACT_RANK[impact] >= IMPACT_RANK[min];
|
|
14
|
+
}
|
|
15
|
+
/** The findings to draw, given the severity filter. */
|
|
16
|
+
export function visibleFindings(findings, min) {
|
|
17
|
+
return min === 'minor' ? findings : findings.filter((f) => meetsMinImpact(f.impact, min));
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The effective settings: the app's defaults with the developer's remembered
|
|
21
|
+
* changes on top. Unknown or malformed stored values are ignored.
|
|
22
|
+
*/
|
|
23
|
+
export function resolveSettings(defaults, stored) {
|
|
24
|
+
const out = { ...defaults };
|
|
25
|
+
if (!stored)
|
|
26
|
+
return out;
|
|
27
|
+
for (const key of ['highlights', 'tabOrder', 'focusPreview']) {
|
|
28
|
+
if (typeof stored[key] === 'boolean')
|
|
29
|
+
out[key] = stored[key];
|
|
30
|
+
}
|
|
31
|
+
if (stored.minImpact && stored.minImpact in IMPACT_RANK)
|
|
32
|
+
out.minImpact = stored.minImpact;
|
|
33
|
+
return out;
|
|
34
|
+
}
|
|
35
|
+
/** The developer's remembered changes, or null. Never throws. */
|
|
36
|
+
export function readStoredSettings(storage) {
|
|
37
|
+
try {
|
|
38
|
+
const raw = storage?.getItem(SETTINGS_STORAGE_KEY);
|
|
39
|
+
if (!raw)
|
|
40
|
+
return null;
|
|
41
|
+
const parsed = JSON.parse(raw);
|
|
42
|
+
return parsed && typeof parsed === 'object' ? parsed : null;
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Remember only the settings that differ from the app's defaults, so a later
|
|
50
|
+
* change to the defaults still reaches developers who never touched that
|
|
51
|
+
* setting. Silently does nothing if storage is blocked.
|
|
52
|
+
*/
|
|
53
|
+
export function writeStoredSettings(storage, settings, defaults) {
|
|
54
|
+
const changed = {};
|
|
55
|
+
for (const key of Object.keys(settings)) {
|
|
56
|
+
if (settings[key] !== defaults[key])
|
|
57
|
+
changed[key] = settings[key];
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
if (Object.keys(changed).length === 0)
|
|
61
|
+
storage?.removeItem(SETTINGS_STORAGE_KEY);
|
|
62
|
+
else
|
|
63
|
+
storage?.setItem(SETTINGS_STORAGE_KEY, JSON.stringify(changed));
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
// Storage blocked or full: the change still applies, it just isn't remembered.
|
|
67
|
+
}
|
|
68
|
+
}
|
package/dist/toggle.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { type PillMenuOptions, type PillMenuStatus } from './pill-menu.js';
|
|
2
|
+
/** Default keyboard shortcut that turns the devtools on/off. */
|
|
3
|
+
export declare const DEFAULT_TOGGLE_SHORTCUT = "Alt+Shift+A";
|
|
4
|
+
/** `localStorage` key holding the remembered on/off choice (`'on'` | `'off'`). */
|
|
5
|
+
export declare const TOGGLE_STORAGE_KEY = "ngbr-a11y-devtools";
|
|
6
|
+
/** Where the on/off pill sits in the viewport. */
|
|
7
|
+
export type PillPosition = 'bottom-left' | 'bottom-right' | 'top-left' | 'top-right';
|
|
8
|
+
/** A parsed shortcut such as `Alt+Shift+A`. */
|
|
9
|
+
export interface Shortcut {
|
|
10
|
+
alt: boolean;
|
|
11
|
+
shift: boolean;
|
|
12
|
+
ctrl: boolean;
|
|
13
|
+
meta: boolean;
|
|
14
|
+
/** The non-modifier key, upper-cased when it's a single character (e.g. `A`, `1`, `F2`). */
|
|
15
|
+
key: string;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Parse `Alt+Shift+A`-style text. Case-insensitive; exactly one non-modifier key.
|
|
19
|
+
* Returns null for anything it can't read, so a typo disables the shortcut
|
|
20
|
+
* rather than throwing inside the app.
|
|
21
|
+
*/
|
|
22
|
+
export declare function parseShortcut(text: string): Shortcut | null;
|
|
23
|
+
/**
|
|
24
|
+
* Does a keydown match the shortcut? Letters and digits match on the physical
|
|
25
|
+
* key (`event.code`), because Alt/Option changes `event.key` on macOS
|
|
26
|
+
* (Alt+Shift+A reports `Å`) and on some keyboard layouts.
|
|
27
|
+
*/
|
|
28
|
+
export declare function matchesShortcut(event: KeyboardEvent, shortcut: Shortcut): boolean;
|
|
29
|
+
/** The same shortcut in `aria-keyshortcuts` / display form, e.g. `Alt+Shift+A`. */
|
|
30
|
+
export declare function formatShortcut(shortcut: Shortcut): string;
|
|
31
|
+
/**
|
|
32
|
+
* The remembered on/off choice, or null if there isn't one (or storage is
|
|
33
|
+
* blocked, e.g. a private window). Never throws.
|
|
34
|
+
*/
|
|
35
|
+
export declare function readStoredEnabled(storage: Storage | undefined): boolean | null;
|
|
36
|
+
/** Remember the on/off choice. Silently does nothing if storage is blocked. */
|
|
37
|
+
export declare function writeStoredEnabled(storage: Storage | undefined, enabled: boolean): void;
|
|
38
|
+
export interface TogglePillOptions {
|
|
39
|
+
/** Document to render into. Defaults to the global `document`. */
|
|
40
|
+
document?: Document;
|
|
41
|
+
/** Initial state. */
|
|
42
|
+
enabled: boolean;
|
|
43
|
+
/** Called with the new state when the user clicks the pill. */
|
|
44
|
+
onToggle: (enabled: boolean) => void;
|
|
45
|
+
/** Shortcut shown in the tooltip and exposed via `aria-keyshortcuts`. */
|
|
46
|
+
shortcut?: Shortcut | null;
|
|
47
|
+
/** Default `bottom-left` (the focus-follow panel uses bottom-right). */
|
|
48
|
+
position?: PillPosition;
|
|
49
|
+
/** Add the `⋯` settings menu next to the switch. */
|
|
50
|
+
menu?: Omit<PillMenuOptions, 'opensUp' | 'alignLeft'>;
|
|
51
|
+
}
|
|
52
|
+
export interface TogglePill {
|
|
53
|
+
/** Reflect a state change made elsewhere (e.g. the keyboard shortcut). */
|
|
54
|
+
setEnabled(enabled: boolean): void;
|
|
55
|
+
/** Update the menu's issue and page counts (no-op without a menu). */
|
|
56
|
+
setStatus(status: PillMenuStatus): void;
|
|
57
|
+
destroy(): void;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* A small on/off switch for the devtools, fixed to a corner of the page. A real
|
|
61
|
+
* `<button role="switch">` so it works by keyboard and screen reader: the
|
|
62
|
+
* accessible name is "a11y devtools" (it contains the visible "a11y"), state is
|
|
63
|
+
* `aria-checked`, and the visible on/off text means state doesn't rely on colour.
|
|
64
|
+
* Marked with {@link OVERLAY_ATTR} so scans never report the tool's own UI.
|
|
65
|
+
*/
|
|
66
|
+
export declare function createTogglePill(options: TogglePillOptions): TogglePill;
|
package/dist/toggle.js
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { OVERLAY_ATTR } from './overlay.js';
|
|
2
|
+
import { keepInTopLayer } from './top-layer.js';
|
|
3
|
+
import { createPillMenu } from './pill-menu.js';
|
|
4
|
+
/** Default keyboard shortcut that turns the devtools on/off. */
|
|
5
|
+
export const DEFAULT_TOGGLE_SHORTCUT = 'Alt+Shift+A';
|
|
6
|
+
/** `localStorage` key holding the remembered on/off choice (`'on'` | `'off'`). */
|
|
7
|
+
export const TOGGLE_STORAGE_KEY = 'ngbr-a11y-devtools';
|
|
8
|
+
const MODIFIERS = {
|
|
9
|
+
alt: 'alt',
|
|
10
|
+
option: 'alt',
|
|
11
|
+
shift: 'shift',
|
|
12
|
+
ctrl: 'ctrl',
|
|
13
|
+
control: 'ctrl',
|
|
14
|
+
meta: 'meta',
|
|
15
|
+
cmd: 'meta',
|
|
16
|
+
command: 'meta',
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Parse `Alt+Shift+A`-style text. Case-insensitive; exactly one non-modifier key.
|
|
20
|
+
* Returns null for anything it can't read, so a typo disables the shortcut
|
|
21
|
+
* rather than throwing inside the app.
|
|
22
|
+
*/
|
|
23
|
+
export function parseShortcut(text) {
|
|
24
|
+
const parts = text
|
|
25
|
+
.split('+')
|
|
26
|
+
.map((p) => p.trim())
|
|
27
|
+
.filter(Boolean);
|
|
28
|
+
const shortcut = { alt: false, shift: false, ctrl: false, meta: false, key: '' };
|
|
29
|
+
for (const part of parts) {
|
|
30
|
+
const mod = MODIFIERS[part.toLowerCase()];
|
|
31
|
+
if (mod) {
|
|
32
|
+
shortcut[mod] = true;
|
|
33
|
+
}
|
|
34
|
+
else if (!shortcut.key) {
|
|
35
|
+
shortcut.key = part.length === 1 ? part.toUpperCase() : part;
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
return null; // two non-modifier keys
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return shortcut.key ? shortcut : null;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Does a keydown match the shortcut? Letters and digits match on the physical
|
|
45
|
+
* key (`event.code`), because Alt/Option changes `event.key` on macOS
|
|
46
|
+
* (Alt+Shift+A reports `Å`) and on some keyboard layouts.
|
|
47
|
+
*/
|
|
48
|
+
export function matchesShortcut(event, shortcut) {
|
|
49
|
+
if (event.altKey !== shortcut.alt ||
|
|
50
|
+
event.shiftKey !== shortcut.shift ||
|
|
51
|
+
event.ctrlKey !== shortcut.ctrl ||
|
|
52
|
+
event.metaKey !== shortcut.meta) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
const { key } = shortcut;
|
|
56
|
+
if (/^[A-Z]$/.test(key))
|
|
57
|
+
return event.code === `Key${key}`;
|
|
58
|
+
if (/^[0-9]$/.test(key))
|
|
59
|
+
return event.code === `Digit${key}`;
|
|
60
|
+
return event.key.toLowerCase() === key.toLowerCase();
|
|
61
|
+
}
|
|
62
|
+
/** The same shortcut in `aria-keyshortcuts` / display form, e.g. `Alt+Shift+A`. */
|
|
63
|
+
export function formatShortcut(shortcut) {
|
|
64
|
+
const mods = [
|
|
65
|
+
shortcut.ctrl && 'Control',
|
|
66
|
+
shortcut.alt && 'Alt',
|
|
67
|
+
shortcut.shift && 'Shift',
|
|
68
|
+
shortcut.meta && 'Meta',
|
|
69
|
+
].filter(Boolean);
|
|
70
|
+
return [...mods, shortcut.key].join('+');
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The remembered on/off choice, or null if there isn't one (or storage is
|
|
74
|
+
* blocked, e.g. a private window). Never throws.
|
|
75
|
+
*/
|
|
76
|
+
export function readStoredEnabled(storage) {
|
|
77
|
+
try {
|
|
78
|
+
const value = storage?.getItem(TOGGLE_STORAGE_KEY);
|
|
79
|
+
return value === 'on' ? true : value === 'off' ? false : null;
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/** Remember the on/off choice. Silently does nothing if storage is blocked. */
|
|
86
|
+
export function writeStoredEnabled(storage, enabled) {
|
|
87
|
+
try {
|
|
88
|
+
storage?.setItem(TOGGLE_STORAGE_KEY, enabled ? 'on' : 'off');
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
// Storage blocked or full: the toggle still works, it just isn't remembered.
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const PILL_ON = '#3ecf8e';
|
|
95
|
+
const PILL_OFF = '#8a8f98';
|
|
96
|
+
/**
|
|
97
|
+
* A small on/off switch for the devtools, fixed to a corner of the page. A real
|
|
98
|
+
* `<button role="switch">` so it works by keyboard and screen reader: the
|
|
99
|
+
* accessible name is "a11y devtools" (it contains the visible "a11y"), state is
|
|
100
|
+
* `aria-checked`, and the visible on/off text means state doesn't rely on colour.
|
|
101
|
+
* Marked with {@link OVERLAY_ATTR} so scans never report the tool's own UI.
|
|
102
|
+
*/
|
|
103
|
+
export function createTogglePill(options) {
|
|
104
|
+
const doc = options.document ?? document;
|
|
105
|
+
const position = options.position ?? 'bottom-left';
|
|
106
|
+
const [vertical, horizontal] = position.split('-');
|
|
107
|
+
const button = doc.createElement('button');
|
|
108
|
+
button.type = 'button';
|
|
109
|
+
button.setAttribute(OVERLAY_ATTR, '');
|
|
110
|
+
button.setAttribute('role', 'switch');
|
|
111
|
+
button.setAttribute('aria-label', 'a11y devtools');
|
|
112
|
+
const keys = options.shortcut ? formatShortcut(options.shortcut) : null;
|
|
113
|
+
if (keys)
|
|
114
|
+
button.setAttribute('aria-keyshortcuts', keys);
|
|
115
|
+
Object.assign(button.style, {
|
|
116
|
+
display: 'inline-flex',
|
|
117
|
+
alignItems: 'center',
|
|
118
|
+
gap: '6px',
|
|
119
|
+
minHeight: '28px',
|
|
120
|
+
padding: '4px 10px',
|
|
121
|
+
border: '1px solid rgba(255,255,255,0.18)',
|
|
122
|
+
borderRadius: '999px',
|
|
123
|
+
background: 'rgba(20,22,28,0.94)',
|
|
124
|
+
color: '#f4f4f5',
|
|
125
|
+
font: '600 12px/1 ui-sans-serif, system-ui, sans-serif',
|
|
126
|
+
boxShadow: '0 2px 10px rgba(0,0,0,0.35)',
|
|
127
|
+
cursor: 'pointer',
|
|
128
|
+
pointerEvents: 'auto',
|
|
129
|
+
});
|
|
130
|
+
const dot = doc.createElement('span');
|
|
131
|
+
dot.setAttribute('aria-hidden', 'true');
|
|
132
|
+
Object.assign(dot.style, { width: '8px', height: '8px', borderRadius: '50%' });
|
|
133
|
+
const name = doc.createElement('span');
|
|
134
|
+
name.textContent = 'a11y';
|
|
135
|
+
// The state word is visible for sighted users; assistive tech gets it from
|
|
136
|
+
// aria-checked, so it's hidden to avoid "a11y on, switch, on".
|
|
137
|
+
const state = doc.createElement('span');
|
|
138
|
+
state.setAttribute('aria-hidden', 'true');
|
|
139
|
+
state.style.fontWeight = '400';
|
|
140
|
+
button.append(dot, name, state);
|
|
141
|
+
// Inline styles can't do :focus-visible, so draw the ring from focus events,
|
|
142
|
+
// only when the browser would show one.
|
|
143
|
+
button.addEventListener('focus', () => {
|
|
144
|
+
if (button.matches(':focus-visible')) {
|
|
145
|
+
button.style.outline = '2px solid #7ab8ff';
|
|
146
|
+
button.style.outlineOffset = '2px';
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
button.addEventListener('blur', () => {
|
|
150
|
+
button.style.outline = '';
|
|
151
|
+
});
|
|
152
|
+
let enabled = options.enabled;
|
|
153
|
+
function setEnabled(next) {
|
|
154
|
+
enabled = next;
|
|
155
|
+
button.setAttribute('aria-checked', String(enabled));
|
|
156
|
+
dot.style.background = enabled ? PILL_ON : PILL_OFF;
|
|
157
|
+
state.textContent = enabled ? 'on' : 'off';
|
|
158
|
+
button.style.opacity = enabled ? '1' : '0.75';
|
|
159
|
+
button.title = `a11y devtools ${enabled ? 'on' : 'off'}${keys ? ` (${keys})` : ''}`;
|
|
160
|
+
}
|
|
161
|
+
setEnabled(enabled);
|
|
162
|
+
button.addEventListener('click', () => {
|
|
163
|
+
setEnabled(!enabled);
|
|
164
|
+
options.onToggle(enabled);
|
|
165
|
+
});
|
|
166
|
+
// A click-through full-viewport layer holds the pill, so the layer (not the
|
|
167
|
+
// button) becomes the top-layer popover and the button keeps its corner.
|
|
168
|
+
const layer = doc.createElement('div');
|
|
169
|
+
layer.setAttribute(OVERLAY_ATTR, '');
|
|
170
|
+
Object.assign(layer.style, {
|
|
171
|
+
position: 'fixed',
|
|
172
|
+
inset: '0',
|
|
173
|
+
pointerEvents: 'none',
|
|
174
|
+
zIndex: '2147483647',
|
|
175
|
+
});
|
|
176
|
+
// The switch, the optional menu button and its panel sit together in a corner.
|
|
177
|
+
const group = doc.createElement('div');
|
|
178
|
+
Object.assign(group.style, {
|
|
179
|
+
position: 'fixed',
|
|
180
|
+
[vertical]: '12px',
|
|
181
|
+
[horizontal]: '12px',
|
|
182
|
+
display: 'flex',
|
|
183
|
+
alignItems: 'center',
|
|
184
|
+
gap: '6px',
|
|
185
|
+
});
|
|
186
|
+
group.appendChild(button);
|
|
187
|
+
const menu = options.menu
|
|
188
|
+
? createPillMenu(doc, { ...options.menu, opensUp: vertical === 'bottom', alignLeft: horizontal === 'left' })
|
|
189
|
+
: undefined;
|
|
190
|
+
if (menu)
|
|
191
|
+
group.append(menu.button, menu.panel);
|
|
192
|
+
layer.appendChild(group);
|
|
193
|
+
doc.body.appendChild(layer);
|
|
194
|
+
const releaseTopLayer = keepInTopLayer(layer, { document: doc });
|
|
195
|
+
return {
|
|
196
|
+
setEnabled,
|
|
197
|
+
setStatus: (status) => menu?.setStatus(status),
|
|
198
|
+
destroy: () => {
|
|
199
|
+
menu?.destroy();
|
|
200
|
+
releaseTopLayer();
|
|
201
|
+
layer.remove();
|
|
202
|
+
},
|
|
203
|
+
};
|
|
204
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeping the devtools' own UI visible over modals.
|
|
3
|
+
*
|
|
4
|
+
* Native `<dialog>.showModal()` and popovers (which Angular CDK overlays use by
|
|
5
|
+
* default from v22 — Material dialogs, menus, selects) render in the browser's
|
|
6
|
+
* **top layer**, above every z-index. A modal dialog also makes everything
|
|
7
|
+
* outside it inert. So a plain fixed-position overlay ends up hidden under the
|
|
8
|
+
* dialog and the pill can't be clicked. Verified in Chromium: making our UI a
|
|
9
|
+
* popover alone isn't enough while a modal is open (it stays inert); it has to
|
|
10
|
+
* be inside the modal. Outside a modal, it has to be the most recently shown
|
|
11
|
+
* popover to sit on top.
|
|
12
|
+
*/
|
|
13
|
+
/** Is this dialog open with `showModal()` (not `show()` or the `open` attribute)? */
|
|
14
|
+
export declare function isModalDialog(el: Element): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* The open modal `<dialog>` that blocks the rest of `doc`, or null. With several
|
|
17
|
+
* open, takes the last in document order as the topmost (nested modals are
|
|
18
|
+
* normally opened later and inserted later).
|
|
19
|
+
*/
|
|
20
|
+
export declare function blockingModalDialog(doc?: Document, isModal?: (el: Element) => boolean): HTMLDialogElement | null;
|
|
21
|
+
export interface TopLayerOptions {
|
|
22
|
+
/** Document to watch. Defaults to the element's own document. */
|
|
23
|
+
document?: Document;
|
|
24
|
+
/** Override modal detection (tests; jsdom has no `:modal`). */
|
|
25
|
+
isModal?: (el: Element) => boolean;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Keep `el` (one of the devtools' own layers, already in the document) on top
|
|
29
|
+
* of whatever the app has in the top layer:
|
|
30
|
+
*
|
|
31
|
+
* - it's shown as a manual popover, so it sits in the top layer itself;
|
|
32
|
+
* - while a modal dialog is open it's moved inside that dialog, so it isn't
|
|
33
|
+
* inert, and moved back to `<body>` when the dialog closes;
|
|
34
|
+
* - when the app shows another popover or dialog, it's re-shown so it stays the
|
|
35
|
+
* topmost.
|
|
36
|
+
*
|
|
37
|
+
* Falls back to a plain fixed element where popovers aren't supported. Returns a
|
|
38
|
+
* function that stops watching.
|
|
39
|
+
*/
|
|
40
|
+
export declare function keepInTopLayer(el: HTMLElement, options?: TopLayerOptions): () => void;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeping the devtools' own UI visible over modals.
|
|
3
|
+
*
|
|
4
|
+
* Native `<dialog>.showModal()` and popovers (which Angular CDK overlays use by
|
|
5
|
+
* default from v22 — Material dialogs, menus, selects) render in the browser's
|
|
6
|
+
* **top layer**, above every z-index. A modal dialog also makes everything
|
|
7
|
+
* outside it inert. So a plain fixed-position overlay ends up hidden under the
|
|
8
|
+
* dialog and the pill can't be clicked. Verified in Chromium: making our UI a
|
|
9
|
+
* popover alone isn't enough while a modal is open (it stays inert); it has to
|
|
10
|
+
* be inside the modal. Outside a modal, it has to be the most recently shown
|
|
11
|
+
* popover to sit on top.
|
|
12
|
+
*/
|
|
13
|
+
/** Resets the UA popover styles so a popover can act as a full-viewport layer. */
|
|
14
|
+
const POPOVER_LAYER_STYLE = {
|
|
15
|
+
position: 'fixed',
|
|
16
|
+
inset: '0',
|
|
17
|
+
width: '100%',
|
|
18
|
+
height: '100%',
|
|
19
|
+
maxWidth: 'none',
|
|
20
|
+
maxHeight: 'none',
|
|
21
|
+
margin: '0',
|
|
22
|
+
padding: '0',
|
|
23
|
+
border: '0',
|
|
24
|
+
background: 'transparent',
|
|
25
|
+
color: 'inherit',
|
|
26
|
+
overflow: 'visible',
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Every layer this module manages. Their own popover toggles are ignored, or two
|
|
30
|
+
* layers (overlay + pill) would keep re-showing to get above each other.
|
|
31
|
+
*/
|
|
32
|
+
const managed = new WeakSet();
|
|
33
|
+
/** True if `el` matches `selector`; false if the browser doesn't know the selector. */
|
|
34
|
+
function safeMatches(el, selector) {
|
|
35
|
+
try {
|
|
36
|
+
return el.matches(selector);
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/** Is this dialog open with `showModal()` (not `show()` or the `open` attribute)? */
|
|
43
|
+
export function isModalDialog(el) {
|
|
44
|
+
return el.tagName === 'DIALOG' && safeMatches(el, ':modal');
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The open modal `<dialog>` that blocks the rest of `doc`, or null. With several
|
|
48
|
+
* open, takes the last in document order as the topmost (nested modals are
|
|
49
|
+
* normally opened later and inserted later).
|
|
50
|
+
*/
|
|
51
|
+
export function blockingModalDialog(doc = document, isModal = isModalDialog) {
|
|
52
|
+
const open = [...doc.querySelectorAll('dialog[open]')].filter(isModal);
|
|
53
|
+
return open.at(-1) ?? null;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Keep `el` (one of the devtools' own layers, already in the document) on top
|
|
57
|
+
* of whatever the app has in the top layer:
|
|
58
|
+
*
|
|
59
|
+
* - it's shown as a manual popover, so it sits in the top layer itself;
|
|
60
|
+
* - while a modal dialog is open it's moved inside that dialog, so it isn't
|
|
61
|
+
* inert, and moved back to `<body>` when the dialog closes;
|
|
62
|
+
* - when the app shows another popover or dialog, it's re-shown so it stays the
|
|
63
|
+
* topmost.
|
|
64
|
+
*
|
|
65
|
+
* Falls back to a plain fixed element where popovers aren't supported. Returns a
|
|
66
|
+
* function that stops watching.
|
|
67
|
+
*/
|
|
68
|
+
export function keepInTopLayer(el, options = {}) {
|
|
69
|
+
const doc = options.document ?? el.ownerDocument;
|
|
70
|
+
const isModal = options.isModal ?? isModalDialog;
|
|
71
|
+
const canPopover = typeof el.showPopover === 'function';
|
|
72
|
+
managed.add(el);
|
|
73
|
+
if (canPopover) {
|
|
74
|
+
el.setAttribute('popover', 'manual');
|
|
75
|
+
Object.assign(el.style, POPOVER_LAYER_STYLE);
|
|
76
|
+
}
|
|
77
|
+
const show = () => {
|
|
78
|
+
if (!canPopover)
|
|
79
|
+
return;
|
|
80
|
+
try {
|
|
81
|
+
if (safeMatches(el, ':popover-open'))
|
|
82
|
+
el.hidePopover();
|
|
83
|
+
el.showPopover();
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
// Not connected, or the browser refused: stay a plain fixed layer.
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
const place = () => {
|
|
90
|
+
const parent = blockingModalDialog(doc, isModal) ?? doc.body;
|
|
91
|
+
// Moving a node closes it as a popover, so only move when needed.
|
|
92
|
+
if (el.parentNode !== parent)
|
|
93
|
+
parent.appendChild(el);
|
|
94
|
+
show();
|
|
95
|
+
};
|
|
96
|
+
// A dialog opening or closing flips its `open` attribute; popovers don't, but
|
|
97
|
+
// they fire `toggle` (it doesn't bubble, so listen in the capture phase).
|
|
98
|
+
const observer = new MutationObserver(place);
|
|
99
|
+
observer.observe(doc.documentElement, {
|
|
100
|
+
subtree: true,
|
|
101
|
+
attributes: true,
|
|
102
|
+
attributeFilter: ['open'],
|
|
103
|
+
});
|
|
104
|
+
const onToggle = (event) => {
|
|
105
|
+
if (event.target instanceof Element && managed.has(event.target))
|
|
106
|
+
return;
|
|
107
|
+
if (event.newState === 'open')
|
|
108
|
+
place();
|
|
109
|
+
};
|
|
110
|
+
doc.addEventListener('toggle', onToggle, true);
|
|
111
|
+
place();
|
|
112
|
+
return () => {
|
|
113
|
+
observer.disconnect();
|
|
114
|
+
doc.removeEventListener('toggle', onToggle, true);
|
|
115
|
+
managed.delete(el);
|
|
116
|
+
};
|
|
117
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ngbracket/a11y-devtools",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "Dev-only in-app accessibility auditing for Angular that maps each axe violation back to the component that rendered it — the attribution React overlay tools can't do.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Duncan Faulkner",
|