-
Notifications
You must be signed in to change notification settings - Fork 3
feat(Books): auto-detect animated thumbnails and support pausing at p… #788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jakeaturner
wants to merge
1
commit into
staging
Choose a base branch
from
feat/pausable-images
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| import { | ||
| useRef, | ||
| useState, | ||
| useEffect, | ||
| useCallback, | ||
| ImgHTMLAttributes, | ||
| } from "react"; | ||
|
|
||
| interface PausableImageProps extends ImgHTMLAttributes<HTMLImageElement> { | ||
| /** Whether this image is animated (e.g. GIF). When true, renders a | ||
| * canvas-based freeze frame and a pause/play toggle button. */ | ||
| isAnimated?: boolean; | ||
| } | ||
|
|
||
| /** | ||
| * Drop-in replacement for <img> that adds a pause/play toggle for animated | ||
| * images. Pass `isAnimated` to enable the pause UI — animation detection | ||
| * is handled upstream (e.g. server-side Content-Type check during sync). | ||
| * | ||
| * Accessibility: | ||
| * - Auto-pauses when prefers-reduced-motion: reduce is active (SC 2.2.2) | ||
| * - Pause button meets 24x24px minimum target size (SC 2.5.8) | ||
| * - Decorative images (alt="") get aria-hidden on the canvas | ||
| */ | ||
| const PausableImage: React.FC<PausableImageProps> = ({ | ||
| src, | ||
| alt, | ||
| className, | ||
| style, | ||
| onLoad, | ||
| isAnimated = false, | ||
| ...props | ||
| }) => { | ||
| const canvasRef = useRef<HTMLCanvasElement>(null); | ||
| const imgRef = useRef<HTMLImageElement>(null); | ||
| const [paused, setPaused] = useState( | ||
| () => window.matchMedia("(prefers-reduced-motion: reduce)").matches | ||
| ); | ||
| const [frameCaptured, setFrameCaptured] = useState(false); | ||
|
|
||
| const captureFrame = useCallback(() => { | ||
| const img = imgRef.current; | ||
| const canvas = canvasRef.current; | ||
| if (!img || !canvas) return; | ||
| canvas.width = img.naturalWidth; | ||
| canvas.height = img.naturalHeight; | ||
| canvas.getContext("2d")?.drawImage(img, 0, 0); | ||
| setFrameCaptured(true); | ||
| }, []); | ||
|
|
||
| // Auto-pause when prefers-reduced-motion is active | ||
| useEffect(() => { | ||
| if (!isAnimated) return; | ||
| const mq = window.matchMedia("(prefers-reduced-motion: reduce)"); | ||
| if (mq.matches) setPaused(true); | ||
| const handler = (e: MediaQueryListEvent) => { | ||
| if (e.matches) setPaused(true); | ||
| }; | ||
| mq.addEventListener("change", handler); | ||
| return () => mq.removeEventListener("change", handler); | ||
| }, [isAnimated]); | ||
|
|
||
| if (!isAnimated) { | ||
| return ( | ||
| <img src={src} alt={alt} className={className} style={style} {...props} /> | ||
| ); | ||
| } | ||
|
|
||
| const showPauseUI = frameCaptured; | ||
| const isDecorative = alt === ""; | ||
|
|
||
| return ( | ||
| <div style={{ position: "relative" }}> | ||
| <img | ||
| ref={imgRef} | ||
| src={src} | ||
| alt={alt} | ||
| className={className} | ||
| style={{ | ||
| ...style, | ||
| ...(showPauseUI && paused ? { display: "none" } : {}), | ||
| }} | ||
| onLoad={(e) => { | ||
| captureFrame(); | ||
| onLoad?.(e); | ||
| }} | ||
| {...props} | ||
| /> | ||
| <canvas | ||
| ref={canvasRef} | ||
| className={className} | ||
| style={{ | ||
| ...style, | ||
| ...(!(showPauseUI && paused) ? { display: "none" } : {}), | ||
| }} | ||
| {...(isDecorative | ||
| ? { "aria-hidden": true as const } | ||
| : { role: "img", "aria-label": alt })} | ||
| /> | ||
| {showPauseUI && ( | ||
| <button | ||
| type="button" | ||
| onClick={(e) => { | ||
| e.stopPropagation(); | ||
| setPaused((p) => !p); | ||
| }} | ||
| aria-label={paused ? "Play animation" : "Pause animation"} | ||
| className="absolute bottom-2 right-2 flex items-center justify-center | ||
| min-w-6 min-h-6 w-8 h-8 rounded-full | ||
| bg-black/60 text-white | ||
| hover:bg-black/80 | ||
| focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-700" | ||
| > | ||
| {paused ? "\u25B6" : "\u23F8"} | ||
| </button> | ||
| )} | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default PausableImage; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You probably want to also match against
image/apng,image/avif, andimage/webpto be safe.