Skip to content

Commit c0c83b4

Browse files
authored
feat(useDelayGroup): add enabled option (#3080)
1 parent 5f4d7fc commit c0c83b4

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

.changeset/metal-dragons-shave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@floating-ui/react": patch
3+
---
4+
5+
feat(useDelayGroup): add `enabled` option

packages/react/src/components/FloatingDelayGroup.tsx

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ export function FloatingDelayGroup(
116116
}
117117

118118
interface UseGroupOptions {
119+
/**
120+
* Whether delay grouping should be enabled.
121+
* @default true
122+
*/
123+
enabled?: boolean;
119124
id?: any;
120125
}
121126

@@ -129,14 +134,15 @@ export function useDelayGroup(
129134
options: UseGroupOptions = {},
130135
): GroupContext {
131136
const {open, onOpenChange, floatingId} = context;
132-
const {id: optionId} = options;
137+
const {id: optionId, enabled = true} = options;
133138
const id = optionId ?? floatingId;
134139

135140
const groupContext = useDelayGroupContext();
136141
const {currentId, setCurrentId, initialDelay, setState, timeoutMs} =
137142
groupContext;
138143

139144
useModernLayoutEffect(() => {
145+
if (!enabled) return;
140146
if (!currentId) return;
141147

142148
setState({
@@ -149,14 +155,15 @@ export function useDelayGroup(
149155
if (currentId !== id) {
150156
onOpenChange(false);
151157
}
152-
}, [id, onOpenChange, setState, currentId, initialDelay]);
158+
}, [enabled, id, onOpenChange, setState, currentId, initialDelay]);
153159

154160
useModernLayoutEffect(() => {
155161
function unset() {
156162
onOpenChange(false);
157163
setState({delay: initialDelay, currentId: null});
158164
}
159165

166+
if (!enabled) return;
160167
if (!currentId) return;
161168

162169
if (!open && currentId === id) {
@@ -169,12 +176,22 @@ export function useDelayGroup(
169176

170177
unset();
171178
}
172-
}, [open, setState, currentId, id, onOpenChange, initialDelay, timeoutMs]);
179+
}, [
180+
enabled,
181+
open,
182+
setState,
183+
currentId,
184+
id,
185+
onOpenChange,
186+
initialDelay,
187+
timeoutMs,
188+
]);
173189

174190
useModernLayoutEffect(() => {
191+
if (!enabled) return;
175192
if (setCurrentId === NOOP || !open) return;
176193
setCurrentId(id);
177-
}, [open, setCurrentId, id]);
194+
}, [enabled, open, setCurrentId, id]);
178195

179196
return groupContext;
180197
}

0 commit comments

Comments
 (0)