-
Notifications
You must be signed in to change notification settings - Fork 34
Add expandable image and table MDX components #684
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,13 @@ | ||
| #!/bin/sh | ||
| . "$(dirname "$0")/_/husky.sh" | ||
|
|
||
| yarn run lint-staged | ||
| if command -v yarn >/dev/null 2>&1; then | ||
| yarn run lint-staged | ||
| elif command -v corepack >/dev/null 2>&1; then | ||
| corepack yarn run lint-staged | ||
| elif command -v npm >/dev/null 2>&1; then | ||
| npm exec -- lint-staged | ||
| else | ||
| echo "Error: yarn/corepack/npm not found in PATH" | ||
| exit 127 | ||
| fi |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import * as React from "react"; | ||
| import { useI18next } from "gatsby-plugin-react-i18next"; | ||
|
|
||
| const IMAGE_LABELS = { | ||
| en: { | ||
| expand: "Expand image", | ||
| collapse: "Collapse image", | ||
| }, | ||
| zh: { | ||
| expand: "Expand image", | ||
| collapse: "Collapse image", | ||
| }, | ||
| ja: { | ||
| expand: "Expand image", | ||
| collapse: "Collapse image", | ||
| }, | ||
| } as const; | ||
|
|
||
| function getImageLabel(language: string, expanded: boolean) { | ||
| const lang = language.startsWith("zh") | ||
| ? "zh" | ||
| : language.startsWith("ja") | ||
| ? "ja" | ||
| : "en"; | ||
| return expanded ? IMAGE_LABELS[lang].collapse : IMAGE_LABELS[lang].expand; | ||
| } | ||
|
|
||
| export function ExpandableImage( | ||
| props: React.ImgHTMLAttributes<HTMLImageElement> | ||
| ) { | ||
| const [open, setOpen] = React.useState(false); | ||
| const { language } = useI18next(); | ||
| const expandLabel = getImageLabel(language, false); | ||
| const collapseLabel = getImageLabel(language, true); | ||
|
|
||
| React.useEffect(() => { | ||
| if (!open) return; | ||
|
|
||
| const originalOverflow = document.body.style.overflow; | ||
| document.body.style.overflow = "hidden"; | ||
|
|
||
| const onKeyDown = (event: KeyboardEvent) => { | ||
| if (event.key === "Escape") { | ||
| setOpen(false); | ||
| } | ||
| }; | ||
|
|
||
| document.addEventListener("keydown", onKeyDown); | ||
|
|
||
| return () => { | ||
| document.body.style.overflow = originalOverflow; | ||
| document.removeEventListener("keydown", onKeyDown); | ||
| }; | ||
| }, [open]); | ||
|
|
||
| return ( | ||
| <div className="expandable-image"> | ||
| <button | ||
| type="button" | ||
| className="expandable-toggle-button" | ||
| onClick={(event) => { | ||
| event.preventDefault(); | ||
| event.stopPropagation(); | ||
| setOpen(true); | ||
| }} | ||
| aria-expanded={open} | ||
| > | ||
| {expandLabel} | ||
| </button> | ||
| <img {...props} /> | ||
| {open && ( | ||
| <div | ||
| className="expandable-modal-backdrop" | ||
| role="presentation" | ||
| onClick={() => setOpen(false)} | ||
| > | ||
| <div | ||
| className="expandable-modal-content expandable-image-modal-content" | ||
| role="dialog" | ||
| aria-modal="true" | ||
| onClick={(event) => event.stopPropagation()} | ||
| > | ||
| <button | ||
| type="button" | ||
| className="expandable-modal-collapse" | ||
| onClick={() => setOpen(false)} | ||
| > | ||
| {collapseLabel} | ||
| </button> | ||
| <div className="expandable-modal-scroll"> | ||
| <img {...props} className="expandable-modal-image" /> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| )} | ||
| </div> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import * as React from "react"; | ||
| import { useI18next } from "gatsby-plugin-react-i18next"; | ||
|
|
||
| const TABLE_LABELS = { | ||
| en: { | ||
| expand: "Expand table", | ||
| collapse: "Collapse table", | ||
| }, | ||
| zh: { | ||
| expand: "Expand table", | ||
| collapse: "Collapse table", | ||
| }, | ||
| ja: { | ||
| expand: "Expand table", | ||
| collapse: "Collapse table", | ||
| }, | ||
| } as const; | ||
|
|
||
| function getTableLabel(language: string, expanded: boolean) { | ||
| const lang = language.startsWith("zh") | ||
| ? "zh" | ||
| : language.startsWith("ja") | ||
| ? "ja" | ||
| : "en"; | ||
| return expanded ? TABLE_LABELS[lang].collapse : TABLE_LABELS[lang].expand; | ||
| } | ||
|
Comment on lines
+19
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
|
|
||
| export function ExpandableTable( | ||
| props: React.TableHTMLAttributes<HTMLTableElement> | ||
| ) { | ||
| const [open, setOpen] = React.useState(false); | ||
| const { language } = useI18next(); | ||
| const expandLabel = getTableLabel(language, false); | ||
| const collapseLabel = getTableLabel(language, true); | ||
|
|
||
| React.useEffect(() => { | ||
| if (!open) return; | ||
|
|
||
| const originalOverflow = document.body.style.overflow; | ||
| document.body.style.overflow = "hidden"; | ||
|
|
||
| const onKeyDown = (event: KeyboardEvent) => { | ||
| if (event.key === "Escape") { | ||
| setOpen(false); | ||
| } | ||
| }; | ||
|
|
||
| document.addEventListener("keydown", onKeyDown); | ||
|
|
||
| return () => { | ||
| document.body.style.overflow = originalOverflow; | ||
| document.removeEventListener("keydown", onKeyDown); | ||
| }; | ||
| }, [open]); | ||
|
|
||
| return ( | ||
| <div className="expandable-table"> | ||
| <button | ||
| type="button" | ||
| className="expandable-toggle-button" | ||
| onClick={() => setOpen(true)} | ||
| aria-expanded={open} | ||
| > | ||
| {expandLabel} | ||
| </button> | ||
| <table {...props} /> | ||
| {open && ( | ||
| <div | ||
| className="expandable-modal-backdrop" | ||
| role="presentation" | ||
| onClick={() => setOpen(false)} | ||
| > | ||
| <div | ||
| className="expandable-modal-content expandable-table-modal-content" | ||
| role="dialog" | ||
| aria-modal="true" | ||
| onClick={(event) => event.stopPropagation()} | ||
| > | ||
| <button | ||
| type="button" | ||
| className="expandable-modal-collapse" | ||
| onClick={() => setOpen(false)} | ||
| > | ||
| {collapseLabel} | ||
| </button> | ||
| <div className="expandable-modal-scroll"> | ||
| <table {...props} /> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| )} | ||
| </div> | ||
| ); | ||
| } | ||
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.
This
getImageLabelfunction and its language detection logic are duplicated inExpandableTable.tsx. To improve maintainability and follow the DRY (Don't Repeat Yourself) principle, please consider extracting the language key detection logic into a shared utility function that both components can use.