MCPcopy Create free account
hub / github.com/dailydotdev/apps / ImageModal

Function ImageModal

packages/shared/src/components/modals/ImageModal.tsx:50–246  ·  view source on GitHub ↗
({
  onRequestClose,
  src,
  alt = 'Image',
  originRect,
}: ImageModalProps)

Source from the content-addressed store, hash-verified

48 * trapped under that portal at the same z-index.
49 */
50export default function ImageModal({
51 onRequestClose,
52 src,
53 alt = 'Image',
54 originRect,
55}: ImageModalProps): ReactElement | null {
56 const close = () => onRequestClose?.(undefined as never);
57 const imgRef = useRef<HTMLImageElement>(null);
58 const closeButtonRef = useRef<HTMLButtonElement>(null);
59 const phoneCloseButtonRef = useRef<HTMLElement>(null);
60 const isTablet = useViewSize(ViewSize.Tablet);
61 const hasPlayedRef = useRef(false);
62 const focusCloseButton = useCallback(() => {
63 const button = isTablet
64 ? closeButtonRef.current
65 : phoneCloseButtonRef.current;
66 button?.focus();
67 }, [isTablet]);
68
69 useEffect(() => {
70 const previousFocus = document.activeElement;
71 focusCloseButton();
72 return () => {
73 if (previousFocus instanceof HTMLElement) {
74 previousFocus.focus();
75 }
76 };
77 }, [focusCloseButton]);
78
79 // FLIP: render the image at its final centered size, but start it transformed
80 // to sit over the clicked thumbnail, then animate the transform away so it
81 // visually grows from the thumbnail into the full view.
82 const playFlip = () => {
83 const img = imgRef.current;
84 if (!originRect || !img || hasPlayedRef.current) {
85 return;
86 }
87 const final = img.getBoundingClientRect();
88 if (!final.width || !final.height) {
89 // Not laid out yet (image still loading) — onLoad will retry.
90 return;
91 }
92 hasPlayedRef.current = true;
93
94 // A single uniform scale (not independent sx/sy) so the image keeps its
95 // aspect ratio and never warps mid-flight. `min` contains the start frame
96 // within the thumbnail's footprint, so it reads as lifting off from there.
97 const scale = Math.min(
98 originRect.width / final.width,
99 originRect.height / final.height,
100 );
101 // Translate centers together (transform-origin is the center too), so the
102 // image grows out of where the thumbnail sat rather than from a corner.
103 const dx =
104 originRect.left + originRect.width / 2 - (final.left + final.width / 2);
105 const dy =
106 originRect.top + originRect.height / 2 - (final.top + final.height / 2);
107 // Transform scales the corner radius along with everything else, so a fixed

Callers

nothing calls this directly

Calls 4

useViewSizeFunction · 0.90
useEventListenerFunction · 0.90
playFlipFunction · 0.85
closeFunction · 0.70

Tested by

no test coverage detected