Comments (4)
The examples are, confusingly, not React. It's a blob of HTML.
What you need to do is write some pure JavaScript code, that injects a <button>
or something into each <pre>
tag. Something like this:
React.useEffect(() => {
if (!error) {
[...document.querySelectorAll('#main pre')].forEach((element) => {
const button = document.createElement("button");
button.onclick = () => {
copyToClipboard(element.textContent);
button.classList.add('copied');
};
});
}
}, [doc, error]);
from yari.
Not sure how this'd work if we server-side render the code examples.
from yari.
@peterbe I'd like to give this a shot. I was around when you implemented syntaxHighlight
for highlighting <pre>
tags so I was tinkering in there. When I log the output of line 80, we get the text that should copied once a clipboard icon is added and clicked.
Not sure how this'd work if we server-side render the code examples.
Im not entirely sure how this will work either since you mentioned the code examples are rendered server side. But here are some things I was thinking about: (ps i'm still a bit of a React noob but I'm trying to get more practice)
I remember that the page is rendered from state with React (or something close to this), so I when I refer to state
in the examples below that's what I am trying to reference. If I understand correctly, we just need to supply the initial state with the code snippet content from the pre tags eg in cheerio$pre.text()
, then once the code for each example is available in state, we can use a hover event or focus to make the clipboard icon appear and then finally a click event to copy that code examples content to keyboard.
- Add an SVG of a clipboard icon to the code examples (ie
<pre class="brush:*">
) that get highlighted withsyntaxHighlight
. Icon could be placed in top right corner (or whichever corner) and downloaded from a free svg icon site like flaticon or somewhere in Yari if it already exists. Maybe using aonClick
function to programmatically write data to the clipboard with the help ofnavigator
like:
import React from 'react';
import ReactDOM from 'react-dom';
// Where clipboard is just the icon that will be added to code examples
class Clipboard extends React.Component {
state = {
value: "",
copied: false
};
...
render() {
return (
// use setState to update state with the $pre.text() values)
// window.clipboardData.setData(this.state.value, "copied!") only for IE 11 (security risks) - https://stackoverflow.com/questions/20509061/window-clipboarddata-getdatatext-doesnt-work-in-chrome/49830762
<div onClick={() => { navigator.clipboard.writeText(this.state.value)}}>
// or a background image with the svg instead of the element in markup
<svg viewBox="" ... />
</div>
)
}
}
- Use external npm package
react-copy-to-clipboard
import React from 'react';
import ReactDOM from 'react-dom';
import {CopyToClipboard} from 'react-copy-to-clipboard';
class App extends React.Component {
state = {
value: '',
copied: false,
};
render() {
return (
<div>
<CopyToClipboard text={this.state.value}
onCopy={() => this.setState({copied: true})}>
<button>Copy to clipboard with button</button>
</CopyToClipboard>
</div>
)
}
}
I'm going to tinker with things in Yari to see if I can get close to a solution. Let me know your thoughts @peterbe, or maybe a good place for me to start looking at the areas that server side render code examples.
from yari.
Ah ok, that makes more sense. I will go down that path, thanks for the help!
from yari.
Related Issues (20)
- MDN Plus menu item wraps in Brave HOT 4
- Rename Markdown links when an internal page is moved using `yarn content move`
- PR test in mdn/translated-content is slower than before HOT 6
- Changing focus from the search box causes top result to not work HOT 2
- Do not raise "Anchor not lowercase" flaw when the anchor is a fragment directive
- chore(short-survey): MDN Web Docs content discovery
- docs/web pages are broken when cookies are disabled HOT 2
- Missing space in the text in BCD table HOT 3
- Sidebar links are duplicated when you are navigating inside the "Common questions" section HOT 5
- The page is not working
- Entire korean learn pages are messed up HOT 3
- Remove Markdown links in content when a page is deleted using command `yarn content delete` HOT 1
- BCD not rendered for `html.global_attributes.virtualkeyboardpolicy` HOT 3
- Error setting up repo on Windows
- [Regression?] Generate clickable tags for description terms HOT 6
- No way (flaws or build log) to detect errors in {{CSSInfo}} HOT 2
- survey: web components survey
- HTML Input type examples don't match the type/description HOT 6
- Left Navigation Bar not Works Properly HOT 12
- UI bug HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from yari.