Skip to content

Commit

Permalink
only activate the Tab on mouseup (#1192)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinMalfait committed Mar 7, 2022
1 parent e0cc62c commit c4e30d6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/@headlessui-react/src/components/tabs/tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import React, {
// Types
ElementType,
MutableRefObject,
MouseEvent as ReactMouseEvent,
KeyboardEvent as ReactKeyboardEvent,
Dispatch,
ContextType,
Expand Down Expand Up @@ -354,11 +355,19 @@ let TabRoot = forwardRefWithAs(function Tab<TTag extends ElementType = typeof DE
change(myIndex)
}, [change, myIndex, internalTabRef])

// This is important because we want to only focus the tab when it gets focus
// OR it finished the click event (mouseup). However, if you perform a `click`,
// then you will first get the `focus` and then get the `click` event.
let handleMouseDown = useCallback((event: ReactMouseEvent<HTMLElement>) => {
event.preventDefault()
}, [])

let slot = useMemo(() => ({ selected }), [selected])
let propsWeControl = {
ref: tabRef,
onKeyDown: handleKeyDown,
onFocus: activation === 'manual' ? handleFocus : handleSelection,
onMouseDown: handleMouseDown,
onClick: handleSelection,
id,
role: 'tab',
Expand Down
8 changes: 8 additions & 0 deletions packages/@headlessui-vue/src/components/tabs/tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,13 @@ export let Tab = defineComponent({
api.setSelectedIndex(myIndex.value)
}

// This is important because we want to only focus the tab when it gets focus
// OR it finished the click event (mouseup). However, if you perform a `click`,
// then you will first get the `focus` and then get the `click` event.
function handleMouseDown(event: MouseEvent) {
event.preventDefault()
}

let type = useResolveButtonType(
computed(() => ({ as: props.as, type: attrs.type })),
tabRef
Expand All @@ -256,6 +263,7 @@ export let Tab = defineComponent({
ref: tabRef,
onKeydown: handleKeyDown,
onFocus: api.activation.value === 'manual' ? handleFocus : handleSelection,
onMousedown: handleMouseDown,
onClick: handleSelection,
id,
role: 'tab',
Expand Down

0 comments on commit c4e30d6

Please sign in to comment.