import { clsx } from 'clsx' import { createEffect, createSignal, For, Show, on, onMount, lazy, onCleanup } from 'solid-js' import SwiperCore, { Manipulation, Navigation, Pagination } from 'swiper' import { throttle } from 'throttle-debounce' import { MediaItem } from '../../../pages/types' import { getImageUrl } from '../../../utils/getImageUrl' import { Icon } from '../Icon' import { Image } from '../Image' 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 [slideIndex, setSlideIndex] = createSignal(0) const [isMobileView, setIsMobileView] = createSignal(false) const mainSwipeRef: { current: SwiperRef } = { current: null } const thumbSwipeRef: { current: SwiperRef } = { current: null } const swiperMainContainer: { current: HTMLDivElement } = { current: null } 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]) }) onMount(() => { const updateDirection = () => { const width = window.innerWidth const direction = width > MIN_WIDTH ? 'vertical' : 'horizontal' if (direction === 'horizontal') { setIsMobileView(true) } else { setIsMobileView(false) } thumbSwipeRef.current?.swiper?.changeDirection(direction) } updateDirection() const handleResize = throttle(100, () => { updateDirection() }) window.addEventListener('resize', handleResize) onCleanup(() => { window.removeEventListener('resize', handleResize) }) }) return (