|
| 1 | +import { usePresence } from '@primereact/hooks'; |
| 2 | +import { useGalleryChangeEvent } from '@primereact/types/shared/gallery'; |
| 3 | +import { Gallery } from 'primereact/gallery'; |
| 4 | +import { Portal } from 'primereact/portal'; |
| 5 | +import * as React from 'react'; |
| 6 | + |
| 7 | +const images = [ |
| 8 | + 'https://images.unsplash.com/photo-1759800805589-54b5fc10a52e?q=80&w=1285&auto=format&fit=crop&ixlib=rb-4.1.0&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D', |
| 9 | + 'https://images.unsplash.com/photo-1759559790290-a3c6fce1d55f?q=80&w=3540&auto=format&fit=crop&ixlib=rb-4.1.0&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D', |
| 10 | + 'https://images.unsplash.com/photo-1755398104149-961fa827e015?q=80&w=1287&auto=format&fit=crop&ixlib=rb-4.1.0&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D', |
| 11 | + 'https://plus.unsplash.com/premium_photo-1722944969391-1d21a2d404ea?q=80&w=2940&auto=format&fit=crop&ixlib=rb-4.1.0&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D', |
| 12 | + 'https://images.unsplash.com/photo-1757684945606-7644757a3eb9?q=80&w=1470&auto=format&fit=crop&ixlib=rb-4.1.0&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D', |
| 13 | + 'https://images.unsplash.com/photo-1743701168206-bd617221b559?q=80&w=2614&auto=format&fit=crop&ixlib=rb-4.1.0&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D', |
| 14 | + 'https://images.unsplash.com/photo-1758944966741-fc79410be873?q=80&w=1470&auto=format&fit=crop&ixlib=rb-4.1.0&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D', |
| 15 | + 'https://images.unsplash.com/photo-1758964112991-84be3393d9b1?q=80&w=2942&auto=format&fit=crop&ixlib=rb-4.1.0&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D' |
| 16 | +]; |
| 17 | + |
| 18 | +function BasicDemo() { |
| 19 | + const [activeIndex, setActiveIndex] = React.useState<number>(3); |
| 20 | + const [open, setOpen] = React.useState<boolean>(false); |
| 21 | + |
| 22 | + const { present, exiting, mounted, ref } = usePresence(open); |
| 23 | + |
| 24 | + const handleOpen = (index: number) => { |
| 25 | + setOpen(true); |
| 26 | + setActiveIndex(index); |
| 27 | + }; |
| 28 | + |
| 29 | + const isVisible = present && mounted && !exiting; |
| 30 | + |
| 31 | + return ( |
| 32 | + <div className="card"> |
| 33 | + <div className="grid grid-cols-4 gap-4"> |
| 34 | + {images.map((image, index) => ( |
| 35 | + <div key={image} className="h-[300px] hover:opacity-75 transition-opacity" onClick={() => handleOpen(index)}> |
| 36 | + <img src={image} alt="image" className="w-full h-full object-cover rounded-lg" /> |
| 37 | + </div> |
| 38 | + ))} |
| 39 | + </div> |
| 40 | + <Portal> |
| 41 | + {present && ( |
| 42 | + <div ref={ref as React.RefObject<HTMLDivElement>} className={`w-full h-[100dvh] top-0 left-0 !fixed z-[100000] ${isVisible ? 'opacity-100' : 'opacity-0'} transition-opacity duration-200`}> |
| 43 | + <Gallery className="w-full h-full" activeIndex={activeIndex} onActiveIndexChange={(e: useGalleryChangeEvent) => setActiveIndex(e.value ?? 0)}> |
| 44 | + <Gallery.Backdrop /> |
| 45 | + <Gallery.Prev> |
| 46 | + <i className="pi pi-arrow-left"></i> |
| 47 | + </Gallery.Prev> |
| 48 | + <Gallery.Next> |
| 49 | + <i className="pi pi-arrow-right"></i> |
| 50 | + </Gallery.Next> |
| 51 | + <Gallery.Toolbar> |
| 52 | + <Gallery.ToolbarItem action="rotateLeft"> |
| 53 | + <i className="pi pi-replay"></i> |
| 54 | + </Gallery.ToolbarItem> |
| 55 | + <Gallery.ToolbarItem action="rotateRight"> |
| 56 | + <i className="pi pi-refresh"></i> |
| 57 | + </Gallery.ToolbarItem> |
| 58 | + <Gallery.ToolbarItem action="zoomIn"> |
| 59 | + <i className="pi pi-search-plus"></i> |
| 60 | + </Gallery.ToolbarItem> |
| 61 | + <Gallery.ToolbarItem action="flipX"> |
| 62 | + <i className="pi pi-arrows-h"></i> |
| 63 | + </Gallery.ToolbarItem> |
| 64 | + <Gallery.ToolbarItem action="flipY"> |
| 65 | + <i className="pi pi-arrows-v"></i> |
| 66 | + </Gallery.ToolbarItem> |
| 67 | + <Gallery.ToolbarItem action="download"> |
| 68 | + <i className="pi pi-download"></i> |
| 69 | + </Gallery.ToolbarItem> |
| 70 | + <Gallery.ToolbarItem onClick={() => setOpen(false)}> |
| 71 | + <i className="pi pi-times"></i> |
| 72 | + </Gallery.ToolbarItem> |
| 73 | + </Gallery.Toolbar> |
| 74 | + <Gallery.Content> |
| 75 | + {images.map((image) => ( |
| 76 | + <Gallery.Item key={image}> |
| 77 | + <img src={image} alt="image" className={`${isVisible ? 'scale-100 blur-none' : 'scale-[0.9] blur-2xl'} transition-[scale,filter] duration-300`} /> |
| 78 | + </Gallery.Item> |
| 79 | + ))} |
| 80 | + </Gallery.Content> |
| 81 | + <Gallery.Thumbnail align="center" slide={activeIndex} spacing={2} loop> |
| 82 | + <Gallery.ThumbnailContent className="h-16"> |
| 83 | + {images.map((image, index) => ( |
| 84 | + <Gallery.ThumbnailItem |
| 85 | + key={index} |
| 86 | + size={20} |
| 87 | + onClick={(e: React.MouseEvent) => { |
| 88 | + e.preventDefault(); |
| 89 | + setActiveIndex(index); |
| 90 | + }} |
| 91 | + > |
| 92 | + <div className={`h-full w-full border-2 rounded-md overflow-hidden ${activeIndex === index ? 'border-orange-500' : 'border-transparent'}`}> |
| 93 | + <img draggable={false} src={image} className="h-full w-full object-cover hover:opacity-75 transition-opacity"></img> |
| 94 | + </div> |
| 95 | + </Gallery.ThumbnailItem> |
| 96 | + ))} |
| 97 | + </Gallery.ThumbnailContent> |
| 98 | + </Gallery.Thumbnail> |
| 99 | + </Gallery> |
| 100 | + </div> |
| 101 | + )} |
| 102 | + </Portal> |
| 103 | + </div> |
| 104 | + ); |
| 105 | +} |
| 106 | + |
| 107 | +export default BasicDemo; |
0 commit comments