import { clsx } from 'clsx' import { createEffect, createSignal, For, Show, on, onMount, onCleanup } from 'solid-js' import SwiperCore from 'swiper' import { Manipulation, Navigation, Pagination } from 'swiper/modules' import { throttle } from 'throttle-debounce' import { MediaItem } from '../../../pages/types' import { getImageUrl } from '../../../utils/getImageUrl' import { Icon } from '../Icon' import { Image } from '../Image' import { Lightbox } from '../Lightbox' import { SwiperRef } from './swiper' import styles from './Swiper.module.scss' type Props = { images: MediaItem[] onImagesAdd?: (value: MediaItem[]) => void onImagesSorted?: (value: MediaItem[]) => void onImageDelete?: (mediaItemIndex: number) => void onImageChange?: (index: number, value: MediaItem) => void } const MIN_WIDTH = 540 export const ImageSwiper = (props: Props) => { const mainSwipeRef: { current: SwiperRef } = { current: null } const thumbSwipeRef: { current: SwiperRef } = { current: null } const swiperMainContainer: { current: HTMLDivElement } = { current: null } const [slideIndex, setSlideIndex] = createSignal(0) const [isMobileView, setIsMobileView] = createSignal(false) const [selectedImage, setSelectedImage] = createSignal('') const handleSlideChange = () => { thumbSwipeRef.current.swiper.slideTo(mainSwipeRef.current.swiper.activeIndex) setSlideIndex(mainSwipeRef.current.swiper.activeIndex) } createEffect( on( () => props.images.length, () => { mainSwipeRef.current?.swiper.update() thumbSwipeRef.current?.swiper.update() }, { defer: true }, ), ) onMount(async () => { const { register } = await import('swiper/element/bundle') register() SwiperCore.use([Pagination, Navigation, Manipulation]) mainSwipeRef.current.swiper.on('slideChange', handleSlideChange) }) onMount(() => { const updateDirection = () => { const width = window.innerWidth setIsMobileView(width < MIN_WIDTH) } updateDirection() const handleResize = throttle(100, () => { updateDirection() }) window.addEventListener('resize', handleResize) onCleanup(() => { window.removeEventListener('resize', handleResize) }) }) const openLightbox = (image) => { setSelectedImage(image) } const handleLightboxClose = () => { setSelectedImage() } const handleImageClick = (event) => { const src = event.target.src openLightbox(getImageUrl(src)) } return (