mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 23:27:31 +00:00
fix(journey): open photos and videos full screen below the status bar, with the close button in reach
On a phone the journey's photo viewer opened inside the screen's own box instead of over the whole screen, and its top bar, close button included, sat under the status bar and a notch. It is now portalled to the page, its top bar is a row of its own below the device's insets, the photo or video fills what is left, and the page behind it no longer scrolls. The trip files viewer, which shows videos too, keeps its header below the insets as well and gets the same larger close button.
This commit is contained in:
@@ -87,9 +87,9 @@ describe('PhotoLightbox', () => {
|
||||
|
||||
it('FE-COMP-LIGHTBOX-007: does not render when photos array is empty', () => {
|
||||
const onClose = vi.fn();
|
||||
const { container } = render(<PhotoLightbox photos={[]} onClose={onClose} />);
|
||||
render(<PhotoLightbox photos={[]} onClose={onClose} />);
|
||||
// Component returns null when photo is undefined (empty array, index 0 is undefined)
|
||||
expect(container.querySelector('img')).not.toBeInTheDocument();
|
||||
expect(document.querySelector('img')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('FE-COMP-LIGHTBOX-008: calls onClose when close button clicked', () => {
|
||||
@@ -100,10 +100,10 @@ describe('PhotoLightbox', () => {
|
||||
});
|
||||
|
||||
it('FE-COMP-LIGHTBOX-009: opens at the requested start index', () => {
|
||||
const { container } = render(<PhotoLightbox photos={samplePhotos} startIndex={2} onClose={vi.fn()} />);
|
||||
render(<PhotoLightbox photos={samplePhotos} startIndex={2} onClose={vi.fn()} />);
|
||||
expect(screen.getByText('3 / 3')).toBeInTheDocument();
|
||||
// The third photo has no caption, so its img is presentational.
|
||||
expect(container.querySelector('img')).toHaveAttribute('src', '/photos/3.jpg');
|
||||
expect(document.querySelector('img')).toHaveAttribute('src', '/photos/3.jpg');
|
||||
});
|
||||
|
||||
it('FE-COMP-LIGHTBOX-010: only offers the arrows that lead somewhere', () => {
|
||||
@@ -162,8 +162,8 @@ describe('PhotoLightbox', () => {
|
||||
});
|
||||
|
||||
it('FE-COMP-LIGHTBOX-015: swipes horizontally to move between photos', () => {
|
||||
const { container } = render(<PhotoLightbox photos={samplePhotos} startIndex={1} onClose={vi.fn()} />);
|
||||
const surface = container.firstElementChild as HTMLElement;
|
||||
render(<PhotoLightbox photos={samplePhotos} startIndex={1} onClose={vi.fn()} />);
|
||||
const surface = screen.getByRole('dialog');
|
||||
|
||||
fireEvent.touchStart(surface, { touches: [{ clientX: 200, clientY: 200 }] });
|
||||
fireEvent.touchEnd(surface, { changedTouches: [{ clientX: 100, clientY: 210 }] });
|
||||
@@ -176,8 +176,8 @@ describe('PhotoLightbox', () => {
|
||||
|
||||
it('FE-COMP-LIGHTBOX-016: swipes down to close', () => {
|
||||
const onClose = vi.fn();
|
||||
const { container } = render(<PhotoLightbox photos={samplePhotos} onClose={onClose} />);
|
||||
const surface = container.firstElementChild as HTMLElement;
|
||||
render(<PhotoLightbox photos={samplePhotos} onClose={onClose} />);
|
||||
const surface = screen.getByRole('dialog');
|
||||
|
||||
fireEvent.touchStart(surface, { touches: [{ clientX: 200, clientY: 100 }] });
|
||||
fireEvent.touchEnd(surface, { changedTouches: [{ clientX: 210, clientY: 300 }] });
|
||||
@@ -187,8 +187,8 @@ describe('PhotoLightbox', () => {
|
||||
|
||||
it('FE-COMP-LIGHTBOX-017: ignores a touch end without a matching touch start', () => {
|
||||
const onClose = vi.fn();
|
||||
const { container } = render(<PhotoLightbox photos={samplePhotos} onClose={onClose} />);
|
||||
const surface = container.firstElementChild as HTMLElement;
|
||||
render(<PhotoLightbox photos={samplePhotos} onClose={onClose} />);
|
||||
const surface = screen.getByRole('dialog');
|
||||
|
||||
fireEvent.touchEnd(surface, { changedTouches: [{ clientX: 210, clientY: 300 }] });
|
||||
|
||||
@@ -294,8 +294,8 @@ describe('PhotoLightbox', () => {
|
||||
});
|
||||
|
||||
it('FE-COMP-LIGHTBOX-026: a swipe to the right turns to the next photo', () => {
|
||||
const { container } = render(<PhotoLightbox photos={samplePhotos} startIndex={1} onClose={vi.fn()} />);
|
||||
const surface = container.firstElementChild as HTMLElement;
|
||||
render(<PhotoLightbox photos={samplePhotos} startIndex={1} onClose={vi.fn()} />);
|
||||
const surface = screen.getByRole('dialog');
|
||||
|
||||
fireEvent.touchStart(surface, { touches: [{ clientX: 100, clientY: 200 }] });
|
||||
fireEvent.touchEnd(surface, { changedTouches: [{ clientX: 200, clientY: 210 }] });
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import { useState, useEffect, useCallback, useRef } from 'react'
|
||||
import { createPortal } from 'react-dom'
|
||||
import { ChevronLeft, ChevronRight, Loader2, X, ZoomIn, ZoomOut } from 'lucide-react'
|
||||
import VideoPlayer from './VideoPlayerLazy'
|
||||
import { usePhotoZoom } from './usePhotoZoom'
|
||||
import { Tooltip } from '../shared/Tooltip'
|
||||
import { isRtlLanguage, useTranslation } from '../../i18n'
|
||||
import { lockBodyScroll } from '../../utils/bodyScrollLock'
|
||||
|
||||
interface LightboxPhoto {
|
||||
id: string
|
||||
@@ -34,10 +36,22 @@ interface Props {
|
||||
/** The round glass buttons of the top bar. */
|
||||
const ROUND_BTN: React.CSSProperties = {
|
||||
background: 'rgba(255,255,255,0.1)', border: 'none', borderRadius: '50%',
|
||||
width: 36, height: 36, display: 'flex', alignItems: 'center', justifyContent: 'center',
|
||||
color: '#fff', cursor: 'pointer',
|
||||
width: 40, height: 40, display: 'flex', alignItems: 'center', justifyContent: 'center',
|
||||
color: '#fff', cursor: 'pointer', flex: 'none',
|
||||
}
|
||||
|
||||
/** Close is the one every visitor needs: a little stronger than the rest. */
|
||||
const CLOSE_BTN: React.CSSProperties = { ...ROUND_BTN, background: 'rgba(255,255,255,0.18)' }
|
||||
|
||||
/**
|
||||
* The device's insets, from whichever source reports them: env() in a browser,
|
||||
* the values Capacitor's SystemBars writes on Android, the ones the iOS app
|
||||
* measures itself. The viewer is portalled to <body>, outside the phone shell
|
||||
* that defines its own --m-safe-* tokens, so it reads the sources directly.
|
||||
*/
|
||||
const SAFE = (side: 'top' | 'bottom' | 'left' | 'right') =>
|
||||
`max(env(safe-area-inset-${side}, 0px), var(--safe-area-inset-${side}, 0px), var(--trek-native-safe-${side}, 0px))`
|
||||
|
||||
export default function PhotoLightbox({ photos, startIndex = 0, onClose, label }: Props) {
|
||||
const [idx, setIdx] = useState(startIndex)
|
||||
const touchStart = useRef<{ x: number; y: number } | null>(null)
|
||||
@@ -51,7 +65,11 @@ export default function PhotoLightbox({ photos, startIndex = 0, onClose, label }
|
||||
useEffect(() => {
|
||||
const opener = document.activeElement instanceof HTMLElement ? document.activeElement : null
|
||||
dialogRef.current?.focus()
|
||||
return () => opener?.focus()
|
||||
const unlock = lockBodyScroll()
|
||||
return () => {
|
||||
unlock()
|
||||
opener?.focus()
|
||||
}
|
||||
}, [])
|
||||
|
||||
const photo = photos[idx]
|
||||
@@ -111,7 +129,9 @@ export default function PhotoLightbox({ photos, startIndex = 0, onClose, label }
|
||||
|
||||
if (!photo) return null
|
||||
|
||||
return (
|
||||
// Portalled to <body>: inside a transformed sheet or pane, `fixed` would be
|
||||
// relative to that box and the viewer would open as a small window in it.
|
||||
return createPortal(
|
||||
<div
|
||||
ref={dialogRef}
|
||||
role="dialog"
|
||||
@@ -120,26 +140,27 @@ export default function PhotoLightbox({ photos, startIndex = 0, onClose, label }
|
||||
tabIndex={-1}
|
||||
style={{
|
||||
outline: 'none',
|
||||
position: 'fixed', inset: 0, zIndex: 10000,
|
||||
position: 'fixed', inset: 0, zIndex: 'var(--z-modal)',
|
||||
background: 'rgba(0,0,0,0.92)', backdropFilter: 'blur(20px)',
|
||||
display: 'flex', flexDirection: 'column',
|
||||
paddingBottom: 'var(--bottom-nav-h)',
|
||||
paddingBottom: SAFE('bottom'),
|
||||
paddingInlineStart: SAFE('left'),
|
||||
paddingInlineEnd: SAFE('right'),
|
||||
}}
|
||||
onTouchStart={onTouchStart}
|
||||
onTouchMove={onTouchMove}
|
||||
onTouchEnd={onTouchEnd}
|
||||
>
|
||||
{/* Photo area — centered with nav overlays */}
|
||||
<div
|
||||
className="group/lightbox"
|
||||
style={{ flex: 1, display: 'flex', alignItems: 'center', justifyContent: 'center', position: 'relative', overflow: 'hidden' }}
|
||||
>
|
||||
{/* Top bar */}
|
||||
<div style={{ position: 'absolute', top: 0, insetInline: 0, zIndex: 10, display: 'flex', justifyContent: 'space-between', alignItems: 'center', padding: '16px 20px' }}>
|
||||
<span style={{ color: 'rgba(255,255,255,0.5)', fontSize: 'calc(13px * var(--fs-scale-body, 1))', fontWeight: 500 }}>
|
||||
{idx + 1} / {photos.length}
|
||||
</span>
|
||||
<div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
|
||||
{/* Top bar: its own row below the status bar and a notch, so the close
|
||||
button is always there to tap and never sits over the photo. */}
|
||||
<div style={{
|
||||
flex: 'none', display: 'flex', justifyContent: 'space-between', alignItems: 'center', gap: 12,
|
||||
paddingBlock: `calc(${SAFE('top')} + 10px) 10px`, paddingInline: 16,
|
||||
}}>
|
||||
<span style={{ color: 'rgba(255,255,255,0.5)', fontSize: 'calc(13px * var(--fs-scale-body, 1))', fontWeight: 500, fontVariantNumeric: 'tabular-nums' }}>
|
||||
{idx + 1} / {photos.length}
|
||||
</span>
|
||||
<div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
|
||||
{isImage && (
|
||||
<>
|
||||
<Tooltip label={t('journey.lightbox.zoomOut')}>
|
||||
@@ -162,16 +183,17 @@ export default function PhotoLightbox({ photos, startIndex = 0, onClose, label }
|
||||
</Tooltip>
|
||||
</>
|
||||
)}
|
||||
<button type="button" onClick={onClose} aria-label={t('common.close')} style={{
|
||||
background: 'rgba(255,255,255,0.1)', border: 'none', borderRadius: '50%',
|
||||
width: 36, height: 36, display: 'flex', alignItems: 'center', justifyContent: 'center',
|
||||
color: '#fff', cursor: 'pointer',
|
||||
}}>
|
||||
<X size={18} />
|
||||
<button type="button" onClick={onClose} aria-label={t('common.close')} style={CLOSE_BTN}>
|
||||
<X size={20} />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Photo area: what is left below the bar, with the page arrows over it. */}
|
||||
<div
|
||||
className="group/lightbox"
|
||||
style={{ flex: 1, minHeight: 0, display: 'flex', alignItems: 'center', justifyContent: 'center', position: 'relative', overflow: 'hidden', padding: '0 8px 8px' }}
|
||||
>
|
||||
{/* Prev button — visible on hover (desktop), always visible (mobile) */}
|
||||
{hasPrev && (
|
||||
<button type="button" onClick={prev} aria-label={t('journey.lightbox.previous')} className="flex sm:opacity-0 sm:group-hover/lightbox:opacity-100 focus-visible:opacity-100 transition-opacity" style={{
|
||||
@@ -200,7 +222,7 @@ export default function PhotoLightbox({ photos, startIndex = 0, onClose, label }
|
||||
onError={() => setLoadedId(photo.id)}
|
||||
{...zoom.mouse}
|
||||
style={{
|
||||
maxWidth: '92vw', maxHeight: '92vh',
|
||||
maxWidth: '100%', maxHeight: '100%',
|
||||
objectFit: 'contain', borderRadius: 4,
|
||||
animation: 'fadeIn 0.15s ease',
|
||||
userSelect: 'none',
|
||||
@@ -212,7 +234,7 @@ export default function PhotoLightbox({ photos, startIndex = 0, onClose, label }
|
||||
{loading && (
|
||||
<div data-testid="lightbox-loading" aria-hidden="true" style={{ position: 'absolute', inset: 0, display: 'flex', alignItems: 'center', justifyContent: 'center', pointerEvents: 'none' }}>
|
||||
{photo.thumbSrc && (
|
||||
<img src={photo.thumbSrc} alt="" draggable={false} style={{ width: '92vw', height: '92vh', objectFit: 'contain', borderRadius: 4 }} />
|
||||
<img src={photo.thumbSrc} alt="" draggable={false} style={{ width: '100%', height: '100%', objectFit: 'contain', borderRadius: 4 }} />
|
||||
)}
|
||||
<span style={{ ...ROUND_BTN, position: 'absolute', cursor: 'default' }}>
|
||||
<Loader2 size={18} className="animate-spin" />
|
||||
@@ -252,6 +274,7 @@ export default function PhotoLightbox({ photos, startIndex = 0, onClose, label }
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>,
|
||||
document.body,
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user