Skip to content

Commit

Permalink
fix: root node for RovingFocusItem when shadowRoot (#1314)
Browse files Browse the repository at this point in the history
* fix: root node for RovingFocusItem when shadowRoot

* chore: replace with usePrimitveElement

---------

Co-authored-by: zernonia <zernonia@gmail.com>
Co-authored-by: sunnylost <sunnylost@users.noreply.github.com>
  • Loading branch information
3 people authored Sep 25, 2024
1 parent 30a40a3 commit fed1551
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 6 additions & 2 deletions packages/radix-vue/src/RovingFocus/RovingFocusItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface RovingFocusItemProps extends PrimitiveProps {
<script setup lang="ts">
import { computed, nextTick, onMounted, onUnmounted } from 'vue'
import { injectRovingFocusGroupContext } from './RovingFocusGroup.vue'
import { Primitive } from '@/Primitive'
import { Primitive, usePrimitiveElement } from '@/Primitive'
import { focusFirst, getFocusIntent, wrapArray } from './utils'
import { useId } from '@/shared'
import { CollectionItem, useCollection } from '@/Collection'
Expand All @@ -31,6 +31,9 @@ const isCurrentTabStop = computed(
const { getItems } = useCollection()
const { primitiveElement, currentElement } = usePrimitiveElement()
const rootNode = computed(() => currentElement.value?.getRootNode() as Document | ShadowRoot)
onMounted(() => {
if (props.focusable)
context.onFocusableItemAdd()
Expand Down Expand Up @@ -76,14 +79,15 @@ function handleKeydown(event: KeyboardEvent) {
: candidateNodes.slice(currentIndex + 1)
}
nextTick(() => focusFirst(candidateNodes))
nextTick(() => focusFirst(candidateNodes, false, rootNode.value))
}
}
</script>

<template>
<CollectionItem>
<Primitive
ref="primitiveElement"
:tabindex="isCurrentTabStop ? 0 : -1"
:data-orientation="context.orientation.value"
:data-active="active"
Expand Down
4 changes: 2 additions & 2 deletions packages/radix-vue/src/RovingFocus/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export function getFocusIntent(
return MAP_KEY_TO_FOCUS_INTENT[key]
}

export function focusFirst(candidates: HTMLElement[], preventScroll = false) {
const PREVIOUSLY_FOCUSED_ELEMENT = document.activeElement
export function focusFirst(candidates: HTMLElement[], preventScroll = false, rootNode?: Document | ShadowRoot) {
const PREVIOUSLY_FOCUSED_ELEMENT = rootNode?.activeElement ?? document.activeElement
for (const candidate of candidates) {
// if focus is already where we want to go, we don't want to keep going through the candidates
if (candidate === PREVIOUSLY_FOCUSED_ELEMENT)
Expand Down

0 comments on commit fed1551

Please sign in to comment.