Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only activate the Tab on mouseup #1192

Merged
merged 1 commit into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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