Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Move the sidebar in the DOM order (#2185)

Olga Bulat3baca4c90b1f
diff --git a/src/components/VAllResultsGrid/VAllResultsGrid.vue b/src/components/VAllResultsGrid/VAllResultsGrid.vue
index 3decd5704..2a5b0473b 100644
--- a/src/components/VAllResultsGrid/VAllResultsGrid.vue
+++ b/src/components/VAllResultsGrid/VAllResultsGrid.vue
@@ -5,13 +5,12 @@
class="results-grid mb-4 grid grid-cols-2 gap-4 lg:grid-cols-5 2xl:grid-cols-6"
>
<VContentLink
- v-for="([mediaType, count], i) in resultCounts"
+ v-for="[mediaType, count] in resultCounts"
:key="mediaType"
:media-type="mediaType"
:results-count="count"
:to="contentLinkPath(mediaType)"
class="lg:col-span-2"
- @shift-tab="handleShiftTab($event, i)"
/>
</div>
<VSnackbar size="large" :is-visible="isSnackbarVisible">
@@ -58,11 +57,8 @@ import { useMediaStore } from "~/stores/media"
import { useSearchStore } from "~/stores/search"
import { useUiStore } from "~/stores/ui"
-import { useFocusFilters } from "~/composables/use-focus-filters"
import { useI18n } from "~/composables/use-i18n"
-import { Focus } from "~/utils/focus-management"
-
import VSnackbar from "~/components/VSnackbar.vue"
import VImageCellSquare from "~/components/VAllResultsGrid/VImageCellSquare.vue"
import VAudioCell from "~/components/VAllResultsGrid/VAudioCell.vue"
@@ -112,17 +108,6 @@ export default defineComponent({
const noResults = computed(
() => fetchState.value.isFinished && allMedia.value.length === 0
)
- const focusFilters = useFocusFilters()
- /**
- * Move focus to the filters sidebar if shift-tab is pressed on the first content link.
- * @param i - the index of the content link.
- * @param event - keydown event
- */
- const handleShiftTab = (event: KeyboardEvent, i: number) => {
- if (i === 0) {
- focusFilters.focusFilterSidebar(event, Focus.Last)
- }
- }
const uiStore = useUiStore()
const isSnackbarVisible = computed(() => uiStore.areInstructionsVisible)
@@ -142,7 +127,6 @@ export default defineComponent({
resultsLoading,
resultCounts,
noResults,
- handleShiftTab,
contentLinkPath,
diff --git a/src/components/VExternalSearch/VExternalSearchForm.vue b/src/components/VExternalSearch/VExternalSearchForm.vue
index 7cf8b6062..cac6e2fb9 100644
--- a/src/components/VExternalSearch/VExternalSearchForm.vue
+++ b/src/components/VExternalSearch/VExternalSearchForm.vue
@@ -4,7 +4,6 @@
ref="sectionRef"
class="external-sources flex flex-row place-items-center justify-center py-4"
data-testid="external-sources-form"
- @keydown.tab.exact="handleTab"
>
<i18n
v-if="!hasNoResults && isSupported"
@@ -95,7 +94,6 @@ import {
SetupContext,
} from "@nuxtjs/composition-api"
-import { getFocusableElements } from "~/utils/focus-management"
import { defineEvent } from "~/types/emits"
import { useUiStore } from "~/stores/ui"
@@ -172,25 +170,10 @@ export default defineComponent({
emit: emit as SetupContext["emit"],
})
- /**
- * Find the last focusable element in VSearchGridFilter to add a 'Tab' keydown event
- * handler to it.
- */
- const lastFocusableElement = computed<HTMLElement>(() => {
- const focusable = getFocusableElements(sectionRef.value)
- return focusable[focusable.length - 1]
- })
-
- const handleTab = (event: KeyboardEvent) => {
- if (event.target === lastFocusableElement.value) {
- emit("tab", event)
- }
- }
return {
sectionRef,
triggerRef,
triggerElement,
- handleTab,
isMd,
closeDialog,
diff --git a/src/components/VFilters/VSearchGridFilter.vue b/src/components/VFilters/VSearchGridFilter.vue
index da03765b2..e9cc18c4a 100644
--- a/src/components/VFilters/VSearchGridFilter.vue
+++ b/src/components/VFilters/VSearchGridFilter.vue
@@ -13,17 +13,11 @@
variant="plain"
class="label-bold absolute py-1 px-4 text-pink end-0 hover:ring hover:ring-pink"
@click="clearFilters"
- @keydown.shift.tab.exact="focusFilterButton"
>
{{ $t("filter-list.clear") }}
</VButton>
</header>
- <form
- ref="filtersFormRef"
- class="filters-form"
- @keydown.tab.exact="handleTabKey"
- @keydown.shift.tab.exact="handleShiftTabKey"
- >
+ <form ref="filtersFormRef" class="filters-form">
<VFilterChecklist
v-for="filterType in filterTypes"
:key="filterType"
@@ -58,9 +52,7 @@ import { watchDebounced } from "@vueuse/core"
import { useSearchStore } from "~/stores/search"
import { areQueriesEqual, ApiQueryParams } from "~/utils/search-query-transform"
-import { Focus, focusIn, getFocusableElements } from "~/utils/focus-management"
import type { NonMatureFilterCategory } from "~/constants/filters"
-import { useFocusFilters } from "~/composables/use-focus-filters"
import { defineEvent } from "~/types/emits"
import VFilterChecklist from "~/components/VFilters/VFilterChecklist.vue"
@@ -94,7 +86,7 @@ export default defineComponent({
emits: {
close: defineEvent(),
},
- setup(props) {
+ setup() {
const searchStore = useSearchStore()
const { i18n } = useContext()
@@ -124,60 +116,7 @@ export default defineComponent({
{ debounce: 800, maxWait: 5000 }
)
- const focusableElements = computed(() =>
- getFocusableElements(filtersFormRef.value)
- )
- /**
- * Find the last focusable element in VSearchGridFilter to add a 'Tab' keydown event
- * handler to it.
- * We could actually hard-code this because 'searchBy' is always the last now.
- */
- const lastFocusableElement = computed<HTMLElement>(() => {
- return focusableElements.value[focusableElements.value.length - 1]
- })
-
- /**
- * We add the `Shift-Tab` handler to the first focusable checkbox so that focus can go back
- * to the filter button
- */
- const firstFocusableElement = computed<HTMLElement | undefined>(
- () => focusableElements.value[0]
- )
-
- /**
- * When the user presses 'Tab' on the last focusable element, we need to
- * move focus to the first focusable element in main.
- * @param event
- */
- const handleTabKey = (event: KeyboardEvent) => {
- if (!props.changeTabOrder) return
- if (lastFocusableElement.value === event.target) {
- event.preventDefault()
- focusIn(document.querySelector("main"), Focus.First)
- }
- }
- /**
- * Move focus to the filter button only when this checkbox is the first focusable
- * element in the search grid filter, i.e. the 'Clear filters' button is hidden.
- * @param event - The keydown event
- */
- const handleShiftTabKey = (event: KeyboardEvent) => {
- if (!props.changeTabOrder) return
- if (
- firstFocusableElement.value === event.target &&
- !isAnyFilterApplied.value
- ) {
- focusFilterButton(event)
- }
- }
-
- const focusFilters = useFocusFilters()
- const focusFilterButton = (event?: KeyboardEvent) => {
- focusFilters.focusFilterButton(event)
- }
-
return {
- firstFocusableElement,
filtersFormRef,
isAnyFilterApplied,
filters,
@@ -185,9 +124,6 @@ export default defineComponent({
filterTypeTitle,
clearFilters: searchStore.clearFilters,
toggleFilter: searchStore.toggleFilter,
- handleTabKey,
- handleShiftTabKey,
- focusFilterButton,
}
},
})
diff --git a/src/components/VHeader/VHeaderDesktop.vue b/src/components/VHeader/VHeaderDesktop.vue
index 549ee8528..c6ad2291e 100644
--- a/src/components/VHeader/VHeaderDesktop.vue
+++ b/src/components/VHeader/VHeaderDesktop.vue
@@ -42,7 +42,6 @@
aria-haspopup="dialog"
:aria-expanded="isSidebarVisible"
@toggle="toggleSidebar"
- @tab="onTab"
/>
</header>
</template>
@@ -55,10 +54,8 @@ import { useUiStore } from "~/stores/ui"
import { IsHeaderScrolledKey, IsSidebarVisibleKey } from "~/types/provides"
-import { useFocusFilters } from "~/composables/use-focus-filters"
import { useSearch } from "~/composables/use-search"
-import { Focus } from "~/utils/focus-management"
import { ensureFocus } from "~/utils/reakit-utils/focus"
import VFilterButton from "~/components/VHeader/VFilterButton.vue"
@@ -113,15 +110,6 @@ export default defineComponent({
const toggleSidebar = () => uiStore.toggleFilters()
- const focusFilters = useFocusFilters()
- /**
- * Focus the first element in the sidebar when navigating from the VFilterButton
- * using keyboard `Tab` key.
- */
- const onTab = (event: KeyboardEvent) => {
- focusFilters.focusFilterSidebar(event, Focus.First)
- }
-
const isXl = computed(() => uiStore.isBreakpoint("xl"))
return {
@@ -140,7 +128,6 @@ export default defineComponent({
searchStatus,
searchTerm,
toggleSidebar,
- onTab,
}
},
})
diff --git a/src/components/VImageGrid/VImageGrid.vue b/src/components/VImageGrid/VImageGrid.vue
index e83386006..f213a40e8 100644
--- a/src/components/VImageGrid/VImageGrid.vue
+++ b/src/components/VImageGrid/VImageGrid.vue
@@ -2,11 +2,10 @@
<section>
<div class="image-grid flex flex-wrap gap-4">
<VImageCell
- v-for="(image, index) in images"
+ v-for="image in images"
:key="image.id"
:image="image"
:search-term="searchTerm"
- @shift-tab="handleShiftTab($event, index)"
/>
</div>
<h5 v-if="isError && !fetchState.isFinished" class="py-4">
@@ -33,8 +32,6 @@ import { useSearchStore } from "~/stores/search"
import type { FetchState } from "~/types/fetch-state"
import type { ImageDetail } from "~/types/media"
-import { defineEvent } from "~/types/emits"
-
import VLoadMore from "~/components/VLoadMore.vue"
import VImageCell from "~/components/VImageGrid/VImageCell.vue"
@@ -61,23 +58,13 @@ export default defineComponent({
required: true,
},
},
- emits: {
- "shift-tab": defineEvent(),
- },
- setup(props, { emit }) {
+ setup(props) {
const searchStore = useSearchStore()
const searchTerm = computed(() => searchStore.searchTerm)
const isError = computed(() => Boolean(props.fetchState.fetchingError))
- const handleShiftTab = (event: KeyboardEvent, index: number) => {
- if (index === 0) {
- event.preventDefault()
- emit("shift-tab")
- }
- }
-
- return { isError, handleShiftTab, searchTerm }
+ return { isError, searchTerm }
},
})
</script>
diff --git a/src/composables/use-focus-filters.ts b/src/composables/use-focus-filters.ts
deleted file mode 100644
index 49fbb0a0c..000000000
--- a/src/composables/use-focus-filters.ts
+++ /dev/null
@@ -1,29 +0,0 @@
-import { Focus, focusIn } from "~/utils/focus-management"
-import { useUiStore } from "~/stores/ui"
-
-export const useFocusFilters = () => {
- const focusFilterSidebar = (event?: KeyboardEvent, focus = Focus.Last) => {
- const uiStore = useUiStore()
- if (uiStore.isDesktopLayout && uiStore.isFilterVisible) {
- if (event) event.preventDefault()
- // Prevent over-tabbing to the element after the target one
- // Cannot use refs when using portals (for sidebar)
- const filtersSidebarElement = document.getElementById("filters")
- if (filtersSidebarElement) {
- focusIn(filtersSidebarElement, focus)
- }
- }
- }
- const focusFilterButton = (event?: KeyboardEvent) => {
- const uiStore = useUiStore()
- if (uiStore.isDesktopLayout && uiStore.isFilterVisible) {
- if (event) event.preventDefault()
- const filterButton = document.getElementById("filter-button")
- if (filterButton) {
- filterButton.focus()
- }
- }
- }
-
- return { focusFilterSidebar, focusFilterButton }
-}
diff --git a/src/layouts/content-layout.vue b/src/layouts/content-layout.vue
index 73b78b9a7..8a1e0d9fb 100644
--- a/src/layouts/content-layout.vue
+++ b/src/layouts/content-layout.vue
@@ -1,9 +1,16 @@
<template>
<div
- class="app flex min-h-screen min-h-[100dvh] flex-col bg-white"
- :class="[isDesktopLayout ? 'desktop' : 'mobile', breakpoint]"
+ class="app flex grid min-h-screen min-h-[100dvh] grid-rows-[auto,1fr] bg-white"
+ :class="[
+ isDesktopLayout ? 'desktop' : 'mobile',
+ breakpoint,
+ { 'has-sidebar': isSidebarVisible },
+ isSidebarVisible
+ ? 'grid-cols-[1fr_var(--filter-sidebar-width)]'
+ : 'grid-cols-1',
+ ]"
>
- <div class="sticky top-0 z-40 block">
+ <div class="header-el sticky top-0 z-40 block">
<VTeleportTarget name="skip-to-content" :force-destroy="true" />
<VBanners />
<template v-if="isSearchHeader">
@@ -17,32 +24,20 @@
/>
</div>
- <main
- class="main grid h-full flex-grow"
- :class="[
- { 'has-sidebar': isSidebarVisible },
- isSidebarVisible
- ? 'grid-cols-[1fr_var(--filter-sidebar-width)]'
- : 'grid-cols-1',
- ]"
+ <aside
+ v-if="isSidebarVisible"
+ class="sidebar fixed z-10 mt-[81px] h-[calc(100vh-81px)] h-[calc(100dvh-81px)] overflow-y-auto border-dark-charcoal-20 bg-dark-charcoal-06 end-0 border-s"
>
- <div
- class="main-page flex h-full w-full min-w-0 flex-col justify-between"
- >
- <Nuxt />
- <VFooter
- :mode="isSearchHeader ? 'content' : 'search'"
- class="border-t border-dark-charcoal-20 bg-white"
- />
- </div>
-
- <aside
- v-if="isSidebarVisible"
- class="sidebar fixed z-10 h-[calc(100vh-81px)] h-[calc(100dvh-81px)] overflow-y-auto border-dark-charcoal-20 bg-dark-charcoal-06 end-0 border-s"
- >
- <VSearchGridFilter class="px-10 pt-8 pb-10" @close="closeSidebar" />
- </aside>
- </main>
+ <VSearchGridFilter class="px-10 pt-8 pb-10" @close="closeSidebar" />
+ </aside>
+
+ <div class="main-page flex h-full w-full min-w-0 flex-col justify-between">
+ <Nuxt />
+ <VFooter
+ :mode="isSearchHeader ? 'content' : 'search'"
+ class="border-t border-dark-charcoal-20 bg-white"
+ />
+ </div>
<VModalTarget class="modal" />
<VGlobalAudioSection />
@@ -178,4 +173,19 @@ export default defineComponent({
.has-sidebar .sidebar {
width: var(--filter-sidebar-width);
}
+.app {
+ grid-template-areas: "header header" "main main";
+}
+.header-el {
+ grid-area: header;
+}
+.main-page {
+ grid-area: main;
+}
+.sidebar {
+ grid-area: sidebar;
+}
+.has-sidebar.app {
+ grid-template-areas: "header header" "main sidebar";
+}
</style>
diff --git a/src/pages/search.vue b/src/pages/search.vue
index 8448f440a..723a51e40 100644
--- a/src/pages/search.vue
+++ b/src/pages/search.vue
@@ -10,7 +10,6 @@
:search-type="searchType"
:results-count="resultCount"
data-testid="search-grid"
- @tab="handleTab($event, 'search-grid')"
>
<template #media>
<NuxtChild
@@ -27,7 +26,6 @@
v-show="showScrollButton"
:is-filter-sidebar-visible="isSidebarVisible"
data-testid="scroll-button"
- @tab="handleTab($event, 'scroll-button')"
/>
</VSkipToContentContainer>
</template>
@@ -36,7 +34,6 @@
import { isShallowEqualObjects } from "@wordpress/is-shallow-equal"
import { computed, defineComponent, inject, ref } from "@nuxtjs/composition-api"
-import { Focus, focusIn } from "~/utils/focus-management"
import { useMediaStore } from "~/stores/media"
import { useSearchStore } from "~/stores/search"
import { IsSidebarVisibleKey } from "~/types/provides"
@@ -160,12 +157,6 @@ export default defineComponent({
},
},
methods: {
- handleTab(event: KeyboardEvent, element: string) {
- if (this.showScrollButton.value && element !== "scroll-button") {
- return
- }
- focusIn(document.getElementById("__layout"), Focus.First)
- },
fetchMedia(payload: { shouldPersistMedia?: boolean } = {}) {
const mediaStore = useMediaStore(this.$pinia)
return mediaStore.fetchMedia(payload)
diff --git a/src/pages/search/audio.vue b/src/pages/search/audio.vue
index 259c1f122..b24f6a8de 100644
--- a/src/pages/search/audio.vue
+++ b/src/pages/search/audio.vue
@@ -18,14 +18,13 @@
is-for-tab="audio"
/>
<VAudioTrack
- v-for="(audio, i) in results"
+ v-for="audio in results"
:key="audio.id"
class="mb-2 md:mb-1"
:audio="audio"
:size="audioTrackSize"
layout="row"
:search-term="searchTerm"
- @shift-tab="handleShiftTab($event, i)"
@interacted="hideSnackbar"
@mousedown.native="handleMouseDown"
@focus.native="showSnackbar"
@@ -43,11 +42,8 @@ import {
inject,
} from "@nuxtjs/composition-api"
-import { useFocusFilters } from "~/composables/use-focus-filters"
import { useSearchStore } from "~/stores/search"
-import { Focus } from "~/utils/focus-management"
-
import { useUiStore } from "~/stores/ui"
import { IsSidebarVisibleKey } from "~/types/provides"
@@ -87,13 +83,6 @@ export default defineComponent({
!isDesktopLayout.value ? "s" : filterVisibleRef.value ? "l" : "m"
)
- const focusFilters = useFocusFilters()
- const handleShiftTab = (event: KeyboardEvent, i: number) => {
- if (i === 0) {
- focusFilters.focusFilterSidebar(event, Focus.Last)
- }
- }
-
const isMouseDown = ref(false)
const handleMouseDown = () => {
isMouseDown.value = true
@@ -118,7 +107,6 @@ export default defineComponent({
results,
audioTrackSize,
- handleShiftTab,
handleMouseDown,
isSnackbarVisible,
diff --git a/src/pages/search/image.vue b/src/pages/search/image.vue
index 6859f0741..21f2d2ad7 100644
--- a/src/pages/search/image.vue
+++ b/src/pages/search/image.vue
@@ -3,7 +3,6 @@
:images="results"
:is-single-page="false"
:fetch-state="fetchState"
- @shift-tab="handleShiftTab"
/>
</template>
@@ -11,7 +10,6 @@
import { computed, defineComponent, useMeta } from "@nuxtjs/composition-api"
import { propTypes } from "~/pages/search/search-page.types"
-import { useFocusFilters } from "~/composables/use-focus-filters"
import VImageGrid from "~/components/VImageGrid/VImageGrid.vue"
@@ -27,12 +25,7 @@ export default defineComponent({
const results = computed(() => props.resultItems.image)
- const focusFilters = useFocusFilters()
- const handleShiftTab = () => {
- focusFilters.focusFilterSidebar()
- }
-
- return { handleShiftTab, results }
+ return { results }
},
head: {},
})
diff --git a/test/playwright/e2e/filters-sidebar-keyboard.spec.ts b/test/playwright/e2e/filters-sidebar-keyboard.spec.ts
new file mode 100644
index 000000000..717381c8f
--- /dev/null
+++ b/test/playwright/e2e/filters-sidebar-keyboard.spec.ts
@@ -0,0 +1,72 @@
+import { test, expect, Page } from "@playwright/test"
+
+import {
+ LanguageDirection,
+ pathWithDir,
+ setCookies,
+ t,
+} from "~~/test/playwright/utils/navigation"
+
+import { keycodes } from "~/constants/key-codes"
+
+const isFilterButtonFocused = async (page: Page) => {
+ return page.evaluate(() => document.activeElement?.id === "filter-button")
+}
+
+const walkToFilterButton = async (page: Page) => {
+ while (!(await isFilterButtonFocused(page))) {
+ await page.keyboard.press(keycodes.Tab)
+ }
+}
+
+const firstFilterCheckbox = (page: Page, dir: LanguageDirection) => {
+ return page
+ .getByRole("region", { name: t("filters.filter-by", dir) })
+ .getByRole("checkbox")
+ .first()
+}
+
+test.describe.configure({ mode: "parallel" })
+
+for (const dir of ["ltr", "rtl"]) {
+ test.describe(`search header keyboard accessibility test in ${dir}`, () => {
+ test.beforeEach(async ({ page }) => {
+ await setCookies(page.context(), { uiBreakpoint: "lg" })
+ /**
+ * To simplify finding the last focusable element in the filters sidebar,
+ * we use the image search page. After the removal of the "searchBy" filter,
+ * the last element on the all media search page is the "license explanation"
+ * button, not a checkbox.
+ */
+ await page.goto(pathWithDir("/search/image?q=birds", dir))
+ })
+
+ test("should move focus to the sidebar after header", async ({ page }) => {
+ await walkToFilterButton(page)
+
+ // Check that the filters sidebar is open
+ expect(
+ await page.locator("#filter-button").getAttribute("aria-expanded")
+ ).toBe("true")
+
+ await page.keyboard.press(keycodes.Tab)
+
+ await expect(
+ firstFilterCheckbox(page, dir as LanguageDirection)
+ ).toBeFocused()
+ })
+
+ test("should move focus to main after filters sidebar", async ({
+ page,
+ }) => {
+ await page.getByRole("checkbox").last().focus()
+
+ await page.keyboard.press(keycodes.Tab)
+
+ const href = await page.evaluate(
+ () => (document.activeElement as HTMLAnchorElement | null)?.href
+ )
+ expect(href).toBeDefined()
+ })
+ })
+}
diff --git a/test/playwright/e2e/header-internal.spec.ts b/test/playwright/e2e/header-internal.spec.ts
index 04bb00a50..50e2ac45d 100644
--- a/test/playwright/e2e/header-internal.spec.ts
+++ b/test/playwright/e2e/header-internal.spec.ts
@@ -84,7 +84,7 @@ test.describe("Header internal", () => {
const homeUrl = page.url()
await clickMenuButton(page)
await page.getByRole("link", { name: t("navigation.about") }).click()
- await page.locator("a[href='/']").click()
+ await page.getByRole("link", { name: t("header.home-link") }).click()
expect(page.url()).toBe(homeUrl)
})
})