MCPcopy Create free account
hub / github.com/danvk/webdiff / ImageBlinker

Function ImageBlinker

ts/ImageBlinker.tsx:10–77  ·  view source on GitHub ↗
(props: ImageDiffProps)

Source from the content-addressed store, hash-verified

8 * This component handles toggling between the two images itself.
9 */
10export function ImageBlinker(props: ImageDiffProps) {
11 const [idx, setIdx] = React.useState(0);
12 const [autoBlink, setAutoBlink] = React.useState(true);
13
14 const autoblinkRef = React.createRef<HTMLInputElement>();
15
16 const toggleAutoBlink = () => {
17 if (autoblinkRef.current) {
18 setAutoBlink(autoblinkRef.current.checked);
19 }
20 };
21
22 const blink = React.useCallback(
23 (e: KeyboardEvent) => {
24 if (!isLegitKeypress(e)) {
25 return;
26 }
27 if (e.key === 'b') {
28 setAutoBlink(false);
29 setIdx(idx => 1 - idx);
30 }
31 },
32 [setIdx, setAutoBlink],
33 );
34
35 // XXX old version also sets this on a[value="blink"], what is that?
36 React.useEffect(() => {
37 document.addEventListener('keydown', blink);
38 return () => {
39 document.removeEventListener('keydown', blink);
40 };
41 }, [blink]);
42
43 React.useEffect(() => {
44 if (autoBlink) {
45 const interval = setInterval(() => {
46 setIdx(idx => 1 - idx);
47 }, 500 /* ms */);
48 return () => {
49 clearInterval(interval);
50 };
51 }
52 }, [autoBlink, setIdx]);
53
54 const side = idx === 0 ? 'a' : 'b';
55 const maxWidth = props.shrinkToFit ? window.innerWidth - 30 : null;
56 return (
57 <div>
58 <input
59 ref={autoblinkRef}
60 type="checkbox"
61 id="autoblink"
62 checked={autoBlink}
63 onChange={toggleAutoBlink}
64 />
65 <label htmlFor="autoblink"> Auto-blink (hit ‘b’ to blink manually)</label>
66 <table id="imagediff">
67 <tbody>

Callers

nothing calls this directly

Calls 1

isLegitKeypressFunction · 0.90

Tested by

no test coverage detected