Files
photon-tbw/src/lib/components/ui/Pageination.svelte
T
Xylight 76c581123a fix: Fix lint errors (#559)
* cleanup: postactions styling

* misc: fix first round of errors

* pkg: remove vitest

* fix: fix lint errors stage 2

* fix: fix lint errors stage 3

* fix: fix lint errors, stage 3

* misc: update eslint rules to warn for writable derived
2025-06-09 16:34:06 -07:00

104 lines
2.8 KiB
Svelte

<script lang="ts">
import { t } from '$lib/i18n/translations'
import { Button } from 'mono-svelte'
import { ChevronLeft, ChevronRight, Icon } from 'svelte-hero-icons'
import { backOut } from 'svelte/easing'
import { fly } from 'svelte/transition'
import { page as pageData } from '$app/state'
import { invalidate } from '$app/navigation'
interface Props {
page?: number
cursor?: { next?: string; back?: string } | undefined
hasMore?: boolean
children?: import('svelte').Snippet
href?: (current: number | string) => string
back?: boolean
}
let {
page = $bindable(0),
cursor = undefined,
hasMore = true,
children,
href,
back = true,
}: Props = $props()
let customHref = (href?: string) => {
if (href?.startsWith('?')) {
const current = new URLSearchParams(pageData.url.searchParams)
const newParams = new URLSearchParams(href)
current.delete(Array.from(newParams.keys())[0])
current.append(
Array.from(newParams.entries())[0][0],
Array.from(newParams.entries())[0][1],
)
return `?${current.toString()}`
} else return href
}
</script>
<nav
aria-label={$t('aria.pagination.nav')}
class="flex flex-row w-full gap-4 items-center justify-center"
>
{#if children}
<span class="text-sm text-slate-600 dark:text-zinc-400 font-medium">
{@render children?.()}
</span>
<hr class="border-slate-200 dark:border-zinc-800 flex-1" />
{/if}
{#if back}
<Button
href={customHref(href?.(cursor?.back ?? page - 1))}
color="ghost"
onclick={() => invalidate(pageData.url)}
title={$t('common.back')}
size="square-md"
rounding="pill"
class="text-inherit dark:text-inherit disabled:opacity-20! disabled:bg-transparent!"
disabled={(cursor?.back == undefined && cursor?.next != undefined) ||
page <= 0}
data-sveltekit-preload-data="off"
>
{#snippet suffix()}
<Icon src={ChevronLeft} size="24" mini />
{/snippet}
</Button>
{/if}
{#if page}
<div style="display: grid;">
{#key page}
<div
in:fly={{ y: 8, easing: backOut, duration: 400, delay: 100 }}
out:fly={{ y: 8, easing: backOut, duration: 400 }}
class="text-md font-medium"
style="grid-column: 1; grid-row: 1;"
>
{page}
</div>
{/key}
</div>
{/if}
<Button
href={customHref(href?.(cursor?.next ?? page + 1))}
color="ghost"
onclick={() => invalidate(pageData.url)}
title={$t('common.next')}
size="square-md"
rounding="pill"
class="text-inherit dark:text-inherit"
disabled={!hasMore}
data-sveltekit-preload-data="off"
>
{#snippet suffix()}
<Icon src={ChevronRight} size="24" mini />
{/snippet}
</Button>
</nav>