@radix-ui/primitive 1.2.0 → 1.2.1-rc.1796411593667
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/dist/index.d.mts +41 -2
- package/dist/index.d.ts +41 -2
- package/dist/index.js +157 -2
- package/dist/index.js.map +3 -3
- package/dist/index.mjs +157 -2
- package/dist/index.mjs.map +3 -3
- package/package.json +6 -33
- package/src/aria-hidden.test.tsx +556 -0
- package/src/aria-hidden.ts +242 -0
- package/src/can-use-dom.ts +6 -0
- package/src/index.ts +4 -0
- package/src/primitive.tsx +68 -0
- package/src/types.ts +3 -0
- package/dist/internal/is-development.false.d.mts +0 -3
- package/dist/internal/is-development.false.d.ts +0 -3
- package/dist/internal/is-development.false.js +0 -27
- package/dist/internal/is-development.false.js.map +0 -7
- package/dist/internal/is-development.false.mjs +0 -6
- package/dist/internal/is-development.false.mjs.map +0 -7
- package/dist/internal/is-development.true.d.mts +0 -3
- package/dist/internal/is-development.true.d.ts +0 -3
- package/dist/internal/is-development.true.js +0 -27
- package/dist/internal/is-development.true.js.map +0 -7
- package/dist/internal/is-development.true.mjs +0 -6
- package/dist/internal/is-development.true.mjs.map +0 -7
|
@@ -0,0 +1,556 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fork of `aria-hidden`
|
|
3
|
+
*
|
|
4
|
+
* MIT License
|
|
5
|
+
* Copyright (c) 2017 Anton Korzunov
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { act, cleanup, render } from '@testing-library/react';
|
|
9
|
+
import * as React from 'react';
|
|
10
|
+
import { createRoot } from 'react-dom/client';
|
|
11
|
+
import type { Root } from 'react-dom/client';
|
|
12
|
+
import { describe, it, expect, vi, afterEach } from 'vitest';
|
|
13
|
+
import { hideOthers, supportsInert, inertOthers, suppressOthers } from '.';
|
|
14
|
+
|
|
15
|
+
afterEach(() => {
|
|
16
|
+
cleanup();
|
|
17
|
+
vi.restoreAllMocks();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
describe('hideOthers', () => {
|
|
21
|
+
const factory = () => {
|
|
22
|
+
const parent = React.createRef() as React.RefObject<HTMLDivElement>;
|
|
23
|
+
const target1 = React.createRef() as React.RefObject<HTMLDivElement>;
|
|
24
|
+
const target2 = React.createRef() as React.RefObject<HTMLDivElement>;
|
|
25
|
+
const targetOutside1 = React.createRef() as React.RefObject<HTMLDivElement>;
|
|
26
|
+
const wrapper = render(
|
|
27
|
+
<div>
|
|
28
|
+
<div ref={parent}>
|
|
29
|
+
<div>hide me 1</div>
|
|
30
|
+
<div ref={target1}>not me 2</div>
|
|
31
|
+
<div>
|
|
32
|
+
<div ref={target2}>not me 3</div>
|
|
33
|
+
</div>
|
|
34
|
+
<div ref={targetOutside1}>hide me 4</div>
|
|
35
|
+
<svg>
|
|
36
|
+
<text>svg</text>
|
|
37
|
+
</svg>
|
|
38
|
+
<div aria-hidden>I am already hidden! 5</div>
|
|
39
|
+
</div>
|
|
40
|
+
<div>dont touch me 6</div>
|
|
41
|
+
</div>,
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
const html = () => wrapper.container.innerHTML;
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
base: html(),
|
|
48
|
+
wrapper: {
|
|
49
|
+
html,
|
|
50
|
+
},
|
|
51
|
+
parent,
|
|
52
|
+
target1,
|
|
53
|
+
target2,
|
|
54
|
+
targetOutside1,
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
it('hides single', () => {
|
|
59
|
+
const { base, parent, target1, target2, targetOutside1, wrapper } = factory();
|
|
60
|
+
|
|
61
|
+
const unhide = hideOthers(new Set([target1.current]), parent.current);
|
|
62
|
+
expect(wrapper.html()).toMatchInlineSnapshot(
|
|
63
|
+
`"<div><div><div data-aria-hidden="true" aria-hidden="true">hide me 1</div><div>not me 2</div><div data-aria-hidden="true" aria-hidden="true"><div>not me 3</div></div><div data-aria-hidden="true" aria-hidden="true">hide me 4</div><svg data-aria-hidden="true" aria-hidden="true"><text>svg</text></svg><div aria-hidden="true" data-aria-hidden="true">I am already hidden! 5</div></div><div>dont touch me 6</div></div>"`,
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
expect(getNearestAttribute(target1.current, 'aria-hidden', parent)).toBe(null);
|
|
67
|
+
expect(getNearestAttribute(target2.current, 'aria-hidden', parent)).toBe('true');
|
|
68
|
+
expect(getNearestAttribute(targetOutside1.current, 'aria-hidden', parent)).toBe('true');
|
|
69
|
+
|
|
70
|
+
unhide();
|
|
71
|
+
expect(wrapper.html()).toEqual(base);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('hides multiple', () => {
|
|
75
|
+
const { base, parent, target1, target2, targetOutside1, wrapper } = factory();
|
|
76
|
+
|
|
77
|
+
const unhide = hideOthers(new Set([target1.current, target2.current]), parent.current);
|
|
78
|
+
expect(wrapper.html()).toMatchInlineSnapshot(
|
|
79
|
+
`"<div><div><div data-aria-hidden="true" aria-hidden="true">hide me 1</div><div>not me 2</div><div><div>not me 3</div></div><div data-aria-hidden="true" aria-hidden="true">hide me 4</div><svg data-aria-hidden="true" aria-hidden="true"><text>svg</text></svg><div aria-hidden="true" data-aria-hidden="true">I am already hidden! 5</div></div><div>dont touch me 6</div></div>"`,
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
expect(getNearestAttribute(target1.current, 'aria-hidden', parent)).toBe(null);
|
|
83
|
+
expect(getNearestAttribute(target2.current, 'aria-hidden', parent)).toBe(null);
|
|
84
|
+
expect(getNearestAttribute(targetOutside1.current, 'aria-hidden', parent)).toBe('true');
|
|
85
|
+
|
|
86
|
+
unhide();
|
|
87
|
+
expect(wrapper.html()).toEqual(base);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('hides cross', () => {
|
|
91
|
+
const { base, parent, target1, target2, targetOutside1, wrapper } = factory();
|
|
92
|
+
|
|
93
|
+
const unhide1 = hideOthers(new Set([target1.current]), parent.current);
|
|
94
|
+
expect(wrapper.html()).toMatchInlineSnapshot(
|
|
95
|
+
`"<div><div><div data-aria-hidden="true" aria-hidden="true">hide me 1</div><div>not me 2</div><div data-aria-hidden="true" aria-hidden="true"><div>not me 3</div></div><div data-aria-hidden="true" aria-hidden="true">hide me 4</div><svg data-aria-hidden="true" aria-hidden="true"><text>svg</text></svg><div aria-hidden="true" data-aria-hidden="true">I am already hidden! 5</div></div><div>dont touch me 6</div></div>"`,
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
expect(getNearestAttribute(target1.current, 'aria-hidden', parent)).toBe(null);
|
|
99
|
+
expect(getNearestAttribute(target2.current, 'aria-hidden', parent)).toBe('true');
|
|
100
|
+
expect(getNearestAttribute(targetOutside1.current, 'aria-hidden', parent)).toBe('true');
|
|
101
|
+
expect(getNearestAttribute(targetOutside1.current, 'data-aria-hidden', parent)).toBe('true');
|
|
102
|
+
|
|
103
|
+
const unhide2 = hideOthers(new Set([target2.current]), parent.current);
|
|
104
|
+
|
|
105
|
+
expect(getNearestAttribute(target1.current, 'aria-hidden', parent)).toBe('true');
|
|
106
|
+
expect(getNearestAttribute(target2.current, 'aria-hidden', parent)).toBe('true');
|
|
107
|
+
expect(getNearestAttribute(targetOutside1.current, 'aria-hidden', parent)).toBe('true');
|
|
108
|
+
expect(getNearestAttribute(targetOutside1.current, 'data-aria-hidden', parent)).toBe('true');
|
|
109
|
+
|
|
110
|
+
expect(wrapper.html()).toMatchInlineSnapshot(
|
|
111
|
+
`"<div><div><div data-aria-hidden="true" aria-hidden="true">hide me 1</div><div data-aria-hidden="true" aria-hidden="true">not me 2</div><div data-aria-hidden="true" aria-hidden="true"><div>not me 3</div></div><div data-aria-hidden="true" aria-hidden="true">hide me 4</div><svg data-aria-hidden="true" aria-hidden="true"><text>svg</text></svg><div aria-hidden="true" data-aria-hidden="true">I am already hidden! 5</div></div><div>dont touch me 6</div></div>"`,
|
|
112
|
+
);
|
|
113
|
+
unhide1();
|
|
114
|
+
|
|
115
|
+
expect(getNearestAttribute(target1.current, 'aria-hidden', parent)).toBe('true');
|
|
116
|
+
expect(getNearestAttribute(target2.current, 'aria-hidden', parent)).toBe(null);
|
|
117
|
+
expect(getNearestAttribute(targetOutside1.current, 'aria-hidden', parent)).toBe('true');
|
|
118
|
+
expect(getNearestAttribute(targetOutside1.current, 'data-aria-hidden', parent)).toBe('true');
|
|
119
|
+
|
|
120
|
+
expect(wrapper.html()).toMatchInlineSnapshot(
|
|
121
|
+
`"<div><div><div data-aria-hidden="true" aria-hidden="true">hide me 1</div><div data-aria-hidden="true" aria-hidden="true">not me 2</div><div><div>not me 3</div></div><div data-aria-hidden="true" aria-hidden="true">hide me 4</div><svg data-aria-hidden="true" aria-hidden="true"><text>svg</text></svg><div aria-hidden="true" data-aria-hidden="true">I am already hidden! 5</div></div><div>dont touch me 6</div></div>"`,
|
|
122
|
+
);
|
|
123
|
+
unhide2();
|
|
124
|
+
|
|
125
|
+
expect(wrapper.html()).toEqual(base);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('hides cross markers', () => {
|
|
129
|
+
const { base, parent, target1, target2, targetOutside1, wrapper } = factory();
|
|
130
|
+
|
|
131
|
+
const unhide1 = hideOthers(new Set([target1.current]), parent.current, 'marker1');
|
|
132
|
+
expect(getNearestAttribute(targetOutside1.current, 'marker1', parent)).toBe('true');
|
|
133
|
+
|
|
134
|
+
const unhide2 = hideOthers(new Set([target2.current]), parent.current, 'marker2');
|
|
135
|
+
|
|
136
|
+
expect(getNearestAttribute(targetOutside1.current, 'marker1', parent)).toBe('true');
|
|
137
|
+
expect(getNearestAttribute(targetOutside1.current, 'marker2', parent)).toBe('true');
|
|
138
|
+
|
|
139
|
+
unhide1();
|
|
140
|
+
|
|
141
|
+
expect(getNearestAttribute(targetOutside1.current, 'marker1', parent)).toBe(null);
|
|
142
|
+
|
|
143
|
+
expect(wrapper.html()).toMatchInlineSnapshot(
|
|
144
|
+
`"<div><div><div aria-hidden="true" marker2="true">hide me 1</div><div marker2="true" aria-hidden="true">not me 2</div><div><div>not me 3</div></div><div aria-hidden="true" marker2="true">hide me 4</div><svg aria-hidden="true" marker2="true"><text>svg</text></svg><div aria-hidden="true" marker2="true">I am already hidden! 5</div></div><div>dont touch me 6</div></div>"`,
|
|
145
|
+
);
|
|
146
|
+
unhide2();
|
|
147
|
+
|
|
148
|
+
expect(wrapper.html()).toEqual(base);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('handles aria-live', () => {
|
|
152
|
+
const wrapper = render(
|
|
153
|
+
<div>
|
|
154
|
+
<div>
|
|
155
|
+
<div id="to-be-hidden">hidden</div>
|
|
156
|
+
<div aria-live="polite">not-hidden life</div>
|
|
157
|
+
<div aria-live="off">hidden life</div>
|
|
158
|
+
</div>
|
|
159
|
+
<div>not-hidden</div>
|
|
160
|
+
</div>,
|
|
161
|
+
);
|
|
162
|
+
const root = wrapper.container;
|
|
163
|
+
|
|
164
|
+
const unhide = hideOthers(new Set([root.firstElementChild!.lastElementChild!]), root);
|
|
165
|
+
|
|
166
|
+
expect(
|
|
167
|
+
getNearestAttribute(root.querySelector('[aria-live]')!, 'aria-hidden', { current: root }),
|
|
168
|
+
).toBe(null);
|
|
169
|
+
expect(
|
|
170
|
+
getNearestAttribute(root.querySelector('#to-be-hidden')!, 'aria-hidden', { current: root }),
|
|
171
|
+
).toBe('true');
|
|
172
|
+
expect(
|
|
173
|
+
getNearestAttribute(root.querySelector('[aria-live="off"]')!, 'aria-hidden', {
|
|
174
|
+
current: root,
|
|
175
|
+
}),
|
|
176
|
+
).toBe(null);
|
|
177
|
+
|
|
178
|
+
expect(wrapper.baseElement.innerHTML).toMatchInlineSnapshot(
|
|
179
|
+
`"<div><div><div><div id="to-be-hidden" data-aria-hidden="true" aria-hidden="true">hidden</div><div aria-live="polite">not-hidden life</div><div aria-live="off">hidden life</div></div><div>not-hidden</div></div></div>"`,
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
unhide();
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('handles script', () => {
|
|
186
|
+
const scriptContainer = React.createRef<HTMLDivElement>();
|
|
187
|
+
const wrapper = render(
|
|
188
|
+
<div>
|
|
189
|
+
<div ref={scriptContainer}>
|
|
190
|
+
<div id="to-be-hidden">hidden</div>
|
|
191
|
+
</div>
|
|
192
|
+
<div>not-hidden</div>
|
|
193
|
+
</div>,
|
|
194
|
+
);
|
|
195
|
+
// React warns when rendering `<script>`, so it is inserted directly
|
|
196
|
+
scriptContainer.current!.append(document.createElement('script'));
|
|
197
|
+
const root = wrapper.container;
|
|
198
|
+
|
|
199
|
+
const unhide = hideOthers(new Set([root.firstElementChild!.lastElementChild!]), root);
|
|
200
|
+
|
|
201
|
+
expect(root.querySelector('script')!.hasAttribute('aria-hidden')).toBe(false);
|
|
202
|
+
expect(root.querySelector('script')!.hasAttribute('data-aria-hidden')).toBe(false);
|
|
203
|
+
expect(scriptContainer.current!.getAttribute('aria-hidden')).toBe('true');
|
|
204
|
+
expect(
|
|
205
|
+
getNearestAttribute(root.querySelector('#to-be-hidden')!, 'aria-hidden', { current: root }),
|
|
206
|
+
).toBe('true');
|
|
207
|
+
|
|
208
|
+
expect(wrapper.baseElement.innerHTML).toMatchInlineSnapshot(
|
|
209
|
+
`"<div><div><div data-aria-hidden="true" aria-hidden="true"><div id="to-be-hidden">hidden</div><script></script></div><div>not-hidden</div></div></div>"`,
|
|
210
|
+
);
|
|
211
|
+
|
|
212
|
+
unhide();
|
|
213
|
+
|
|
214
|
+
expect(scriptContainer.current!.hasAttribute('aria-hidden')).toBe(false);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it('handles style', () => {
|
|
218
|
+
const container = React.createRef<HTMLDivElement>();
|
|
219
|
+
const wrapper = render(
|
|
220
|
+
<div>
|
|
221
|
+
<div ref={container}>
|
|
222
|
+
<div id="to-be-hidden">hidden</div>
|
|
223
|
+
</div>
|
|
224
|
+
<div>not-hidden</div>
|
|
225
|
+
</div>,
|
|
226
|
+
);
|
|
227
|
+
container.current!.append(document.createElement('style'));
|
|
228
|
+
const root = wrapper.container;
|
|
229
|
+
|
|
230
|
+
const unhide = hideOthers(new Set([root.firstElementChild!.lastElementChild!]), root);
|
|
231
|
+
|
|
232
|
+
expect(root.querySelector('style')!.hasAttribute('aria-hidden')).toBe(false);
|
|
233
|
+
expect(root.querySelector('style')!.hasAttribute('data-aria-hidden')).toBe(false);
|
|
234
|
+
expect(container.current!.getAttribute('aria-hidden')).toBe('true');
|
|
235
|
+
|
|
236
|
+
expect(wrapper.baseElement.innerHTML).toMatchInlineSnapshot(
|
|
237
|
+
`"<div><div><div data-aria-hidden="true" aria-hidden="true"><div id="to-be-hidden">hidden</div><style></style></div><div>not-hidden</div></div></div>"`,
|
|
238
|
+
);
|
|
239
|
+
|
|
240
|
+
unhide();
|
|
241
|
+
|
|
242
|
+
expect(container.current!.hasAttribute('aria-hidden')).toBe(false);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it('handles link', () => {
|
|
246
|
+
const container = React.createRef<HTMLDivElement>();
|
|
247
|
+
const wrapper = render(
|
|
248
|
+
<div>
|
|
249
|
+
<div ref={container}>
|
|
250
|
+
<div id="to-be-hidden">hidden</div>
|
|
251
|
+
</div>
|
|
252
|
+
<div>not-hidden</div>
|
|
253
|
+
</div>,
|
|
254
|
+
);
|
|
255
|
+
// React hoists `<link>` into `<head>`, so it is inserted directly
|
|
256
|
+
container.current!.append(document.createElement('link'));
|
|
257
|
+
const root = wrapper.container;
|
|
258
|
+
|
|
259
|
+
const unhide = hideOthers(new Set([root.firstElementChild!.lastElementChild!]), root);
|
|
260
|
+
|
|
261
|
+
expect(root.querySelector('link')!.hasAttribute('aria-hidden')).toBe(false);
|
|
262
|
+
expect(root.querySelector('link')!.hasAttribute('data-aria-hidden')).toBe(false);
|
|
263
|
+
expect(container.current!.getAttribute('aria-hidden')).toBe('true');
|
|
264
|
+
|
|
265
|
+
expect(wrapper.baseElement.innerHTML).toMatchInlineSnapshot(
|
|
266
|
+
`"<div><div><div data-aria-hidden="true" aria-hidden="true"><div id="to-be-hidden">hidden</div><link></div><div>not-hidden</div></div></div>"`,
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
unhide();
|
|
270
|
+
|
|
271
|
+
expect(container.current!.hasAttribute('aria-hidden')).toBe(false);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it('handles meta', () => {
|
|
275
|
+
const container = React.createRef<HTMLDivElement>();
|
|
276
|
+
const wrapper = render(
|
|
277
|
+
<div>
|
|
278
|
+
<div ref={container}>
|
|
279
|
+
<div id="to-be-hidden">hidden</div>
|
|
280
|
+
</div>
|
|
281
|
+
<div>not-hidden</div>
|
|
282
|
+
</div>,
|
|
283
|
+
);
|
|
284
|
+
// React hoists `<meta>` into `<head>`, so it is inserted directly
|
|
285
|
+
container.current!.append(document.createElement('meta'));
|
|
286
|
+
const root = wrapper.container;
|
|
287
|
+
|
|
288
|
+
const unhide = hideOthers(new Set([root.firstElementChild!.lastElementChild!]), root);
|
|
289
|
+
|
|
290
|
+
expect(root.querySelector('meta')!.hasAttribute('aria-hidden')).toBe(false);
|
|
291
|
+
expect(root.querySelector('meta')!.hasAttribute('data-aria-hidden')).toBe(false);
|
|
292
|
+
expect(container.current!.getAttribute('aria-hidden')).toBe('true');
|
|
293
|
+
|
|
294
|
+
expect(wrapper.baseElement.innerHTML).toMatchInlineSnapshot(
|
|
295
|
+
`"<div><div><div data-aria-hidden="true" aria-hidden="true"><div id="to-be-hidden">hidden</div><meta></div><div>not-hidden</div></div></div>"`,
|
|
296
|
+
);
|
|
297
|
+
|
|
298
|
+
unhide();
|
|
299
|
+
|
|
300
|
+
expect(container.current!.hasAttribute('aria-hidden')).toBe(false);
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
it('handles template', () => {
|
|
304
|
+
const container = React.createRef<HTMLDivElement>();
|
|
305
|
+
const wrapper = render(
|
|
306
|
+
<div>
|
|
307
|
+
<div ref={container}>
|
|
308
|
+
<div id="to-be-hidden">hidden</div>
|
|
309
|
+
</div>
|
|
310
|
+
<div>not-hidden</div>
|
|
311
|
+
</div>,
|
|
312
|
+
);
|
|
313
|
+
container.current!.append(document.createElement('template'));
|
|
314
|
+
const root = wrapper.container;
|
|
315
|
+
|
|
316
|
+
const unhide = hideOthers(new Set([root.firstElementChild!.lastElementChild!]), root);
|
|
317
|
+
|
|
318
|
+
expect(root.querySelector('template')!.hasAttribute('aria-hidden')).toBe(false);
|
|
319
|
+
expect(root.querySelector('template')!.hasAttribute('data-aria-hidden')).toBe(false);
|
|
320
|
+
expect(container.current!.getAttribute('aria-hidden')).toBe('true');
|
|
321
|
+
|
|
322
|
+
expect(wrapper.baseElement.innerHTML).toMatchInlineSnapshot(
|
|
323
|
+
`"<div><div><div data-aria-hidden="true" aria-hidden="true"><div id="to-be-hidden">hidden</div><template></template></div><div>not-hidden</div></div></div>"`,
|
|
324
|
+
);
|
|
325
|
+
|
|
326
|
+
unhide();
|
|
327
|
+
|
|
328
|
+
expect(container.current!.hasAttribute('aria-hidden')).toBe(false);
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
it('handles noscript', () => {
|
|
332
|
+
const container = React.createRef<HTMLDivElement>();
|
|
333
|
+
const wrapper = render(
|
|
334
|
+
<div>
|
|
335
|
+
<div ref={container}>
|
|
336
|
+
<div id="to-be-hidden">hidden</div>
|
|
337
|
+
</div>
|
|
338
|
+
<div>not-hidden</div>
|
|
339
|
+
</div>,
|
|
340
|
+
);
|
|
341
|
+
container.current!.append(document.createElement('noscript'));
|
|
342
|
+
const root = wrapper.container;
|
|
343
|
+
|
|
344
|
+
const unhide = hideOthers(new Set([root.firstElementChild!.lastElementChild!]), root);
|
|
345
|
+
|
|
346
|
+
expect(root.querySelector('noscript')!.hasAttribute('aria-hidden')).toBe(false);
|
|
347
|
+
expect(root.querySelector('noscript')!.hasAttribute('data-aria-hidden')).toBe(false);
|
|
348
|
+
expect(container.current!.getAttribute('aria-hidden')).toBe('true');
|
|
349
|
+
|
|
350
|
+
expect(wrapper.baseElement.innerHTML).toMatchInlineSnapshot(
|
|
351
|
+
`"<div><div><div data-aria-hidden="true" aria-hidden="true"><div id="to-be-hidden">hidden</div><noscript></noscript></div><div>not-hidden</div></div></div>"`,
|
|
352
|
+
);
|
|
353
|
+
|
|
354
|
+
unhide();
|
|
355
|
+
|
|
356
|
+
expect(container.current!.hasAttribute('aria-hidden')).toBe(false);
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
it('does not skip svg title elements', () => {
|
|
360
|
+
const wrapper = render(
|
|
361
|
+
<div>
|
|
362
|
+
<svg>
|
|
363
|
+
<title>icon</title>
|
|
364
|
+
<circle />
|
|
365
|
+
</svg>
|
|
366
|
+
<div>not-hidden</div>
|
|
367
|
+
</div>,
|
|
368
|
+
);
|
|
369
|
+
const root = wrapper.container;
|
|
370
|
+
|
|
371
|
+
const unhide = hideOthers(new Set([root.querySelector('circle')!]), root);
|
|
372
|
+
|
|
373
|
+
expect(root.querySelector('title')!.getAttribute('aria-hidden')).toBe('true');
|
|
374
|
+
|
|
375
|
+
unhide();
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
it('does not mark script siblings of hidden elements', () => {
|
|
379
|
+
const wrapper = render(
|
|
380
|
+
<div>
|
|
381
|
+
<div id="to-be-hidden">hidden</div>
|
|
382
|
+
<div>not-hidden</div>
|
|
383
|
+
</div>,
|
|
384
|
+
);
|
|
385
|
+
// React warns when rendering `<script>`, so it is inserted directly
|
|
386
|
+
wrapper.container.firstElementChild!.append(document.createElement('script'));
|
|
387
|
+
const root = wrapper.container;
|
|
388
|
+
|
|
389
|
+
const unhide = hideOthers(new Set([root.firstElementChild!.children[1]!]), root);
|
|
390
|
+
|
|
391
|
+
expect(root.querySelector('script')!.hasAttribute('aria-hidden')).toBe(false);
|
|
392
|
+
expect(root.querySelector('script')!.hasAttribute('data-aria-hidden')).toBe(false);
|
|
393
|
+
expect(root.querySelector('#to-be-hidden')!.getAttribute('aria-hidden')).toBe('true');
|
|
394
|
+
|
|
395
|
+
expect(wrapper.baseElement.innerHTML).toMatchInlineSnapshot(
|
|
396
|
+
`"<div><div><div id="to-be-hidden" data-aria-hidden="true" aria-hidden="true">hidden</div><div>not-hidden</div><script></script></div></div>"`,
|
|
397
|
+
);
|
|
398
|
+
|
|
399
|
+
unhide();
|
|
400
|
+
});
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
describe('inert', () => {
|
|
404
|
+
it('supports in jsdom', () => {
|
|
405
|
+
// not yet
|
|
406
|
+
expect(supportsInert()).toBeFalsy();
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
it('inertOthers', () => {
|
|
410
|
+
const wrapper = render(
|
|
411
|
+
<div>
|
|
412
|
+
<div>
|
|
413
|
+
<div id="to-be-hidden">hidden</div>
|
|
414
|
+
</div>
|
|
415
|
+
<div id="not-to-be-hidden">not-hidden</div>
|
|
416
|
+
</div>,
|
|
417
|
+
);
|
|
418
|
+
const root = wrapper.container;
|
|
419
|
+
|
|
420
|
+
const unhide = inertOthers(new Set([root.firstElementChild!.lastElementChild!]), root);
|
|
421
|
+
|
|
422
|
+
expect(
|
|
423
|
+
getNearestAttribute(root.querySelector('#to-be-hidden')!, 'inert', { current: root }),
|
|
424
|
+
).toBe('true');
|
|
425
|
+
expect(
|
|
426
|
+
getNearestAttribute(root.querySelector('#not-to-be-hidden')!, 'inert', { current: root }),
|
|
427
|
+
).toBe(null);
|
|
428
|
+
|
|
429
|
+
expect(wrapper.baseElement.innerHTML).toMatchInlineSnapshot(
|
|
430
|
+
`"<div><div><div data-inert-ed="true" inert="true"><div id="to-be-hidden">hidden</div></div><div id="not-to-be-hidden">not-hidden</div></div></div>"`,
|
|
431
|
+
);
|
|
432
|
+
|
|
433
|
+
unhide();
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
it('preserves inert="false" since it is still inert', () => {
|
|
437
|
+
const wrapper = render(
|
|
438
|
+
<div>
|
|
439
|
+
<div id="already-inert">already inert</div>
|
|
440
|
+
<div id="not-to-be-hidden">not-hidden</div>
|
|
441
|
+
</div>,
|
|
442
|
+
);
|
|
443
|
+
const root = wrapper.container;
|
|
444
|
+
const alreadyInert = root.querySelector('#already-inert')!;
|
|
445
|
+
|
|
446
|
+
alreadyInert.setAttribute('inert', 'false');
|
|
447
|
+
|
|
448
|
+
const unhide = inertOthers(new Set([root.querySelector('#not-to-be-hidden')!]), root);
|
|
449
|
+
|
|
450
|
+
expect(alreadyInert.getAttribute('inert')).toBe('false');
|
|
451
|
+
expect(alreadyInert.getAttribute('data-inert-ed')).toBe('true');
|
|
452
|
+
|
|
453
|
+
unhide();
|
|
454
|
+
|
|
455
|
+
expect(alreadyInert.getAttribute('inert')).toBe('false');
|
|
456
|
+
expect(alreadyInert.hasAttribute('data-inert-ed')).toBe(false);
|
|
457
|
+
});
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
describe('suppressOthers', () => {
|
|
461
|
+
describe('shadow dom', () => {
|
|
462
|
+
it('Understand the host location', () => {
|
|
463
|
+
function App() {
|
|
464
|
+
return (
|
|
465
|
+
<div className="App">
|
|
466
|
+
<input id="input" />
|
|
467
|
+
<input id="another-input" />
|
|
468
|
+
</div>
|
|
469
|
+
);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
class WebComp extends HTMLElement {
|
|
473
|
+
root: ShadowRoot;
|
|
474
|
+
reactRoot: Root;
|
|
475
|
+
getTarget() {
|
|
476
|
+
return this.root.querySelector<HTMLInputElement>('#input')!;
|
|
477
|
+
}
|
|
478
|
+
getAnotherTarget() {
|
|
479
|
+
return this.root.querySelector<HTMLInputElement>('#another-input')!;
|
|
480
|
+
}
|
|
481
|
+
constructor() {
|
|
482
|
+
super();
|
|
483
|
+
|
|
484
|
+
// attach to the Shadow DOM
|
|
485
|
+
this.root = this.attachShadow({ mode: 'closed' });
|
|
486
|
+
this.reactRoot = createRoot(this.root);
|
|
487
|
+
act(() => this.reactRoot.render(<App />));
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
if (!window.customElements.get('web-comp')) {
|
|
492
|
+
window.customElements.define('web-comp', WebComp);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
const webComp = document.createElement('web-comp') as WebComp;
|
|
496
|
+
document.body.innerHTML = '<div id="test"></div>';
|
|
497
|
+
document.body.appendChild(webComp);
|
|
498
|
+
|
|
499
|
+
const unsuppressBody = suppressOthers(new Set([webComp.getTarget()]), document.body);
|
|
500
|
+
expect(webComp.getAttribute('aria-hidden')).toBeFalsy();
|
|
501
|
+
|
|
502
|
+
expect(document.body.innerHTML).toMatchInlineSnapshot(
|
|
503
|
+
`"<div id="test" data-suppressed="true" aria-hidden="true"></div><web-comp></web-comp>"`,
|
|
504
|
+
);
|
|
505
|
+
|
|
506
|
+
expect(webComp.root.innerHTML).toMatchInlineSnapshot(
|
|
507
|
+
`"<div class="App"><input id="input"><input id="another-input"></div>"`,
|
|
508
|
+
);
|
|
509
|
+
|
|
510
|
+
const unsuppressShadowRoot = suppressOthers(new Set([webComp.getTarget()]), webComp.root);
|
|
511
|
+
expect(webComp.getTarget().getAttribute('aria-hidden')).toBeFalsy();
|
|
512
|
+
expect(webComp.getAnotherTarget().getAttribute('aria-hidden')).not.toBeFalsy();
|
|
513
|
+
|
|
514
|
+
unsuppressShadowRoot();
|
|
515
|
+
unsuppressBody();
|
|
516
|
+
document.body.innerHTML = '';
|
|
517
|
+
|
|
518
|
+
// assert error
|
|
519
|
+
const error = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
520
|
+
const unsuppressDetached = suppressOthers(new Set([webComp.getTarget()]), document.body);
|
|
521
|
+
|
|
522
|
+
expect(error).toHaveBeenCalledWith(
|
|
523
|
+
'aria-hidden',
|
|
524
|
+
webComp.getTarget(),
|
|
525
|
+
expect.any(String),
|
|
526
|
+
document.body,
|
|
527
|
+
expect.any(String),
|
|
528
|
+
);
|
|
529
|
+
|
|
530
|
+
unsuppressDetached();
|
|
531
|
+
act(() => webComp.reactRoot.unmount());
|
|
532
|
+
});
|
|
533
|
+
});
|
|
534
|
+
});
|
|
535
|
+
|
|
536
|
+
export function getNearestAttribute(
|
|
537
|
+
node: Element,
|
|
538
|
+
name: string,
|
|
539
|
+
parent: React.RefObject<Element> | Element,
|
|
540
|
+
) {
|
|
541
|
+
const attr = node.getAttribute(name);
|
|
542
|
+
|
|
543
|
+
if (attr) {
|
|
544
|
+
return attr;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
if (
|
|
548
|
+
node === parent ||
|
|
549
|
+
node === (parent as React.RefObject<Element>).current ||
|
|
550
|
+
!node.parentNode
|
|
551
|
+
) {
|
|
552
|
+
return null;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
return getNearestAttribute(node.parentNode as Element, name, parent);
|
|
556
|
+
}
|