Skip to content

Commit c279519

Browse files
authored
Merge pull request #216 from ethereum/fix-heading-anchor-focus
Fix focus state for auto-linked headings
2 parents 59fb6b6 + 86cd70d commit c279519

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

src/components/MdComponents.tsx

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,40 +43,39 @@ import InfoBanner from "./InfoBanner"
4343
/**
4444
* Base HTML elements
4545
*/
46-
const headingPropsForAnchor = (id?: string): HeadingProps =>
47-
id
48-
? ({
49-
scrollMarginTop: 28,
50-
id,
51-
"data-group": true,
52-
position: "relative",
53-
} as HeadingProps)
54-
: {}
46+
const headingPropsForAnchor = (id?: string): HeadingProps => {
47+
if (!id) return {}
48+
return {
49+
scrollMarginTop: 28,
50+
id,
51+
"data-group": true,
52+
position: "relative",
53+
} as HeadingProps
54+
}
5555

5656
export const commonHeadingProps = (id?: string): HeadingProps => ({
5757
fontWeight: 700,
5858
lineHeight: 1.4,
5959
...headingPropsForAnchor(id),
6060
})
6161

62-
const IdAnchor: React.FC<{ id?: string }> = ({ id }) =>
63-
id ? (
62+
const IdAnchor: React.FC<{ id?: string }> = ({ id }) => {
63+
if (!id) return null
64+
return (
6465
<Link
65-
href={`#${id}`}
66+
href={"#" + id}
6667
position="absolute"
6768
insetInlineEnd="100%"
6869
aria-label={id.replaceAll("-", " ") + " permalink"}
70+
opacity={0}
71+
_groupHover={{ opacity: 1 }}
72+
_focus={{ opacity: 1 }}
73+
transition="opacity 0.1s ease-in-out"
6974
>
70-
<Icon
71-
as={CiLink}
72-
opacity={0}
73-
_groupHover={{ opacity: 1 }}
74-
transition="opacity 0.1s ease-in-out"
75-
fontSize="xl"
76-
me={1}
77-
/>
75+
<Icon as={CiLink} fontSize="xl" me="1" />
7876
</Link>
79-
) : null
77+
)
78+
}
8079

8180
export const Heading1 = ({ children, ...rest }: HeadingProps) => (
8281
<OldHeading as="h1" {...commonHeadingProps()} fontSize="2.5rem" {...rest}>

0 commit comments

Comments
 (0)