Skip to content

Commit 022039c

Browse files
committed
fix: pagination update
1 parent faaf129 commit 022039c

File tree

13 files changed

+299
-21
lines changed

13 files changed

+299
-21
lines changed

src/lib/alert/theme.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ const alert = tv({
3030
fuchsia: "bg-fuchsia-100 text-fuchsia-500 hover:bg-fuchsia-200 focus:ring-fuchsia-400 dark:bg-fuchsia-200 dark:text-fuchsia-600 dark:hover:bg-fuchsia-300",
3131
pink: "bg-pink-100 text-pink-500 hover:bg-pink-200 focus:ring-pink-400 dark:bg-pink-200 dark:text-pink-600 dark:hover:bg-pink-300",
3232
rose: "bg-rose-100 text-rose-500 hover:bg-rose-200 focus:ring-rose-400 dark:bg-rose-200 dark:text-rose-600 dark:hover:bg-rose-300",
33-
info: "bg-info-100 text-info-500 hover:bg-info-200 focus:ring-info-400 dark:bg-info-200 dark:text-info-600 dark:hover:bg-info-300",
34-
success: "bg-success-100 text-success-500 hover:bg-success-200 focus:ring-success-400 dark:bg-success-200 dark:text-success-600 dark:hover:bg-success-300",
35-
warning: "bg-warning-100 text-warning-500 hover:bg-warning-200 focus:ring-warning-400 dark:bg-warning-200 dark:text-warning-600 dark:hover:bg-warning-300",
36-
failure: "bg-failure-100 text-failure-500 hover:bg-failure-200 focus:ring-failure-400 dark:bg-failure-200 dark:text-failure-600 dark:hover:bg-failure-300"
3733
},
3834
rounded: {
3935
true: 'rounded-lg'

src/lib/forms/range/Range.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
let {
55
value = $bindable(),
66
appearance = 'none',
7-
color = 'primary',
7+
color = 'blue',
88
rangeSize = 'md',
99
inputClass,
1010
...attributes

src/lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export * from './indicator';
2020
export * from './kbd';
2121
export * from './list-group';
2222
export * from './nav';
23+
export * from './pagination';
2324
export * from './progress';
2425
export * from './rating';
2526
export * from './sidebar';
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<script lang="ts">
2+
import { setContext } from 'svelte';
3+
import { type PaginationProps as Props, pagination, PaginationItem } from '.';
4+
5+
let {
6+
pages = [],
7+
previous,
8+
next,
9+
prevContent,
10+
nextContent,
11+
activeClass,
12+
normalClass,
13+
ulClass,
14+
table,
15+
size,
16+
ariaLabel,
17+
...attributes
18+
}: Props = $props()
19+
20+
setContext('group', true);
21+
setContext('table', table);
22+
setContext('size', size);
23+
24+
const paginationClass = $derived(pagination({ table, size }));
25+
</script>
26+
27+
<nav aria-label={ariaLabel}>
28+
<ul class={paginationClass}>
29+
{#if typeof previous === 'function'}
30+
<li>
31+
<PaginationItem {size} onclick={()=>previous()} class={table ? 'rounded-l' : 'rounded-s-lg'}>
32+
{#if prevContent}
33+
{@render prevContent()}
34+
{:else}
35+
Previous
36+
{/if}
37+
</PaginationItem>
38+
</li>
39+
{/if}
40+
{#each pages as { name, href, active }}
41+
<li>
42+
<PaginationItem {size} {active} {href} {...attributes}>
43+
{name}
44+
</PaginationItem>
45+
</li>
46+
{/each}
47+
{#if typeof next === 'function'}
48+
<li>
49+
<PaginationItem size={size} onclick={()=>next()} class={table ? 'rounded-r' : 'rounded-e-lg'}>
50+
{#if nextContent}
51+
{@render nextContent()}
52+
{:else}
53+
Next
54+
{/if}
55+
</PaginationItem>
56+
</li>
57+
{/if}
58+
</ul>
59+
</nav>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<script lang="ts">
2+
import { paginationItem, type PaginationItemProps as Props } from '.';
3+
import { getContext } from 'svelte';
4+
5+
let {
6+
children,
7+
size,
8+
class: className,
9+
href,
10+
active,
11+
activeClass,
12+
normalClass,
13+
...attributes
14+
}: Props = $props()
15+
16+
const group = getContext<boolean>('group');
17+
const table = getContext<boolean>('table');
18+
const paginationClass = $derived(paginationItem({ size, active, group, table }));
19+
20+
</script>
21+
22+
<svelte:element this={href ? 'a' : 'button'} {href} class={paginationClass} role={href ? 'button' : undefined} {...attributes}>
23+
{#if children}
24+
{@render children()}
25+
{/if}
26+
</svelte:element>

src/lib/pagination/index.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { paginationItem, pagination } from './theme'
2+
import type { Snippet } from 'svelte';
3+
import Pagination from './Pagination.svelte'
4+
import { type VariantProps } from 'tailwind-variants';
5+
import PaginationItem from './PaginationItem.svelte';
6+
import type { HTMLAnchorAttributes, HTMLButtonAttributes } from 'svelte/elements';
7+
type PaginationItemProps = PaginationItemAttributes & (
8+
| (HTMLAnchorAttributes & { href: string })
9+
| (HTMLButtonAttributes & { href?: never })
10+
);
11+
12+
type PaginationItemType = VariantProps<typeof paginationItem>;
13+
interface PaginationItemAttributes {
14+
children?: Snippet;
15+
name?: string | undefined;
16+
href?: string | undefined;
17+
active?: boolean;
18+
activeClass?: string;
19+
normalClass?: string;
20+
rel?: string;
21+
size?: PaginationItemType['size'];
22+
}
23+
24+
interface PaginationProps {
25+
prevContent?: Snippet;
26+
nextContent?: Snippet;
27+
pages?: PaginationItemProps[];
28+
previous?: ()=>void;
29+
next?: ()=>void;
30+
activeClass?: string;
31+
normalClass?: string;
32+
ulClass?: string;
33+
table?: boolean;
34+
size?: PaginationItemType['size'];
35+
ariaLabel?: string;
36+
}
37+
38+
export {
39+
Pagination,
40+
PaginationItem,
41+
paginationItem,
42+
pagination,
43+
type PaginationItemProps,
44+
type PaginationProps
45+
}

src/lib/pagination/theme.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { tv } from 'tailwind-variants';
2+
3+
export const paginationItem = tv({
4+
base: 'flex items-center font-medium',
5+
variants: {
6+
size: {
7+
default: 'h-8 px-3 text-sm',
8+
large: 'h-10 px-4 text-base',
9+
},
10+
active: {
11+
true: 'text-blue-600 border border-gray-300 bg-blue-50 hover:bg-blue-100 hover:text-blue-700 dark:border-gray-700 dark:bg-gray-700 dark:text-white',
12+
false: 'text-gray-500 bg-white hover:bg-gray-100 hover:text-gray-700 dark:bg-gray-800 dark:border-gray-700 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white',
13+
},
14+
group: {
15+
true: '',
16+
false: 'rounded-lg',
17+
},
18+
table: {
19+
true: 'rounded',
20+
false: 'border',
21+
},
22+
},
23+
compoundVariants: [
24+
{
25+
group: false,
26+
table: false,
27+
class: 'rounded-lg',
28+
},
29+
],
30+
defaultVariants: {
31+
size: 'default',
32+
active: false,
33+
group: false,
34+
table: false,
35+
},
36+
});
37+
38+
export const pagination = tv({
39+
base: 'inline-flex -space-x-px rtl:space-x-reverse items-center',
40+
variants: {
41+
table: {
42+
true: 'divide-x rtl:divide-x-reverse dark divide-gray-700 dark:divide-gray-700',
43+
false: '',
44+
},
45+
size: {
46+
default: '',
47+
large: '',
48+
},
49+
},
50+
defaultVariants: {
51+
table: false,
52+
size: 'default',
53+
},
54+
});

src/routes/components/badge/+page.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@
206206
<EnvelopeSolid class="text-white dark:text-white" />
207207
<span class="sr-only">Notifications</span>
208208
<Indicator
209-
color="dark"
209+
color="sky"
210210
border
211211
size="xl"
212212
placement="bottom-right"

src/routes/components/button/+page.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@
201201
<Button class="gap-2">
202202
Messages
203203
<Indicator
204-
color="none"
204+
color="cyan"
205205
class="bg-primary-200 text-xs font-semibold text-primary-800"
206206
size="lg">2</Indicator
207207
>
@@ -230,7 +230,7 @@
230230
<H2>Loader</H2>
231231
<CodeWrapper>
232232
<Button>
233-
<Spinner class="me-3" size="4" color="white" />Loading ...
233+
<Spinner class="me-3" size="4" color="teal" />Loading ...
234234
</Button>
235235
<Button color="alternative">
236236
<Spinner class="me-3" size="4" />Loading ...

src/routes/components/pagination/+page.svelte

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<script lang="ts">
2-
import { Select, Label } from '$lib';
2+
import { Select, Label, Pagination, PaginationItem } from '$lib';
3+
import { ChevronLeftOutline, ChevronRightOutline, ArrowLeftOutline, ArrowRightOutline } from 'flowbite-svelte-icons';
4+
import { page } from '$app/stores';
35
import HighlightCompo from '../../utils/HighlightCompo.svelte';
46
import CodeWrapper from '../../utils/CodeWrapper.svelte';
57
import H1 from '../../utils/H1.svelte';
@@ -10,6 +12,101 @@
1012
import: 'default',
1113
eager: true
1214
});
15+
16+
let activeUrl = $state($page.url.searchParams.get('page'));
17+
let pages = $state([
18+
{ name: "1", href: '/components/pagination?page=1', active: false },
19+
{ name: "2", href: '/components/pagination?page=2', active: false },
20+
{ name: "3", href: '/components/pagination?page=3', active: false },
21+
{ name: "4", href: '/components/pagination?page=4', active: false },
22+
{ name: "5", href: '/components/pagination?page=5', active: false }
23+
]);
24+
let helper = { start: 1, end: 10, total: 100 };
25+
26+
$effect(()=>{
27+
pages.forEach((page) => {
28+
let splitUrl = page.href.split('?');
29+
let queryString = splitUrl.slice(1).join('?');
30+
const hrefParams = new URLSearchParams(queryString);
31+
let hrefValue = hrefParams.get('page');
32+
if (hrefValue === activeUrl) {
33+
page.active = true;
34+
} else {
35+
page.active = false;
36+
}
37+
});
38+
pages = pages;
39+
})
40+
41+
const previous = () => {
42+
alert('Previous btn clicked. Make a call to your server to fetch data.');
43+
};
44+
const next = () => {
45+
alert('Next btn clicked. Make a call to your server to fetch data.');
46+
};
1347
</script>
1448

1549
<H1>Pagination</H1>
50+
51+
<H2>Default pagination</H2>
52+
<CodeWrapper class="space-y-4 flex flex-col">
53+
<Pagination {pages} {previous} {next} />
54+
<Pagination {pages} size="large" {previous} {next} />
55+
</CodeWrapper>
56+
57+
<H2>Pagination with icons</H2>
58+
<CodeWrapper>
59+
<Pagination {pages} {previous} {next}>
60+
{#snippet prevContent()}
61+
<span class="sr-only">Previous</span>
62+
<ChevronLeftOutline class="w-5 h-5" />
63+
{/snippet}
64+
{#snippet nextContent()}
65+
<span class="sr-only">Next</span>
66+
<ChevronRightOutline class="w-5 h-5" />
67+
{/snippet}
68+
</Pagination>
69+
</CodeWrapper>
70+
71+
<H2>Previous and next</H2>
72+
<CodeWrapper>
73+
<div class="flex space-x-3 rtl:space-x-reverse">
74+
<PaginationItem onclick={previous}>Previous</PaginationItem>
75+
<PaginationItem onclick={next}>Next</PaginationItem>
76+
</div>
77+
</CodeWrapper>
78+
79+
80+
<H2>Previous and next with icons</H2>
81+
<CodeWrapper>
82+
<div class="flex space-x-3 rtl:space-x-reverse">
83+
<PaginationItem class="flex items-center" onclick={previous}>
84+
<ArrowLeftOutline class="me-2 w-5 h-5" />
85+
Previous
86+
</PaginationItem>
87+
<PaginationItem class="flex items-center" onclick={next}>
88+
Next
89+
<ArrowRightOutline class="ms-2 w-5 h-5" />
90+
</PaginationItem>
91+
</div>
92+
</CodeWrapper>
93+
94+
<H2>Table data pagination</H2>
95+
<CodeWrapper>
96+
<div class="flex flex-col items-center justify-center gap-2">
97+
<div class="text-sm text-gray-700 dark:text-gray-400">
98+
Showing <span class="font-semibold text-gray-900 dark:text-white">{helper.start}</span>
99+
to
100+
<span class="font-semibold text-gray-900 dark:text-white">{helper.end}</span>
101+
of
102+
<span class="font-semibold text-gray-900 dark:text-white">{helper.total}</span>
103+
Entries
104+
</div>
105+
106+
<Pagination table>
107+
{#snippet prevContent()}
108+
Prev
109+
{/snippet}
110+
</Pagination>
111+
</div>
112+
</CodeWrapper>

0 commit comments

Comments
 (0)