@rescui/dropdown 0.14.0 → 0.15.1

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.
@@ -1,7 +1,8 @@
1
1
  /**
2
- * `autoUpdate()` that applies a new trigger width in the next animation frame.
3
- * The new width resizes the dropdown content, which `useScrollableContainer` observes.
4
- * If it happens in the same ResizeObserver loop as the trigger resize, the browser can't
5
- * deliver that notification and reports "ResizeObserver loop completed with undelivered notifications".
2
+ * `autoUpdate()` that applies element resizes in the next animation frame.
3
+ * An update can change the dropdown's size (e.g. its max-height) and its content's size,
4
+ * which `useScrollableContainer` observes. If it happens in the same ResizeObserver loop,
5
+ * the browser can't deliver those notifications and reports
6
+ * "ResizeObserver loop completed with undelivered notifications".
6
7
  */
7
8
  export declare function autoUpdateDropdown(reference: Element, floating: HTMLElement, update: () => void): () => void;
@@ -1,10 +1,11 @@
1
1
  import { autoUpdate } from '@floating-ui/react-dom';
2
2
 
3
3
  /**
4
- * `autoUpdate()` that applies a new trigger width in the next animation frame.
5
- * The new width resizes the dropdown content, which `useScrollableContainer` observes.
6
- * If it happens in the same ResizeObserver loop as the trigger resize, the browser can't
7
- * deliver that notification and reports "ResizeObserver loop completed with undelivered notifications".
4
+ * `autoUpdate()` that applies element resizes in the next animation frame.
5
+ * An update can change the dropdown's size (e.g. its max-height) and its content's size,
6
+ * which `useScrollableContainer` observes. If it happens in the same ResizeObserver loop,
7
+ * the browser can't deliver those notifications and reports
8
+ * "ResizeObserver loop completed with undelivered notifications".
8
9
  */
9
10
  function autoUpdateDropdown(reference, floating, update) {
10
11
  // Without ResizeObserver (e.g. in JSDOM), `autoUpdate()` skips element resize updates too
@@ -14,40 +15,23 @@ function autoUpdateDropdown(reference, floating, update) {
14
15
  const cleanupAutoUpdate = autoUpdate(reference, floating, update, {
15
16
  elementResize: false
16
17
  });
17
- let referenceWidth = reference.getBoundingClientRect().width;
18
- let updateFrame = -1;
19
- let reobserveFrame = -1;
20
- const resizeObserver = new ResizeObserver(entries => {
21
- if (entries.some(_ref => {
22
- let {
23
- target
24
- } = _ref;
25
- return target === reference;
26
- })) {
27
- // Like `autoUpdate()`, stop observing the dropdown until the next frame:
28
- // the new position can change its size within the same loop
29
- resizeObserver.unobserve(floating);
30
- cancelAnimationFrame(reobserveFrame);
31
- reobserveFrame = requestAnimationFrame(() => {
32
- resizeObserver.observe(floating);
18
+ let updateFrame = null;
19
+ const resizeObserver = new ResizeObserver(() => {
20
+ if (updateFrame === null) {
21
+ updateFrame = requestAnimationFrame(() => {
22
+ updateFrame = null;
23
+ update();
33
24
  });
34
25
  }
35
- const previousWidth = referenceWidth;
36
- referenceWidth = reference.getBoundingClientRect().width;
37
- if (referenceWidth !== previousWidth) {
38
- cancelAnimationFrame(updateFrame);
39
- updateFrame = requestAnimationFrame(update);
40
- } else {
41
- update();
42
- }
43
26
  });
44
27
  resizeObserver.observe(reference);
45
28
  resizeObserver.observe(floating);
46
29
  return () => {
47
30
  cleanupAutoUpdate();
48
31
  resizeObserver.disconnect();
49
- cancelAnimationFrame(updateFrame);
50
- cancelAnimationFrame(reobserveFrame);
32
+ if (updateFrame !== null) {
33
+ cancelAnimationFrame(updateFrame);
34
+ }
51
35
  };
52
36
  }
53
37
 
package/lib/dropdown.d.ts CHANGED
@@ -1,12 +1,14 @@
1
1
  import React from 'react';
2
- import type { ReactNode, JSX } from 'react';
2
+ import type { ReactNode } from 'react';
3
3
  import { Theme } from '@rescui/ui-contexts';
4
+ import type { DropdownInitialFocus, DropdownRole, DropdownTrigger } from './types';
4
5
  import { DropdownMaxWidth, DropdownMinWidth, DropdownPlacement } from './custom-middleware';
5
- interface DropdownTriggerProps {
6
- ref: React.Ref<any>;
7
- }
8
- export type DropdownTrigger = ((props: DropdownTriggerProps) => JSX.Element) | JSX.Element;
9
- export interface DropdownProps {
6
+ export type { DropdownInitialFocus, DropdownRole, DropdownTrigger, DropdownTriggerProps } from './types';
7
+ export interface DropdownProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'role' | 'children' | 'className' | 'onFocus' | 'onBlur'> {
8
+ /**
9
+ * The element that opens the dropdown. With `role`, it gets `aria-haspopup`, `aria-expanded`
10
+ * and `aria-controls`: a function trigger in its props, an element trigger through `cloneElement`.
11
+ */
10
12
  trigger: DropdownTrigger;
11
13
  isOpen: boolean;
12
14
  onRequestClose?: () => void;
@@ -34,7 +36,48 @@ export interface DropdownProps {
34
36
  children?: ReactNode;
35
37
  closeOnEsc?: boolean;
36
38
  closeOnTab?: boolean;
39
+ /**
40
+ * Turns off the focus management: the focus doesn't move inside, isn't kept there and doesn't return.
41
+ * Use it to keep the focus in the trigger, like in a combobox.
42
+ */
37
43
  disableFocusLock?: boolean;
44
+ /**
45
+ * Hides the rest of the page from screen readers while the dropdown is open
46
+ * and adds `aria-modal="true"` to a dialog. Tab stays inside the dropdown either way.
47
+ * `role="alertdialog"` is always modal. Doesn't work with `disableFocusLock`.
48
+ * @default false
49
+ */
50
+ modal?: boolean;
51
+ /**
52
+ * The element focused on open: an index of a tabbable element inside, a ref to an element inside,
53
+ * or `'container'` for the dropdown itself, for example, when the first tabbable element reacts
54
+ * to the focus, like a native select on iOS. By default, the first tabbable element
55
+ * or the one with `data-autofocus`; it's also the fallback if the element isn't found.
56
+ * @type number | RefObject<HTMLElement | null> | 'container'
57
+ * @default 0
58
+ */
59
+ initialFocus?: DropdownInitialFocus;
60
+ /**
61
+ * Returns the focus on close to the element that had it before opening,
62
+ * or to the trigger if nothing had it (Safari doesn't focus buttons on click).
63
+ * @default true
64
+ */
65
+ returnFocus?: boolean;
66
+ /**
67
+ * The ARIA role of the dropdown. Sets `role` and `id` on the dropdown and the ARIA attributes on the trigger.
68
+ * - `dialog`, `alertdialog`: any content; name it with `aria-label` or `aria-labelledby`.
69
+ * - `menu`: named by the trigger unless `aria-label` is passed; the content must be `menuitem` elements.
70
+ * - `listbox`: makes the trigger a `combobox`; the content must be `option` elements.
71
+ * - `grid`, `tree`: the content must follow the grid or tree pattern.
72
+ */
73
+ role?: DropdownRole;
74
+ /**
75
+ * Adds visually hidden close buttons for touch screen readers, which have no Esc key.
76
+ * Use it in a modal dropdown without a visible close button. A string sets the label, "Dismiss" by default.
77
+ * Works only with `modal`.
78
+ * @default false
79
+ */
80
+ visuallyHiddenDismiss?: boolean | string;
38
81
  /**
39
82
  * Render dropdown inside trigger function or wrapper. May be useful when the trigger placed in the fixed layout.
40
83
  * */
package/lib/dropdown.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use client";
2
2
  import { c } from 'react-compiler-runtime';
3
- import React, { useState, useRef, useImperativeHandle, useContext, useEffect } from 'react';
3
+ import React, { useState, useRef, useImperativeHandle, useContext, useEffect, isValidElement, cloneElement } from 'react';
4
4
  import PropTypes from 'prop-types';
5
5
  import cn from 'classnames';
6
6
  import { useFloating, offset, autoPlacement, flip, shift, limitShift } from '@floating-ui/react-dom';
@@ -8,7 +8,10 @@ import { useLayeringContext, ThemeContext, getInverseTheme, LayeringProvider, Th
8
8
  import FocusLock from 'react-focus-lock';
9
9
  import ReactDOM from 'react-dom';
10
10
  import { autoUpdateDropdown } from './auto-update.js';
11
+ import { getInitialFocusElement, getFirstTabbableElement } from './focus-utils.js';
12
+ import { useDropdownAria } from './use-dropdown-aria.js';
11
13
  import { useDropdownState, DROPDOWN_STATES } from './use-dropdown-state.js';
14
+ import { useHideOthers } from './use-hide-others.js';
12
15
  import { useScrollableContainer } from './use-scrollable-container.js';
13
16
  import styles from './dropdown.p.module.css.js';
14
17
  import { isAutoPlacement, sameWidth, AUTO_PLACEMENTS, applyMaxSize } from './custom-middleware.js';
@@ -150,6 +153,31 @@ function useKeyPress(t0) {
150
153
  }
151
154
  useEffect(t1, t2);
152
155
  }
156
+ function isFragment(trigger) {
157
+ return typeof trigger !== 'function' && /*#__PURE__*/isValidElement(trigger) && trigger.type === React.Fragment;
158
+ }
159
+ function renderTrigger(_ref3) {
160
+ let {
161
+ trigger,
162
+ triggerProps,
163
+ setTriggerRef,
164
+ hasRole
165
+ } = _ref3;
166
+ if (typeof trigger === 'function') {
167
+ return trigger({
168
+ ...triggerProps,
169
+ ref: setTriggerRef
170
+ });
171
+ }
172
+ return /*#__PURE__*/React.createElement("span", {
173
+ "data-test": "dropdown-trigger",
174
+ className: styles.triggerWrapper,
175
+ ref: setTriggerRef
176
+ }, hasRole && /*#__PURE__*/isValidElement(trigger) && !isFragment(trigger) ? /*#__PURE__*/cloneElement(trigger, triggerProps) : trigger);
177
+ }
178
+ const RETURN_FOCUS_OPTIONS = {
179
+ preventScroll: true
180
+ };
153
181
  const THEME_STYLES = {
154
182
  light: styles.themeLight,
155
183
  dark: styles.themeDark
@@ -159,7 +187,7 @@ const MODE_STYLES = {
159
187
  rock: styles.modeRock
160
188
  };
161
189
  const DropdownRender = (t0, externalRef) => {
162
- const $ = c(70);
190
+ const $ = c(57);
163
191
  const {
164
192
  children,
165
193
  trigger,
@@ -177,8 +205,15 @@ const DropdownRender = (t0, externalRef) => {
177
205
  closeOnEsc: t6,
178
206
  closeOnTab: t7,
179
207
  disableFocusLock: t8,
180
- renderInTrigger: t9,
181
- disableFlip: t10
208
+ modal: t9,
209
+ initialFocus: t10,
210
+ returnFocus: t11,
211
+ role,
212
+ visuallyHiddenDismiss: t12,
213
+ renderInTrigger: t13,
214
+ disableFlip: t14,
215
+ style: styleFromProps,
216
+ ...restProps
182
217
  } = t0;
183
218
  const isOpenFromProps = t1 === undefined ? false : t1;
184
219
  const placement = t2 === undefined ? "bottom-start" : t2;
@@ -188,8 +223,12 @@ const DropdownRender = (t0, externalRef) => {
188
223
  const closeOnEsc = t6 === undefined ? true : t6;
189
224
  const closeOnTab = t7 === undefined ? false : t7;
190
225
  const disableFocusLock = t8 === undefined ? false : t8;
191
- const renderInTrigger = t9 === undefined ? false : t9;
192
- const disableFlip = t10 === undefined ? false : t10;
226
+ const modal = t9 === undefined ? false : t9;
227
+ const initialFocus = t10 === undefined ? 0 : t10;
228
+ const returnFocus = t11 === undefined ? true : t11;
229
+ const visuallyHiddenDismiss = t12 === undefined ? false : t12;
230
+ const renderInTrigger = t13 === undefined ? false : t13;
231
+ const disableFlip = t14 === undefined ? false : t14;
193
232
  const [popperRef, setPopperRef] = useState(null);
194
233
  const [triggerRef, setTriggerRef] = useState(undefined);
195
234
  const {
@@ -199,19 +238,19 @@ const DropdownRender = (t0, externalRef) => {
199
238
  const scrollableRef = useRef(null);
200
239
  const isOpen = typeof triggerRef !== "undefined" || renderInTrigger ? isOpenFromProps : false;
201
240
  const ownerDocument = triggerRef?.ownerDocument;
202
- let t11;
241
+ let t15;
203
242
  if ($[0] !== popperRef || $[1] !== triggerRef) {
204
- t11 = [popperRef, triggerRef];
243
+ t15 = [popperRef, triggerRef];
205
244
  $[0] = popperRef;
206
245
  $[1] = triggerRef;
207
- $[2] = t11;
246
+ $[2] = t15;
208
247
  } else {
209
- t11 = $[2];
248
+ t15 = $[2];
210
249
  }
211
- const outsideClickIgnoreElements = t11;
212
- let t12;
250
+ const outsideClickIgnoreElements = t15;
251
+ let t16;
213
252
  if ($[3] !== isOpen || $[4] !== onRequestClose || $[5] !== outsideClickIgnoreElements || $[6] !== ownerDocument) {
214
- t12 = {
253
+ t16 = {
215
254
  isEnabled: isOpen,
216
255
  onOutsideClick: onRequestClose,
217
256
  ignoreElements: outsideClickIgnoreElements,
@@ -221,61 +260,61 @@ const DropdownRender = (t0, externalRef) => {
221
260
  $[4] = onRequestClose;
222
261
  $[5] = outsideClickIgnoreElements;
223
262
  $[6] = ownerDocument;
224
- $[7] = t12;
263
+ $[7] = t16;
225
264
  } else {
226
- t12 = $[7];
265
+ t16 = $[7];
227
266
  }
228
- useOnOutsideClick(t12);
229
- const t13 = isOpen && closeOnEsc;
230
- let t14;
231
- if ($[8] !== onRequestClose || $[9] !== ownerDocument || $[10] !== t13) {
232
- t14 = {
267
+ useOnOutsideClick(t16);
268
+ const t17 = isOpen && closeOnEsc;
269
+ let t18;
270
+ if ($[8] !== onRequestClose || $[9] !== ownerDocument || $[10] !== t17) {
271
+ t18 = {
233
272
  key: "Escape",
234
- isEnabled: t13,
273
+ isEnabled: t17,
235
274
  onKeyPress: onRequestClose,
236
275
  ownerDocument
237
276
  };
238
277
  $[8] = onRequestClose;
239
278
  $[9] = ownerDocument;
240
- $[10] = t13;
241
- $[11] = t14;
279
+ $[10] = t17;
280
+ $[11] = t18;
242
281
  } else {
243
- t14 = $[11];
282
+ t18 = $[11];
244
283
  }
245
- useKeyPress(t14);
246
- const t15 = isOpen && closeOnTab;
247
- let t16;
248
- if ($[12] !== onRequestClose || $[13] !== ownerDocument || $[14] !== t15) {
249
- t16 = {
284
+ useKeyPress(t18);
285
+ const t19 = isOpen && closeOnTab;
286
+ let t20;
287
+ if ($[12] !== onRequestClose || $[13] !== ownerDocument || $[14] !== t19) {
288
+ t20 = {
250
289
  key: "Tab",
251
- isEnabled: t15,
290
+ isEnabled: t19,
252
291
  onKeyPress: onRequestClose,
253
292
  ownerDocument
254
293
  };
255
294
  $[12] = onRequestClose;
256
295
  $[13] = ownerDocument;
257
- $[14] = t15;
258
- $[15] = t16;
296
+ $[14] = t19;
297
+ $[15] = t20;
259
298
  } else {
260
- t16 = $[15];
299
+ t20 = $[15];
261
300
  }
262
- useKeyPress(t16);
263
- let t17;
301
+ useKeyPress(t20);
302
+ let t21;
264
303
  if ($[16] !== popperRef || $[17] !== triggerRef) {
265
- t17 = {
304
+ t21 = {
266
305
  reference: triggerRef,
267
306
  floating: popperRef
268
307
  };
269
308
  $[16] = popperRef;
270
309
  $[17] = triggerRef;
271
- $[18] = t17;
310
+ $[18] = t21;
272
311
  } else {
273
- t17 = $[18];
312
+ t21 = $[18];
274
313
  }
275
- const t18 = isAutoPlacement(placement) ? undefined : placement;
276
- let t19;
314
+ const t22 = isAutoPlacement(placement) ? undefined : placement;
315
+ let t23;
277
316
  if ($[19] !== disableFlip || $[20] !== leastMaxHeight || $[21] !== maxHeight || $[22] !== maxWidth || $[23] !== minWidth || $[24] !== placement) {
278
- t19 = getMiddleware({
317
+ t23 = getMiddleware({
279
318
  placement,
280
319
  maxHeight,
281
320
  leastMaxHeight,
@@ -289,27 +328,27 @@ const DropdownRender = (t0, externalRef) => {
289
328
  $[22] = maxWidth;
290
329
  $[23] = minWidth;
291
330
  $[24] = placement;
292
- $[25] = t19;
331
+ $[25] = t23;
293
332
  } else {
294
- t19 = $[25];
333
+ t23 = $[25];
295
334
  }
296
- const t20 = popperRef !== null;
297
- let t21;
298
- if ($[26] !== t17 || $[27] !== t18 || $[28] !== t19 || $[29] !== t20) {
299
- t21 = {
300
- elements: t17,
301
- placement: t18,
302
- middleware: t19,
335
+ const t24 = popperRef !== null;
336
+ let t25;
337
+ if ($[26] !== t21 || $[27] !== t22 || $[28] !== t23 || $[29] !== t24) {
338
+ t25 = {
339
+ elements: t21,
340
+ placement: t22,
341
+ middleware: t23,
303
342
  whileElementsMounted: autoUpdateDropdown,
304
- open: t20
343
+ open: t24
305
344
  };
306
- $[26] = t17;
307
- $[27] = t18;
308
- $[28] = t19;
309
- $[29] = t20;
310
- $[30] = t21;
345
+ $[26] = t21;
346
+ $[27] = t22;
347
+ $[28] = t23;
348
+ $[29] = t24;
349
+ $[30] = t25;
311
350
  } else {
312
- t21 = $[30];
351
+ t25 = $[30];
313
352
  }
314
353
  const {
315
354
  floatingStyles,
@@ -317,11 +356,11 @@ const DropdownRender = (t0, externalRef) => {
317
356
  placement: floatingPlacement,
318
357
  isPositioned,
319
358
  update
320
- } = useFloating(t21);
359
+ } = useFloating(t25);
321
360
  const resolvedPlacement = isPositioned ? floatingPlacement : null;
322
- let t22;
361
+ let t26;
323
362
  if ($[31] !== cancelCloseAnimation || $[32] !== isOpen || $[33] !== popperRef || $[34] !== resolvedPlacement) {
324
- t22 = {
363
+ t26 = {
325
364
  isOpen,
326
365
  cancelCloseAnimation,
327
366
  contentRef: scrollableRef,
@@ -332,14 +371,14 @@ const DropdownRender = (t0, externalRef) => {
332
371
  $[32] = isOpen;
333
372
  $[33] = popperRef;
334
373
  $[34] = resolvedPlacement;
335
- $[35] = t22;
374
+ $[35] = t26;
336
375
  } else {
337
- t22 = $[35];
376
+ t26 = $[35];
338
377
  }
339
- const dropdownState = useDropdownState(t22);
340
- let t23;
378
+ const dropdownState = useDropdownState(t26);
379
+ let t27;
341
380
  if ($[36] !== popperRef || $[37] !== update) {
342
- t23 = () => ({
381
+ t27 = () => ({
343
382
  scheduleUpdate: () => {
344
383
  if (popperRef) {
345
384
  update();
@@ -348,29 +387,92 @@ const DropdownRender = (t0, externalRef) => {
348
387
  });
349
388
  $[36] = popperRef;
350
389
  $[37] = update;
351
- $[38] = t23;
390
+ $[38] = t27;
352
391
  } else {
353
- t23 = $[38];
392
+ t27 = $[38];
354
393
  }
355
- useImperativeHandle(externalRef, t23);
394
+ useImperativeHandle(externalRef, t27);
356
395
  const {
357
396
  dropdownZIndex
358
397
  } = useLayeringContext();
359
- let t24;
360
- if ($[39] !== trigger) {
361
- t24 = typeof trigger === "function" ? trigger({
362
- ref: setTriggerRef
363
- }) : /*#__PURE__*/React.createElement("span", {
364
- "data-test": "dropdown-trigger",
365
- className: styles.triggerWrapper,
366
- ref: setTriggerRef
367
- }, trigger);
368
- $[39] = trigger;
369
- $[40] = t24;
398
+ const isFocusLockActive = !disableFocusLock && (dropdownState === DROPDOWN_STATES.OPENING_PREPARE || dropdownState === DROPDOWN_STATES.OPENING || dropdownState === DROPDOWN_STATES.OPEN);
399
+ const isModal = (role === "alertdialog" || modal) && !disableFocusLock;
400
+ const {
401
+ triggerProps,
402
+ getPopupProps
403
+ } = useDropdownAria({
404
+ role,
405
+ isOpen,
406
+ isModal,
407
+ trigger,
408
+ id: restProps.id
409
+ });
410
+ useHideOthers(isModal && isFocusLockActive, popperRef);
411
+ const t28 = role !== undefined;
412
+ let t29;
413
+ if ($[39] !== t28 || $[40] !== trigger || $[41] !== triggerProps) {
414
+ t29 = renderTrigger({
415
+ trigger,
416
+ triggerProps,
417
+ setTriggerRef,
418
+ hasRole: t28
419
+ });
420
+ $[39] = t28;
421
+ $[40] = trigger;
422
+ $[41] = triggerProps;
423
+ $[42] = t29;
424
+ } else {
425
+ t29 = $[42];
426
+ }
427
+ const triggerRenderer = t29;
428
+ let t30;
429
+ if ($[43] !== initialFocus) {
430
+ t30 = lockElement => {
431
+ if (lockElement.contains(lockElement.ownerDocument.activeElement)) {
432
+ return;
433
+ }
434
+ getInitialFocusElement(lockElement, initialFocus)?.focus({
435
+ preventScroll: true
436
+ });
437
+ };
438
+ $[43] = initialFocus;
439
+ $[44] = t30;
370
440
  } else {
371
- t24 = $[40];
441
+ t30 = $[44];
372
442
  }
373
- const triggerRenderer = t24;
443
+ const handleFocusLockActivation = t30;
444
+ let t31;
445
+ if ($[45] !== triggerRef) {
446
+ t31 = returnTo => {
447
+ const triggerElement = returnTo === returnTo.ownerDocument.body ? getFirstTabbableElement(triggerRef) : null;
448
+ if (!triggerElement) {
449
+ return RETURN_FOCUS_OPTIONS;
450
+ }
451
+ Promise.resolve().then(() => triggerElement.focus(RETURN_FOCUS_OPTIONS));
452
+ return false;
453
+ };
454
+ $[45] = triggerRef;
455
+ $[46] = t31;
456
+ } else {
457
+ t31 = $[46];
458
+ }
459
+ const handleReturnFocus = t31;
460
+ let t32;
461
+ if ($[47] !== isModal || $[48] !== onRequestClose || $[49] !== visuallyHiddenDismiss) {
462
+ t32 = isModal && visuallyHiddenDismiss ? /*#__PURE__*/React.createElement("button", {
463
+ type: "button",
464
+ tabIndex: -1,
465
+ className: styles.visuallyHidden,
466
+ onClick: onRequestClose
467
+ }, typeof visuallyHiddenDismiss === "string" ? visuallyHiddenDismiss : "Dismiss") : null;
468
+ $[47] = isModal;
469
+ $[48] = onRequestClose;
470
+ $[49] = visuallyHiddenDismiss;
471
+ $[50] = t32;
472
+ } else {
473
+ t32 = $[50];
474
+ }
475
+ const dismissButton = t32;
374
476
  const dropdownShouldBeRendered = dropdownState !== DROPDOWN_STATES.CLOSE;
375
477
  const wrapperIsInitial = dropdownState === DROPDOWN_STATES.OPENING_PREPARE;
376
478
  const wrapperShouldBeHidden = wrapperIsInitial || dropdownState === DROPDOWN_STATES.CLOSING;
@@ -383,88 +485,66 @@ const DropdownRender = (t0, externalRef) => {
383
485
  noProvider
384
486
  } = useContext(ThemeContext);
385
487
  const theme = themeFromProps || (noProvider ? themeFromCss : themeFromContext);
386
- let t25;
387
- if ($[41] !== mode || $[42] !== theme) {
388
- t25 = mode === "rock" ? getInverseTheme(theme) : theme;
389
- $[41] = mode;
390
- $[42] = theme;
391
- $[43] = t25;
488
+ let t33;
489
+ if ($[51] !== mode || $[52] !== theme) {
490
+ t33 = mode === "rock" ? getInverseTheme(theme) : theme;
491
+ $[51] = mode;
492
+ $[52] = theme;
493
+ $[53] = t33;
392
494
  } else {
393
- t25 = $[43];
495
+ t33 = $[53];
394
496
  }
395
- const childrenTheme = t25;
396
- let t26;
397
- if ($[44] !== children || $[45] !== childrenTheme || $[46] !== className || $[47] !== disableFocusLock || $[48] !== dropdownShouldBeRendered || $[49] !== dropdownState || $[50] !== dropdownZIndex || $[51] !== floatingStyles || $[52] !== maxHeight || $[53] !== middlewareData || $[54] !== mode || $[55] !== noProvider || $[56] !== ownerDocument?.body || $[57] !== primaryColors || $[58] !== renderInTrigger || $[59] !== resolvedPlacement || $[60] !== scrollableStyles || $[61] !== setContentEl || $[62] !== theme || $[63] !== triggerRef || $[64] !== wrapperIsInitial || $[65] !== wrapperShouldBeHidden) {
398
- t26 = dropdownShouldBeRendered && /*#__PURE__*/React.createElement(Portal, {
399
- target: renderInTrigger ? triggerRef : ownerDocument?.body
400
- }, /*#__PURE__*/React.createElement("div", {
401
- ref: setPopperRef,
402
- style: {
403
- maxHeight: middlewareData.applyMaxSize?.maxHeight ?? maxHeight,
404
- ...floatingStyles,
405
- zIndex: dropdownZIndex,
406
- ...primaryColors
407
- },
408
- className: cn(styles.dropdown, className, MODE_STYLES[mode], THEME_STYLES[theme]),
409
- "data-test": "dropdown",
410
- "data-popper-placement": resolvedPlacement ?? undefined
411
- }, /*#__PURE__*/React.createElement(LayeringProvider, {
412
- baseLayer: "dropdown"
413
- }, /*#__PURE__*/React.createElement("div", {
414
- ref: scrollableRef,
415
- style: scrollableStyles,
416
- className: cn(styles.contentWrapper, wrapperIsInitial && styles.wrapperInitial, wrapperShouldBeHidden && styles.wrapperHidden),
417
- "data-test": "dropdown-content"
418
- }, /*#__PURE__*/React.createElement(ThemeProvider, {
419
- ignoreThemeValueForCSSApi: noProvider,
420
- theme: childrenTheme
421
- }, /*#__PURE__*/React.createElement(FocusLock, {
422
- className: THEME_STYLES[childrenTheme],
423
- ref: setContentEl,
424
- disabled: dropdownState === DROPDOWN_STATES.CLOSING || disableFocusLock,
425
- returnFocus: {
426
- preventScroll: true
427
- },
428
- focusOptions: {
429
- preventScroll: true
430
- }
431
- }, children))))));
432
- $[44] = children;
433
- $[45] = childrenTheme;
434
- $[46] = className;
435
- $[47] = disableFocusLock;
436
- $[48] = dropdownShouldBeRendered;
437
- $[49] = dropdownState;
438
- $[50] = dropdownZIndex;
439
- $[51] = floatingStyles;
440
- $[52] = maxHeight;
441
- $[53] = middlewareData;
442
- $[54] = mode;
443
- $[55] = noProvider;
444
- $[56] = ownerDocument?.body;
445
- $[57] = primaryColors;
446
- $[58] = renderInTrigger;
447
- $[59] = resolvedPlacement;
448
- $[60] = scrollableStyles;
449
- $[61] = setContentEl;
450
- $[62] = theme;
451
- $[63] = triggerRef;
452
- $[64] = wrapperIsInitial;
453
- $[65] = wrapperShouldBeHidden;
454
- $[66] = t26;
455
- } else {
456
- t26 = $[66];
457
- }
458
- let t27;
459
- if ($[67] !== t26 || $[68] !== triggerRenderer) {
460
- t27 = /*#__PURE__*/React.createElement(React.Fragment, null, triggerRenderer, t26);
461
- $[67] = t26;
462
- $[68] = triggerRenderer;
463
- $[69] = t27;
497
+ const childrenTheme = t33;
498
+ const t34 = dropdownShouldBeRendered && /*#__PURE__*/React.createElement(Portal, {
499
+ target: renderInTrigger ? triggerRef : ownerDocument?.body
500
+ }, /*#__PURE__*/React.createElement("div", {
501
+ ref: setPopperRef,
502
+ style: {
503
+ ...styleFromProps,
504
+ maxHeight: middlewareData.applyMaxSize?.maxHeight ?? maxHeight,
505
+ ...floatingStyles,
506
+ zIndex: dropdownZIndex,
507
+ ...primaryColors
508
+ },
509
+ className: cn(styles.dropdown, className, MODE_STYLES[mode], THEME_STYLES[theme]),
510
+ "data-test": "dropdown",
511
+ "data-popper-placement": resolvedPlacement ?? undefined
512
+ }, /*#__PURE__*/React.createElement(LayeringProvider, {
513
+ baseLayer: "dropdown"
514
+ }, /*#__PURE__*/React.createElement("div", {
515
+ ref: scrollableRef,
516
+ style: scrollableStyles,
517
+ className: cn(styles.contentWrapper, wrapperIsInitial && styles.wrapperInitial, wrapperShouldBeHidden && styles.wrapperHidden),
518
+ "data-test": "dropdown-content"
519
+ }, /*#__PURE__*/React.createElement(ThemeProvider, {
520
+ ignoreThemeValueForCSSApi: noProvider,
521
+ theme: childrenTheme
522
+ }, /*#__PURE__*/React.createElement(FocusLock, {
523
+ className: THEME_STYLES[childrenTheme],
524
+ ref: setContentEl,
525
+ disabled: !isFocusLockActive,
526
+ returnFocus: returnFocus && handleReturnFocus,
527
+ focusOptions: {
528
+ preventScroll: true
529
+ },
530
+ onActivation: handleFocusLockActivation,
531
+ lockProps: {
532
+ ...getPopupProps(restProps),
533
+ ...(initialFocus === "container" && {
534
+ tabIndex: -1
535
+ })
536
+ }
537
+ }, dismissButton, children, dismissButton))))));
538
+ let t35;
539
+ if ($[54] !== t34 || $[55] !== triggerRenderer) {
540
+ t35 = /*#__PURE__*/React.createElement(React.Fragment, null, triggerRenderer, t34);
541
+ $[54] = t34;
542
+ $[55] = triggerRenderer;
543
+ $[56] = t35;
464
544
  } else {
465
- t27 = $[69];
545
+ t35 = $[56];
466
546
  }
467
- return t27;
547
+ return t35;
468
548
  };
469
549
  /* eslint-enable react/prop-types */
470
550
  const Dropdown = /*#__PURE__*/React.forwardRef(DropdownRender);
@@ -479,6 +559,13 @@ Dropdown.propTypes = {
479
559
  maxWidth: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
480
560
  maxHeight: PropTypes.number,
481
561
  leastMaxHeight: PropTypes.number,
562
+ modal: PropTypes.bool,
563
+ initialFocus: PropTypes.oneOfType([PropTypes.number, PropTypes.oneOf(['container']), PropTypes.shape({
564
+ current: PropTypes.any
565
+ })]),
566
+ returnFocus: PropTypes.bool,
567
+ role: PropTypes.oneOf(['dialog', 'alertdialog', 'menu', 'listbox', 'grid', 'tree']),
568
+ visuallyHiddenDismiss: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
482
569
  renderInTrigger: PropTypes.bool,
483
570
  disableFlip: PropTypes.bool
484
571
  };
@@ -1,3 +1,3 @@
1
- var styles = {"themeLight":"_themeLight_111hzak_7","themeDark":"_themeDark_111hzak_10","dropdown":"_dropdown_111hzak_14","triggerWrapper":"_triggerWrapper_111hzak_21","contentWrapper":"_contentWrapper_111hzak_25","wrapperInitial":"_wrapperInitial_111hzak_41","wrapperHidden":"_wrapperHidden_111hzak_45","modeClassic":"_modeClassic_111hzak_65","modeRock":"_modeRock_111hzak_69"};
1
+ var styles = {"themeLight":"_themeLight_x3ltd_7","themeDark":"_themeDark_x3ltd_10","dropdown":"_dropdown_x3ltd_14","triggerWrapper":"_triggerWrapper_x3ltd_21","visuallyHidden":"_visuallyHidden_x3ltd_25","contentWrapper":"_contentWrapper_x3ltd_41","wrapperInitial":"_wrapperInitial_x3ltd_57","wrapperHidden":"_wrapperHidden_x3ltd_61","modeClassic":"_modeClassic_x3ltd_81","modeRock":"_modeRock_x3ltd_85"};
2
2
 
3
3
  export { styles as default };
@@ -0,0 +1,9 @@
1
+ import type { DropdownInitialFocus } from './types';
2
+ /** The element itself, if it's tabbable, or its first tabbable descendant */
3
+ export declare function getFirstTabbableElement(element: Element | null | undefined): HTMLElement | null;
4
+ /**
5
+ * The element to focus when the focus lock activates.
6
+ * `null` leaves the default behavior of the lock: it focuses the first tabbable element
7
+ * or the one with `data-autofocus`.
8
+ */
9
+ export declare function getInitialFocusElement(lockElement: HTMLElement, initialFocus: DropdownInitialFocus): HTMLElement | null;
@@ -0,0 +1,30 @@
1
+ const TABBABLE_SELECTOR = ['a[href]', 'area[href]', 'button:not([disabled])', 'input:not([disabled]):not([type="hidden"])', 'select:not([disabled])', 'textarea:not([disabled])', 'iframe', 'summary', 'audio[controls]', 'video[controls]', '[contenteditable]:not([contenteditable="false"])', '[tabindex]'].join(', ');
2
+ function isTabbable(element) {
3
+ return element.matches(TABBABLE_SELECTOR) && element.tabIndex >= 0 && element.getClientRects().length > 0 && !element.closest('[inert], fieldset[disabled]');
4
+ }
5
+ function getTabbableElements(container) {
6
+ return Array.from(container.querySelectorAll(TABBABLE_SELECTOR)).filter(isTabbable);
7
+ }
8
+ /** The element itself, if it's tabbable, or its first tabbable descendant */
9
+ function getFirstTabbableElement(element) {
10
+ if (!element) {
11
+ return null;
12
+ }
13
+ return isTabbable(element) ? element : getTabbableElements(element)[0] ?? null;
14
+ }
15
+ /**
16
+ * The element to focus when the focus lock activates.
17
+ * `null` leaves the default behavior of the lock: it focuses the first tabbable element
18
+ * or the one with `data-autofocus`.
19
+ */
20
+ function getInitialFocusElement(lockElement, initialFocus) {
21
+ if (initialFocus === 'container') {
22
+ return lockElement;
23
+ }
24
+ if (typeof initialFocus === 'number') {
25
+ return initialFocus === 0 ? null : getTabbableElements(lockElement)[initialFocus] ?? null;
26
+ }
27
+ return initialFocus.current;
28
+ }
29
+
30
+ export { getFirstTabbableElement, getInitialFocusElement };
package/lib/index.css CHANGED
@@ -4,14 +4,14 @@
4
4
  initial-value:0;
5
5
  }
6
6
 
7
- ._themeLight_111hzak_7{
7
+ ._themeLight_x3ltd_7{
8
8
  --rs-theme-dark:0;
9
9
  }
10
- ._themeDark_111hzak_10{
10
+ ._themeDark_x3ltd_10{
11
11
  --rs-theme-dark:1;
12
12
  }
13
13
 
14
- ._dropdown_111hzak_14{
14
+ ._dropdown_x3ltd_14{
15
15
  --_rs-theme-dark:var(
16
16
  --_rs-internal-force-theme-dark-consult-rescui-before-using,
17
17
  var(--rs-theme-dark, 0)
@@ -23,7 +23,7 @@
23
23
 
24
24
  @supports (color: color-mix(in srgb, #fff 50%, #000 50%)){
25
25
 
26
- ._dropdown_111hzak_14{
26
+ ._dropdown_x3ltd_14{
27
27
  --_rs-color-primary:color-mix(in srgb, var(--rs-color-primary-light-theme, #6B57FF) calc(100% * var(--_rs-theme-light-coefficient, 1)), var(--rs-color-primary-dark-theme, #8473FF) calc(100% * var(--_rs-theme-dark-coefficient, 0)));
28
28
  --_rs-color-primary-dim:color-mix(in srgb, var(--rs-color-primary-dim-light-theme, #8979ff) calc(100% * var(--_rs-theme-light-coefficient, 1)), var(--rs-color-primary-dim-dark-theme, #6f61d2) calc(100% * var(--_rs-theme-dark-coefficient, 0)));
29
29
  --_rs-color-primary-fog:color-mix(in srgb, var(--rs-color-primary-fog-light-theme, #e1ddff) calc(100% * var(--_rs-theme-light-coefficient, 1)), var(--rs-color-primary-fog-dark-theme, #2e2b49) calc(100% * var(--_rs-theme-dark-coefficient, 0)));
@@ -34,7 +34,7 @@
34
34
 
35
35
  @supports not (color: color-mix(in srgb, #fff 50%, #000 50%)){
36
36
 
37
- ._dropdown_111hzak_14{
37
+ ._dropdown_x3ltd_14{
38
38
  --_rs-color-primary:rgb(calc(107 * var(--_rs-theme-light-coefficient, 1) + 132 * var(--_rs-theme-dark-coefficient, 0)), calc(87 * var(--_rs-theme-light-coefficient, 1) + 115 * var(--_rs-theme-dark-coefficient, 0)), 255);
39
39
  --_rs-color-primary-dim:rgb(calc(137 * var(--_rs-theme-light-coefficient, 1) + 111 * var(--_rs-theme-dark-coefficient, 0)), calc(121 * var(--_rs-theme-light-coefficient, 1) + 97 * var(--_rs-theme-dark-coefficient, 0)), calc(255 * var(--_rs-theme-light-coefficient, 1) + 210 * var(--_rs-theme-dark-coefficient, 0)));
40
40
  --_rs-color-primary-fog:rgb(calc(225 * var(--_rs-theme-light-coefficient, 1) + 46 * var(--_rs-theme-dark-coefficient, 0)), calc(221 * var(--_rs-theme-light-coefficient, 1) + 43 * var(--_rs-theme-dark-coefficient, 0)), calc(255 * var(--_rs-theme-light-coefficient, 1) + 73 * var(--_rs-theme-dark-coefficient, 0)));
@@ -43,16 +43,32 @@
43
43
  }
44
44
  }
45
45
 
46
- ._dropdown_111hzak_14 {
46
+ ._dropdown_x3ltd_14 {
47
47
  display:flex;
48
48
  flex-direction:column;
49
49
  }
50
50
 
51
- ._triggerWrapper_111hzak_21{
51
+ ._triggerWrapper_x3ltd_21{
52
52
  display:inline-block;
53
53
  }
54
54
 
55
- ._contentWrapper_111hzak_25{
55
+ ._visuallyHidden_x3ltd_25{
56
+ position:absolute;
57
+
58
+ overflow:hidden;
59
+
60
+ width:1px;
61
+ height:1px;
62
+ margin:-1px;
63
+ padding:0;
64
+ border:0;
65
+
66
+ white-space:nowrap;
67
+
68
+ clip-path:inset(50%);
69
+ }
70
+
71
+ ._contentWrapper_x3ltd_41{
56
72
  position:relative;
57
73
 
58
74
  display:block;
@@ -67,42 +83,42 @@
67
83
  transition:opacity var(--rs-transition-function, ease) 200ms, transform var(--rs-transition-function, ease) 200ms;
68
84
  }
69
85
 
70
- ._wrapperInitial_111hzak_41{
86
+ ._wrapperInitial_x3ltd_57{
71
87
  transition:none;
72
88
  }
73
89
 
74
- ._wrapperHidden_111hzak_45{
90
+ ._wrapperHidden_x3ltd_61{
75
91
  opacity:0;
76
92
  }
77
93
 
78
- [data-popper-placement^='top'] ._wrapperHidden_111hzak_45{
94
+ [data-popper-placement^='top'] ._wrapperHidden_x3ltd_61{
79
95
  transform:translateY(10px);
80
96
  }
81
97
 
82
- [data-popper-placement^='bottom'] ._wrapperHidden_111hzak_45{
98
+ [data-popper-placement^='bottom'] ._wrapperHidden_x3ltd_61{
83
99
  transform:translateY(-10px);
84
100
  }
85
101
 
86
- [data-popper-placement^='right'] ._wrapperHidden_111hzak_45{
102
+ [data-popper-placement^='right'] ._wrapperHidden_x3ltd_61{
87
103
  transform:translateX(-10px);
88
104
  }
89
105
 
90
- [data-popper-placement^='left'] ._wrapperHidden_111hzak_45{
106
+ [data-popper-placement^='left'] ._wrapperHidden_x3ltd_61{
91
107
  transform:translateX(10px);
92
108
  }
93
109
 
94
- ._modeClassic_111hzak_65 ._contentWrapper_111hzak_25{
110
+ ._modeClassic_x3ltd_81 ._contentWrapper_x3ltd_41{
95
111
  background-color:rgb(calc(255 - 230 * var(--_rs-theme-dark-coefficient, 0)), calc(255 - 230 * var(--_rs-theme-dark-coefficient, 0)), calc(255 - 227 * var(--_rs-theme-dark-coefficient, 0)));
96
112
  }
97
113
 
98
- ._modeRock_111hzak_69 ._contentWrapper_111hzak_25{
114
+ ._modeRock_x3ltd_85 ._contentWrapper_x3ltd_41{
99
115
  background-color:rgb(calc(25 + 230 * var(--_rs-theme-dark-coefficient, 0)), calc(25 + 230 * var(--_rs-theme-dark-coefficient, 0)), calc(28 + 227 * var(--_rs-theme-dark-coefficient, 0)));
100
116
  }
101
117
 
102
- ._modeClassic_111hzak_65._themeDark_111hzak_10 ._contentWrapper_111hzak_25, ._modeRock_111hzak_69._themeLight_111hzak_7 ._contentWrapper_111hzak_25{
118
+ ._modeClassic_x3ltd_81._themeDark_x3ltd_10 ._contentWrapper_x3ltd_41, ._modeRock_x3ltd_85._themeLight_x3ltd_7 ._contentWrapper_x3ltd_41{
103
119
  color-scheme:dark;
104
120
  }
105
- ._modeRock_111hzak_69{
121
+ ._modeRock_x3ltd_85{
106
122
  --rs-parent-color-primary-light-theme:var(--rs-color-primary-light-theme);
107
123
  --rs-parent-color-primary-dim-light-theme:var(
108
124
  --rs-color-primary-dim-light-theme
@@ -132,7 +148,7 @@
132
148
  );
133
149
  }
134
150
 
135
- ._modeRock_111hzak_69 > *{
151
+ ._modeRock_x3ltd_85 > *{
136
152
  --rs-color-primary-dark-theme:var(--rs-parent-color-primary-light-theme);
137
153
  --rs-color-primary-dim-dark-theme:var(
138
154
  --rs-parent-color-primary-dim-light-theme
package/lib/index.d.ts CHANGED
@@ -4,4 +4,4 @@
4
4
  declare const DefaultDropdown: import("react").ForwardRefExoticComponent<import("./dropdown").DropdownProps & import("react").RefAttributes<import("./dropdown").DropdownImperativeHandle>>;
5
5
  export default DefaultDropdown;
6
6
  export { Dropdown } from './dropdown';
7
- export type { DropdownProps, DropdownImperativeHandle } from './dropdown';
7
+ export type { DropdownProps, DropdownImperativeHandle, DropdownTrigger, DropdownTriggerProps, DropdownRole, DropdownInitialFocus } from './dropdown';
package/lib/types.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import type React from 'react';
2
+ import type { JSX, RefObject } from 'react';
3
+ export type DropdownRole = 'dialog' | 'alertdialog' | 'menu' | 'listbox' | 'grid' | 'tree';
4
+ export type DropdownInitialFocus = number | RefObject<HTMLElement | null> | 'container';
5
+ export type DropdownTriggerAriaProps = Pick<React.HTMLAttributes<HTMLElement>, 'id' | 'role' | 'aria-haspopup' | 'aria-expanded' | 'aria-controls'>;
6
+ export interface DropdownTriggerProps extends DropdownTriggerAriaProps {
7
+ ref: React.Ref<any>;
8
+ }
9
+ export type DropdownTrigger = ((props: DropdownTriggerProps) => JSX.Element) | JSX.Element;
@@ -0,0 +1,19 @@
1
+ import React from 'react';
2
+ import type { DropdownRole, DropdownTrigger, DropdownTriggerAriaProps } from './types';
3
+ interface UseDropdownAriaOptions {
4
+ role?: DropdownRole;
5
+ isOpen: boolean;
6
+ isModal: boolean;
7
+ trigger: DropdownTrigger;
8
+ /** `id` passed to Dropdown, it's used instead of a generated one */
9
+ id?: string;
10
+ }
11
+ /**
12
+ * ARIA attributes of the trigger and the popup element for `role`.
13
+ * Without `role`, the trigger gets no attributes.
14
+ */
15
+ export declare function useDropdownAria({ role, isOpen, isModal, trigger, id }: UseDropdownAriaOptions): {
16
+ triggerProps: DropdownTriggerAriaProps;
17
+ getPopupProps: (userProps: React.HTMLAttributes<HTMLDivElement>) => React.HTMLAttributes<HTMLDivElement>;
18
+ };
19
+ export {};
@@ -0,0 +1,121 @@
1
+ "use client";
2
+ import { c } from 'react-compiler-runtime';
3
+ import React, { isValidElement, useState } from 'react';
4
+
5
+ // React 17 has no `useId`. The generated ids are rendered only while the dropdown is open,
6
+ // and it opens only on the client, so a counter doesn't break hydration.
7
+ const useReactId = React.useId ?? (() => undefined);
8
+ let idCounter = 0;
9
+ function useGeneratedId(prefix) {
10
+ const $ = c(6);
11
+ const reactId = useReactId();
12
+ let t0;
13
+ if ($[0] !== prefix) {
14
+ t0 = () => {
15
+ idCounter = idCounter + 1;
16
+ return `${prefix}-${idCounter}`;
17
+ };
18
+ $[0] = prefix;
19
+ $[1] = t0;
20
+ } else {
21
+ t0 = $[1];
22
+ }
23
+ const [fallbackId] = useState(t0);
24
+ let t1;
25
+ if ($[2] !== fallbackId || $[3] !== prefix || $[4] !== reactId) {
26
+ t1 = reactId ? `${prefix}-${reactId.replace(/:/g, "")}` : fallbackId;
27
+ $[2] = fallbackId;
28
+ $[3] = prefix;
29
+ $[4] = reactId;
30
+ $[5] = t1;
31
+ } else {
32
+ t1 = $[5];
33
+ }
34
+ return t1;
35
+ }
36
+ const ARIA_HASPOPUP = {
37
+ dialog: 'dialog',
38
+ alertdialog: 'dialog',
39
+ menu: 'menu',
40
+ listbox: 'listbox',
41
+ grid: 'grid',
42
+ tree: 'tree'
43
+ };
44
+ /**
45
+ * ARIA attributes of the trigger and the popup element for `role`.
46
+ * Without `role`, the trigger gets no attributes.
47
+ */
48
+ function useDropdownAria(t0) {
49
+ const $ = c(14);
50
+ const {
51
+ role,
52
+ isOpen,
53
+ isModal,
54
+ trigger,
55
+ id
56
+ } = t0;
57
+ const generatedPopupId = useGeneratedId("rs-dropdown");
58
+ const generatedTriggerId = useGeneratedId("rs-dropdown-trigger");
59
+ const popupId = id ?? generatedPopupId;
60
+ const elementTriggerId = /*#__PURE__*/isValidElement(trigger) ? trigger.props.id : undefined;
61
+ const menuTriggerId = elementTriggerId ?? generatedTriggerId;
62
+ let t1;
63
+ if ($[0] !== isOpen || $[1] !== menuTriggerId || $[2] !== popupId || $[3] !== role) {
64
+ t1 = role === undefined ? {} : {
65
+ "aria-haspopup": ARIA_HASPOPUP[role],
66
+ "aria-expanded": isOpen,
67
+ "aria-controls": isOpen ? popupId : undefined,
68
+ ...(role === "listbox" && {
69
+ role: "combobox"
70
+ }),
71
+ ...(role === "menu" && isOpen && {
72
+ id: menuTriggerId
73
+ })
74
+ };
75
+ $[0] = isOpen;
76
+ $[1] = menuTriggerId;
77
+ $[2] = popupId;
78
+ $[3] = role;
79
+ $[4] = t1;
80
+ } else {
81
+ t1 = $[4];
82
+ }
83
+ const triggerProps = t1;
84
+ const isDialog = role === "dialog" || role === "alertdialog";
85
+ let t2;
86
+ if ($[5] !== isDialog || $[6] !== isModal || $[7] !== menuTriggerId || $[8] !== popupId || $[9] !== role) {
87
+ t2 = userProps => ({
88
+ ...(role === "menu" && !userProps["aria-label"] && {
89
+ "aria-labelledby": menuTriggerId
90
+ }),
91
+ ...userProps,
92
+ id: role === undefined ? userProps.id : popupId,
93
+ role,
94
+ "aria-modal": isDialog && isModal ? "true" : undefined
95
+ });
96
+ $[5] = isDialog;
97
+ $[6] = isModal;
98
+ $[7] = menuTriggerId;
99
+ $[8] = popupId;
100
+ $[9] = role;
101
+ $[10] = t2;
102
+ } else {
103
+ t2 = $[10];
104
+ }
105
+ const getPopupProps = t2;
106
+ let t3;
107
+ if ($[11] !== getPopupProps || $[12] !== triggerProps) {
108
+ t3 = {
109
+ triggerProps,
110
+ getPopupProps
111
+ };
112
+ $[11] = getPopupProps;
113
+ $[12] = triggerProps;
114
+ $[13] = t3;
115
+ } else {
116
+ t3 = $[13];
117
+ }
118
+ return t3;
119
+ }
120
+
121
+ export { useDropdownAria };
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Hides everything except the element from screen readers with `aria-hidden`, like a modal dialog does.
3
+ * Live regions stay visible, so their announcements are still heard.
4
+ * Elements that already have `aria-hidden` keep it after the dropdown closes.
5
+ */
6
+ export declare function useHideOthers(isEnabled: boolean, element: HTMLElement | null): void;
@@ -0,0 +1,36 @@
1
+ "use client";
2
+ import { c } from 'react-compiler-runtime';
3
+ import { useEffect } from 'react';
4
+ import { hideOthers } from 'aria-hidden';
5
+
6
+ const LIVE_REGIONS_SELECTOR = '[aria-live], [role="status"], [role="alert"], output';
7
+ /**
8
+ * Hides everything except the element from screen readers with `aria-hidden`, like a modal dialog does.
9
+ * Live regions stay visible, so their announcements are still heard.
10
+ * Elements that already have `aria-hidden` keep it after the dropdown closes.
11
+ */
12
+ function useHideOthers(isEnabled, element) {
13
+ const $ = c(4);
14
+ let t0;
15
+ let t1;
16
+ if ($[0] !== element || $[1] !== isEnabled) {
17
+ t0 = () => {
18
+ if (!isEnabled || !element) {
19
+ return;
20
+ }
21
+ const liveRegions = Array.from(element.ownerDocument.querySelectorAll(LIVE_REGIONS_SELECTOR));
22
+ return hideOthers([element, ...liveRegions]);
23
+ };
24
+ t1 = [element, isEnabled];
25
+ $[0] = element;
26
+ $[1] = isEnabled;
27
+ $[2] = t0;
28
+ $[3] = t1;
29
+ } else {
30
+ t0 = $[2];
31
+ t1 = $[3];
32
+ }
33
+ useEffect(t0, t1);
34
+ }
35
+
36
+ export { useHideOthers };
@@ -70,3 +70,62 @@ const Demo = () => {
70
70
 
71
71
  render(<Demo />);
72
72
  ```
73
+
74
+ ### Focus management
75
+
76
+ When the dropdown opens, it moves the focus to the first tabbable element inside, or to the element with the `data-autofocus` attribute. If there is nothing to focus, the focus stays where it was. While the dropdown is open, Tab and Shift+Tab move the focus only between its elements. When it closes, on `Esc`, on a click outside or, with `closeOnTab`, on Tab, the focus returns to the element that had it before the dropdown opened, usually the trigger. If nothing had the focus, for example, because Safari doesn't focus a button on click, the focus returns to the trigger.
77
+
78
+ To change this behavior:
79
+
80
+ - `initialFocus` picks the element that gets the focus on open: an index of a tabbable element, a ref to an element inside the dropdown, or `'container'` for the dropdown itself. `'container'` helps when the first tabbable element reacts to the focus, like a native select on iOS, which opens its picker;
81
+ - `returnFocus={false}` leaves the focus where it is on close;
82
+ - `disableFocusLock` turns the focus management off, for example, when the focus has to stay in the trigger, like in a combobox.
83
+
84
+ ### Accessibility
85
+
86
+ Without `role`, the dropdown doesn't set ARIA attributes, so the trigger and the content have to describe themselves. With `role`, the dropdown sets `role` and `id` on the element that wraps its children and `aria-haspopup`, `aria-expanded` and `aria-controls` on the trigger. A function trigger gets these attributes in its props, spread them onto the element. An element trigger gets them through `cloneElement`, so its component has to pass them to the DOM element; RescUI components do. `DropdownMenu` and `Select` set the roles of their lists themselves.
87
+
88
+ - `role="dialog"` or `role="alertdialog"` is for a popup with any content. Name it with `aria-label` or `aria-labelledby`, and describe it with `aria-describedby` if needed: these and other unknown props go to the element with the role. `className` and `style` go to the outer element that positions the dropdown.
89
+ - `modal` hides the rest of the page from screen readers with `aria-hidden` while the dropdown is open, and a dialog gets `aria-modal="true"`. Use it when the dropdown behaves like a modal dialog. `role="alertdialog"` is always modal.
90
+ - `visuallyHiddenDismiss` adds visually hidden buttons that close a modal dropdown. Touch screen readers have no `Esc` key, so use it when the dropdown has no visible close button.
91
+
92
+ ```jsx
93
+ import { useId, useState } from 'react';
94
+ import { Dropdown } from '@rescui/dropdown';
95
+ import { Button } from '@rescui/button';
96
+ import { Input } from '@rescui/input';
97
+ import { textCn } from '@rescui/typography';
98
+
99
+ const Demo = () => {
100
+ const [isOpen, setIsOpen] = useState(false);
101
+ const titleId = useId();
102
+
103
+ return (
104
+ <Dropdown
105
+ isOpen={isOpen}
106
+ onRequestClose={() => setIsOpen(false)}
107
+ role="dialog"
108
+ modal
109
+ aria-labelledby={titleId}
110
+ trigger={
111
+ <Button onClick={() => setIsOpen(open => !open)}>Subscribe</Button>
112
+ }
113
+ >
114
+ <div style={{ padding: 16 }}>
115
+ <h3 id={titleId} className={textCn('rs-h4')}>
116
+ Subscribe to the newsletter
117
+ </h3>
118
+ <Input className="rs-docs-offset-top-12" label="Email" type="email" />
119
+ <Button
120
+ className="rs-docs-offset-top-12"
121
+ onClick={() => setIsOpen(false)}
122
+ >
123
+ Subscribe
124
+ </Button>
125
+ </div>
126
+ </Dropdown>
127
+ );
128
+ };
129
+
130
+ render(<Demo />);
131
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rescui/dropdown",
3
- "version": "0.14.0",
3
+ "version": "0.15.1",
4
4
  "description": "",
5
5
  "license": "Apache-2.0",
6
6
  "author": "JetBrains",
@@ -18,6 +18,7 @@
18
18
  "dependencies": {
19
19
  "@floating-ui/react-dom": "^2.1.9",
20
20
  "@rescui/use-resize-observer": "^0.5.0",
21
+ "aria-hidden": "^1.2.6",
21
22
  "classnames": "^2.2.6",
22
23
  "react-compiler-runtime": "^1.0.0",
23
24
  "react-focus-lock": "^2.8.1"
@@ -39,5 +40,5 @@
39
40
  "build": "rescui-scripts build"
40
41
  },
41
42
  "nx": {},
42
- "gitHead": "d6b7459741eae7931b60e9145216fd334fbb0b80"
43
+ "gitHead": "edc876dc0d9672bd1653525e3a9569ce623d1e47"
43
44
  }