@rescui/dropdown 0.13.4 → 0.15.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/lib/auto-update.d.ts +7 -0
- package/lib/auto-update.js +54 -0
- package/lib/custom-middleware.d.ts +23 -0
- package/lib/custom-middleware.js +151 -0
- package/lib/dropdown.d.ts +51 -7
- package/lib/dropdown.js +299 -187
- package/lib/dropdown.p.module.css.js +1 -1
- package/lib/focus-utils.d.ts +9 -0
- package/lib/focus-utils.js +30 -0
- package/lib/index.css +35 -19
- package/lib/index.d.ts +1 -1
- package/lib/types.d.ts +9 -0
- package/lib/use-dropdown-aria.d.ts +19 -0
- package/lib/use-dropdown-aria.js +121 -0
- package/lib/use-hide-others.d.ts +6 -0
- package/lib/use-hide-others.js +36 -0
- package/llm/components/dropdown.md +59 -0
- package/package.json +5 -6
- package/lib/custom-modifiers.d.ts +0 -12
- package/lib/custom-modifiers.js +0 -91
|
@@ -0,0 +1,7 @@
|
|
|
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".
|
|
6
|
+
*/
|
|
7
|
+
export declare function autoUpdateDropdown(reference: Element, floating: HTMLElement, update: () => void): () => void;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { autoUpdate } from '@floating-ui/react-dom';
|
|
2
|
+
|
|
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".
|
|
8
|
+
*/
|
|
9
|
+
function autoUpdateDropdown(reference, floating, update) {
|
|
10
|
+
// Without ResizeObserver (e.g. in JSDOM), `autoUpdate()` skips element resize updates too
|
|
11
|
+
if (typeof ResizeObserver !== 'function') {
|
|
12
|
+
return autoUpdate(reference, floating, update);
|
|
13
|
+
}
|
|
14
|
+
const cleanupAutoUpdate = autoUpdate(reference, floating, update, {
|
|
15
|
+
elementResize: false
|
|
16
|
+
});
|
|
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);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
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
|
+
});
|
|
44
|
+
resizeObserver.observe(reference);
|
|
45
|
+
resizeObserver.observe(floating);
|
|
46
|
+
return () => {
|
|
47
|
+
cleanupAutoUpdate();
|
|
48
|
+
resizeObserver.disconnect();
|
|
49
|
+
cancelAnimationFrame(updateFrame);
|
|
50
|
+
cancelAnimationFrame(reobserveFrame);
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export { autoUpdateDropdown };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Middleware, Placement } from '@floating-ui/react-dom';
|
|
2
|
+
import { Property } from 'csstype';
|
|
3
|
+
export type DropdownMaxWidth = 'trigger' | Property.MaxWidth;
|
|
4
|
+
export type DropdownMinWidth = 'trigger' | Property.MinWidth;
|
|
5
|
+
type AutoPlacement = 'auto' | 'auto-start' | 'auto-end';
|
|
6
|
+
export type DropdownPlacement = Placement | AutoPlacement;
|
|
7
|
+
export declare const isAutoPlacement: (placement: DropdownPlacement) => placement is AutoPlacement;
|
|
8
|
+
/**
|
|
9
|
+
* Candidates for `autoPlacement()`, in the order Popper.js used to try them when
|
|
10
|
+
* several placements have the same space: `auto-start` and `auto-end` both try
|
|
11
|
+
* all aligned placements, `-start` first.
|
|
12
|
+
*/
|
|
13
|
+
export declare const AUTO_PLACEMENTS: Record<AutoPlacement, Placement[]>;
|
|
14
|
+
export declare const sameWidth: ({ maxWidth, minWidth }: {
|
|
15
|
+
maxWidth?: DropdownMaxWidth;
|
|
16
|
+
minWidth?: DropdownMinWidth;
|
|
17
|
+
}) => Middleware;
|
|
18
|
+
export declare const applyMaxSize: ({ leastMaxHeight, maxHeight, flip }: {
|
|
19
|
+
leastMaxHeight?: number;
|
|
20
|
+
maxHeight?: number;
|
|
21
|
+
flip: boolean;
|
|
22
|
+
}) => Middleware;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { detectOverflow } from '@floating-ui/react-dom';
|
|
2
|
+
|
|
3
|
+
const isAutoPlacement = placement => placement.startsWith('auto');
|
|
4
|
+
const ALIGNED_PLACEMENTS = ['top-start', 'top-end', 'bottom-start', 'bottom-end', 'right-start', 'right-end', 'left-start', 'left-end'];
|
|
5
|
+
/**
|
|
6
|
+
* Candidates for `autoPlacement()`, in the order Popper.js used to try them when
|
|
7
|
+
* several placements have the same space: `auto-start` and `auto-end` both try
|
|
8
|
+
* all aligned placements, `-start` first.
|
|
9
|
+
*/
|
|
10
|
+
const AUTO_PLACEMENTS = {
|
|
11
|
+
auto: ['top', 'bottom', 'right', 'left'],
|
|
12
|
+
'auto-start': ALIGNED_PLACEMENTS,
|
|
13
|
+
'auto-end': ALIGNED_PLACEMENTS
|
|
14
|
+
};
|
|
15
|
+
const OPPOSITE_SIDES = {
|
|
16
|
+
top: 'bottom',
|
|
17
|
+
bottom: 'top'
|
|
18
|
+
};
|
|
19
|
+
function getMaxWidth(dropdownMaxWidth, referenceWidth) {
|
|
20
|
+
if (dropdownMaxWidth === 'trigger') {
|
|
21
|
+
return `${referenceWidth}px`;
|
|
22
|
+
} else if (typeof dropdownMaxWidth === 'number') {
|
|
23
|
+
return `${dropdownMaxWidth}px`;
|
|
24
|
+
} else if (dropdownMaxWidth) {
|
|
25
|
+
return dropdownMaxWidth;
|
|
26
|
+
}
|
|
27
|
+
return ''; //rely on CSS by default
|
|
28
|
+
}
|
|
29
|
+
function getMinWidth(dropdownMinWidth, referenceWidth) {
|
|
30
|
+
if (dropdownMinWidth === 'trigger') {
|
|
31
|
+
return `${referenceWidth}px`;
|
|
32
|
+
}
|
|
33
|
+
if (typeof dropdownMinWidth === 'number') {
|
|
34
|
+
return `${dropdownMinWidth}px`;
|
|
35
|
+
} else if (dropdownMinWidth) {
|
|
36
|
+
return dropdownMinWidth;
|
|
37
|
+
}
|
|
38
|
+
return ''; //rely on CSS by default
|
|
39
|
+
}
|
|
40
|
+
const sameWidth = _ref => {
|
|
41
|
+
let {
|
|
42
|
+
maxWidth,
|
|
43
|
+
minWidth
|
|
44
|
+
} = _ref;
|
|
45
|
+
return {
|
|
46
|
+
name: 'sameWidth',
|
|
47
|
+
options: {
|
|
48
|
+
maxWidth,
|
|
49
|
+
minWidth
|
|
50
|
+
},
|
|
51
|
+
async fn(_ref2) {
|
|
52
|
+
let {
|
|
53
|
+
elements,
|
|
54
|
+
platform,
|
|
55
|
+
rects
|
|
56
|
+
} = _ref2;
|
|
57
|
+
// Apply the widths right away, so the next middleware get the final size of the dropdown
|
|
58
|
+
elements.floating.style.minWidth = getMinWidth(minWidth, rects.reference.width);
|
|
59
|
+
elements.floating.style.maxWidth = getMaxWidth(maxWidth, rects.reference.width);
|
|
60
|
+
const {
|
|
61
|
+
width
|
|
62
|
+
} = await platform.getDimensions(elements.floating);
|
|
63
|
+
return width === rects.floating.width ? {} : {
|
|
64
|
+
reset: {
|
|
65
|
+
rects: true
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
const applyMaxSize = _ref3 => {
|
|
72
|
+
let {
|
|
73
|
+
leastMaxHeight = 0,
|
|
74
|
+
maxHeight = Infinity,
|
|
75
|
+
flip
|
|
76
|
+
} = _ref3;
|
|
77
|
+
return {
|
|
78
|
+
name: 'applyMaxSize',
|
|
79
|
+
options: {
|
|
80
|
+
leastMaxHeight,
|
|
81
|
+
maxHeight,
|
|
82
|
+
flip
|
|
83
|
+
},
|
|
84
|
+
async fn(state) {
|
|
85
|
+
const {
|
|
86
|
+
placement,
|
|
87
|
+
initialPlacement,
|
|
88
|
+
rects,
|
|
89
|
+
middlewareData,
|
|
90
|
+
elements,
|
|
91
|
+
platform
|
|
92
|
+
} = state;
|
|
93
|
+
const data = middlewareData.applyMaxSize ?? {};
|
|
94
|
+
const measuredHeight = data.measuredHeight ?? rects.floating.height;
|
|
95
|
+
const [side, alignment] = placement.split('-');
|
|
96
|
+
const overflow = await detectOverflow(state);
|
|
97
|
+
const heightLeft = rects.floating.height - overflow[side === 'top' ? 'top' : 'bottom'];
|
|
98
|
+
if (flip && !data.flipped && placement === initialPlacement && OPPOSITE_SIDES[side] && heightLeft < leastMaxHeight && measuredHeight >= leastMaxHeight) {
|
|
99
|
+
// Even `leastMaxHeight` doesn't fit: check if it fits on the opposite side
|
|
100
|
+
return {
|
|
101
|
+
data: {
|
|
102
|
+
measuredHeight,
|
|
103
|
+
flipped: true
|
|
104
|
+
},
|
|
105
|
+
reset: {
|
|
106
|
+
placement: [OPPOSITE_SIDES[side], alignment].filter(Boolean).join('-'),
|
|
107
|
+
rects: {
|
|
108
|
+
...rects,
|
|
109
|
+
floating: {
|
|
110
|
+
...rects.floating,
|
|
111
|
+
height: leastMaxHeight
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
let resultMaxHeight;
|
|
118
|
+
if (maxHeight > heightLeft && heightLeft > leastMaxHeight) {
|
|
119
|
+
resultMaxHeight = heightLeft;
|
|
120
|
+
} else if (heightLeft < leastMaxHeight) {
|
|
121
|
+
resultMaxHeight = leastMaxHeight;
|
|
122
|
+
} else {
|
|
123
|
+
resultMaxHeight = maxHeight;
|
|
124
|
+
}
|
|
125
|
+
const resultData = {
|
|
126
|
+
measuredHeight,
|
|
127
|
+
maxHeight: resultMaxHeight === Infinity ? undefined : resultMaxHeight
|
|
128
|
+
};
|
|
129
|
+
// Apply the limit right away and position the dropdown by its final size, it matters for the top placements.
|
|
130
|
+
// Borders, paddings and min-height make the dropdown taller than its max-height.
|
|
131
|
+
elements.floating.style.maxHeight = typeof resultData.maxHeight === 'number' ? `${resultData.maxHeight}px` : resultData.maxHeight ?? '';
|
|
132
|
+
const {
|
|
133
|
+
width,
|
|
134
|
+
height
|
|
135
|
+
} = await platform.getDimensions(elements.floating);
|
|
136
|
+
if (width !== rects.floating.width || height !== rects.floating.height) {
|
|
137
|
+
return {
|
|
138
|
+
data: resultData,
|
|
139
|
+
reset: {
|
|
140
|
+
rects: true
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
data: resultData
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
export { AUTO_PLACEMENTS, applyMaxSize, isAutoPlacement, sameWidth };
|
package/lib/dropdown.d.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import type { ReactNode
|
|
3
|
-
import type { ReferenceChildrenProps } from 'react-popper';
|
|
2
|
+
import type { ReactNode } from 'react';
|
|
4
3
|
import { Theme } from '@rescui/ui-contexts';
|
|
5
|
-
import type {
|
|
6
|
-
import { DropdownMaxWidth, DropdownMinWidth } from './custom-
|
|
7
|
-
export type
|
|
8
|
-
export interface DropdownProps {
|
|
4
|
+
import type { DropdownInitialFocus, DropdownRole, DropdownTrigger } from './types';
|
|
5
|
+
import { DropdownMaxWidth, DropdownMinWidth, DropdownPlacement } from './custom-middleware';
|
|
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
|
+
*/
|
|
9
12
|
trigger: DropdownTrigger;
|
|
10
13
|
isOpen: boolean;
|
|
11
14
|
onRequestClose?: () => void;
|
|
@@ -27,13 +30,54 @@ export interface DropdownProps {
|
|
|
27
30
|
maxWidth?: DropdownMaxWidth;
|
|
28
31
|
className?: string;
|
|
29
32
|
mode?: 'classic' | 'rock';
|
|
30
|
-
placement?:
|
|
33
|
+
placement?: DropdownPlacement;
|
|
31
34
|
/** @ignore */
|
|
32
35
|
cancelCloseAnimation?: boolean;
|
|
33
36
|
children?: ReactNode;
|
|
34
37
|
closeOnEsc?: boolean;
|
|
35
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
|
+
*/
|
|
36
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;
|
|
37
81
|
/**
|
|
38
82
|
* Render dropdown inside trigger function or wrapper. May be useful when the trigger placed in the fixed layout.
|
|
39
83
|
* */
|