-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #26 Eject `MDXComponents/Img` using Docusaurus swizzling. Adapt the expandable image logic from the `Image` component in `gravitational/docs` to the Docusaurus site, copying the `ModalImage` and `PlainImage` components, plus their supporting hooks, into the ejected `MDXImg` component.
- Loading branch information
Showing
4 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,124 @@ | ||
import cn from "classnames"; | ||
import { React, useEffect, useState, useCallback, useRef } from "react"; | ||
import clsx from "clsx"; | ||
import type { Props } from "@theme/MDXComponents/Img"; | ||
|
||
import styles from "./styles.module.css"; | ||
|
||
function useClickInside(ref, handler) { | ||
useEffect(() => { | ||
const listener = (e: MouseEvent) => { | ||
if (ref.current) { | ||
handler(e); | ||
} | ||
}; | ||
document.addEventListener("mousedown", listener); | ||
return () => { | ||
document.removeEventListener("mousedown", listener); | ||
}; | ||
}, [ref, handler]); | ||
} | ||
|
||
function useEscape(handler) { | ||
useEffect(() => { | ||
const listener = (e: KeyboardEvent) => { | ||
if (e.key === "Escape") { | ||
handler(e); | ||
} | ||
}; | ||
document.addEventListener("keydown", listener); | ||
return () => { | ||
document.removeEventListener("keydown", listener); | ||
}; | ||
}, [handler]); | ||
} | ||
|
||
function useDisableBodyScroll(shouldDisable) { | ||
useEffect(() => { | ||
if (shouldDisable) { | ||
document.body.style.overflow = "hidden"; | ||
} else { | ||
document.body.style.overflow = "unset"; | ||
} | ||
}, [shouldDisable]); | ||
} | ||
|
||
function transformImgClassName(className?: string): string { | ||
return clsx(className, styles.img); | ||
} | ||
|
||
//** Maximum width of content block where images are placed. | ||
/* If the original image is smaller, then there is no sense to expand the image by clicking. */ | ||
const MAX_CONTENT_WIDTH = 900; | ||
|
||
type ModalImageProps = { | ||
setShowExpandedImage: Dispatch<SetStateAction<boolean>>; | ||
} & ImageProps; | ||
|
||
const ModalImage = ({ setShowExpandedImage, ...props }: ModalImageProps) => { | ||
const closeHandler = useCallback( | ||
() => setShowExpandedImage(false), | ||
[setShowExpandedImage] | ||
); | ||
const modalRef = useRef<HTMLDivElement>(); | ||
useClickInside(modalRef, closeHandler); | ||
useEscape(closeHandler); | ||
|
||
return ( | ||
<div ref={modalRef}> | ||
<div className={styles.overlay} /> | ||
<div className={styles.dialog}> | ||
<img | ||
decoding="async" | ||
loading="lazy" | ||
{...props} | ||
className={transformImgClassName(props.className)} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default function MDXImg(props: Props): JSX.Element { | ||
const [showExpandedImage, setShowExpandedImage] = useState(false); | ||
const shouldExpand = props.width > MAX_CONTENT_WIDTH; | ||
useDisableBodyScroll(showExpandedImage); | ||
const handleClickImage = () => { | ||
if (shouldExpand) { | ||
setShowExpandedImage(true); | ||
} | ||
}; | ||
const PlainImage = () => { | ||
if (shouldExpand) { | ||
return ( | ||
<button onClick={handleClickImage} className={styles.zoomable}> | ||
<img | ||
decoding="async" | ||
loading="lazy" | ||
{...props} | ||
className={transformImgClassName(props.className)} | ||
/> | ||
</button> | ||
); | ||
} else | ||
return ( | ||
<img | ||
decoding="async" | ||
loading="lazy" | ||
{...props} | ||
className={transformImgClassName(props.className)} | ||
/> | ||
); | ||
}; | ||
|
||
return ( | ||
<> | ||
<span className={cn(styles.wrapper)}> | ||
<PlainImage /> | ||
</span> | ||
{showExpandedImage && ( | ||
<ModalImage setShowExpandedImage={setShowExpandedImage} {...props} /> | ||
)} | ||
</> | ||
); | ||
} |
This file contains 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,45 @@ | ||
.img { | ||
height: auto; | ||
} | ||
|
||
.overlay { | ||
background-color: rgba(0, 0, 0, 0.7); | ||
width: 100vw; | ||
height: 100%; | ||
min-width: 100%; | ||
top: 50%; | ||
left: 50%; | ||
z-index: 3002; | ||
transform: translate(-50%, -50%); | ||
position: fixed; | ||
cursor: zoom-out; | ||
} | ||
|
||
.zoomable { | ||
/* | ||
* The background-color and border styles override browser defaults for the | ||
* button element. | ||
*/ | ||
background-color: inherit; | ||
border: inherit; | ||
|
||
cursor: zoom-in; | ||
cursor: -moz-zoom-in; | ||
cursor: -webkit-zoom-in; | ||
} | ||
|
||
.dialog { | ||
position: fixed; | ||
top: 50%; | ||
left: 50%; | ||
transform: translate(-50%, -50%); | ||
background: rgba(0,0,0,0); | ||
cursor: zoom-out; | ||
cursor: -moz-zoom-out; | ||
cursor: -webkit-zoom-out; | ||
z-index: 3002; | ||
& .img { | ||
max-width: 95vw; | ||
max-height: 95vh; | ||
} | ||
} |
This file contains 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