-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add heading anchors to @keystone-next/website (#4838)
* add anchors and remove backticks from headings * add click to copy functionality to anchors * remove unnecessary imports Co-authored-by: Jed Watson <[email protected]>
- Loading branch information
1 parent
b224d47
commit f4163a0
Showing
13 changed files
with
289 additions
and
57 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
'@keystone-next/website': patch | ||
--- | ||
|
||
Remove backticks from headings in docs |
This file contains 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,5 @@ | ||
--- | ||
'@keystone-next/website': minor | ||
--- | ||
|
||
Added anchoring and copy to clipboard functionality to headings |
This file contains 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,69 @@ | ||
/** @jsx jsx */ | ||
import { jsx } from '@keystone-ui/core'; | ||
import copy from 'copy-to-clipboard'; | ||
import { LinkIcon } from '@primer/octicons-react'; | ||
import { useToasts } from '@keystone-ui/toast'; | ||
|
||
export const CopyToClipboard = ({ value }: { value: string }) => { | ||
const iconSize = 24; // arch-ui theme | ||
const gridSize = 8; // arch-ui theme | ||
const { addToast } = useToasts(); | ||
|
||
const onSuccess = () => { | ||
addToast({ title: 'Copied to clipboard', tone: 'positive' }); | ||
}; | ||
const onFailure = () => { | ||
addToast({ title: 'Faild to oopy to clipboard', tone: 'negative' }); | ||
}; | ||
const onClick = (event: React.SyntheticEvent) => { | ||
event.preventDefault(); | ||
if (typeof value !== 'string' || typeof window === 'undefined') return; | ||
const url = `${window.location.protocol}//${window.location.host}${window.location.pathname}`; | ||
const text = `${url}#${value}`; | ||
if (navigator) { | ||
// use the new navigator.clipboard API if it exists | ||
navigator.clipboard.writeText(text).then(onSuccess, onFailure); | ||
return; | ||
} else { | ||
// Fallback to a library that leverages document.execCommand | ||
// for browser versions that dont' support the navigator object. | ||
// As document.execCommand | ||
try { | ||
copy(text); | ||
} catch (e) { | ||
addToast({ title: 'Faild to oopy to clipboard', tone: 'negative' }); | ||
} | ||
|
||
return; | ||
} | ||
}; | ||
|
||
return ( | ||
<a | ||
href={`#`} | ||
css={{ | ||
alignItems: 'center', | ||
color: '#97A0AF', // colors.n40 | ||
display: 'flex', | ||
fontSize: '1rem', | ||
height: iconSize, | ||
justifyContent: 'center', | ||
marginTop: -iconSize / 2, | ||
opacity: 0, | ||
overflow: 'visible', | ||
paddingRight: gridSize / 2, | ||
position: 'absolute', | ||
top: '50%', | ||
transform: 'translateX(-100%)', | ||
width: iconSize, | ||
|
||
'&:hover': { | ||
color: '#2684FF', // colors.primary | ||
}, | ||
}} | ||
onClick={onClick} | ||
> | ||
<LinkIcon /> | ||
</a> | ||
); | ||
}; |
This file contains 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,125 @@ | ||
/** @jsx jsx */ | ||
import { useRef } from 'react'; | ||
import { jsx } from '@keystone-ui/core'; | ||
import slugify from '@sindresorhus/slugify'; | ||
import { CopyToClipboard } from './CopyToClipboard'; | ||
|
||
// offset header height for hash links | ||
const hashLinkOffset = { | ||
'::before': { | ||
content: '" "', | ||
height: 'calc(32px + 60px)', | ||
display: 'block', | ||
marginTop: -60, | ||
}, | ||
}; | ||
const getAnchor = (text: string) => { | ||
if (typeof text === 'string') { | ||
return slugify(text); | ||
} | ||
return ''; | ||
}; | ||
|
||
// emotions JSX pragma appends the correct css prop type | ||
// if the underlying component expects an optional className prop | ||
interface StringOnlyChildren { | ||
children: string; | ||
className?: string; | ||
} | ||
|
||
interface HeadingProps extends StringOnlyChildren { | ||
as: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6'; | ||
} | ||
const Heading = ({ as: Tag, children, ...props }: HeadingProps) => { | ||
const headingRef = useRef(null); | ||
const depth = parseInt(Tag.slice(1), 10); | ||
const hasCopy = depth > 1 && depth < 5; | ||
const id = getAnchor(children); | ||
return ( | ||
<Tag | ||
css={{ | ||
color: '#091E42', //N100 in arch | ||
fontWeight: 600, | ||
lineHeight: 1, | ||
marginBottom: '0.66em', | ||
marginTop: '1.66em', | ||
}} | ||
id={id} | ||
{...props} | ||
> | ||
<span | ||
ref={headingRef} | ||
css={{ | ||
display: 'block', | ||
position: 'relative', | ||
|
||
'&:hover a, &:focus-within a': { | ||
opacity: 1, | ||
}, | ||
}} | ||
> | ||
{hasCopy && <CopyToClipboard value={id} />} | ||
{children} | ||
</span> | ||
</Tag> | ||
); | ||
}; | ||
|
||
export const H1 = (props: StringOnlyChildren) => ( | ||
<Heading | ||
css={{ | ||
fontSize: '2.4rem', | ||
fontWeight: 500, | ||
letterSpacing: '-0.025em', | ||
marginTop: 0, | ||
}} | ||
{...props} | ||
as="h1" | ||
/> | ||
); | ||
export const H2 = (props: StringOnlyChildren) => ( | ||
<Heading | ||
{...props} | ||
css={{ | ||
fontSize: '1.8rem', | ||
fontWeight: 500, | ||
letterSpacing: '-0.025em', | ||
marginTop: 0, | ||
...hashLinkOffset, | ||
}} | ||
as="h2" | ||
/> | ||
); | ||
export const H3 = (props: StringOnlyChildren) => ( | ||
<Heading | ||
css={{ | ||
fontSize: '1.4rem', | ||
fontWeight: 500, | ||
letterSpacing: '-0.025em', | ||
marginTop: 0, | ||
...hashLinkOffset, | ||
}} | ||
{...props} | ||
as="h3" | ||
/> | ||
); | ||
|
||
export const H4 = (props: StringOnlyChildren) => ( | ||
<Heading | ||
css={{ | ||
fontSize: '1.2rem', | ||
...hashLinkOffset, | ||
}} | ||
{...props} | ||
as="h4" | ||
/> | ||
); | ||
|
||
export const H5 = (props: StringOnlyChildren) => ( | ||
<Heading css={{ fontSize: '1rem' }} as="h5" {...props} /> | ||
); | ||
export const H6 = (props: StringOnlyChildren) => ( | ||
<Heading css={{ fontSize: '0.9rem' }} as="h6" {...props} /> | ||
); | ||
|
||
export default Heading; |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
f4163a0
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.
Successfully deployed to the following URLs: