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

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion css/office-main.css
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/* extracted by css-entry-points-plugin */
@import './main-BaoRzybt.chunk.css';
@import './main-BzIE_mSC.chunk.css';
50 changes: 25 additions & 25 deletions js/office-main.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/office-main.mjs.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@nextcloud/l10n": "^3.4.1",
"@nextcloud/router": "^3.1.0",
"@nextcloud/vue": "^9.13.1",
"@vueuse/core": "^14.4.0",
"vue": "^3.5.43",
"vue-router": "^5.3.1"
},
Expand Down
40 changes: 30 additions & 10 deletions src/components/TemplateSection.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { mount } from '@vue/test-utils'
import { nextTick } from 'vue'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import TemplateSection from './TemplateSection.vue'
import type { TemplateCreator, TemplateFile } from '../services/templates.ts'
Expand Down Expand Up @@ -142,26 +143,45 @@ describe('TemplateSection > themeType', () => {
})
})

describe('TemplateSection > accessibility', () => {
it('labels the list with the creator name', () => {
const creator = makeCreator({ label: 'Spreadsheet' })
const wrapper = mount(TemplateSection, { props: { creator } })

expect(wrapper.find('.template-section__list').attributes('aria-label'))
.toBe('Spreadsheet templates')
})
})

describe('TemplateSection > scroll arrows', () => {
function setScrollMetrics(el: HTMLElement, { scrollLeft = 0, scrollWidth = 0, clientWidth = 0 }) {
Object.defineProperty(el, 'scrollLeft', { value: scrollLeft, configurable: true })
Object.defineProperty(el, 'scrollLeft', { value: scrollLeft, writable: true, configurable: true })
Object.defineProperty(el, 'scrollWidth', { value: scrollWidth, configurable: true })
Object.defineProperty(el, 'clientWidth', { value: clientWidth, configurable: true })
}

it('hides the nav entirely when the list does not overflow', () => {
const creator = makeCreator()
// useScrollArrows attaches its scroll listener from a watcher that only
// runs once Vue flushes post-render effects — mount() itself doesn't wait
// for that, so anything dispatched immediately after would be missed.
async function mountAndSettle(creator: TemplateCreator) {
const wrapper = mount(TemplateSection, { props: { creator } })
setScrollMetrics(wrapper.find('.template-section__list').element as HTMLElement, {
scrollLeft: 0, scrollWidth: 100, clientWidth: 100,
})
await nextTick()
return wrapper
}

it('hides the nav entirely when the list does not overflow', async () => {
const creator = makeCreator()
const wrapper = await mountAndSettle(creator)
const list = wrapper.find('.template-section__list').element as HTMLElement
setScrollMetrics(list, { scrollLeft: 0, scrollWidth: 100, clientWidth: 100 })
await wrapper.find('.template-section__list').trigger('scroll')

expect(wrapper.find('.template-section__nav').exists()).toBe(false)
})

it('enables the right arrow (not left) when scrolled to the start of an overflowing list', async () => {
const creator = makeCreator()
const wrapper = mount(TemplateSection, { props: { creator } })
const wrapper = await mountAndSettle(creator)
const list = wrapper.find('.template-section__list').element as HTMLElement
setScrollMetrics(list, { scrollLeft: 0, scrollWidth: 500, clientWidth: 200 })
await wrapper.find('.template-section__list').trigger('scroll')
Expand All @@ -173,7 +193,7 @@ describe('TemplateSection > scroll arrows', () => {

it('enables the left arrow (not right) when scrolled to the end of an overflowing list', async () => {
const creator = makeCreator()
const wrapper = mount(TemplateSection, { props: { creator } })
const wrapper = await mountAndSettle(creator)
const list = wrapper.find('.template-section__list').element as HTMLElement
setScrollMetrics(list, { scrollLeft: 300, scrollWidth: 500, clientWidth: 200 })
await wrapper.find('.template-section__list').trigger('scroll')
Expand All @@ -185,7 +205,7 @@ describe('TemplateSection > scroll arrows', () => {

it('scrolls right by max(card+gap, clientWidth - (card+gap))', async () => {
const creator = makeCreator()
const wrapper = mount(TemplateSection, { props: { creator } })
const wrapper = await mountAndSettle(creator)
const list = wrapper.find('.template-section__list').element as HTMLElement
// CARD_WIDTH=160, CARD_GAP=12: card+gap=172, clientWidth-172=28 -> max is 172.
setScrollMetrics(list, { scrollLeft: 0, scrollWidth: 500, clientWidth: 200 })
Expand All @@ -199,7 +219,7 @@ describe('TemplateSection > scroll arrows', () => {

it('scrolls left as a negative step when the left arrow is clicked', async () => {
const creator = makeCreator()
const wrapper = mount(TemplateSection, { props: { creator } })
const wrapper = await mountAndSettle(creator)
const list = wrapper.find('.template-section__list').element as HTMLElement
setScrollMetrics(list, { scrollLeft: 300, scrollWidth: 500, clientWidth: 200 })
await wrapper.find('.template-section__list').trigger('scroll')
Expand Down
49 changes: 14 additions & 35 deletions src/components/TemplateSection.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
-->

<script setup lang="ts">
import { computed, onMounted, onUnmounted, ref, watch } from 'vue'
import { computed, ref, watch } from 'vue'
import { translate as t } from '@nextcloud/l10n'
import { generateUrl } from '@nextcloud/router'
import NcButton from '@nextcloud/vue/components/NcButton'
import NcIconSvgWrapper from '@nextcloud/vue/components/NcIconSvgWrapper'
import { mdiChevronLeft, mdiChevronRight } from '@mdi/js'
import { useScrollArrows } from '../composables/useScrollArrows.ts'
import { creatorTheme, previewAspectRatio } from '../utils/creatorTheme.ts'
import type { TemplateCreator, TemplateFile } from '../services/templates.ts'
import type { CreatorTheme } from '../utils/creatorTheme.ts'
Expand All @@ -30,10 +31,13 @@ const props = defineProps<{ creator: TemplateCreator }>()
const emit = defineEmits<{ select: [creator: TemplateCreator, template: TemplateFile | null] }>()

const list = ref<HTMLUListElement | null>(null)
const canScrollLeft = ref(false)
const canScrollRight = ref(false)
const failedPreviews = ref<Record<number, boolean>>({})
let resizeObserver: ResizeObserver | null = null

// A step short of the full page (card+gap) leaves a card of context visible
// at the edge the user scrolled from.
const { canScrollLeft, canScrollRight, scrollByStep, scrollToStart } = useScrollArrows(list, {
step: () => Math.max(CARD_WIDTH + CARD_GAP, (list.value?.clientWidth ?? 0) - (CARD_WIDTH + CARD_GAP)),
})

const themeType = computed(() => creatorTheme(props.creator))

Expand Down Expand Up @@ -62,26 +66,11 @@ const items = computed(() => [{ blank: true as const }, ...(props.creator.templa

watch(() => props.creator, () => {
// New creator means a different template count — jump back to the start.
if (list.value) {
list.value.scrollLeft = 0
}
updateArrows()
// Recomputing canScrollLeft/canScrollRight is useScrollArrows' job: the
// template list swapping is a DOM mutation it already observes.
scrollToStart()
})

function updateArrows() {
if (!list.value) return
const { scrollLeft, scrollWidth, clientWidth } = list.value
canScrollLeft.value = scrollLeft > 0
// 1px tolerance absorbs sub-pixel rounding at the far end.
canScrollRight.value = scrollLeft + clientWidth < scrollWidth - 1
}

function scrollByStep(direction: number) {
if (!list.value) return
const step = Math.max(CARD_WIDTH + CARD_GAP, list.value.clientWidth - (CARD_WIDTH + CARD_GAP))
list.value.scrollBy({ left: direction * step, behavior: 'smooth' })
}

function nameWithoutExt(basename: string) {
const dot = basename.lastIndexOf('.')
return dot > 0 ? basename.slice(0, dot) : basename
Expand All @@ -93,18 +82,6 @@ function templatePreviewUrl(template: TemplateFile) {
}
return generateUrl('/core/preview?fileId={fileid}&x=256&y=256&a=1', { fileid: template.fileid })
}

onMounted(() => {
resizeObserver = new ResizeObserver(() => updateArrows())
if (list.value) {
resizeObserver.observe(list.value)
}
updateArrows()
})

onUnmounted(() => {
resizeObserver?.disconnect()
})
</script>

<template>
Expand Down Expand Up @@ -136,7 +113,9 @@ onUnmounted(() => {
</div>
</div>

<ul ref="list" class="template-section__list" @scroll="updateArrows">
<ul ref="list"
class="template-section__list"
:aria-label="t('office', '{creator} templates', { creator: creator.label })">
<li v-for="item in items"
:key="'blank' in item ? 'blank' : item.fileid"
class="template-section__item">
Expand Down
88 changes: 88 additions & 0 deletions src/composables/useScrollArrows.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { nextTick, ref } from 'vue'
import { describe, expect, it, vi } from 'vitest'
import { useScrollArrows } from './useScrollArrows.ts'

function makeScrollableElement({ scrollLeft = 0, scrollWidth = 0, clientWidth = 0 } = {}) {
const el = document.createElement('ul')
Object.defineProperty(el, 'scrollLeft', { value: scrollLeft, writable: true, configurable: true })
Object.defineProperty(el, 'scrollWidth', { value: scrollWidth, configurable: true })
Object.defineProperty(el, 'clientWidth', { value: clientWidth, configurable: true })
el.scrollBy = vi.fn()
document.body.appendChild(el)
return el
}

describe('useScrollArrows', () => {
it('reflects the element\'s scroll position at creation', () => {
const el = makeScrollableElement({ scrollLeft: 0, scrollWidth: 500, clientWidth: 200 })

const { canScrollLeft, canScrollRight } = useScrollArrows(ref(el))

expect(canScrollLeft.value).toBe(false)
expect(canScrollRight.value).toBe(true)
})

it('hides both arrows when the content does not overflow', () => {
const el = makeScrollableElement({ scrollLeft: 0, scrollWidth: 100, clientWidth: 100 })

const { canScrollLeft, canScrollRight } = useScrollArrows(ref(el))

expect(canScrollLeft.value).toBe(false)
expect(canScrollRight.value).toBe(false)
})

it('updates on a real scroll event', async () => {
const el = makeScrollableElement({ scrollLeft: 0, scrollWidth: 500, clientWidth: 200 })
const { canScrollLeft, canScrollRight } = useScrollArrows(ref(el))

Object.defineProperty(el, 'scrollLeft', { value: 300, configurable: true })
el.dispatchEvent(new Event('scroll'))
await nextTick()

expect(canScrollLeft.value).toBe(true)
expect(canScrollRight.value).toBe(false)
})

describe('scrollByStep', () => {
it('scrolls by the element\'s clientWidth by default', () => {
const el = makeScrollableElement({ clientWidth: 250 })
const { scrollByStep } = useScrollArrows(ref(el))

scrollByStep(1)

expect(el.scrollBy).toHaveBeenCalledWith({ left: 250, behavior: 'smooth' })
})

it('scrolls left as a negative step', () => {
const el = makeScrollableElement({ clientWidth: 250 })
const { scrollByStep } = useScrollArrows(ref(el))

scrollByStep(-1)

expect(el.scrollBy).toHaveBeenCalledWith({ left: -250, behavior: 'smooth' })
})

it('uses the step option over the element\'s clientWidth when given', () => {
const el = makeScrollableElement({ clientWidth: 250 })
const { scrollByStep } = useScrollArrows(ref(el), { step: 172 })

scrollByStep(1)

expect(el.scrollBy).toHaveBeenCalledWith({ left: 172, behavior: 'smooth' })
})
})

it('scrollToStart resets scrollLeft to 0', () => {
const el = makeScrollableElement({ scrollLeft: 300, scrollWidth: 500, clientWidth: 200 })
const { scrollToStart } = useScrollArrows(ref(el))

scrollToStart()

expect(el.scrollLeft).toBe(0)
})
})
53 changes: 53 additions & 0 deletions src/composables/useScrollArrows.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { computed, toValue } from 'vue'
import { useResizeObserver, useScroll } from '@vueuse/core'
import type { MaybeRefOrGetter } from 'vue'

export interface UseScrollArrowsOptions {
/**
* Distance to scroll per arrow click, in pixels. Defaults to the
* element's own `clientWidth` — a full page — when omitted.
*/
step?: MaybeRefOrGetter<number>
}

/**
* Tracks whether a horizontally-scrollable element can still scroll left or
* right, and exposes the scroll actions an arrow-button pair needs. Built on
* `useScroll`'s `arrivedState` rather than reading `scrollLeft`/`scrollWidth`
* by hand — it already recomputes on scroll, on mount, and (with
* `observe.mutation`) when the content itself changes, so only element
* resize (e.g. the window resizing) needs wiring up here.
*
* @param element the scrollable element, or a ref/getter for one
* @param options behaviour overrides
*/
export function useScrollArrows(
element: MaybeRefOrGetter<HTMLElement | null | undefined>,
options: UseScrollArrowsOptions = {},
) {
const { arrivedState, measure } = useScroll(element, { observe: { mutation: true } })

useResizeObserver(element, () => measure())

const canScrollLeft = computed(() => !arrivedState.left)
const canScrollRight = computed(() => !arrivedState.right)

function scrollByStep(direction: -1 | 1) {
const el = toValue(element)
if (!el) return
const distance = toValue(options.step) ?? el.clientWidth
el.scrollBy({ left: direction * distance, behavior: 'smooth' })
}

function scrollToStart() {
const el = toValue(element)
if (el) el.scrollLeft = 0
}

return { canScrollLeft, canScrollRight, scrollByStep, scrollToStart }
}
Loading