-
Notifications
You must be signed in to change notification settings - Fork 130
feat: Add copy to clipboard Heading #1999
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
Merged
birosrichard
merged 5 commits into
master
from
feat/add-copy-to-clipboard-functionality
Oct 13, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,85 @@ | ||
import { useThemeConfig } from '@docusaurus/theme-common'; | ||
import { translate } from '@docusaurus/Translate'; | ||
import useBrokenLinks from '@docusaurus/useBrokenLinks'; | ||
import clsx from 'clsx'; | ||
import React, { useEffect } from 'react'; | ||
|
||
import { LinkIcon } from '@apify/ui-icons'; | ||
import { useCopyToClipboard } from '@apify/ui-library'; | ||
|
||
import styles from './styles.module.css'; | ||
|
||
export default function Heading({ as: As, id, ...props }) { | ||
const brokenLinks = useBrokenLinks(); | ||
const { | ||
navbar: { hideOnScroll }, | ||
} = useThemeConfig(); | ||
|
||
const { isCopied, copyToClipboard } = useCopyToClipboard(); | ||
|
||
// Register the anchor ID so Docusaurus can scroll to it | ||
useEffect(() => { | ||
if (id) { | ||
brokenLinks.collectAnchor(id); | ||
|
||
// Handle scroll on page load if this heading matches the URL hash | ||
const hash = decodeURIComponent(window.location.hash.slice(1)); | ||
if (hash === id) { | ||
// Use setTimeout to ensure the page is fully rendered | ||
setTimeout(() => { | ||
const element = document.getElementById(id); | ||
if (element) { | ||
element.scrollIntoView({ behavior: 'smooth', block: 'start' }); | ||
} | ||
}, 100); | ||
} | ||
} | ||
}, [id, brokenLinks]); | ||
|
||
// H1 headings and headings without an id shouldn't have the copy to clipboard button | ||
if (As === 'h1' || !id) { | ||
return <As {...props} {...(id && { id })} />; | ||
} | ||
|
||
const handleCopy = async (e) => { | ||
e.preventDefault(); | ||
const url = new URL(window.location.href); | ||
url.hash = `#${id ?? ''}`; | ||
window.location.hash = `#${id ?? ''}`; | ||
await copyToClipboard(url.toString()); | ||
}; | ||
|
||
const anchorTitle = translate( | ||
{ | ||
id: 'theme.common.headingLinkTitle', | ||
message: 'Direct link to {heading}', | ||
description: 'Title for link to heading', | ||
}, | ||
{ | ||
heading: typeof props.children === 'string' ? props.children : id, | ||
}, | ||
); | ||
|
||
return ( | ||
<As | ||
{...props} | ||
className={clsx( | ||
'anchor', | ||
hideOnScroll | ||
? styles.anchorWithHideOnScrollNavbar | ||
: styles.anchorWithStickyNavbar, | ||
props.className, | ||
)} | ||
id={id}> | ||
{props.children} | ||
<a | ||
onClick={handleCopy} | ||
href={`#${id}`} | ||
className={clsx(styles.headingCopyIcon, isCopied && styles.copied)} | ||
aria-label={anchorTitle} | ||
> | ||
<LinkIcon size="16" /> | ||
</a> | ||
</As> | ||
); | ||
} |
This file contains hidden or 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,47 @@ | ||
.anchorWithStickyNavbar { | ||
scroll-margin-top: calc(var(--ifm-navbar-height) + 0.5rem); | ||
} | ||
|
||
.anchorWithHideOnScrollNavbar { | ||
scroll-margin-top: 0.5rem; | ||
} | ||
|
||
.headingCopyIcon { | ||
display: none; | ||
position: relative; | ||
left: .4rem; | ||
color: var(--ifm-color-emphasis-700); | ||
text-decoration: none; | ||
} | ||
|
||
.headingCopyIcon svg { | ||
stroke: var(--ifm-color-primary); | ||
max-height: 1em !important; | ||
} | ||
|
||
h2:hover .headingCopyIcon, | ||
h3:hover .headingCopyIcon, | ||
h4:hover .headingCopyIcon, | ||
h5:hover .headingCopyIcon, | ||
h6:hover .headingCopyIcon { | ||
display: inline-block; | ||
} | ||
|
||
.headingCopyIcon.copied { | ||
display: inline-block !important; | ||
} | ||
|
||
.headingCopyIcon.copied svg { | ||
display: none; | ||
} | ||
|
||
.headingCopyIcon.copied::after { | ||
content: 'Copied!'; | ||
font-size: 12px; | ||
color: var(--ifm-font-color-secondary); | ||
font-weight: 600; | ||
margin-left: 8px; | ||
vertical-align: middle; | ||
line-height: 1; | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Bug: Anchor Link Renders Incorrectly Without ID
The component renders an anchor link and copy button even when the
id
prop is undefined or null. This causes the anchor'shref
attribute to become"#undefined"
, while the copy-to-clipboard functionality handles a missingid
differently. This inconsistency leads to broken navigation and unexpected copied URLs.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.
Seems reasonable to add nullable check for
id