Add numbered pagination and redesign the IT/EN language switch
Blog and category archives now show a numbered page list (first, last, current +/-1, collapsing gaps into an ellipsis) instead of only previous/next — see shared/utils/pagination.ts's paginationRange(). Renders page links via NuxtLink's custom/v-slot API rather than letting it render its own <a>: RouterLink's built-in active-class/aria-current detection compares only the route's path, not its query string, so every ?page=N link was incorrectly marked as the current page. The language switch now shows both "Italiano / English" at all times, with the current one as plain non-interactive text and the other as a link, moved to the header's top-right corner instead of stacked under the logo.
This commit is contained in:
1 parent
364fda3980
commit
1247a2ca07
12 files changed
+369
-67
No files matched your search
+11
-2
@@ -155,8 +155,17 @@ layout shift results.
|
|||||||
prefix and date formatting.
|
prefix and date formatting.
|
||||||
- `app/components/SocialIcon.vue` — inlines SVG brand marks from `simple-icons` at build time
|
- `app/components/SocialIcon.vue` — inlines SVG brand marks from `simple-icons` at build time
|
||||||
(`?raw` imports) rather than bundling the whole icon set.
|
(`?raw` imports) rather than bundling the whole icon set.
|
||||||
- `app/components/LanguageSwitch.vue` — the header's language-switch link; reads
|
- `app/components/LanguageSwitch.vue` — the header's language toggle: both language names are
|
||||||
`useLangSwitchState()` and `useLocale()`, computes the equivalent path in the other language.
|
always shown (`Italiano / English`), the current one as plain non-interactive text
|
||||||
|
(`aria-current="true"`), the other as a link — reads `useLangSwitchState()` and `useLocale()` to
|
||||||
|
compute the target path, per the fallback rules in [English content](#english-content).
|
||||||
|
- `app/components/PaginationNav.vue` — numbered pagination (not just prev/next): first, last, the
|
||||||
|
current page and its immediate neighbors, collapsing gaps into a single `…` via
|
||||||
|
`shared/utils/pagination.ts`'s `paginationRange()`. Used by `BlogIndexView`/`CategoryView`.
|
||||||
|
Renders page links via `NuxtLink`'s `custom`/`v-slot` API rather than letting the component
|
||||||
|
render its own `<a>` — `RouterLink`'s built-in active-class/`aria-current` detection compares
|
||||||
|
only the route's `path`, not its query string, so with plain `NuxtLink`s every `?page=N` link
|
||||||
|
would end up (wrongly) marked as the current page.
|
||||||
- `app/components/views/*.vue` (`HomeView`, `BlogIndexView`, `ArticleView`, `CategoryView`) — the
|
- `app/components/views/*.vue` (`HomeView`, `BlogIndexView`, `ArticleView`, `CategoryView`) — the
|
||||||
actual page markup/logic, parametrized by a `locale` prop; the real `pages/**` and `pages/en/**`
|
actual page markup/logic, parametrized by a `locale` prop; the real `pages/**` and `pages/en/**`
|
||||||
files are thin wrappers around these (see [English content](#english-content)). Registered
|
files are thin wrappers around these (see [English content](#english-content)). Registered
|
||||||
|
|||||||
@@ -94,6 +94,19 @@ h3 {
|
|||||||
color: var(--color-muted);
|
color: var(--color-muted);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Visually hidden but still announced by screen readers. */
|
||||||
|
.sr-only {
|
||||||
|
position: absolute;
|
||||||
|
width: 1px;
|
||||||
|
height: 1px;
|
||||||
|
padding: 0;
|
||||||
|
margin: -1px;
|
||||||
|
overflow: hidden;
|
||||||
|
clip: rect(0, 0, 0, 0);
|
||||||
|
white-space: nowrap;
|
||||||
|
border: 0;
|
||||||
|
}
|
||||||
|
|
||||||
/* Small uppercase label used for categories, dates and section headings. */
|
/* Small uppercase label used for categories, dates and section headings. */
|
||||||
.kicker {
|
.kicker {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
@@ -223,16 +236,3 @@ h3 {
|
|||||||
font-size: 0.9em;
|
font-size: 0.9em;
|
||||||
}
|
}
|
||||||
|
|
||||||
.pagination {
|
|
||||||
display: flex;
|
|
||||||
flex-wrap: wrap;
|
|
||||||
gap: 1rem 2rem;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: center;
|
|
||||||
margin-top: var(--gap-section);
|
|
||||||
padding-top: 2rem;
|
|
||||||
border-top: 1px solid var(--color-border);
|
|
||||||
font-size: 0.85rem;
|
|
||||||
letter-spacing: 0.06em;
|
|
||||||
text-transform: uppercase;
|
|
||||||
}
|
|
||||||
@@ -1,37 +1,71 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { STRINGS } from '#shared/utils/i18n'
|
import { STRINGS } from '#shared/utils/i18n'
|
||||||
import { bareLocalePath, localePath } from '#shared/utils/locale'
|
import { bareLocalePath, localePath } from '#shared/utils/locale'
|
||||||
|
import type { Locale } from '#shared/utils/locale'
|
||||||
|
|
||||||
const route = useRoute()
|
const route = useRoute()
|
||||||
const locale = useLocale()
|
const locale = useLocale()
|
||||||
const langSwitch = useLangSwitchState()
|
const langSwitch = useLangSwitchState()
|
||||||
|
|
||||||
const target = computed(() => {
|
const strings = computed(() => STRINGS[locale.value])
|
||||||
if (!langSwitch.value.available) return langSwitch.value.fallback
|
|
||||||
const otherLocale = locale.value === 'en' ? 'it' : 'en'
|
|
||||||
return localePath(otherLocale, bareLocalePath(route.path))
|
|
||||||
})
|
|
||||||
|
|
||||||
const label = computed(() => STRINGS[locale.value].langSwitchLabel)
|
function targetFor(otherLocale: Locale): string {
|
||||||
|
if (!langSwitch.value.available) return langSwitch.value.fallback
|
||||||
|
return localePath(otherLocale, bareLocalePath(route.path))
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<NuxtLink class="lang-switch" :to="target">{{ label }}</NuxtLink>
|
<div class="lang-switch" :aria-label="strings.langSwitch.ariaLabel" role="group">
|
||||||
|
<span v-if="locale === 'it'" class="lang-option is-current" aria-current="true">
|
||||||
|
{{ strings.langSwitch.it }}
|
||||||
|
</span>
|
||||||
|
<NuxtLink v-else class="lang-option" :to="targetFor('it')">
|
||||||
|
{{ strings.langSwitch.it }}
|
||||||
|
</NuxtLink>
|
||||||
|
|
||||||
|
<span class="lang-divider" aria-hidden="true">/</span>
|
||||||
|
|
||||||
|
<span v-if="locale === 'en'" class="lang-option is-current" aria-current="true">
|
||||||
|
{{ strings.langSwitch.en }}
|
||||||
|
</span>
|
||||||
|
<NuxtLink v-else class="lang-option" :to="targetFor('en')">
|
||||||
|
{{ strings.langSwitch.en }}
|
||||||
|
</NuxtLink>
|
||||||
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
.lang-switch {
|
.lang-switch {
|
||||||
display: inline-block;
|
display: inline-flex;
|
||||||
font-size: 0.75rem;
|
align-items: center;
|
||||||
|
gap: 0.4rem;
|
||||||
|
font-size: 0.7rem;
|
||||||
font-weight: 600;
|
font-weight: 600;
|
||||||
letter-spacing: 0.14em;
|
letter-spacing: 0.1em;
|
||||||
text-transform: uppercase;
|
text-transform: uppercase;
|
||||||
|
}
|
||||||
|
|
||||||
|
.lang-option {
|
||||||
|
display: inline-block;
|
||||||
text-decoration: none;
|
text-decoration: none;
|
||||||
padding-block: 0.6rem;
|
color: var(--color-muted);
|
||||||
|
padding-block: 0.25rem;
|
||||||
border-bottom: 1px solid transparent;
|
border-bottom: 1px solid transparent;
|
||||||
}
|
}
|
||||||
|
|
||||||
.lang-switch:hover {
|
a.lang-option:hover,
|
||||||
|
a.lang-option:focus-visible {
|
||||||
|
color: var(--color-ink);
|
||||||
border-bottom-color: var(--color-ink);
|
border-bottom-color: var(--color-ink);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.lang-option.is-current {
|
||||||
|
color: var(--color-ink);
|
||||||
|
border-bottom-color: var(--color-ink);
|
||||||
|
}
|
||||||
|
|
||||||
|
.lang-divider {
|
||||||
|
color: var(--color-border);
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
@@ -0,0 +1,131 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import type { Locale } from '#shared/utils/locale'
|
||||||
|
import { STRINGS, interpolate } from '#shared/utils/i18n'
|
||||||
|
import { paginationRange } from '#shared/utils/pagination'
|
||||||
|
|
||||||
|
const props = defineProps<{ page: number; pageCount: number; locale: Locale }>()
|
||||||
|
|
||||||
|
const strings = computed(() => STRINGS[props.locale])
|
||||||
|
const items = computed(() => paginationRange(props.page, props.pageCount))
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<nav v-if="pageCount > 1" class="pagination" :aria-label="strings.blog.paginationAriaLabel">
|
||||||
|
<!-- `custom` bypasses NuxtLink/RouterLink's own active-class and aria-current
|
||||||
|
computation, which compares only the path, not the query string — every
|
||||||
|
page link would otherwise be (wrongly) marked as the current page. -->
|
||||||
|
<NuxtLink
|
||||||
|
v-if="page > 1"
|
||||||
|
v-slot="{ href, navigate }"
|
||||||
|
:to="{ query: { page: page - 1 } }"
|
||||||
|
custom
|
||||||
|
>
|
||||||
|
<a :href="href ?? undefined" class="pagination-arrow" rel="prev" :aria-label="strings.blog.prevPage" @click="navigate">
|
||||||
|
<span aria-hidden="true">‹</span>
|
||||||
|
</a>
|
||||||
|
</NuxtLink>
|
||||||
|
<span v-else class="pagination-arrow is-disabled" aria-hidden="true">‹</span>
|
||||||
|
|
||||||
|
<ul class="pagination-list">
|
||||||
|
<li v-for="(item, index) in items" :key="index">
|
||||||
|
<span v-if="item === 'ellipsis'" class="pagination-ellipsis" aria-hidden="true">…</span>
|
||||||
|
<span v-else-if="item === page" class="pagination-link is-current" aria-current="page">
|
||||||
|
{{ item }}
|
||||||
|
</span>
|
||||||
|
<NuxtLink v-else v-slot="{ href, navigate }" :to="{ query: { page: item } }" custom>
|
||||||
|
<a
|
||||||
|
:href="href ?? undefined"
|
||||||
|
class="pagination-link"
|
||||||
|
:aria-label="interpolate(strings.blog.goToPage, { page: item })"
|
||||||
|
@click="navigate"
|
||||||
|
>
|
||||||
|
{{ item }}
|
||||||
|
</a>
|
||||||
|
</NuxtLink>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<NuxtLink
|
||||||
|
v-if="page < pageCount"
|
||||||
|
v-slot="{ href, navigate }"
|
||||||
|
:to="{ query: { page: page + 1 } }"
|
||||||
|
custom
|
||||||
|
>
|
||||||
|
<a :href="href ?? undefined" class="pagination-arrow" rel="next" :aria-label="strings.blog.nextPage" @click="navigate">
|
||||||
|
<span aria-hidden="true">›</span>
|
||||||
|
</a>
|
||||||
|
</NuxtLink>
|
||||||
|
<span v-else class="pagination-arrow is-disabled" aria-hidden="true">›</span>
|
||||||
|
|
||||||
|
<p class="sr-only" role="status">
|
||||||
|
{{ interpolate(strings.blog.pageOf, { current: page, total: pageCount }) }}
|
||||||
|
</p>
|
||||||
|
</nav>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.pagination {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
gap: 0.5rem;
|
||||||
|
margin-top: var(--gap-section);
|
||||||
|
padding-top: 2rem;
|
||||||
|
border-top: 1px solid var(--color-border);
|
||||||
|
}
|
||||||
|
|
||||||
|
.pagination-list {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.25rem;
|
||||||
|
padding: 0;
|
||||||
|
margin: 0;
|
||||||
|
list-style: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.pagination-arrow,
|
||||||
|
.pagination-link {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
min-width: 2rem;
|
||||||
|
height: 2rem;
|
||||||
|
padding-inline: 0.4rem;
|
||||||
|
border-radius: var(--radius);
|
||||||
|
font-size: 0.85rem;
|
||||||
|
text-decoration: none;
|
||||||
|
color: var(--color-body);
|
||||||
|
}
|
||||||
|
|
||||||
|
.pagination-arrow {
|
||||||
|
font-size: 1.1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.pagination-arrow.is-disabled {
|
||||||
|
color: var(--color-border);
|
||||||
|
}
|
||||||
|
|
||||||
|
.pagination-link:hover {
|
||||||
|
background: var(--color-surface);
|
||||||
|
color: var(--color-ink);
|
||||||
|
}
|
||||||
|
|
||||||
|
.pagination-arrow:not(.is-disabled):hover {
|
||||||
|
background: var(--color-surface);
|
||||||
|
color: var(--color-ink);
|
||||||
|
}
|
||||||
|
|
||||||
|
.pagination-link.is-current {
|
||||||
|
background: var(--color-ink);
|
||||||
|
color: var(--color-bg);
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
|
||||||
|
.pagination-ellipsis {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
min-width: 1.5rem;
|
||||||
|
color: var(--color-muted);
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -47,21 +47,7 @@ useSeo({
|
|||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<nav v-if="data.pageCount > 1" class="pagination" :aria-label="strings.blog.paginationAriaLabel">
|
<PaginationNav :page="data.page" :page-count="data.pageCount" :locale="locale" />
|
||||||
<NuxtLink v-if="data.page > 1" :to="{ query: { page: data.page - 1 } }" rel="prev">
|
|
||||||
{{ strings.blog.prevPage }}
|
|
||||||
</NuxtLink>
|
|
||||||
<span class="muted">
|
|
||||||
{{ interpolate(strings.blog.pageOf, { current: data.page, total: data.pageCount }) }}
|
|
||||||
</span>
|
|
||||||
<NuxtLink
|
|
||||||
v-if="data.page < data.pageCount"
|
|
||||||
:to="{ query: { page: data.page + 1 } }"
|
|
||||||
rel="next"
|
|
||||||
>
|
|
||||||
{{ strings.blog.nextPage }}
|
|
||||||
</NuxtLink>
|
|
||||||
</nav>
|
|
||||||
</template>
|
</template>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
@@ -55,21 +55,7 @@ useSeo({
|
|||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<nav v-if="data.pageCount > 1" class="pagination" :aria-label="strings.blog.paginationAriaLabel">
|
<PaginationNav :page="data.page" :page-count="data.pageCount" :locale="locale" />
|
||||||
<NuxtLink v-if="data.page > 1" :to="{ query: { page: data.page - 1 } }" rel="prev">
|
|
||||||
{{ strings.blog.prevPage }}
|
|
||||||
</NuxtLink>
|
|
||||||
<span class="muted">
|
|
||||||
{{ interpolate(strings.blog.pageOf, { current: data.page, total: data.pageCount }) }}
|
|
||||||
</span>
|
|
||||||
<NuxtLink
|
|
||||||
v-if="data.page < data.pageCount"
|
|
||||||
:to="{ query: { page: data.page + 1 } }"
|
|
||||||
rel="next"
|
|
||||||
>
|
|
||||||
{{ strings.blog.nextPage }}
|
|
||||||
</NuxtLink>
|
|
||||||
</nav>
|
|
||||||
</template>
|
</template>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
@@ -11,16 +11,15 @@ const strings = computed(() => STRINGS[locale.value])
|
|||||||
<a class="skip-link" href="#main">{{ strings.skipLink }}</a>
|
<a class="skip-link" href="#main">{{ strings.skipLink }}</a>
|
||||||
|
|
||||||
<header class="site-header">
|
<header class="site-header">
|
||||||
<div class="container">
|
<div class="container header-inner">
|
||||||
|
<LanguageSwitch class="lang-switch-header" />
|
||||||
|
|
||||||
<p class="wordmark">
|
<p class="wordmark">
|
||||||
<NuxtLink :to="localePath(locale, '/')">
|
<NuxtLink :to="localePath(locale, '/')">
|
||||||
<img src="/logo.png" :alt="SITE_NAME" width="400" height="400">
|
<img src="/logo.png" :alt="SITE_NAME" width="400" height="400">
|
||||||
</NuxtLink>
|
</NuxtLink>
|
||||||
</p>
|
</p>
|
||||||
<p class="tagline kicker">{{ strings.tagline }}</p>
|
<p class="tagline kicker">{{ strings.tagline }}</p>
|
||||||
<p class="lang-switch-header">
|
|
||||||
<LanguageSwitch />
|
|
||||||
</p>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<nav class="site-nav" :aria-label="strings.navAriaLabel">
|
<nav class="site-nav" :aria-label="strings.navAriaLabel">
|
||||||
@@ -102,6 +101,16 @@ const strings = computed(() => STRINGS[locale.value])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.header-inner {
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
|
||||||
|
.lang-switch-header {
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
right: var(--space);
|
||||||
|
}
|
||||||
|
|
||||||
.wordmark {
|
.wordmark {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
}
|
}
|
||||||
@@ -121,10 +130,6 @@ const strings = computed(() => STRINGS[locale.value])
|
|||||||
margin-top: 0.6rem;
|
margin-top: 0.6rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
.lang-switch-header {
|
|
||||||
margin-top: 0.5rem;
|
|
||||||
}
|
|
||||||
|
|
||||||
.site-nav {
|
.site-nav {
|
||||||
margin-top: 1.25rem;
|
margin-top: 1.25rem;
|
||||||
border-top: 1px solid var(--color-border);
|
border-top: 1px solid var(--color-border);
|
||||||
|
|||||||
@@ -20,7 +20,11 @@ export const STRINGS = {
|
|||||||
footerFollowAriaLabel: 'Seguici',
|
footerFollowAriaLabel: 'Seguici',
|
||||||
contactLabel: 'Contatti:',
|
contactLabel: 'Contatti:',
|
||||||
copyright: 'Tutti i diritti riservati.',
|
copyright: 'Tutti i diritti riservati.',
|
||||||
langSwitchLabel: 'English',
|
langSwitch: {
|
||||||
|
ariaLabel: 'Lingua del sito',
|
||||||
|
it: 'Italiano',
|
||||||
|
en: 'English',
|
||||||
|
},
|
||||||
home: {
|
home: {
|
||||||
heading: "La fine dell'Unione Europea per come l'abbiamo conosciuta.",
|
heading: "La fine dell'Unione Europea per come l'abbiamo conosciuta.",
|
||||||
img1Alt: 'Manifesti e scritte contro le banche durante una crisi bancaria',
|
img1Alt: 'Manifesti e scritte contro le banche durante una crisi bancaria',
|
||||||
@@ -43,6 +47,7 @@ export const STRINGS = {
|
|||||||
prevPage: 'Pagina precedente',
|
prevPage: 'Pagina precedente',
|
||||||
nextPage: 'Pagina successiva',
|
nextPage: 'Pagina successiva',
|
||||||
pageOf: 'Pagina {current} di {total}',
|
pageOf: 'Pagina {current} di {total}',
|
||||||
|
goToPage: 'Vai a pagina {page}',
|
||||||
paginationAriaLabel: 'Paginazione',
|
paginationAriaLabel: 'Paginazione',
|
||||||
},
|
},
|
||||||
category: {
|
category: {
|
||||||
@@ -71,7 +76,11 @@ export const STRINGS = {
|
|||||||
footerFollowAriaLabel: 'Follow us',
|
footerFollowAriaLabel: 'Follow us',
|
||||||
contactLabel: 'Contact:',
|
contactLabel: 'Contact:',
|
||||||
copyright: 'All rights reserved.',
|
copyright: 'All rights reserved.',
|
||||||
langSwitchLabel: 'Italiano',
|
langSwitch: {
|
||||||
|
ariaLabel: 'Site language',
|
||||||
|
it: 'Italiano',
|
||||||
|
en: 'English',
|
||||||
|
},
|
||||||
home: {
|
home: {
|
||||||
heading: "The end of the European Union as we've known it.",
|
heading: "The end of the European Union as we've known it.",
|
||||||
img1Alt: 'Banners and graffiti against banks during a banking crisis',
|
img1Alt: 'Banners and graffiti against banks during a banking crisis',
|
||||||
@@ -94,6 +103,7 @@ export const STRINGS = {
|
|||||||
prevPage: 'Previous page',
|
prevPage: 'Previous page',
|
||||||
nextPage: 'Next page',
|
nextPage: 'Next page',
|
||||||
pageOf: 'Page {current} of {total}',
|
pageOf: 'Page {current} of {total}',
|
||||||
|
goToPage: 'Go to page {page}',
|
||||||
paginationAriaLabel: 'Pagination',
|
paginationAriaLabel: 'Pagination',
|
||||||
},
|
},
|
||||||
category: {
|
category: {
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
export type PaginationItem = number | 'ellipsis'
|
||||||
|
|
||||||
|
function range(start: number, end: number): number[] {
|
||||||
|
const length = end - start + 1
|
||||||
|
return Array.from({ length }, (_, i) => start + i)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Windowed page list for numbered pagination: always shows the first and
|
||||||
|
* last page, the current page and its immediate siblings, collapsing any
|
||||||
|
* gap into a single 'ellipsis' marker. Returns every page when the total
|
||||||
|
* is small enough that no collapsing is needed.
|
||||||
|
*/
|
||||||
|
export function paginationRange(current: number, total: number, siblingCount = 1): PaginationItem[] {
|
||||||
|
if (total <= 0) return []
|
||||||
|
|
||||||
|
const totalVisible = siblingCount * 2 + 5 // first + last + current + 2 siblings + 2 ellipses
|
||||||
|
if (totalVisible >= total) return range(1, total)
|
||||||
|
|
||||||
|
const leftSibling = Math.max(current - siblingCount, 1)
|
||||||
|
const rightSibling = Math.min(current + siblingCount, total)
|
||||||
|
|
||||||
|
const showLeftEllipsis = leftSibling > 2
|
||||||
|
const showRightEllipsis = rightSibling < total - 1
|
||||||
|
|
||||||
|
if (!showLeftEllipsis && showRightEllipsis) {
|
||||||
|
const leftRange = range(1, 3 + siblingCount * 2)
|
||||||
|
return [...leftRange, 'ellipsis', total]
|
||||||
|
}
|
||||||
|
|
||||||
|
if (showLeftEllipsis && !showRightEllipsis) {
|
||||||
|
const rightRange = range(total - (3 + siblingCount * 2) + 1, total)
|
||||||
|
return [1, 'ellipsis', ...rightRange]
|
||||||
|
}
|
||||||
|
|
||||||
|
return [1, 'ellipsis', ...range(leftSibling, rightSibling), 'ellipsis', total]
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
import { describe, expect, it } from 'vitest'
|
||||||
|
import { mountSuspended, mockNuxtImport } from '@nuxt/test-utils/runtime'
|
||||||
|
import { ref } from 'vue'
|
||||||
|
import LanguageSwitch from '~/components/LanguageSwitch.vue'
|
||||||
|
|
||||||
|
const langSwitchState = ref({ available: true, fallback: '/en/blog' })
|
||||||
|
|
||||||
|
mockNuxtImport('useRoute', () => () => ({ path: '/blog/mio-slug' }))
|
||||||
|
mockNuxtImport('useLocale', () => () => ref('it'))
|
||||||
|
mockNuxtImport('useLangSwitchState', () => () => langSwitchState)
|
||||||
|
|
||||||
|
describe('LanguageSwitch', () => {
|
||||||
|
it('shows the current language as non-interactive and the other as a link to /en', async () => {
|
||||||
|
langSwitchState.value = { available: true, fallback: '/en/blog' }
|
||||||
|
|
||||||
|
const wrapper = await mountSuspended(LanguageSwitch)
|
||||||
|
|
||||||
|
expect(wrapper.find('.lang-option.is-current').text()).toBe('Italiano')
|
||||||
|
expect(wrapper.find('.lang-option.is-current').attributes('aria-current')).toBe('true')
|
||||||
|
|
||||||
|
const link = wrapper.find('a.lang-option')
|
||||||
|
expect(link.text()).toBe('English')
|
||||||
|
expect(link.attributes('href')).toBe('/en/blog/mio-slug')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('falls back to the given path when the current content has no translation', async () => {
|
||||||
|
langSwitchState.value = { available: false, fallback: '/en/blog' }
|
||||||
|
|
||||||
|
const wrapper = await mountSuspended(LanguageSwitch)
|
||||||
|
expect(wrapper.find('a.lang-option').attributes('href')).toBe('/en/blog')
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
import { describe, expect, it } from 'vitest'
|
||||||
|
import { mountSuspended } from '@nuxt/test-utils/runtime'
|
||||||
|
import PaginationNav from '~/components/PaginationNav.vue'
|
||||||
|
|
||||||
|
describe('PaginationNav', () => {
|
||||||
|
it('renders nothing when there is only one page', async () => {
|
||||||
|
const wrapper = await mountSuspended(PaginationNav, { props: { page: 1, pageCount: 1, locale: 'it' } })
|
||||||
|
expect(wrapper.find('nav').exists()).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('marks only the current page as aria-current, others as plain links', async () => {
|
||||||
|
const wrapper = await mountSuspended(PaginationNav, { props: { page: 4, pageCount: 7, locale: 'it' } })
|
||||||
|
const current = wrapper.find('.pagination-link.is-current')
|
||||||
|
expect(current.text()).toBe('4')
|
||||||
|
expect(current.attributes('aria-current')).toBe('page')
|
||||||
|
|
||||||
|
const links = wrapper.findAll('a.pagination-link')
|
||||||
|
expect(links.map((l) => l.text())).toEqual(['1', '2', '3', '5', '6', '7'])
|
||||||
|
for (const link of links) {
|
||||||
|
expect(link.attributes('aria-current')).toBeUndefined()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it('collapses far-away pages into an ellipsis', async () => {
|
||||||
|
const wrapper = await mountSuspended(PaginationNav, { props: { page: 1, pageCount: 20, locale: 'it' } })
|
||||||
|
expect(wrapper.find('.pagination-ellipsis').exists()).toBe(true)
|
||||||
|
expect(wrapper.findAll('a.pagination-link').length).toBeLessThan(20)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('disables the previous arrow on the first page and the next arrow on the last', async () => {
|
||||||
|
const first = await mountSuspended(PaginationNav, { props: { page: 1, pageCount: 5, locale: 'it' } })
|
||||||
|
expect(first.find('a.pagination-arrow[rel="prev"]').exists()).toBe(false)
|
||||||
|
expect(first.find('a.pagination-arrow[rel="next"]').exists()).toBe(true)
|
||||||
|
|
||||||
|
const last = await mountSuspended(PaginationNav, { props: { page: 5, pageCount: 5, locale: 'it' } })
|
||||||
|
expect(last.find('a.pagination-arrow[rel="next"]').exists()).toBe(false)
|
||||||
|
expect(last.find('a.pagination-arrow[rel="prev"]').exists()).toBe(true)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
import { describe, expect, it } from 'vitest'
|
||||||
|
import { paginationRange } from '../../frontend/shared/utils/pagination'
|
||||||
|
|
||||||
|
describe('paginationRange', () => {
|
||||||
|
it('returns an empty array for zero or negative totals', () => {
|
||||||
|
expect(paginationRange(1, 0)).toEqual([])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns every page when the total fits within the visible window', () => {
|
||||||
|
expect(paginationRange(1, 5)).toEqual([1, 2, 3, 4, 5])
|
||||||
|
expect(paginationRange(3, 7)).toEqual([1, 2, 3, 4, 5, 6, 7])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('collapses the right side when near the start', () => {
|
||||||
|
expect(paginationRange(1, 20)).toEqual([1, 2, 3, 4, 5, 'ellipsis', 20])
|
||||||
|
expect(paginationRange(2, 20)).toEqual([1, 2, 3, 4, 5, 'ellipsis', 20])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('collapses the left side when near the end', () => {
|
||||||
|
expect(paginationRange(20, 20)).toEqual([1, 'ellipsis', 16, 17, 18, 19, 20])
|
||||||
|
expect(paginationRange(19, 20)).toEqual([1, 'ellipsis', 16, 17, 18, 19, 20])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('collapses both sides when in the middle', () => {
|
||||||
|
expect(paginationRange(10, 20)).toEqual([1, 'ellipsis', 9, 10, 11, 'ellipsis', 20])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('always includes the current page', () => {
|
||||||
|
for (let current = 1; current <= 27; current++) {
|
||||||
|
expect(paginationRange(current, 27)).toContain(current)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
Reference in new issue
Block a user