Skip to content

Commit 694fbd5

Browse files
authored
only activate the Tab on mouseup (#1192)
1 parent 4f52d83 commit 694fbd5

File tree

2 files changed

+17
-0
lines changed
  • packages
    • @headlessui-react/src/components/tabs
    • @headlessui-vue/src/components/tabs

2 files changed

+17
-0
lines changed

packages/@headlessui-react/src/components/tabs/tabs.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import React, {
1111
// Types
1212
ElementType,
1313
MutableRefObject,
14+
MouseEvent as ReactMouseEvent,
1415
KeyboardEvent as ReactKeyboardEvent,
1516
Dispatch,
1617
ContextType,
@@ -354,11 +355,19 @@ let TabRoot = forwardRefWithAs(function Tab<TTag extends ElementType = typeof DE
354355
change(myIndex)
355356
}, [change, myIndex, internalTabRef])
356357

358+
// This is important because we want to only focus the tab when it gets focus
359+
// OR it finished the click event (mouseup). However, if you perform a `click`,
360+
// then you will first get the `focus` and then get the `click` event.
361+
let handleMouseDown = useCallback((event: ReactMouseEvent<HTMLElement>) => {
362+
event.preventDefault()
363+
}, [])
364+
357365
let slot = useMemo(() => ({ selected }), [selected])
358366
let propsWeControl = {
359367
ref: tabRef,
360368
onKeyDown: handleKeyDown,
361369
onFocus: activation === 'manual' ? handleFocus : handleSelection,
370+
onMouseDown: handleMouseDown,
362371
onClick: handleSelection,
363372
id,
364373
role: 'tab',

packages/@headlessui-vue/src/components/tabs/tabs.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,13 @@ export let Tab = defineComponent({
245245
api.setSelectedIndex(myIndex.value)
246246
}
247247

248+
// This is important because we want to only focus the tab when it gets focus
249+
// OR it finished the click event (mouseup). However, if you perform a `click`,
250+
// then you will first get the `focus` and then get the `click` event.
251+
function handleMouseDown(event: MouseEvent) {
252+
event.preventDefault()
253+
}
254+
248255
let type = useResolveButtonType(
249256
computed(() => ({ as: props.as, type: attrs.type })),
250257
tabRef
@@ -256,6 +263,7 @@ export let Tab = defineComponent({
256263
ref: tabRef,
257264
onKeydown: handleKeyDown,
258265
onFocus: api.activation.value === 'manual' ? handleFocus : handleSelection,
266+
onMousedown: handleMouseDown,
259267
onClick: handleSelection,
260268
id,
261269
role: 'tab',

0 commit comments

Comments
 (0)