Skip to content

Commit 227be4c

Browse files
authored
fix(headings): copies heading link to clipboard (#4805)
* fix(headings): copies heading link to clipboard * fix(headings): add tooltip to hover of the copy icon button * fix(headings): prevent flash of original tooltip after copy * fix(headings): match click size for copy button
1 parent 46716c4 commit 227be4c

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

packages/documentation-framework/components/autoLinkHeader/autoLinkHeader.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,20 @@
77
transform: translate(calc(-100% - var(--pf-t--global--spacer--xs)), -50%);
88
opacity: 0;
99
position: absolute;
10+
display: flex;
1011
left: 0;
1112
top: 50%;
13+
background: none;
14+
border: none;
15+
padding: 0;
16+
tab-index: -1;
17+
}
18+
19+
.ws-heading-anchor::before {
20+
content: '';
21+
position: absolute;
22+
inset: 0;
23+
margin: calc(var(--pf-t--global--spacer--xs) * -1);
1224
}
1325

1426
.ws-heading-anchor-icon {

packages/documentation-framework/components/autoLinkHeader/autoLinkHeader.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import React from 'react';
2-
import { Flex, FlexItem, Content } from '@patternfly/react-core';
1+
import React, { useState } from 'react';
2+
import { Flex, FlexItem, Content, Tooltip } from '@patternfly/react-core';
33
import LinkIcon from '@patternfly/react-icons/dist/esm/icons/link-icon';
4-
import { Link } from '../link/link';
54
import { slugger } from '../../helpers/slugger';
65
import { css } from '@patternfly/react-styles';
76

@@ -13,6 +12,15 @@ export const AutoLinkHeader = ({
1312
className
1413
}) => {
1514
const slug = id || slugger(children);
15+
const [copied, setCopied] = useState(false);
16+
17+
const handleCopyUrl = (e) => {
18+
e.preventDefault();
19+
const url = `${window.location.origin}${window.location.pathname}#${slug}`;
20+
navigator.clipboard.writeText(url).then(() => {
21+
setCopied(true);
22+
});
23+
};
1624

1725
return (
1826
<Flex alignItems={{ default: 'alignItemsCenter'}} spaceItems={{ default: "spaceItemsSm" }}>
@@ -24,9 +32,21 @@ export const AutoLinkHeader = ({
2432
tabIndex={-1}
2533
isEditorial
2634
>
27-
<Link href={`#${slug}`} className="ws-heading-anchor" tabIndex="-1" aria-hidden>
28-
<LinkIcon className="ws-heading-anchor-icon" style={{ verticalAlign: 'middle' }} />
29-
</Link>
35+
<Tooltip
36+
content={copied ? "Link copied" : "Copy link to clipboard"}
37+
exitDelay={copied ? 1600 : 300}
38+
onTooltipHidden={() => setCopied(false)}
39+
>
40+
<button
41+
onClick={handleCopyUrl}
42+
className="ws-heading-anchor"
43+
tabIndex="-1"
44+
aria-hidden
45+
aria-label={`Copy link to ${children}`}
46+
>
47+
<LinkIcon className="ws-heading-anchor-icon" />
48+
</button>
49+
</Tooltip>
3050
{children}
3151
</Content>
3252
</FlexItem>

0 commit comments

Comments
 (0)