-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathThumbnails.svelte
More file actions
59 lines (51 loc) · 1.69 KB
/
Copy pathThumbnails.svelte
File metadata and controls
59 lines (51 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<script lang="ts">
import type { ThumbnailsProps } from "$lib";
import Thumbnail from "./Thumbnail.svelte";
import { getTheme } from "$lib/theme/themeUtils";
import clsx from "clsx";
import { thumbnails } from "./theme";
let { children, images = [], index = $bindable(), ariaLabel = "Click to view image", imgClass, throttleDelay = 650, class: className }: ThumbnailsProps = $props();
const theme = $derived(getTheme("thumbnails"));
// Initialize so the first click is never throttled
let lastClickedAt = -Infinity;
const btnClick = (newIndex: number) => {
const now = Date.now();
if (now - lastClickedAt < throttleDelay) {
console.warn("Thumbnail action throttled");
return;
}
lastClickedAt = now;
index = newIndex;
};
$effect(() => {
if (images.length > 0) {
index = (index + images.length) % images.length;
}
});
</script>
<div class={thumbnails({ class: clsx(theme, className) })}>
{#each images as image, idx (image.src || idx)}
{@const selected = index === idx}
<button onclick={() => btnClick(idx)} aria-label={ariaLabel} aria-current={selected ? "true" : undefined}>
{#if children}
{@render children({ image, selected, imgClass: clsx(imgClass), Thumbnail })}
{:else}
<Thumbnail {...image} {selected} class={clsx(imgClass)} />
{/if}
</button>
{/each}
</div>
<!--
@component
[Go to docs](https://flowbite-svelte.com/)
## Type
[ThumbnailsProps](https://github.com/themesberg/flowbite-svelte/blob/main/src/lib/types.ts#L506)
## Props
@prop children
@prop images = []
@prop index = $bindable()
@prop ariaLabel = "Click to view image"
@prop imgClass
@prop throttleDelay = 650
@prop class: className
-->