Skip to content

Commit

Permalink
fix: root node for RovingFocusItem when shadowRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
cain committed Sep 23, 2024
1 parent 30a40a3 commit f62a469
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 @@ -10,7 +10,7 @@ export interface RovingFocusItemProps extends PrimitiveProps {
</script>

<script setup lang="ts">
import { computed, nextTick, onMounted, onUnmounted } from 'vue'
import { computed, nextTick, onMounted, onUnmounted, ref } from 'vue'
import { injectRovingFocusGroupContext } from './RovingFocusGroup.vue'
import { Primitive } from '@/Primitive'
import { focusFirst, getFocusIntent, wrapArray } from './utils'
Expand All @@ -31,6 +31,9 @@ const isCurrentTabStop = computed(
const { getItems } = useCollection()
const elRef = ref()
const rootNode = computed<Document | ShadowRoot>(() => elRef.value?.$el.getRootNode())
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="elRef"
: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 f62a469

Please sign in to comment.