|
| 1 | +import cn from "classnames"; |
| 2 | +import { React, useEffect, useState, useCallback, useRef } from "react"; |
| 3 | +import clsx from "clsx"; |
| 4 | +import type { Props } from "@theme/MDXComponents/Img"; |
| 5 | + |
| 6 | +import styles from "./styles.module.css"; |
| 7 | + |
| 8 | +function useClickInside(ref, handler) { |
| 9 | + useEffect(() => { |
| 10 | + const listener = (e: MouseEvent) => { |
| 11 | + if (ref.current) { |
| 12 | + handler(e); |
| 13 | + } |
| 14 | + }; |
| 15 | + document.addEventListener("mousedown", listener); |
| 16 | + return () => { |
| 17 | + document.removeEventListener("mousedown", listener); |
| 18 | + }; |
| 19 | + }, [ref, handler]); |
| 20 | +} |
| 21 | + |
| 22 | +function useEscape(handler) { |
| 23 | + useEffect(() => { |
| 24 | + const listener = (e: KeyboardEvent) => { |
| 25 | + if (e.key === "Escape") { |
| 26 | + handler(e); |
| 27 | + } |
| 28 | + }; |
| 29 | + document.addEventListener("keydown", listener); |
| 30 | + return () => { |
| 31 | + document.removeEventListener("keydown", listener); |
| 32 | + }; |
| 33 | + }, [handler]); |
| 34 | +} |
| 35 | + |
| 36 | +function useDisableBodyScroll(shouldDisable) { |
| 37 | + useEffect(() => { |
| 38 | + if (shouldDisable) { |
| 39 | + document.body.style.overflow = "hidden"; |
| 40 | + } else { |
| 41 | + document.body.style.overflow = "unset"; |
| 42 | + } |
| 43 | + }, [shouldDisable]); |
| 44 | +} |
| 45 | + |
| 46 | +function transformImgClassName(className?: string): string { |
| 47 | + return clsx(className, styles.img); |
| 48 | +} |
| 49 | + |
| 50 | +//** Maximum width of content block where images are placed. |
| 51 | +/* If the original image is smaller, then there is no sense to expand the image by clicking. */ |
| 52 | +const MAX_CONTENT_WIDTH = 900; |
| 53 | + |
| 54 | +type ModalImageProps = { |
| 55 | + setShowExpandedImage: Dispatch<SetStateAction<boolean>>; |
| 56 | +} & ImageProps; |
| 57 | + |
| 58 | +const ModalImage = ({ setShowExpandedImage, ...props }: ModalImageProps) => { |
| 59 | + const closeHandler = useCallback( |
| 60 | + () => setShowExpandedImage(false), |
| 61 | + [setShowExpandedImage] |
| 62 | + ); |
| 63 | + const modalRef = useRef<HTMLDivElement>(); |
| 64 | + useClickInside(modalRef, closeHandler); |
| 65 | + useEscape(closeHandler); |
| 66 | + |
| 67 | + return ( |
| 68 | + <div ref={modalRef}> |
| 69 | + <div className={styles.overlay} /> |
| 70 | + <div className={styles.dialog}> |
| 71 | + <img |
| 72 | + decoding="async" |
| 73 | + loading="lazy" |
| 74 | + {...props} |
| 75 | + className={transformImgClassName(props.className)} |
| 76 | + /> |
| 77 | + </div> |
| 78 | + </div> |
| 79 | + ); |
| 80 | +}; |
| 81 | + |
| 82 | +export default function MDXImg(props: Props): JSX.Element { |
| 83 | + const [showExpandedImage, setShowExpandedImage] = useState(false); |
| 84 | + const shouldExpand = props.width > MAX_CONTENT_WIDTH; |
| 85 | + useDisableBodyScroll(showExpandedImage); |
| 86 | + const handleClickImage = () => { |
| 87 | + if (shouldExpand) { |
| 88 | + setShowExpandedImage(true); |
| 89 | + } |
| 90 | + }; |
| 91 | + const PlainImage = () => { |
| 92 | + if (shouldExpand) { |
| 93 | + return ( |
| 94 | + <button onClick={handleClickImage} className={styles.zoomable}> |
| 95 | + <img |
| 96 | + decoding="async" |
| 97 | + loading="lazy" |
| 98 | + {...props} |
| 99 | + className={transformImgClassName(props.className)} |
| 100 | + /> |
| 101 | + </button> |
| 102 | + ); |
| 103 | + } else |
| 104 | + return ( |
| 105 | + <img |
| 106 | + decoding="async" |
| 107 | + loading="lazy" |
| 108 | + {...props} |
| 109 | + className={transformImgClassName(props.className)} |
| 110 | + /> |
| 111 | + ); |
| 112 | + }; |
| 113 | + |
| 114 | + return ( |
| 115 | + <> |
| 116 | + <span className={cn(styles.wrapper)}> |
| 117 | + <PlainImage /> |
| 118 | + </span> |
| 119 | + {showExpandedImage && ( |
| 120 | + <ModalImage setShowExpandedImage={setShowExpandedImage} {...props} /> |
| 121 | + )} |
| 122 | + </> |
| 123 | + ); |
| 124 | +} |
0 commit comments