@octanejs/base-ui 0.1.1 → 0.1.2
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/README.md +7 -1
- package/package.json +12 -4
- package/src/alert-dialog.ts +47 -0
- package/src/checkbox.ts +7 -16
- package/src/dialog.ts +859 -0
- package/src/field.ts +4 -30
- package/src/index.ts +3 -0
- package/src/number-field.ts +8 -45
- package/src/popover.ts +1205 -0
- package/src/radio.ts +3 -24
- package/src/slider.ts +4 -5
- package/src/switch.ts +4 -30
- package/src/utils/InternalBackdrop.ts +28 -0
- package/src/utils/adaptiveOriginMiddleware.ts +82 -0
- package/src/utils/closePart.ts +61 -0
- package/src/utils/constants.ts +9 -0
- package/src/utils/createChangeEventDetails.ts +7 -0
- package/src/utils/dom.ts +9 -0
- package/src/utils/empty.ts +7 -0
- package/src/utils/floating/FloatingFocusManager.ts +833 -0
- package/src/utils/floating/FloatingPortal.ts +268 -0
- package/src/utils/floating/FloatingRootStore.ts +127 -0
- package/src/utils/floating/FloatingTree.ts +70 -0
- package/src/utils/floating/FloatingTreeStore.ts +22 -0
- package/src/utils/floating/FocusGuard.ts +34 -0
- package/src/utils/floating/composite.ts +20 -0
- package/src/utils/floating/constants.ts +8 -0
- package/src/utils/floating/createAttribute.ts +4 -0
- package/src/utils/floating/createEventEmitter.ts +20 -0
- package/src/utils/floating/element.ts +54 -0
- package/src/utils/floating/enqueueFocus.ts +38 -0
- package/src/utils/floating/event.ts +53 -0
- package/src/utils/floating/getEmptyRootContext.ts +19 -0
- package/src/utils/floating/markOthers.ts +197 -0
- package/src/utils/floating/nodes.ts +31 -0
- package/src/utils/floating/tabbable.ts +260 -0
- package/src/utils/floating/types.ts +57 -0
- package/src/utils/floating/useClick.ts +174 -0
- package/src/utils/floating/useDismiss.ts +641 -0
- package/src/utils/floating/useFloating.ts +198 -0
- package/src/utils/floating/useFloatingRootContext.ts +81 -0
- package/src/utils/floating/useHoverFloatingInteraction.ts +12 -0
- package/src/utils/floating/useHoverReferenceInteraction.ts +17 -0
- package/src/utils/floating/useSyncedFloatingRootContext.ts +93 -0
- package/src/utils/getDisabledMountTransitionStyles.ts +12 -0
- package/src/utils/hideMiddleware.ts +22 -0
- package/src/utils/inertValue.ts +5 -0
- package/src/utils/mergeCleanups.ts +13 -0
- package/src/utils/platform.ts +46 -2
- package/src/utils/popupStateMapping.ts +48 -0
- package/src/utils/popups/index.ts +4 -0
- package/src/utils/popups/popupStoreUtils.ts +484 -0
- package/src/utils/popups/popupTriggerMap.ts +62 -0
- package/src/utils/popups/store.ts +147 -0
- package/src/utils/popups/useTriggerFocusGuards.ts +73 -0
- package/src/utils/store/ReactStore.ts +168 -0
- package/src/utils/store/Store.ts +84 -0
- package/src/utils/store/createSelector.ts +66 -0
- package/src/utils/store/useStore.ts +49 -0
- package/src/utils/useAnchorPositioning.ts +581 -0
- package/src/utils/useAnchoredPopupScrollLock.ts +50 -0
- package/src/utils/useAnimationFrame.ts +28 -5
- package/src/utils/useEnhancedClickHandler.ts +47 -0
- package/src/utils/useOnFirstRender.ts +11 -0
- package/src/utils/useOpenInteractionType.ts +32 -0
- package/src/utils/usePositioner.ts +48 -0
- package/src/utils/useScrollLock.ts +253 -0
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// Ported from .base-ui/packages/react/src/floating-ui-react/hooks/useClick.ts (v1.6.0), octane-
|
|
2
|
+
// adapted: reads the FloatingRootStore (`store.select`/`store.setOpen`/`store.context.dataRef`);
|
|
3
|
+
// native events (no `.nativeEvent`); slot-threaded. Returns an `ElementProps` bag (`{ reference }`)
|
|
4
|
+
// the trigger merges onto its button.
|
|
5
|
+
import { useRef, useMemo } from 'octane';
|
|
6
|
+
|
|
7
|
+
import { subSlot } from '../../internal';
|
|
8
|
+
import { useAnimationFrame } from '../useAnimationFrame';
|
|
9
|
+
import { useTimeout } from '../useTimeout';
|
|
10
|
+
import { EMPTY_OBJECT } from '../empty';
|
|
11
|
+
import { getTarget, isTypeableElement } from './element';
|
|
12
|
+
import { isMouseLikePointerType } from './event';
|
|
13
|
+
import { createChangeEventDetails, REASONS } from '../createChangeEventDetails';
|
|
14
|
+
import type { ElementProps, FloatingContext, FloatingRootContext } from './types';
|
|
15
|
+
|
|
16
|
+
export interface UseClickProps {
|
|
17
|
+
enabled?: boolean | undefined;
|
|
18
|
+
event?: 'click' | 'mousedown' | 'mousedown-only' | undefined;
|
|
19
|
+
toggle?: boolean | undefined;
|
|
20
|
+
ignoreMouse?: boolean | undefined;
|
|
21
|
+
stickIfOpen?: boolean | undefined;
|
|
22
|
+
touchOpenDelay?: number | undefined;
|
|
23
|
+
reason?: string | undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function useClick(
|
|
27
|
+
context: FloatingRootContext | FloatingContext,
|
|
28
|
+
props: UseClickProps,
|
|
29
|
+
slot: symbol | undefined,
|
|
30
|
+
): ElementProps {
|
|
31
|
+
const {
|
|
32
|
+
enabled = true,
|
|
33
|
+
event: eventOption = 'click',
|
|
34
|
+
toggle = true,
|
|
35
|
+
ignoreMouse = false,
|
|
36
|
+
stickIfOpen = true,
|
|
37
|
+
touchOpenDelay = 0,
|
|
38
|
+
reason = REASONS.triggerPress,
|
|
39
|
+
} = props;
|
|
40
|
+
|
|
41
|
+
const store = (context && 'rootStore' in context ? context.rootStore : context) as any;
|
|
42
|
+
const dataRef = store.context.dataRef;
|
|
43
|
+
|
|
44
|
+
const pointerTypeRef = useRef<'mouse' | 'pen' | 'touch' | undefined>(
|
|
45
|
+
undefined,
|
|
46
|
+
subSlot(slot, 'pt'),
|
|
47
|
+
);
|
|
48
|
+
const frame = useAnimationFrame(subSlot(slot, 'frame'));
|
|
49
|
+
const touchOpenTimeout = useTimeout(subSlot(slot, 'to'));
|
|
50
|
+
|
|
51
|
+
const reference = useMemo<ElementProps['reference']>(
|
|
52
|
+
() => {
|
|
53
|
+
function setOpenWithTouchDelay(
|
|
54
|
+
nextOpen: boolean,
|
|
55
|
+
nativeEvent: any,
|
|
56
|
+
target: HTMLElement,
|
|
57
|
+
pointerType: 'mouse' | 'pen' | 'touch' | undefined,
|
|
58
|
+
) {
|
|
59
|
+
const details = createChangeEventDetails(reason, nativeEvent, target);
|
|
60
|
+
if (nextOpen && pointerType === 'touch' && touchOpenDelay > 0) {
|
|
61
|
+
touchOpenTimeout.start(touchOpenDelay, () => {
|
|
62
|
+
store.setOpen(true, details);
|
|
63
|
+
});
|
|
64
|
+
} else {
|
|
65
|
+
store.setOpen(nextOpen, details);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function getNextOpen(
|
|
70
|
+
open: boolean,
|
|
71
|
+
currentTarget: EventTarget | null,
|
|
72
|
+
isClickLikeOpenEvent: (eventType: string | undefined) => boolean,
|
|
73
|
+
) {
|
|
74
|
+
const openEvent = dataRef.current.openEvent;
|
|
75
|
+
const hasClickedOnInactiveTrigger = store.select('domReferenceElement') !== currentTarget;
|
|
76
|
+
if (open && hasClickedOnInactiveTrigger) {
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
if (!open) {
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
if (!toggle) {
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
if (openEvent && stickIfOpen) {
|
|
86
|
+
return !isClickLikeOpenEvent(openEvent.type);
|
|
87
|
+
}
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
onPointerDown(event: any) {
|
|
93
|
+
pointerTypeRef.current = event.pointerType;
|
|
94
|
+
},
|
|
95
|
+
onMouseDown(event: any) {
|
|
96
|
+
const pointerType = pointerTypeRef.current;
|
|
97
|
+
const nativeEvent = event;
|
|
98
|
+
const open = store.select('open');
|
|
99
|
+
|
|
100
|
+
if (
|
|
101
|
+
event.button !== 0 ||
|
|
102
|
+
eventOption === 'click' ||
|
|
103
|
+
(isMouseLikePointerType(pointerType, true) && ignoreMouse)
|
|
104
|
+
) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const nextOpen = getNextOpen(
|
|
109
|
+
open,
|
|
110
|
+
event.currentTarget,
|
|
111
|
+
(openEventType) => openEventType === 'click' || openEventType === 'mousedown',
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
const target = getTarget(nativeEvent);
|
|
115
|
+
if (isTypeableElement(target)) {
|
|
116
|
+
setOpenWithTouchDelay(nextOpen, nativeEvent, target as HTMLElement, pointerType);
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const eventCurrentTarget = event.currentTarget as HTMLElement;
|
|
121
|
+
frame.request(() => {
|
|
122
|
+
setOpenWithTouchDelay(nextOpen, nativeEvent, eventCurrentTarget, pointerType);
|
|
123
|
+
});
|
|
124
|
+
},
|
|
125
|
+
onClick(event: any) {
|
|
126
|
+
if (eventOption === 'mousedown-only') {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
const pointerType = pointerTypeRef.current;
|
|
130
|
+
if (eventOption === 'mousedown' && pointerType) {
|
|
131
|
+
pointerTypeRef.current = undefined;
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
if (isMouseLikePointerType(pointerType, true) && ignoreMouse) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
const open = store.select('open');
|
|
138
|
+
const nextOpen = getNextOpen(
|
|
139
|
+
open,
|
|
140
|
+
event.currentTarget,
|
|
141
|
+
(openEventType) =>
|
|
142
|
+
openEventType === 'click' ||
|
|
143
|
+
openEventType === 'mousedown' ||
|
|
144
|
+
openEventType === 'keydown' ||
|
|
145
|
+
openEventType === 'keyup',
|
|
146
|
+
);
|
|
147
|
+
setOpenWithTouchDelay(nextOpen, event, event.currentTarget as HTMLElement, pointerType);
|
|
148
|
+
},
|
|
149
|
+
onKeyDown() {
|
|
150
|
+
pointerTypeRef.current = undefined;
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
},
|
|
154
|
+
[
|
|
155
|
+
dataRef,
|
|
156
|
+
eventOption,
|
|
157
|
+
ignoreMouse,
|
|
158
|
+
reason,
|
|
159
|
+
store,
|
|
160
|
+
stickIfOpen,
|
|
161
|
+
toggle,
|
|
162
|
+
frame,
|
|
163
|
+
touchOpenTimeout,
|
|
164
|
+
touchOpenDelay,
|
|
165
|
+
],
|
|
166
|
+
subSlot(slot, 'ref'),
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
return useMemo(
|
|
170
|
+
() => (enabled ? { reference } : EMPTY_OBJECT),
|
|
171
|
+
[enabled, reference],
|
|
172
|
+
subSlot(slot, 'out'),
|
|
173
|
+
);
|
|
174
|
+
}
|