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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(useFocus): support :focus-visible #3254

Merged
merged 2 commits into from Jul 30, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/core/useFocus/index.test.ts
Expand Up @@ -48,6 +48,23 @@ describe('useFocus', () => {
await retry(() => expect(document.activeElement).not.toBe(target.value))
})

it('should only focus when :focus-visible matches with focusVisible=true', () => {
const { focused } = useFocus(target, { focusVisible: true })

let event = new Event('focus')
Object.defineProperty(event, 'target', { value: { matches: () => true }, enumerable: true })
target.value?.dispatchEvent(event)
expect(focused.value).toBeTruthy()

target.value?.dispatchEvent(new Event('blur'))
expect(focused.value).toBeFalsy()

event = new Event('focus')
Object.defineProperty(event, 'target', { value: { matches: () => false }, enumerable: true })
target.value?.dispatchEvent(event)
expect(focused.value).toBeFalsy()
})

describe('when target is missing', () => {
it('should initialize properly', () => {
const { focused } = useFocus(null)
Expand Down
14 changes: 12 additions & 2 deletions packages/core/useFocus/index.ts
Expand Up @@ -12,6 +12,13 @@ export interface UseFocusOptions extends ConfigurableWindow {
* @default false
*/
initialValue?: boolean

/**
* Replicate the :focus-visible behavior of CSS
*
* @default false
*/
focusVisible?: boolean
}

export interface UseFocusReturn {
Expand All @@ -30,12 +37,15 @@ export interface UseFocusReturn {
* @param options
*/
export function useFocus(target: MaybeElementRef, options: UseFocusOptions = {}): UseFocusReturn {
const { initialValue = false } = options
const { initialValue = false, focusVisible = false } = options

const innerFocused = ref(false)
const targetElement = computed(() => unrefElement(target))

useEventListener(targetElement, 'focus', () => innerFocused.value = true)
useEventListener(targetElement, 'focus', (event) => {
if (!focusVisible || (event.target as HTMLElement).matches?.(':focus-visible'))
innerFocused.value = true
})
useEventListener(targetElement, 'blur', () => innerFocused.value = false)

const focused = computed({
Expand Down