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

[next] fix(NcEmojiPicker): not focusable when used in another focus trap #6561

Draft
wants to merge 3 commits into
base: next
Choose a base branch
from
Draft
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
40 changes: 9 additions & 31 deletions src/components/NcActions/NcActions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@
<script>
import { useElementBounding, useWindowSize } from '@vueuse/core'
import { Fragment, computed, h, mergeProps, ref, toRef, warn } from 'vue'
import { getTrapStack } from '../../utils/focusTrap.js'
import { useTrapStackControl } from '../../composables/useTrapStackControl.ts'
import { t } from '../../l10n.js'

import NcButton from '../NcButton/index.ts'
Expand Down Expand Up @@ -1154,7 +1154,6 @@
* @type {'menu'|'navigation'|'dialog'|'tooltip'|'unknown'}
*/
actionsMenuSemanticType: 'unknown',
externalFocusTrapStack: [],
}
},

Expand Down Expand Up @@ -1238,8 +1237,6 @@
},

opened() {
this.intersectIntoCurrentFocusTrapStack()

// Ensure that pressing escape will close the menu even if the menu is not hovered
// and not currently active, e.g. because user opened the context menu
if (this.opened) {
Expand All @@ -1250,6 +1247,14 @@
},
},

created() {
// When component has its own custom focus management
// The global focus trap stack should be paused
useTrapStackControl(() => this.opened, {
disabled: () => this.config.withFocusTrap,
})
},

methods: {
/**
* Get the name of the action component
Expand All @@ -1261,33 +1266,6 @@
return action?.type?.name
},

/**
* When the component has its own focus trap, then it is managed by global trap stack by focus-trap.
*
* However if the component has no focus trap and is used inside another focus trap - there is an issue.
* By default popover content is rendered in body or other container, which is likely outside the current focus trap containers.
* It results in broken behavior from focus-trap.
*
* We need to pause all the focus traps for opening popover and then unpause them back after closing.
*/
intersectIntoCurrentFocusTrapStack() {
if (this.config.withFocusTrap) {
return
}

if (this.opened) {
this.externalFocusTrapStack = [...getTrapStack()]
for (const trap of this.externalFocusTrapStack) {
trap.pause()
}
} else {
for (const trap of this.externalFocusTrapStack) {
trap.unpause()
}
this.externalFocusTrapStack = []
}
},

/**
* Do we have exactly one Action and
* is it allowed as a standalone element?
Expand Down Expand Up @@ -1418,7 +1396,7 @@
},

/**
* @return {NodeListOf<HTMLElement>}

Check warning on line 1399 in src/components/NcActions/NcActions.vue

View workflow job for this annotation

GitHub Actions / NPM lint

The type 'NodeListOf' is undefined
*/
getFocusableMenuItemElements() {
return this.$refs.menu.querySelectorAll(focusableSelector)
Expand Down
2 changes: 1 addition & 1 deletion src/components/NcAppNavigation/NcAppNavigation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ emit('toggle-navigation', {

<script>
import { useIsMobile } from '../../composables/useIsMobile/index.js'
import { getTrapStack } from '../../utils/focusTrap.js'
import { getTrapStack } from '../../utils/focusTrap.ts'
import { emit, subscribe, unsubscribe } from '@nextcloud/event-bus'
import { createFocusTrap } from 'focus-trap'

Expand Down
2 changes: 1 addition & 1 deletion src/components/NcAppSidebar/NcAppSidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@
import Linkify from '../../directives/Linkify/index.js'
import { useIsSmallMobile } from '../../composables/useIsMobile/index.js'
import GenRandomId from '../../utils/GenRandomId.js'
import { getTrapStack } from '../../utils/focusTrap.js'
import { getTrapStack } from '../../utils/focusTrap.ts'
import { t } from '../../l10n.js'
import isSlotPopulated from '../../utils/isSlotPopulated.ts'

Expand Down Expand Up @@ -1100,7 +1100,7 @@
* @type {Event}
*/
// eslint-disable-next-line vue/require-explicit-emits
this.$emit('figure-click', e)

Check warning on line 1103 in src/components/NcAppSidebar/NcAppSidebar.vue

View workflow job for this annotation

GitHub Actions / NPM lint

Custom event name 'figure-click' must be camelCase
},

/**
Expand Down Expand Up @@ -1191,7 +1191,7 @@
*
* @type {Event}
*/
this.$emit('submit-name', event)

Check warning on line 1194 in src/components/NcAppSidebar/NcAppSidebar.vue

View workflow job for this annotation

GitHub Actions / NPM lint

Custom event name 'submit-name' must be camelCase
},
onDismissEditing() {
// Disable editing
Expand All @@ -1201,7 +1201,7 @@
*
* @type {Event}
*/
this.$emit('dismiss-editing')

Check warning on line 1204 in src/components/NcAppSidebar/NcAppSidebar.vue

View workflow job for this annotation

GitHub Actions / NPM lint

Custom event name 'dismiss-editing' must be camelCase
},
onUpdateActive(activeTab) {
/**
Expand Down
7 changes: 7 additions & 0 deletions src/components/NcEmojiPicker/NcEmojiPicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@

<script>
import { getCurrentSkinTone, setCurrentSkinTone } from '../../functions/emoji/emoji.ts'
import { useTrapStackControl } from '../../composables/useTrapStackControl.ts'
import { Color } from '../../utils/GenColors.js'

import IconCircle from 'vue-material-design-icons/Circle.vue'
Expand Down Expand Up @@ -345,6 +346,12 @@
},
},

created() {
// Component has its own custom focus management
// The global focus trap stack should be paused
useTrapStackControl(() => this.open)
},

methods: {
t,

Expand Down Expand Up @@ -378,7 +385,7 @@
/**
* Emits a object with more data about the picked emoji
*/
this.$emit('select-data', emojiObject)

Check warning on line 388 in src/components/NcEmojiPicker/NcEmojiPicker.vue

View workflow job for this annotation

GitHub Actions / NPM lint

Custom event name 'select-data' must be camelCase

if (this.closeOnSelect) {
this.open = false
Expand Down
2 changes: 1 addition & 1 deletion src/components/NcHeaderMenu/NcHeaderMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ import type { FocusTrap } from 'focus-trap'
import { onClickOutside } from '@vueuse/core'
import { createFocusTrap } from 'focus-trap'
import { computed, nextTick, ref, useTemplateRef, watch, type Slot } from 'vue'
import { getTrapStack } from '../../utils/focusTrap.js'
import { getTrapStack } from '../../utils/focusTrap.ts'
import { useHotKey } from '../../composables/index.js'

import GenRandomId from '../../utils/GenRandomId.js'
Expand Down
2 changes: 1 addition & 1 deletion src/components/NcModal/NcModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ import { useSwipe } from '@vueuse/core'
import { createFocusTrap } from 'focus-trap'
import { warn as VueWarn } from 'vue'

import { getTrapStack } from '../../utils/focusTrap.js'
import { getTrapStack } from '../../utils/focusTrap.ts'
import { t } from '../../l10n.js'
import GenRandomId from '../../utils/GenRandomId.js'
import NcActions from '../NcActions/index.js'
Expand Down
2 changes: 1 addition & 1 deletion src/components/NcPopover/NcPopover.vue
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ See: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/
import { warn } from 'vue'
import { Dropdown } from 'floating-vue'
import { createFocusTrap } from 'focus-trap'
import { getTrapStack } from '../../utils/focusTrap.js'
import { getTrapStack } from '../../utils/focusTrap.ts'
import NcPopoverTriggerProvider from './NcPopoverTriggerProvider.vue'

/**
Expand Down
37 changes: 37 additions & 0 deletions src/composables/useTrapStackControl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import type { Ref } from 'vue'
import type { MaybeRefOrGetter } from '@vueuse/core'
import { onUnmounted, watch } from 'vue'
import { toValue } from '@vueuse/core'
import { createTrapStackController } from '../utils/focusTrap.ts'

/**
* Control global focus trap stack when focus trap is managed manually
*
* @param shouldPause - Whether focus trap should be paused
* @param options - Additional options
* @param options.disabled - Whether the controller should be disabled
*/
export function useTrapStackControl(shouldPause: Ref<boolean> | (() => boolean), options: { disabled?: MaybeRefOrGetter<boolean> } = {}) {
const trapStackController = createTrapStackController()

watch(shouldPause, () => {
if (toValue(options.disabled)) {
return
}

if (toValue(shouldPause)) {
trapStackController.pause()
} else {
trapStackController.unpause()
}
})

onUnmounted(() => {
trapStackController.unpause()
})
}
16 changes: 0 additions & 16 deletions src/utils/focusTrap.js

This file was deleted.

56 changes: 56 additions & 0 deletions src/utils/focusTrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import type { FocusTrap } from 'focus-trap'

declare global {
interface Window {
_nc_focus_trap: FocusTrap[]
}
}

/**
* Get the default global focus trap stack
*/
export function getTrapStack() {
// Create global stack if undefined
window._nc_focus_trap ??= []

return window._nc_focus_trap
}

/**
* Create controller to pause/unpause the global focus trap stack.
*
* When some component manually implements its own focus-trap without using global focus-trap stack,
* it also needs to pause the global stack to avoid conflict.
*/
export function createTrapStackController() {
/**
* Stack of focus traps that were paused to unpause the exact stack later
*/
let pausedStack: FocusTrap[] = []

return {
/**
* Pause the current focus-trap stack
*/
pause() {
pausedStack = [...getTrapStack()]
for (const trap of pausedStack) {
trap.pause()
}
},
/**
* Unpause the paused focus trap stack
*/
unpause() {
for (const trap of pausedStack) {
trap.unpause()
}
pausedStack = []
},
}
}
Loading