···
it('should not allow the dialog to be tabbable', () => {
const ref = useRef<HTMLUListElement>(null);
62
-
useDialogFocus(ref);
62
+
const ownerRef = useRef<HTMLInputElement>(null);
63
+
useDialogFocus(ref, { ownerRef });
65
-
<input type="text" name="text" />
66
+
<input type="text" name="text" ref={ownerRef} />
<ul ref={ref} role="dialog">
···
const [visible, setVisible] = useState(false);
const [nested, setNested] = useState(false);
const ref = useRef<HTMLUListElement>(null);
187
+
const ownerRef = useRef<HTMLInputElement>(null);
187
-
useDialogFocus(ref, { disabled: !visible });
189
+
useDialogFocus(ref, { disabled: !visible, ownerRef });
191
-
<input type="text" name="text" onFocus={() => setVisible(true)} />
193
-
<ul ref={ref} role="dialog">
194
-
<li tabIndex={0}>Outer #1</li>
195
-
<li tabIndex={0} onFocus={() => setNested(true)}>Outer #2</li>
196
-
{nested && <InnerDialog />}
199
-
<button>after</button>
204
-
mount(<OuterDialog />);
206
-
cy.get('input').first().as('input').focus();
207
-
cy.focused().should('have.property.name', 'text');
209
-
// select first dialog
210
-
cy.realPress('ArrowDown');
211
-
cy.focused().contains('Outer #1');
212
-
cy.realPress('ArrowDown');
213
-
cy.focused().contains('Outer #2');
215
-
// select second dialog
216
-
cy.realPress('ArrowDown');
217
-
cy.focused().contains('Inner #1');
218
-
cy.realPress('ArrowDown');
219
-
cy.focused().contains('Inner #2');
221
-
// remains in inner dialog
222
-
cy.realPress('ArrowDown');
223
-
cy.focused().contains('Inner #1');
225
-
// tabs to last dialog
226
-
cy.realPress(['Shift', 'Tab']);
227
-
cy.focused().contains('Outer #2');
229
-
// arrows bring us back to the inner dialog
230
-
cy.realPress('ArrowUp');
231
-
cy.focused().contains('Inner #2');
233
-
// tab out of dialogs
234
-
cy.realPress('Tab');
235
-
cy.focused().contains('after');
236
-
// we can't reenter the dialogs
237
-
cy.realPress(['Shift', 'Tab']);
238
-
cy.get('@input').should('have.focus');
241
-
it('supports nested dialogs', () => {
242
-
const InnerDialog = () => {
243
-
const ref = useRef<HTMLUListElement>(null);
244
-
useDialogFocus(ref);
247
-
<ul ref={ref} role="dialog">
248
-
<li tabIndex={0}>Inner #1</li>
249
-
<li tabIndex={0}>Inner #2</li>
254
-
const OuterDialog = () => {
255
-
const [visible, setVisible] = useState(false);
256
-
const [nested, setNested] = useState(false);
257
-
const ref = useRef<HTMLUListElement>(null);
259
-
useDialogFocus(ref, { disabled: !visible });
263
-
<input type="text" name="text" onFocus={() => setVisible(true)} />
193
+
<input type="text" name="text" onFocus={() => setVisible(true)} ref={ownerRef} />
<ul ref={ref} role="dialog">
<li tabIndex={0}>Outer #1</li>