-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathuseSelectTrigger.ts
189 lines (164 loc) · 5.45 KB
/
useSelectTrigger.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import * as React from 'react';
import { contains } from '@floating-ui/react/utils';
import { useButton } from '../../use-button/useButton';
import type { GenericHTMLProps } from '../../utils/types';
import { mergeReactProps } from '../../utils/mergeReactProps';
import { useForkRef } from '../../utils/useForkRef';
import { useSelectRootContext } from '../root/SelectRootContext';
import { ownerDocument } from '../../utils/owner';
import { useFieldRootContext } from '../../field/root/FieldRootContext';
import { getPseudoElementBounds } from '../../utils/getPseudoElementBounds';
export function useSelectTrigger(
parameters: useSelectTrigger.Parameters,
): useSelectTrigger.ReturnValue {
const BOUNDARY_OFFSET = 2;
const { disabled = false, rootRef: externalRef } = parameters;
const {
open,
setOpen,
setTriggerElement,
selectionRef,
value,
fieldControlValidation,
setTouchModality,
positionerElement,
alignItemToTrigger,
readOnly,
} = useSelectRootContext();
const { labelId, setTouched, setFocused } = useFieldRootContext();
const triggerRef = React.useRef<HTMLElement | null>(null);
const timeoutRef = React.useRef(-1);
const mergedRef = useForkRef(externalRef, triggerRef);
const { getButtonProps, buttonRef } = useButton({
disabled,
buttonRef: mergedRef,
});
const handleRef = useForkRef<HTMLElement>(buttonRef, setTriggerElement);
React.useEffect(() => {
if (open) {
// mousedown -> mouseup on selected item should not select within 400ms.
const timeoutId1 = window.setTimeout(() => {
selectionRef.current.allowSelectedMouseUp = true;
}, 400);
// mousedown -> move to unselected item -> mouseup should not select within 200ms.
const timeoutId2 = window.setTimeout(() => {
selectionRef.current.allowUnselectedMouseUp = true;
}, 200);
return () => {
clearTimeout(timeoutId1);
clearTimeout(timeoutId2);
};
}
selectionRef.current = {
allowSelectedMouseUp: false,
allowUnselectedMouseUp: false,
allowSelect: true,
};
clearTimeout(timeoutRef.current);
return undefined;
}, [open, selectionRef]);
const getTriggerProps = React.useCallback(
(externalProps?: GenericHTMLProps): GenericHTMLProps => {
return getButtonProps(
mergeReactProps<'button'>(fieldControlValidation.getValidationProps(externalProps), {
'aria-labelledby': labelId,
'aria-readonly': readOnly || undefined,
tabIndex: disabled ? -1 : 0, // this is needed to make the button focused after click in Safari
ref: handleRef,
onFocus() {
setFocused(true);
// The popup element shouldn't obscure the focused trigger.
if (open && alignItemToTrigger) {
setOpen(false);
}
},
onBlur() {
setTouched(true);
setFocused(false);
fieldControlValidation.commitValidation(value);
},
onPointerMove({ pointerType }) {
setTouchModality(pointerType === 'touch');
},
onPointerDown({ pointerType }) {
setTouchModality(pointerType === 'touch');
},
onMouseDown(event) {
if (open) {
return;
}
const doc = ownerDocument(event.currentTarget);
function handleMouseUp(mouseEvent: MouseEvent) {
if (!triggerRef.current) {
return;
}
const mouseUpTarget = mouseEvent.target as Element | null;
// Early return if clicked on trigger element or its children
if (
contains(triggerRef.current, mouseUpTarget) ||
contains(positionerElement, mouseUpTarget) ||
mouseUpTarget === triggerRef.current
) {
return;
}
const bounds = getPseudoElementBounds(triggerRef.current);
if (
mouseEvent.clientX >= bounds.left - BOUNDARY_OFFSET &&
mouseEvent.clientX <= bounds.right + BOUNDARY_OFFSET &&
mouseEvent.clientY >= bounds.top - BOUNDARY_OFFSET &&
mouseEvent.clientY <= bounds.bottom + BOUNDARY_OFFSET
) {
return;
}
setOpen(false, mouseEvent);
}
// Firefox can fire this upon mousedown
timeoutRef.current = window.setTimeout(() => {
doc.addEventListener('mouseup', handleMouseUp, { once: true });
});
},
}),
);
},
[
getButtonProps,
fieldControlValidation,
labelId,
readOnly,
disabled,
handleRef,
setFocused,
open,
alignItemToTrigger,
setOpen,
setTouched,
value,
setTouchModality,
positionerElement,
],
);
return React.useMemo(
() => ({
getTriggerProps,
rootRef: handleRef,
}),
[getTriggerProps, handleRef],
);
}
export namespace useSelectTrigger {
export interface Parameters {
/**
* Whether the component should ignore user interaction.
* @default false
*/
disabled?: boolean;
/**
* The ref to the root element.
*/
rootRef?: React.Ref<HTMLElement>;
}
export interface ReturnValue {
getTriggerProps: (externalProps?: GenericHTMLProps) => GenericHTMLProps;
rootRef: React.RefCallback<Element> | null;
}
}