Skip to content

Commit 13f7900

Browse files
committed
Added IdAnchor from MdComponents
1 parent cf2e80d commit 13f7900

File tree

1 file changed

+48
-38
lines changed
  • src/components/Glossary/GlossaryDefinition

1 file changed

+48
-38
lines changed

src/components/Glossary/GlossaryDefinition/index.tsx

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { Box, Flex, Link, Text } from "@chakra-ui/react"
2-
import { ComponentProps, useState } from "react"
1+
import { ComponentProps } from "react"
2+
import { CiLink } from "react-icons/ci"
3+
import { Box, type HeadingProps, Icon, Link, Text } from "@chakra-ui/react"
34

4-
import { DEFAULT_GLOSSARY_NS } from "@/lib/constants"
55
import InlineLink from "@/components/Link"
66
import OldHeading from "@/components/OldHeading"
77
import Translation from "@/components/Translation"
8-
import { VscLink } from "react-icons/vsc"
8+
9+
import { DEFAULT_GLOSSARY_NS } from "@/lib/constants"
910

1011
interface GlossaryDefinitionProps {
1112
term: string
@@ -24,46 +25,55 @@ const GlossaryDefinition = ({
2425
size = "md",
2526
options = { ns: DEFAULT_GLOSSARY_NS },
2627
}: GlossaryDefinitionProps) => {
27-
const [showLink, setShowLink] = useState(false)
28-
const headingStyles =
29-
size === "sm"
30-
? { fontSize: "md", mt: 0, mb: 2 }
31-
: { fontSize: { base: "xl", md: "2xl" } }
32-
3328
const textStyles = size === "sm" ? { mb: 0 } : {}
3429

35-
const linkStyles = {
36-
color: "inherit",
37-
textDecoration: "none",
30+
const IdAnchor = ({ id }: { id?: string }) => {
31+
if (!id) return null
32+
return (
33+
<Link
34+
href={"#" + id}
35+
position="absolute"
36+
insetInlineEnd="100%"
37+
aria-label={id.replaceAll("-", " ") + " permalink"}
38+
opacity={0}
39+
_groupHover={{ opacity: 1 }}
40+
_focus={{ opacity: 1 }}
41+
transition="opacity 0.1s ease-in-out"
42+
>
43+
<Icon as={CiLink} fontSize="xl" me="1" />
44+
</Link>
45+
)
3846
}
39-
40-
const linkIconStyles = {
41-
left: "-1.5em",
42-
top: "0.8em",
43-
fontSize: "0.5em",
44-
color: "var(--eth-colors-primary-base)",
45-
display: showLink ? "" : "none",
46-
cursor: "pointer",
47+
const headingPropsForAnchor = (id?: string): HeadingProps => {
48+
if (!id) return {}
49+
return {
50+
scrollMarginTop: 28,
51+
id,
52+
"data-group": true,
53+
position: "relative",
54+
} as HeadingProps
4755
}
56+
const commonHeadingProps = (id?: string): HeadingProps => ({
57+
fontWeight: 700,
58+
lineHeight: 1.4,
59+
...headingPropsForAnchor(id),
60+
})
61+
const Heading3 = ({ id, children, ...rest }: HeadingProps) => (
62+
<OldHeading as="h3" {...commonHeadingProps(id)} fontSize="2xl" {...rest}>
63+
<IdAnchor id={id} />
64+
{children}
65+
</OldHeading>
66+
)
4867

4968
return (
50-
<Box textAlign="start" onMouseEnter={() => setShowLink(true)} onMouseLeave={() => setShowLink(false)}>
51-
<OldHeading as="h3" lineHeight={1.4} id={term} {...headingStyles}>
52-
<Link href={`#${term}`} {...linkStyles} _hover={{ border: 'none', textDecoration: 'none' }}>
53-
<Flex pos="relative">
54-
<Box pos="absolute" _hover={{ opacity: '0.8' }} {...linkIconStyles}>
55-
<VscLink />
56-
</Box>
57-
<Box>
58-
<Translation
59-
id={term + "-term"}
60-
options={options}
61-
transform={components}
62-
/>
63-
</Box>
64-
</Flex>
65-
</Link>
66-
</OldHeading>
69+
<Box textAlign="start">
70+
<Heading3 id={term}>
71+
<Translation
72+
id={term + "-term"}
73+
options={options}
74+
transform={components}
75+
/>
76+
</Heading3>
6777

6878
<Text {...textStyles}>
6979
<Translation

0 commit comments

Comments
 (0)