Skip to content
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
15 changes: 13 additions & 2 deletions src/components/Questions/AnswerInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
-->

<template>
<li class="question__item" @focusout="handleTabbing">
<li
class="question__item"
:class="{ 'question__item--menu-open': isMenuOpen }"
@focusout="handleTabbing">
<NcIconSvgWrapper
v-if="!isDropdown"
:svg="pseudoIcon"
Expand All @@ -30,6 +33,7 @@
<div v-if="!answer.local" class="option__actions">
<NcActions
:id="optionDragMenuId"
v-model:open="isMenuOpen"
:container="`#${optionDragMenuId}`"
:aria-label="t('forms', 'Move option actions')"
class="option__drag-handle"
Expand Down Expand Up @@ -104,6 +108,7 @@ import NcActions from '@nextcloud/vue/components/NcActions'
import NcButton from '@nextcloud/vue/components/NcButton'
import NcIconSvgWrapper from '@nextcloud/vue/components/NcIconSvgWrapper'
import { INPUT_DEBOUNCE_MS, OptionType } from '../../models/Constants.ts'
import focusActionItem from '../../utils/FocusActionItem.ts'
import logger from '../../utils/Logger.ts'
import OcsResponse2Data from '../../utils/OcsResponse2Data.ts'

Expand Down Expand Up @@ -181,6 +186,7 @@ export default defineComponent({
const buttonOptionDown = ref<{ $el?: HTMLElement } | null>(null)
const buttonOptionUp = ref<{ $el?: HTMLElement } | null>(null)
const isIMEComposing = ref(false)
const isMenuOpen = ref(false)
const answer = computed(() => props.answer)
const localText = ref(answer.value.text ?? '')

Expand Down Expand Up @@ -527,7 +533,7 @@ export default defineComponent({
refName === 'buttonOptionDown'
? buttonOptionDown.value
: buttonOptionUp.value
button?.$el?.focus()
focusActionItem(button?.$el)
})
}

Expand Down Expand Up @@ -569,6 +575,7 @@ export default defineComponent({
input,
localText,
isIMEComposing,
isMenuOpen,
debounceOnInput,
handleTabbing,
focus,
Expand All @@ -592,6 +599,10 @@ export default defineComponent({
min-height: var(--default-clickable-area);
width: 100%;

&--menu-open {
z-index: 2;
}

&__pseudoInput {
color: var(--color-primary-element);
margin-inline-start: 2px;
Expand Down
100 changes: 66 additions & 34 deletions src/components/Questions/QuestionRanking.vue
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,26 @@
<li
v-for="(option, index) in rankedOptions"
:key="option.id"
class="ranking-item">
class="ranking-item"
:class="{
'ranking-item--menu-open':
openMenuOptionId === option.id,
}">
<NcActions
:id="`ranking-${option.id}-drag`"
:container="`#ranking-${option.id}-drag`"
:aria-label="t('forms', 'Move option actions')"
class="ranking-item__drag-handle"
variant="tertiary-no-background">
variant="tertiary-no-background"
@update:open="onMenuToggle(option.id, $event)">
<template #icon>
<NcIconSvgWrapper :svg="IconDragIndicator" />
</template>
<NcActionButton
ref="buttonOptionUp"
:ref="
(button) =>
setMoveButton(`${option.id}-up`, button)
"
:disabled="index === 0"
@click="onMoveUp(index)">
<template #icon>
Expand All @@ -113,7 +121,13 @@
{{ t('forms', 'Move option up') }}
</NcActionButton>
<NcActionButton
ref="buttonOptionDown"
:ref="
(button) =>
setMoveButton(
`${option.id}-down`,
button,
)
"
:disabled="index === rankedOptions.length - 1"
@click="onMoveDown(index)">
<template #icon>
Expand Down Expand Up @@ -227,6 +241,7 @@ import {
useQuestionMultiple,
} from '../../composables/useQuestionMultiple.ts'
import { OptionType } from '../../models/Constants.ts'
import focusActionItem from '../../utils/FocusActionItem.ts'

export default defineComponent({
name: 'QuestionRanking',
Expand Down Expand Up @@ -265,12 +280,8 @@ export default defineComponent({
input,
isLoading,
})
const buttonOptionUp = ref<Array<{ $el?: { focus?: () => void } } | null>>(
[],
)
const buttonOptionDown = ref<Array<{ $el?: { focus?: () => void } } | null>>(
[],
)
const moveButtons = new Map<string, HTMLElement>()
const openMenuOptionId = ref<number | null>(null)
const isDragging = ref(false)
const isRanking = ref(false)
const isOptionDialogShown = ref(false)
Expand Down Expand Up @@ -400,23 +411,43 @@ export default defineComponent({
}

/**
* Re-focus a button ref inside a v-for after reorder
* Register or unregister the root element of a move button.
*
* @param key The option id and direction, e.g. `12-up`
* @param button The `NcActionButton` instance, or null when unmounted
*/
const setMoveButton = (key: string, button: unknown): void => {
const item = (button as { $el?: HTMLElement } | null)?.$el
if (item) {
moveButtons.set(key, item)
} else {
moveButtons.delete(key)
}
}

/**
* Track which ranked option has its move menu open.
*
* @param refName The ref name ('buttonOptionUp' or 'buttonOptionDown')
* @param index The index of the item in the v-for
* @param optionId The option id of the menu
* @param open Whether the menu is open
*/
const focusButton = (
refName: 'buttonOptionUp' | 'buttonOptionDown',
index: number,
): void => {
const onMenuToggle = (optionId: number, open: boolean): void => {
if (open) {
openMenuOptionId.value = optionId
} else if (openMenuOptionId.value === optionId) {
openMenuOptionId.value = null
}
}

/**
* Restore focus to a move button of an option after reordering.
*
* @param direction The move button to focus
* @param optionId The id of the moved option
*/
const focusButton = (direction: 'up' | 'down', optionId: number): void => {
nextTick(() => {
const refs =
refName === 'buttonOptionUp'
? buttonOptionUp.value
: buttonOptionDown.value
if (Array.isArray(refs) && refs[index]) {
refs[index].$el?.focus?.()
}
focusActionItem(moveButtons.get(`${optionId}-${direction}`))
})
}

Expand All @@ -432,10 +463,7 @@ export default defineComponent({
rankedOptions.value = items
emitValues()
const newIndex = index - 1
focusButton(
newIndex > 0 ? 'buttonOptionUp' : 'buttonOptionDown',
newIndex,
)
focusButton(newIndex > 0 ? 'up' : 'down', items[newIndex].id)
}

/**
Expand All @@ -451,10 +479,8 @@ export default defineComponent({
emitValues()
const newIndex = index + 1
focusButton(
newIndex < rankedOptions.value.length - 1
? 'buttonOptionDown'
: 'buttonOptionUp',
newIndex,
newIndex < items.length - 1 ? 'down' : 'up',
items[newIndex].id,
)
}

Expand Down Expand Up @@ -492,8 +518,6 @@ export default defineComponent({
...question,
rootElement,
...questionMultiple,
buttonOptionDown,
buttonOptionUp,
choices,
input,
initRankedOptions,
Expand All @@ -508,13 +532,16 @@ export default defineComponent({
IconDragIndicator,
onDragEnd,
onDragStart,
onMenuToggle,
onMoveDown,
onMoveUp,
onRankingEnd,
onRankingStart,
openMenuOptionId,
OptionType,
rankedOptions,
rankOption,
setMoveButton,
shiftDragHandle,
t,
unrankedOptions,
Expand Down Expand Up @@ -592,6 +619,7 @@ export default defineComponent({
}

.ranking-item {
position: relative;
display: flex;
align-items: center;
min-height: var(--default-clickable-area);
Expand All @@ -606,6 +634,10 @@ export default defineComponent({
background-color: var(--color-background-hover);
}

&--menu-open {
z-index: 1;
}

&__position {
font-weight: bold;
min-width: 1.5em;
Expand Down
22 changes: 22 additions & 0 deletions src/utils/FocusActionItem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

/**
* Focus the `<button>` of an action item and mark the item as the only
* active item of its actions menu.
*
* @param item The root element of an `NcActionButton`
*/
export default function focusActionItem(item: HTMLElement | null | undefined): void {
if (!item) {
return
}

item.parentElement
?.querySelectorAll(':scope > li.active')
.forEach((element) => element.classList.remove('active'))
item.classList.add('active')
;(item.querySelector('button') ?? item).focus()
}
Loading