|
1 | 1 | // Copyright (c) Meta Platforms, Inc. and affiliates. |
2 | 2 |
|
3 | | -import {useState} from 'react'; |
| 3 | +import {useRef, useState} from 'react'; |
4 | 4 | import type {Meta, StoryObj} from '@storybook/react'; |
| 5 | +import {Dialog} from '@astryxdesign/core/Dialog'; |
5 | 6 | import {Lightbox, useLightbox} from '@astryxdesign/core/Lightbox'; |
6 | 7 |
|
7 | 8 | const meta: Meta<typeof Lightbox> = { |
@@ -179,6 +180,162 @@ export const GalleryKeepsIndex: Story = { |
179 | 180 | }, |
180 | 181 | }; |
181 | 182 |
|
| 183 | +/** |
| 184 | + * Photos open from their thumbnails and close back into the one showing: |
| 185 | + * `sourceFor` hands the viewer each item's thumbnail, and the photo zooms |
| 186 | + * out of it (UIKit's zoom transition). Page to another photo and close, or |
| 187 | + * drag it down: it lands in its own thumbnail. |
| 188 | + */ |
| 189 | +export const ZoomFromThumbnails: Story = { |
| 190 | + render: () => { |
| 191 | + const [isOpen, setIsOpen] = useState(false); |
| 192 | + const [index, setIndex] = useState(0); |
| 193 | + const thumbnails = useRef<(HTMLImageElement | null)[]>([]); |
| 194 | + return ( |
| 195 | + <> |
| 196 | + <div style={{display: 'flex', gap: '8px', flexWrap: 'wrap'}}> |
| 197 | + {GALLERY_MEDIA.map((item, i) => ( |
| 198 | + <button |
| 199 | + key={item.src} |
| 200 | + type="button" |
| 201 | + aria-label={`Open ${item.alt}`} |
| 202 | + style={{padding: 0, border: 0, background: 'none'}} |
| 203 | + onClick={() => { |
| 204 | + setIndex(i); |
| 205 | + setIsOpen(true); |
| 206 | + }}> |
| 207 | + <img |
| 208 | + ref={element => { |
| 209 | + thumbnails.current[i] = element; |
| 210 | + }} |
| 211 | + src={item.src} |
| 212 | + alt="" |
| 213 | + style={{ |
| 214 | + display: 'block', |
| 215 | + width: 96, |
| 216 | + height: 96, |
| 217 | + objectFit: 'cover', |
| 218 | + borderRadius: 8, |
| 219 | + }} |
| 220 | + /> |
| 221 | + </button> |
| 222 | + ))} |
| 223 | + </div> |
| 224 | + <Lightbox |
| 225 | + isOpen={isOpen} |
| 226 | + onOpenChange={setIsOpen} |
| 227 | + media={GALLERY_MEDIA} |
| 228 | + index={index} |
| 229 | + onIndexChange={setIndex} |
| 230 | + sourceFor={i => thumbnails.current[i] ?? null} |
| 231 | + hasZoom |
| 232 | + /> |
| 233 | + </> |
| 234 | + ); |
| 235 | + }, |
| 236 | +}; |
| 237 | + |
| 238 | +/** |
| 239 | + * A viewer the page renders only while it is open: closing it, by any path, |
| 240 | + * gives the thumbnail back. |
| 241 | + */ |
| 242 | +export const MountedWhileOpen: Story = { |
| 243 | + render: () => { |
| 244 | + const [openIndex, setOpenIndex] = useState<number | null>(null); |
| 245 | + const thumbnails = useRef<(HTMLImageElement | null)[]>([]); |
| 246 | + return ( |
| 247 | + <> |
| 248 | + <div style={{display: 'flex', gap: '8px', flexWrap: 'wrap'}}> |
| 249 | + {GALLERY_MEDIA.map((item, i) => ( |
| 250 | + <button |
| 251 | + key={item.src} |
| 252 | + type="button" |
| 253 | + aria-label={`Open ${item.alt}`} |
| 254 | + style={{padding: 0, border: 0, background: 'none'}} |
| 255 | + onClick={() => setOpenIndex(i)}> |
| 256 | + <img |
| 257 | + ref={element => { |
| 258 | + thumbnails.current[i] = element; |
| 259 | + }} |
| 260 | + src={item.src} |
| 261 | + alt="" |
| 262 | + style={{ |
| 263 | + display: 'block', |
| 264 | + width: 96, |
| 265 | + height: 96, |
| 266 | + objectFit: 'cover', |
| 267 | + borderRadius: 8, |
| 268 | + }} |
| 269 | + /> |
| 270 | + </button> |
| 271 | + ))} |
| 272 | + </div> |
| 273 | + {openIndex != null && ( |
| 274 | + <Lightbox |
| 275 | + isOpen |
| 276 | + onOpenChange={open => { |
| 277 | + if (!open) { |
| 278 | + setOpenIndex(null); |
| 279 | + } |
| 280 | + }} |
| 281 | + media={GALLERY_MEDIA} |
| 282 | + index={openIndex} |
| 283 | + onIndexChange={setOpenIndex} |
| 284 | + sourceFor={i => thumbnails.current[i] ?? null} |
| 285 | + hasZoom |
| 286 | + /> |
| 287 | + )} |
| 288 | + </> |
| 289 | + ); |
| 290 | + }, |
| 291 | +}; |
| 292 | + |
| 293 | +/** |
| 294 | + * A photo opened from a dialog: Escape closes the photo first, and while it |
| 295 | + * zooms back into its thumbnail the dialog stays. |
| 296 | + */ |
| 297 | +export const InsideADialog: Story = { |
| 298 | + render: () => { |
| 299 | + const [isDialogOpen, setIsDialogOpen] = useState(false); |
| 300 | + const [isOpen, setIsOpen] = useState(false); |
| 301 | + const thumbnail = useRef<HTMLImageElement | null>(null); |
| 302 | + return ( |
| 303 | + <> |
| 304 | + <button onClick={() => setIsDialogOpen(true)}>Open dialog</button> |
| 305 | + <Dialog isOpen={isDialogOpen} onOpenChange={setIsDialogOpen}> |
| 306 | + <div style={{padding: 16}}> |
| 307 | + <button |
| 308 | + type="button" |
| 309 | + aria-label="Open Forest path" |
| 310 | + style={{padding: 0, border: 0, background: 'none'}} |
| 311 | + onClick={() => setIsOpen(true)}> |
| 312 | + <img |
| 313 | + ref={thumbnail} |
| 314 | + src={SAMPLE_IMAGE} |
| 315 | + alt="" |
| 316 | + style={{ |
| 317 | + display: 'block', |
| 318 | + width: 96, |
| 319 | + height: 96, |
| 320 | + objectFit: 'cover', |
| 321 | + borderRadius: 8, |
| 322 | + }} |
| 323 | + /> |
| 324 | + </button> |
| 325 | + </div> |
| 326 | + </Dialog> |
| 327 | + <Lightbox |
| 328 | + isOpen={isOpen} |
| 329 | + onOpenChange={setIsOpen} |
| 330 | + media={{src: SAMPLE_IMAGE, alt: 'Forest path'}} |
| 331 | + sourceFor={() => thumbnail.current} |
| 332 | + hasZoom |
| 333 | + /> |
| 334 | + </> |
| 335 | + ); |
| 336 | + }, |
| 337 | +}; |
| 338 | + |
182 | 339 | export const WithCaption: Story = { |
183 | 340 | render: () => { |
184 | 341 | const [isOpen, setIsOpen] = useState(false); |
|
0 commit comments