huntabyte / shadcn-svelte

shadcn/ui, but for Svelte. ✨
https://next.shadcn-svelte.com
MIT License
5.41k stars 339 forks source link

Use links for pagination #945

Closed jasongitmail closed 7 months ago

jasongitmail commented 7 months ago

Describe the feature

My understanding is the pagination element uses onPageChange() to programmatically navigate.

This creates downsides related to progressive enhancement (links won't work for SSR'd pages when user has JS disabled), SEO (b/c no anchor elements for a crawler to follow), and potentially speed (but I haven't confirmed if SvelteKit preloads destination pages, b/c I decided to keep my custom pagination element for now for the reasons described here).

An alternate approach could be to:

  1. Use a elements for the page links.
  2. Allow the component to accept a property representing a function that specifies how to format the href values. Then you can use this to build the href values and it's a simpler DX than needing to import the correct function to programmatically navigate and write code for this. E.g.
href=((page: number) => `/blog/${page}`}

The caveat is that the current callback may be more flexible for something like in-page tables, but you could argue the dev should be updating the URL state and that nudges them in the right direction. Or alternatively, onPageChange() could be retained as a fallback if the href prop is not specified, although this brings some added implementation complexity.

(Will let you transfer the issue to Bits UI if you prefer it to live there.)

huntabyte commented 7 months ago

Hey Jason, you should be able to use the asChild prop and render a link to get the behavior you would expect. You are correct in that the reason it doesn't use it by default as there are a number of use cases for pagination.

jasongitmail commented 7 months ago

An example would be helpful in the docs since this will be a very common need for pagination, but its not clear about how to currently