Skip to content

Commit cff7a87

Browse files
authored
Merge pull request #12236 from ethereum/adding-glossary-tooltips
Improving comprehension epic - glossary tooltip setup
2 parents 7b17ba5 + 0787193 commit cff7a87

File tree

16 files changed

+327
-85
lines changed

16 files changed

+327
-85
lines changed

src/components/Glossary/GlossaryDefinition/index.tsx

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
1+
import { ComponentProps } from "react"
12
import { Box, Text } from "@chakra-ui/react"
23

4+
import InlineLink from "@/components/Link"
35
import OldHeading from "@/components/OldHeading"
46
import Translation from "@/components/Translation"
57

8+
import { DEFAULT_GLOSSARY_NS } from "@/lib/constants"
9+
610
interface GlossaryDefinitionProps {
711
term: string
812
size?: "md" | "sm"
13+
options?: ComponentProps<typeof Translation>["options"]
14+
}
15+
16+
// Override the default `a` mapping to prevent displaying the glossary tooltip
17+
// in the glossary definition
18+
const components = {
19+
a: InlineLink,
920
}
1021

11-
const GlossaryDefinition = ({ term, size = "md" }: GlossaryDefinitionProps) => {
22+
const GlossaryDefinition = ({
23+
term,
24+
size = "md",
25+
options = { ns: DEFAULT_GLOSSARY_NS },
26+
}: GlossaryDefinitionProps) => {
1227
const headingStyles =
1328
size === "sm"
1429
? { fontSize: "md", mt: 0, mb: 2 }
@@ -17,12 +32,20 @@ const GlossaryDefinition = ({ term, size = "md" }: GlossaryDefinitionProps) => {
1732
const textStyles = size === "sm" ? { mb: 0 } : {}
1833

1934
return (
20-
<Box>
35+
<Box textAlign="start">
2136
<OldHeading as="h3" lineHeight={1.4} id={term} {...headingStyles}>
22-
<Translation id={"glossary:" + term + "-term"} />
37+
<Translation
38+
id={term + "-term"}
39+
options={options}
40+
transform={components}
41+
/>
2342
</OldHeading>
2443
<Text {...textStyles}>
25-
<Translation id={"glossary:" + term + "-definition"} />
44+
<Translation
45+
id={term + "-definition"}
46+
options={options}
47+
transform={components}
48+
/>
2649
</Text>
2750
</Box>
2851
)

src/components/Glossary/GlossaryTooltip/index.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@ const GlossaryTooltip = ({ children, termKey }: GlossaryTooltipProps) => {
1313
const isLargeScreen = useBreakpointValue({ base: false, lg: true })
1414

1515
return isLargeScreen ? (
16-
<Tooltip content={<GlossaryDefinition term={termKey} size="sm" />}>
16+
<Tooltip
17+
content={
18+
<GlossaryDefinition
19+
term={termKey}
20+
size="sm"
21+
options={{ ns: "glossary-tooltip" }}
22+
/>
23+
}
24+
>
1725
<Text
1826
as="u"
1927
textDecorationStyle="dotted"

src/components/MdComponents.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ import { ButtonLink } from "@/components/Buttons"
2727
import Contributors from "@/components/Contributors"
2828
import Link from "@/components/Link"
2929
import MarkdownImage from "@/components/MarkdownImage"
30-
import MdLink from "@/components/MdLink"
3130
import OldHeading from "@/components/OldHeading"
3231
import { mdxTableComponents } from "@/components/Table"
32+
import TooltipLink from "@/components/TooltipLink"
3333
import YouTube from "@/components/YouTube"
3434

3535
import GlossaryTooltip from "./Glossary/GlossaryTooltip"
@@ -148,7 +148,7 @@ export const HR = () => (
148148

149149
// All base html element components
150150
export const htmlElements = {
151-
a: MdLink,
151+
a: TooltipLink,
152152
div: Box,
153153
h1: Heading1,
154154
h2: Heading2,

src/components/MdLink.tsx renamed to src/components/TooltipLink.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import GlossaryTooltip from "./Glossary/GlossaryTooltip"
44
import InlineLink from "./Link"
55

66
interface Props {
7-
href: string
8-
children: ReactNode
7+
href?: string
8+
children?: ReactNode
99
}
1010

1111
/**
@@ -18,9 +18,14 @@ interface Props {
1818
* inside of our MD files. The native link syntax has a better UX with Crowdin
1919
* translations.
2020
*/
21-
function MdLink(props: Props) {
21+
function TooltipLink(props: Props) {
22+
const { href } = props
23+
if (!href) {
24+
return <InlineLink {...props} />
25+
}
26+
2227
const regex = new RegExp(/\/glossary\/#(.+)/)
23-
const matches = props.href.match(regex)
28+
const matches = href.match(regex)
2429

2530
// get the `termKey` from the `href`
2631
// e.g. in `/glossary/#new-term` => "new-term" is the `termKey`
@@ -33,4 +38,4 @@ function MdLink(props: Props) {
3338
return <InlineLink {...props} />
3439
}
3540

36-
export default MdLink
41+
export default TooltipLink

src/components/Translation.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
1-
import htmr from "htmr"
1+
import htmr, { type HtmrOptions } from "htmr"
22
import type { TOptions } from "i18next"
33
import { useRouter } from "next/router"
44
import { useTranslation } from "next-i18next"
55

66
import { getRequiredNamespacesForPage } from "@/lib/utils/translations"
77

8-
import InlineLink from "./Link"
8+
import TooltipLink from "./TooltipLink"
99

1010
type TranslationProps = {
1111
id: string
1212
options?: TOptions
13+
transform?: HtmrOptions["transform"]
1314
}
1415

1516
// Custom components mapping to be used by `htmr` when parsing the translation
1617
// text
17-
const transform = {
18-
a: InlineLink,
18+
const defaultTransform = {
19+
a: TooltipLink,
1920
}
2021

2122
// Renders the translation string for the given translation key `id`. It
2223
// fallback to English if it doesn't find the given key in the current language
23-
const Translation = ({ id, options }: TranslationProps) => {
24+
const Translation = ({ id, options, transform = {} }: TranslationProps) => {
2425
const { asPath } = useRouter()
2526
const requiredNamespaces = getRequiredNamespacesForPage(asPath)
2627

2728
const { t } = useTranslation(requiredNamespaces)
2829
const translatedText = t(id, options)
2930

3031
// Use `htmr` to parse html content in the translation text
31-
// @ts-ignore
32-
return htmr(translatedText, { transform })
32+
return htmr(translatedText, {
33+
transform: { ...defaultTransform, ...transform },
34+
})
3335
}
3436

3537
export default Translation

0 commit comments

Comments
 (0)