mirror of
https://github.com/roc-lang/roc.git
synced 2025-07-24 06:55:15 +00:00
fix mobile support for button container
This commit is contained in:
parent
128122d28a
commit
4756781f25
4 changed files with 92 additions and 55 deletions
|
@ -65,45 +65,60 @@
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const isTouchSupported = () => {
|
||||||
|
try{ document.createEvent("TouchEvent"); return true; }
|
||||||
|
catch(e){ return false; }
|
||||||
|
}
|
||||||
|
|
||||||
// Select all <samp> elements that are children of <pre> elements
|
// Select all <samp> elements that are children of <pre> elements
|
||||||
const codeBlocks = document.querySelectorAll("pre > samp");
|
const codeBlocks = document.querySelectorAll("pre > samp");
|
||||||
|
|
||||||
// Iterate over each code block
|
// Iterate over each code block
|
||||||
codeBlocks.forEach((codeBlock) => {
|
codeBlocks.forEach((codeBlock) => {
|
||||||
// Create a "Copy" button
|
// Create a "Copy" button
|
||||||
const copyButton = document.createElement("button");
|
const copyButton = document.createElement("button");
|
||||||
copyButton.classList.add("copy-button");
|
copyButton.classList.add("copy-button");
|
||||||
copyButton.textContent = "Copy";
|
copyButton.textContent = "Copy";
|
||||||
|
|
||||||
// Add event listener to copy button
|
// Add event listener to copy button
|
||||||
copyButton.addEventListener("click", () => {
|
copyButton.addEventListener("click", () => {
|
||||||
const codeText = codeBlock.innerText;
|
const codeText = codeBlock.innerText;
|
||||||
navigator.clipboard.writeText(codeText);
|
navigator.clipboard.writeText(codeText);
|
||||||
copyButton.textContent = "Copied!";
|
copyButton.textContent = "Copied!";
|
||||||
copyButton.classList.add("copy-button-copied");
|
copyButton.classList.add("copy-button-copied");
|
||||||
copyButton.addEventListener("mouseleave", () => {
|
copyButton.addEventListener("mouseleave", () => {
|
||||||
copyButton.textContent = "Copy";
|
copyButton.textContent = "Copy";
|
||||||
copyButton.classList.remove('copy-button-copied');
|
copyButton.classList.remove('copy-button-copied');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
// Create a container for the copy button and append it to the document
|
||||||
// Create a container for the copy button and append it to the document
|
const buttonContainer = document.createElement("div");
|
||||||
const buttonContainer = document.createElement("div");
|
buttonContainer.classList.add("button-container");
|
||||||
buttonContainer.classList.add("button-container");
|
buttonContainer.appendChild(copyButton);
|
||||||
buttonContainer.appendChild(copyButton);
|
codeBlock.parentNode.insertBefore(buttonContainer, codeBlock);
|
||||||
codeBlock.parentNode.insertBefore(buttonContainer, codeBlock);
|
|
||||||
|
// Hide the button container by default
|
||||||
// Hide the button container by default
|
|
||||||
buttonContainer.style.display = "none";
|
|
||||||
|
|
||||||
// Show the button container on hover
|
|
||||||
codeBlock.parentNode.addEventListener("mouseenter", () => {
|
|
||||||
buttonContainer.style.display = "block";
|
|
||||||
});
|
|
||||||
|
|
||||||
codeBlock.parentNode.addEventListener("mouseleave", () => {
|
|
||||||
buttonContainer.style.display = "none";
|
buttonContainer.style.display = "none";
|
||||||
|
|
||||||
|
if (isTouchSupported()) {
|
||||||
|
// Show the button container on click for touch support (e.g. mobile)
|
||||||
|
document.addEventListener("click", (event) => {
|
||||||
|
if (event.target.closest("pre > samp") !== codeBlock) {
|
||||||
|
buttonContainer.style.display = "none";
|
||||||
|
} else {
|
||||||
|
buttonContainer.style.display = "block";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// Show the button container on hover for non-touch support (e.g. desktop)
|
||||||
|
codeBlock.parentNode.addEventListener("mouseenter", () => {
|
||||||
|
buttonContainer.style.display = "block";
|
||||||
|
});
|
||||||
|
|
||||||
|
codeBlock.parentNode.addEventListener("mouseleave", () => {
|
||||||
|
buttonContainer.style.display = "none";
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
|
@ -389,7 +389,14 @@ pre {
|
||||||
padding: 8px 16px;
|
padding: 8px 16px;
|
||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
background-color: var(--code-bg);
|
background-color: var(--code-bg);
|
||||||
|
overflow-x: hidden;
|
||||||
|
position: relative;
|
||||||
|
word-wrap: normal;
|
||||||
|
}
|
||||||
|
|
||||||
|
pre>samp {
|
||||||
overflow-x: auto;
|
overflow-x: auto;
|
||||||
|
display: block;
|
||||||
}
|
}
|
||||||
|
|
||||||
.hidden {
|
.hidden {
|
||||||
|
@ -657,10 +664,6 @@ code .dim {
|
||||||
opacity: 0.55;
|
opacity: 0.55;
|
||||||
}
|
}
|
||||||
|
|
||||||
pre {
|
|
||||||
position: relative;
|
|
||||||
}
|
|
||||||
|
|
||||||
.button-container {
|
.button-container {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
top: 0;
|
top: 0;
|
||||||
|
@ -668,7 +671,7 @@ pre {
|
||||||
}
|
}
|
||||||
|
|
||||||
.copy-button {
|
.copy-button {
|
||||||
background: none;
|
background: var(--code-bg);
|
||||||
border: 1px solid var(--magenta);
|
border: 1px solid var(--magenta);
|
||||||
color: var(--magenta);
|
color: var(--magenta);
|
||||||
border-radius: 3px;
|
border-radius: 3px;
|
||||||
|
|
|
@ -145,17 +145,24 @@ a:visited code {
|
||||||
}
|
}
|
||||||
|
|
||||||
pre {
|
pre {
|
||||||
|
position: relative;
|
||||||
margin-bottom: 16px;
|
margin-bottom: 16px;
|
||||||
padding: 8px 16px;
|
padding: 8px 16px;
|
||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
border-radius: 8px;
|
border-radius: 8px;
|
||||||
background-color: var(--code-bg);
|
background-color: var(--code-bg);
|
||||||
overflow-x: auto;
|
overflow-x: hidden;
|
||||||
|
word-wrap: normal;
|
||||||
font-size: 1.2rem;
|
font-size: 1.2rem;
|
||||||
line-height: 1.76em;
|
line-height: 1.76em;
|
||||||
white-space: pre;
|
white-space: pre;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pre>samp {
|
||||||
|
overflow-x: auto;
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
|
||||||
.repl-prompt:before {
|
.repl-prompt:before {
|
||||||
/* Add this using CSS so it isn't selectable, which would be annoying when trying to copy/paste! */
|
/* Add this using CSS so it isn't selectable, which would be annoying when trying to copy/paste! */
|
||||||
color: var(--repl-prompt);
|
color: var(--repl-prompt);
|
||||||
|
@ -675,10 +682,6 @@ code .dim {
|
||||||
opacity: 0.55;
|
opacity: 0.55;
|
||||||
}
|
}
|
||||||
|
|
||||||
pre {
|
|
||||||
position: relative;
|
|
||||||
}
|
|
||||||
|
|
||||||
.button-container {
|
.button-container {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
top: 0;
|
top: 0;
|
||||||
|
@ -686,7 +689,7 @@ pre {
|
||||||
}
|
}
|
||||||
|
|
||||||
.copy-button {
|
.copy-button {
|
||||||
background: none;
|
background: var(--code-bg);
|
||||||
border: 1px solid var(--magenta);
|
border: 1px solid var(--magenta);
|
||||||
color: var(--magenta);
|
color: var(--magenta);
|
||||||
border-radius: 3px;
|
border-radius: 3px;
|
||||||
|
|
|
@ -14,6 +14,11 @@ document.addEventListener("keydown", (event) => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const isTouchSupported = () => {
|
||||||
|
try{ document.createEvent("TouchEvent"); return true; }
|
||||||
|
catch(e){ return false; }
|
||||||
|
}
|
||||||
|
|
||||||
// Select all <samp> elements that are children of <pre> elements
|
// Select all <samp> elements that are children of <pre> elements
|
||||||
const codeBlocks = document.querySelectorAll("pre > samp");
|
const codeBlocks = document.querySelectorAll("pre > samp");
|
||||||
|
|
||||||
|
@ -45,12 +50,23 @@ codeBlocks.forEach((codeBlock) => {
|
||||||
// Hide the button container by default
|
// Hide the button container by default
|
||||||
buttonContainer.style.display = "none";
|
buttonContainer.style.display = "none";
|
||||||
|
|
||||||
// Show the button container on hover
|
if (isTouchSupported()) {
|
||||||
codeBlock.parentNode.addEventListener("mouseenter", () => {
|
// Show the button container on click for touch support (e.g. mobile)
|
||||||
buttonContainer.style.display = "block";
|
document.addEventListener("click", (event) => {
|
||||||
});
|
if (event.target.closest("pre > samp") !== codeBlock) {
|
||||||
|
buttonContainer.style.display = "none";
|
||||||
|
} else {
|
||||||
|
buttonContainer.style.display = "block";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// Show the button container on hover for non-touch support (e.g. desktop)
|
||||||
|
codeBlock.parentNode.addEventListener("mouseenter", () => {
|
||||||
|
buttonContainer.style.display = "block";
|
||||||
|
});
|
||||||
|
|
||||||
codeBlock.parentNode.addEventListener("mouseleave", () => {
|
codeBlock.parentNode.addEventListener("mouseleave", () => {
|
||||||
buttonContainer.style.display = "none";
|
buttonContainer.style.display = "none";
|
||||||
});
|
});
|
||||||
|
}
|
||||||
});
|
});
|
Loading…
Add table
Add a link
Reference in a new issue