-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
feat(www): add copy button to code snippets #15834
Merged
+639
−33
Merged
Changes from 6 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
d9905de
feat(www): add standalone pre component with copy functionality
DSchau ed4801a
chore: make it vaguely appealing
DSchau 246eb4f
chore: make even prettier
DSchau d5068a8
chore: keep iterating
DSchau b8bb626
chore: make it work nicely again
DSchau b63020c
chore: get syntax highlighting working; todo line highlighting
DSchau bcaf18a
feat: improve accessibility (not done!)
DSchau 16d3cbe
feat: add screenreader text
DSchau 5755947
chore: swap to name
DSchau d4f06ee
chore: add missing status
DSchau a6fe77e
feat: get line highlighting mostly working
DSchau 2ea5c81
feat: get hide directive working too
DSchau 253eef5
feat: iron out highlights and use correct aria-role
DSchau a8eff91
chore: iron-out hide
DSchau 083c71f
feat: add support for braces in language
DSchau 1919a03
style: implement flo's new design
DSchau d37f914
test: get tests passing
DSchau fc095b7
Merge branch 'master' of github.com:gatsbyjs/gatsby into www/copy
DSchau e05e395
chore: restore monospace stack
DSchau 7db21a8
feat: get {} working again
DSchau 82ec189
chore: make sure to trim
DSchau e1feaea
chore: add some tests
DSchau e08c990
test: finish tests
DSchau d86d8a4
test: more of 'em of course
DSchau 45d92a4
chore: more fixes
DSchau 7a89649
test: more tests; and calling this done!
DSchau 1742d2b
chore: tiny fix
DSchau 1be3da0
chore: add back missing autolink headers
DSchau 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import React, { useState } from "react" | ||
import PropTypes from "prop-types" | ||
|
||
import { | ||
space, | ||
fonts, | ||
fontSizes, | ||
colors, | ||
radii, | ||
lineHeights, | ||
letterSpacings, | ||
} from "../utils/presets" | ||
|
||
const copyToClipboard = content => { | ||
const el = document.createElement(`textarea`) | ||
el.value = content | ||
el.setAttribute(`readonly`, ``) | ||
el.style.position = `absolute` | ||
el.style.left = `-9999px` | ||
document.body.appendChild(el) | ||
el.select() | ||
document.execCommand(`copy`) | ||
document.body.removeChild(el) | ||
} | ||
|
||
const delay = duration => new Promise(resolve => setTimeout(resolve, duration)) | ||
|
||
function Copy({ content, duration = 2500, trim = false }) { | ||
const [text, setText] = useState(`Copy`) | ||
|
||
return ( | ||
<button | ||
aria-label={ | ||
text === `Copy` | ||
? `Copy text to clipboard` | ||
: `Text has been copied to clipboard` | ||
} | ||
css={{ | ||
background: colors.text.header, | ||
borderRadius: `0 0 ${radii[2]}px ${radii[2]}px`, | ||
color: `#ddd`, | ||
fontSize: fontSizes[0], | ||
fontFamily: fonts.monospace, | ||
letterSpacing: letterSpacings.tracked, | ||
lineHeight: lineHeights.solid, | ||
padding: `${space[1]} ${space[2]}`, | ||
position: `absolute`, | ||
right: space[6], | ||
textAlign: `right`, | ||
textTransform: `uppercase`, | ||
top: `0`, | ||
}} | ||
onClick={async () => { | ||
copyToClipboard(trim ? content.trim() : content) | ||
|
||
setText(`Copied`) | ||
|
||
await delay(duration) | ||
|
||
setText(`Copy`) | ||
}} | ||
> | ||
{text} | ||
</button> | ||
) | ||
} | ||
|
||
Copy.propTypes = { | ||
content: PropTypes.string.isRequired, | ||
duration: PropTypes.number, | ||
trim: PropTypes.bool, | ||
} | ||
|
||
Copy.defaultProps = { | ||
duration: 2500, | ||
} | ||
|
||
export default Copy |
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,51 @@ | ||
import React from "react" | ||
import Highlight, { defaultProps } from "prism-react-renderer" | ||
|
||
import Copy from "./copy" | ||
import { space } from "../utils/presets" | ||
|
||
const getParams = (name = ``) => { | ||
const [lang, params = ``] = name.split(`:`) | ||
return [lang.split(`language-`).pop()].concat( | ||
params.split(`&`).reduce((merged, param) => { | ||
const [key, value] = param.split(`=`) | ||
merged[key] = value | ||
return merged | ||
}, {}) | ||
) | ||
} | ||
|
||
export default ({ children }) => { | ||
const [language, { title = `` }] = getParams(children.props.className) | ||
const content = children.props.children | ||
return ( | ||
<Highlight | ||
{...defaultProps} | ||
code={content} | ||
language={language} | ||
theme={undefined} | ||
> | ||
{({ tokens, getLineProps, getTokenProps }) => ( | ||
<div className="gatsby-highlight"> | ||
<div className="gatsby-highlight-header"> | ||
{title && ( | ||
<div className="gatsby-code-title" css={{ paddingTop: space[4] }}> | ||
{title} | ||
</div> | ||
)} | ||
<Copy content={content} /> | ||
</div> | ||
<pre className={`language-${language}`}> | ||
{tokens.map((line, i) => ( | ||
<div key={i} {...getLineProps({ line, key: i })}> | ||
{line.map((token, key) => ( | ||
<span key={key} {...getTokenProps({ token, key })} /> | ||
))} | ||
</div> | ||
))} | ||
</pre> | ||
</div> | ||
)} | ||
</Highlight> | ||
) | ||
} |
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
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.
Note: we’ll probably want to pass our custom Prism theme as a theme here — will do just that!