Add keyboard activation to the interactive elements and use stable keys. [skip build]
This commit is contained in:
@@ -5,6 +5,7 @@ import { Icon, Icons } from '../icons';
|
||||
import * as css from './ImageViewer.css';
|
||||
import { downloadAndSaveMedia } from '../../utils/saveMedia';
|
||||
import { getCurrentAccessToken } from '../../utils/auth';
|
||||
import { onEnterOrSpace } from '../../utils/keyboard';
|
||||
|
||||
const ZOOM_MIN = 1;
|
||||
const ZOOM_MAX = 5;
|
||||
@@ -270,6 +271,14 @@ export const ImageViewer = as<'div', ImageViewerProps>(
|
||||
}
|
||||
};
|
||||
|
||||
const toggleZoomAt = (x: number, y: number) => {
|
||||
if (zoomRef.current.scale <= 1.01) {
|
||||
zoomAround(x, y, ZOOM_CLICK);
|
||||
} else {
|
||||
resetZoom();
|
||||
}
|
||||
};
|
||||
|
||||
const handleClick = (event: React.MouseEvent) => {
|
||||
if (suppressClickRef.current) {
|
||||
suppressClickRef.current = false;
|
||||
@@ -283,13 +292,16 @@ export const ImageViewer = as<'div', ImageViewerProps>(
|
||||
if (!img?.src) return;
|
||||
event.preventDefault();
|
||||
const { x, y } = framePoint(event.clientX, event.clientY);
|
||||
if (zoomRef.current.scale <= 1.01) {
|
||||
zoomAround(x, y, ZOOM_CLICK);
|
||||
} else {
|
||||
resetZoom();
|
||||
}
|
||||
toggleZoomAt(x, y);
|
||||
};
|
||||
|
||||
// Keyboard activation has no pointer position, so it zooms around the frame centre.
|
||||
const handleKeyDown = onEnterOrSpace(() => {
|
||||
const frame = frameRef.current;
|
||||
if (!frame || !imgRef.current?.src) return;
|
||||
toggleZoomAt(frame.clientWidth / 2, frame.clientHeight / 2);
|
||||
});
|
||||
|
||||
const handlePointerDown = (event: React.PointerEvent) => {
|
||||
if (event.button !== 0 && event.pointerType === 'mouse') return;
|
||||
|
||||
@@ -410,6 +422,10 @@ export const ImageViewer = as<'div', ImageViewerProps>(
|
||||
<div
|
||||
ref={frameRef}
|
||||
className={css.Frame}
|
||||
role="button"
|
||||
tabIndex={0}
|
||||
aria-label={alt || undefined}
|
||||
onKeyDown={handleKeyDown}
|
||||
data-zoomed={zoomed || undefined}
|
||||
data-pannable={zoomed || undefined}
|
||||
data-dragging={dragging || undefined}
|
||||
|
||||
Reference in New Issue
Block a user