-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
[docs-infra] Use icons instead of words for the code copy button #37664
Conversation
But swapping these strings for icons doesn't work as it seems it only welcomes strings. How to solve this issue? 😬 |
The one that cause problems to you are the codes coming from the markdown tokens
It's not react elements, but HTML rendered from the markdown parser. Here is the line
If you want to modify the button, it's here you should do it |
@alexfauquette Thanks for pointing this file out! Though it seems that importing the icon on this file doesn't work as usual? Also, where would I go to change the "Copied" string, too? The |
Effectively you will not be able to put some React components in it. The solution could be to put the HTML content of your icons in the string. And instead of modifying return `<div class="MuiCode-root"><pre><code class="language-${escape(lang, true)}">${
escaped ? code : escape(code, true)
}</code></pre>
<button data-ga-event-category="code" data-ga-event-action="copy-click" aria-label="Copy the code"0
class="MuiCode-copy">
- Copy
+ <svg class="MuiCode-copy-icon"><path ... /></svg>
+ <svg class="MuiCode-copied-icon"><path ... /></svg>
<span class="MuiCode-copyKeypress">
<span>
(or</span> $keyC<span>)</span>
</span>
</button></div>\n`; @danilo-leal After some trial and error here is a solution. I do not manage to commit on top of your branch, so I just copy past the diff diff --git a/docs/src/modules/components/MarkdownElement.js b/docs/src/modules/components/MarkdownElement.js
index 8465638e81..6468446a19 100644
--- a/docs/src/modules/components/MarkdownElement.js
+++ b/docs/src/modules/components/MarkdownElement.js
@@ -400,6 +400,15 @@ const Root = styled('div')(
borderColor: lightTheme.palette.primaryDark[500],
backgroundColor: lightTheme.palette.primaryDark[600],
color: lightTheme.palette.primaryDark[50],
+ '& svg': {
+ userSelect: 'none',
+ width: '1em',
+ height: '1em',
+ display: 'inline-block',
+ fill: 'currentcolor',
+ flexShrink: 0,
+ fontSize: '18px',
+ },
'&:hover, &:focus': {
opacity: 1,
color: '#fff',
@@ -417,11 +426,20 @@ const Root = styled('div')(
'& .MuiCode-copyKeypress': {
display: 'none',
},
+ '& .MuiCode-copied-icon': {
+ display: 'none',
+ },
'&[data-copied]': {
// style of the button when it is in copied state.
borderColor: lightTheme.palette.primary[700],
color: '#fff',
backgroundColor: lightTheme.palette.primaryDark[600],
+ '& .MuiCode-copy-icon': {
+ display: 'none',
+ },
+ '& .MuiCode-copied-icon': {
+ display: 'block',
+ },
},
'&:focus-visible': {
outline: '2px solid', diff --git a/packages/markdown/parseMarkdown.js b/packages/markdown/parseMarkdown.js
index bca606f483..439d2ec739 100644
--- a/packages/markdown/parseMarkdown.js
+++ b/packages/markdown/parseMarkdown.js
@@ -343,7 +343,8 @@ function createRender(context) {
return `<div class="MuiCode-root"><pre><code class="language-${escape(lang, true)}">${
escaped ? code : escape(code, true)
- }</code></pre><button data-ga-event-category="code" data-ga-event-action="copy-click" aria-label="Copy the code" class="MuiCode-copy">Copy <span class="MuiCode-copyKeypress"><span>(or</span> $keyC<span>)</span></span></button></div>\n`;
+ }</code></pre><button data-ga-event-category="code" data-ga-event-action="copy-click" aria-label="Copy the code" class="MuiCode-copy">
+ <svg focusable="false" aria-hidden="true" viewBox="0 0 24 24" data-testid="ContentCopyRoundedIcon"><use class="MuiCode-copy-icon" xlink:href="#copy-icon" /><use class="MuiCode-copied-icon" xlink:href="#copied-icon" /></svg> <span class="MuiCode-copyKeypress"><span>(or</span> $keyC<span>)</span></span></button></div>\n`;
};
const markedOptions = {
@@ -550,8 +551,18 @@ ${headers.hooks
<symbol id="comment-link-icon" viewBox="0 0 24 24">
<path d="M20 2H4c-1.1 0-1.99.9-1.99 2L2 22l4-4h14c1.1 0 2-.9 2-2V4c0-1.1-.9-2-2-2zM6 14v-2.47l6.88-6.88c.2-.2.51-.2.71 0l1.77 1.77c.2.2.2.51 0 .71L8.47 14H6zm12 0h-7.5l2-2H18v2z" />
</symbol>
+</svg>`);
+ rendered.unshift(`<svg style="display: none;" xmlns="http://www.w3.org/2000/svg">
+<symbol id="copy-icon" viewBox="0 0 24 24">
+ <path d="M15 20H5V7c0-.55-.45-1-1-1s-1 .45-1 1v13c0 1.1.9 2 2 2h10c.55 0 1-.45 1-1s-.45-1-1-1zm5-4V4c0-1.1-.9-2-2-2H9c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h9c1.1 0 2-.9 2-2zm-2 0H9V4h9v12z" />
+</symbol>
</svg>`);
+ rendered.unshift(`<svg style="display: none;" xmlns="http://www.w3.org/2000/svg">
+<symbol id="copied-icon" viewBox="0 0 24 24">
+ <path d="M20 2H8c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V4c0-1.1-.9-2-2-2zm-8.24 11.28L9.69 11.2c-.38-.39-.38-1.01 0-1.4.39-.39 1.02-.39 1.41 0l1.36 1.37 4.42-4.46c.39-.39 1.02-.39 1.41 0 .38.39.38 1.01 0 1.4l-5.13 5.17c-.37.4-1.01.4-1.4 0zM3 6c-.55 0-1 .45-1 1v13c0 1.1.9 2 2 2h13c.55 0 1-.45 1-1s-.45-1-1-1H5c-.55 0-1-.45-1-1V7c0-.55-.45-1-1-1z" />
+</symbol>
+</svg>`);
docs[userLanguage] = {
description,
location,
|
@alexfauquette Ah, nice! Really appreciate the detailed instructions & attempts at solving it! Just pushed exactly what you proposed but it's not quite there yet... could you take a look when you have time? I think the |
Netlify deploy previewhttps://deploy-preview-37664--material-ui.netlify.app/ Bundle size report |
@alexfauquette I think we're looking good now aside from this on command code blocks: |
@danilo-leal const btn = elm.querySelector('.MuiCode-copy') as HTMLButtonElement | null;
if (btn) {
const keyNode = btn.childNodes[1]?.childNodes[1];
if (!keyNode) {
// skip the logic if the btn is not generated from the markdown.
return;
}
keyNode.textContent = keyNode?.textContent?.replace('$key', key) || null;
btn.addEventListener('click', handleClick);
listeners.push(() => btn.removeEventListener('click', handleClick));
}
}); Should be fixed |
@alexfauquette Sweet! I think we're good to go ⎯ really appreciate all of your help here and the thorough info explaining to me what was wrong, I really learn from it! I'm only missing your approval now if we're done 😬 There's just this |
It's because - theme.transitions.create('background', 'borderColor', 'display', options)
+ theme.transitions.create(['background', 'borderColor', 'display'], options) @siriwatknp could you have a look at the new error message? I added it because before the message was This array comes from the 10 letters of the string |
@alexfauquette is it good to merge or do we need to wait for Jun's perspective? |
I would prefer to get Jun's perspective before merging |
@siriwatknp, could you please take a look? I'd like to include it in the release if possible. |
@michaldudak If you want, it's about the following diff on the styling helpers: packages/mui-material/src/styles/createTransitions.js |
@alexfauquette In these last few commits of mine, I made the button always visible, regardless if the code block is hovered or not... but I thought I had fixed its moving positioning 😅 Would you mind taking a look? I'm a bit unsure why that is happening, given the border added on hover is actually using Screen.Recording.2023-07-04.at.11.36.12.mov |
When you do the Screencast.from.04-07-2023.16.47.17.mp4 |
@alexfauquette oh, thanks for the tip! It's weird, though, because when running locally, that wasn't happening... only on the deploy preview. Anyway, just pushed the fix removing the display change on hover, hopefully, it solves this out! Are still waiting for @siriwatknp's perspective on that question you had before merging it? 😬 |
@alexfauquette Uhm, the problem seems to persist even after the above fix 😅 Not really sure why, given the display is now the same. I'm supposing it can have something to do with the added space the And for some reason, again, don't see this behavior locally, just on the deploy preview. 🤔 Screen.Recording.2023-07-05.at.09.20.05.mov |
Should be good now. The button had some incoherent instruction: |
@alexfauquette Awesome, thank you again for helping me out 🙏 What we're missing to move forward? 😬 |
What the title says 😅 Thought we could swap that out for icons to make less use of space in code blocks. What triggered me to do that was the code snippet within Joy's intro demos, which could use more breathing room.