/ chandeskpro / src / stores / lightboxStore.ts
swadhinbiswas Initial release v0.1.0 · 1/16/2026
ed7a4e4
73 lines · 1.5 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
 * Lightbox Store
 * 
 * Global state for fullscreen image lightbox.
 * Allows opening lightbox from anywhere in the app.
 */

import { create } from 'zustand'

interface LightboxImage {
    url: string
    thumbnail: string
    filename?: string
    width?: number
    height?: number
}

interface LightboxStore {
    isOpen: boolean
    images: LightboxImage[]
    currentIndex: number

    // Actions
    openLightbox: (images: LightboxImage[], index?: number) => void
    closeLightbox: () => void
    setIndex: (index: number) => void
    next: () => void
    prev: () => void
}

export const useLightboxStore = create<LightboxStore>((set, get) => ({
    isOpen: false,
    images: [],
    currentIndex: 0,

    openLightbox: (images, index = 0) => {
        set({
            isOpen: true,
            images,
            currentIndex: index,
        })
    },

    closeLightbox: () => {
        set({
            isOpen: false,
            images: [],
            currentIndex: 0,
        })
    },

    setIndex: (index) => {
        const { images } = get()
        if (index >= 0 && index < images.length) {
            set({ currentIndex: index })
        }
    },

    next: () => {
        const { currentIndex, images } = get()
        if (currentIndex < images.length - 1) {
            set({ currentIndex: currentIndex + 1 })
        }
    },

    prev: () => {
        const { currentIndex } = get()
        if (currentIndex > 0) {
            set({ currentIndex: currentIndex - 1 })
        }
    },
}))