Skip to content

Commit 5586095

Browse files
committed
feat(Lightbox): zoom from a thumbnail and back, like Photos
1 parent e1a3389 commit 5586095

17 files changed

Lines changed: 2197 additions & 90 deletions
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
'@astryxdesign/core': patch
3+
---
4+
5+
[feat] A Lightbox photo can now zoom out of its thumbnail when it opens and back into it when it closes, like the iOS Photos viewer (UIKit's zoom transition). Pass `sourceFor`, which returns each item's thumbnail element. The photo grows from the thumbnail to its fitted size while the thumbnail's picture fades out, and the backdrop and controls fade in with it.
6+
7+
Closing works the same way: the Close button, Escape, a tap beside the photo, or a swipe to another photo and then a close each send the photo back into the thumbnail of the photo showing, and the viewer stays open until it lands. A photo dragged down shrinks into its thumbnail. Without `sourceFor`, a viewer opened from an element that shows a picture (an image, or a button or link around one) zooms from that element for the photo it opened on. A text button opens and closes as before. Under Reduce Motion the photo cross-fades in place.
8+
9+
`sourceFor` is proposed and pending owner review.
10+
11+
@vjeux

‎apps/storybook/stories/Lightbox.stories.tsx‎

Lines changed: 158 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Copyright (c) Meta Platforms, Inc. and affiliates.
22

3-
import {useState} from 'react';
3+
import {useRef, useState} from 'react';
44
import type {Meta, StoryObj} from '@storybook/react';
5+
import {Dialog} from '@astryxdesign/core/Dialog';
56
import {Lightbox, useLightbox} from '@astryxdesign/core/Lightbox';
67

78
const meta: Meta<typeof Lightbox> = {
@@ -179,6 +180,162 @@ export const GalleryKeepsIndex: Story = {
179180
},
180181
};
181182

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+
182339
export const WithCaption: Story = {
183340
render: () => {
184341
const [isOpen, setIsOpen] = useState(false);

‎packages/cli/assets/docs/motion.doc.mjs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,24 @@ const calmTheme = defineTheme({
481481
'curve',
482482
'cubic-bezier(0.42, 0, 0.58, 1)',
483483
],
484+
[
485+
'zoomMedia.transition.closeFadeSpring',
486+
'--motion-zoom-media-transition-close-fade-spring',
487+
'spring',
488+
'0.34s 0',
489+
],
490+
[
491+
'zoomMedia.transition.closeSpring',
492+
'--motion-zoom-media-transition-close-spring',
493+
'spring',
494+
'0.34s 0.08',
495+
],
496+
[
497+
'zoomMedia.transition.openSpring',
498+
'--motion-zoom-media-transition-open-spring',
499+
'spring',
500+
'0.34s 0',
501+
],
484502
// motion-roles:end
485503
],
486504
},

‎packages/cli/test/__golden__/no-icon-set/docs-topics.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@
201201
"touch-motion",
202202
"best-practices"
203203
],
204-
"skeleton": "f4ca77e597757da189a101c9897cebe7ef52f7df78c6826ffb3fe794de014cf1",
204+
"skeleton": "2dc1f794d5e0daf1e6254cd419dbdace2b347f8b7f44a9bf927c8cd3bdfb89b7",
205205
"mentions": []
206206
},
207207
"principles": {

‎packages/core/src/Lightbox/Lightbox.doc.mjs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ export const docs = {
116116
'Automatically start video playback when a video media item is shown.',
117117
default: 'false',
118118
},
119+
{
120+
name: 'sourceFor',
121+
type: '(index: number) => HTMLElement | null',
122+
description:
123+
"The element an item opens from and closes into, usually its thumbnail: the photo zooms out of it on open and back into it on close (for the item showing then), and a photo dragged down lands in it. Return null when there is none or it is off screen. Without it, the element that opened the viewer is the opened item's source when it shows a picture. Proposed; pending owner review.",
124+
},
119125
{
120126
name: 'xstyle',
121127
type: 'StyleXStyles',
@@ -217,6 +223,12 @@ export const docsZh = {
217223
'启用缩放(仅图片):双指捏合,或双击/双点按在该处放大并还原,或使用 Enter/Space/+/- 键。缩放后可拖动(带惯性)或用方向键平移。',
218224
default: 'false',
219225
},
226+
{
227+
name: 'sourceFor',
228+
type: '(index: number) => HTMLElement | null',
229+
description:
230+
'某一项打开时放大自、关闭时缩回的元素(通常是其缩略图);向下拖动的照片也会落入其中。没有或不在屏幕上时返回 null。未提供时,若打开查看器的元素显示图片,则它是所打开项的来源。提议中,待负责人审核。',
231+
},
220232
{
221233
name: 'xstyle',
222234
type: 'StyleXStyles',
@@ -302,6 +314,8 @@ export const docsDense = {
302314
'Callback when gallery index changes (buttons, keys or a swipe).',
303315
hasZoom:
304316
'Enable zoom (pinch, double-tap or double-click, Enter/Space, or +/-) and pan (drag or arrow keys) for images.',
317+
sourceFor:
318+
'The element an item zooms out of on open and back into on close (its thumbnail).',
305319
xstyle: 'StyleX styles for layout customization.',
306320
},
307321
};

‎packages/core/src/Lightbox/Lightbox.test.tsx‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,51 @@ describe('Lightbox', () => {
273273
expect(onOpenChange).not.toHaveBeenCalled();
274274
});
275275

276+
it('asks sourceFor for the item opening and the item showing at close', () => {
277+
const sourceFor = vi.fn((): HTMLElement | null => null);
278+
const {rerender} = render(
279+
<Lightbox
280+
isOpen={false}
281+
onOpenChange={() => {}}
282+
media={media}
283+
index={0}
284+
sourceFor={sourceFor}
285+
/>,
286+
);
287+
expect(sourceFor).not.toHaveBeenCalled();
288+
rerender(
289+
<Lightbox
290+
isOpen={true}
291+
onOpenChange={() => {}}
292+
media={media}
293+
index={0}
294+
sourceFor={sourceFor}
295+
/>,
296+
);
297+
expect(sourceFor).toHaveBeenLastCalledWith(0);
298+
rerender(
299+
<Lightbox
300+
isOpen={true}
301+
onOpenChange={() => {}}
302+
media={media}
303+
index={2}
304+
sourceFor={sourceFor}
305+
/>,
306+
);
307+
rerender(
308+
<Lightbox
309+
isOpen={false}
310+
onOpenChange={() => {}}
311+
media={media}
312+
index={2}
313+
sourceFor={sourceFor}
314+
/>,
315+
);
316+
expect(sourceFor).toHaveBeenLastCalledWith(2);
317+
// No source box to zoom into: the dialog closes at once.
318+
expect(HTMLDialogElement.prototype.close).toHaveBeenCalled();
319+
});
320+
276321
it('shows gallery counter', () => {
277322
render(
278323
<Lightbox

0 commit comments

Comments
 (0)