Reveal then copy recovery value in HTML, CSS and vanilla JS
A copy-paste Reveal then copy recovery value in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.
Live preview
Source (copy-ready)
<section class="dcl-copy-button-reveal" data-dcl-scheme="light">
<h2>Recovery sample</h2><div data-stack data-copy-row><p data-mask>•••• •••• ••••</p>
<code data-value id="dcl-copy-button-reveal-value" hidden>DEMO-W7Q4-ZP2K</code>
<div data-row><button type="button" data-open aria-expanded="false" aria-controls="dcl-copy-button-reveal-value">Show sample value</button>
<button type="button" class="dcl-copy-button-reveal-primary" data-copy="Sample value" disabled>Copy sample value</button></div></div><p class="dcl-copy-button-reveal-status" role="status" data-status></p>
<script>
(() => {
const root = document.currentScript.parentElement;
const instance = [...document.getElementsByClassName("dcl-copy-button-reveal")].indexOf(root);
if (instance > 0) {
for (const node of root.querySelectorAll("[id], [for], [aria-labelledby], [aria-controls], [aria-describedby], a[href], input[name]")) {
for (const key of ["id", "for", "aria-labelledby", "aria-controls", "aria-describedby", "name"]) {
if (node.hasAttribute(key)) node.setAttribute(key, node.getAttribute(key).split(" ").map(value => value + "--" + instance).join(" "));
}
const href = node.getAttribute("href");
if (href && href.startsWith("#")) node.setAttribute("href", href + "--" + instance);
}
}
const scheme = matchMedia("(prefers-color-scheme: dark)");
const applyScheme = () => { root.dataset.dclScheme = scheme.matches ? "dark" : "light"; };
scheme.addEventListener("change", applyScheme);
applyScheme();
const status = root.querySelector("[data-status]");
root.querySelectorAll("[data-copy]").forEach(button => {
button.addEventListener("click", async () => {
const value = button.closest("[data-copy-row]").querySelector("[data-value]");
const text = value.value === undefined ? value.textContent : value.value;
try {
await navigator.clipboard.writeText(text);
status.textContent = button.dataset.copy + " copied.";
} catch {
if (value.select) { value.focus(); value.select(); }
else {
const range = new Range();
range.selectNodeContents(value);
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
}
status.textContent = "Automatic copy unavailable. Copy the selected text using your keyboard or selection menu.";
}
});
});
const show = root.querySelector("[data-open]");
const value = root.querySelector("[data-value]");
show.addEventListener("click", () => {
value.hidden = !value.hidden;
root.querySelector("[data-mask]").hidden = !value.hidden;
root.querySelector("[data-copy]").disabled = value.hidden;
show.setAttribute("aria-expanded", String(!value.hidden));
show.textContent = value.hidden ? "Show sample value" : "Hide sample value";
status.textContent = "";
});
})();
</script>
</section>
<style>
.dcl-copy-button-reveal { box-sizing: border-box; max-inline-size: 42rem; padding: clamp(.75rem, 4vw, 1.5rem); border: 1px solid; border-radius: .75rem; font: 1rem/1.5 system-ui, sans-serif; overflow-wrap: anywhere; }
.dcl-copy-button-reveal *, .dcl-copy-button-reveal *::before, .dcl-copy-button-reveal *::after { box-sizing: border-box; }
.dcl-copy-button-reveal h2, .dcl-copy-button-reveal h3, .dcl-copy-button-reveal p, .dcl-copy-button-reveal ul, .dcl-copy-button-reveal dl, .dcl-copy-button-reveal figure { margin: 0; }
.dcl-copy-button-reveal h2 { font-size: 1.25rem; line-height: 1.25; }
.dcl-copy-button-reveal h3 { font-size: 1rem; }
.dcl-copy-button-reveal > * + * { margin-block-start: .75rem; }
.dcl-copy-button-reveal button, .dcl-copy-button-reveal summary, .dcl-copy-button-reveal input, .dcl-copy-button-reveal select, .dcl-copy-button-reveal textarea { font: inherit; }
.dcl-copy-button-reveal button, .dcl-copy-button-reveal summary { padding: .45rem .7rem; min-block-size: 2.75rem; border: 1px solid; border-radius: .375rem; cursor: pointer; text-align: start; }
.dcl-copy-button-reveal select, .dcl-copy-button-reveal input[type="text"], .dcl-copy-button-reveal textarea { max-inline-size: 100%; padding: .45rem; border: 1px solid; border-radius: .375rem; }
.dcl-copy-button-reveal button:active { transform: translateY(1px); }
.dcl-copy-button-reveal button[disabled], .dcl-copy-button-reveal button[aria-disabled="true"] { cursor: not-allowed; border-style: dashed; transform: none; }
.dcl-copy-button-reveal button[aria-pressed="true"] { border-style: double; border-width: 3px; }
.dcl-copy-button-reveal details > summary { inline-size: fit-content; }
.dcl-copy-button-reveal [data-panel], .dcl-copy-button-reveal dialog { border: 1px solid; border-radius: .625rem; padding: .8rem; }
.dcl-copy-button-reveal dialog { max-inline-size: min(calc(100% - 2rem), 28rem); max-block-size: calc(100% - 2rem); }
.dcl-copy-button-reveal dialog > * + * { margin-block-start: .75rem; }
.dcl-copy-button-reveal [data-row] { display: flex; flex-wrap: wrap; align-items: center; gap: .6rem; min-inline-size: 0; }
.dcl-copy-button-reveal [data-stack] { display: grid; gap: .6rem; min-inline-size: 0; }
.dcl-copy-button-reveal [data-list] { list-style: none; padding: 0; display: grid; gap: .55rem; }
.dcl-copy-button-reveal [data-wide] { flex: 1 1 11rem; min-inline-size: 0; }
.dcl-copy-button-reveal .dcl-copy-button-reveal-status { min-block-size: 1.5em; font-weight: 600; }
.dcl-copy-button-reveal .dcl-copy-button-reveal-glyph { display: inline-grid; place-items: center; font-size: 1.2em; line-height: 1; }
.dcl-copy-button-reveal [hidden] { display: none; }
.dcl-copy-button-reveal { color: #11292f; background-color: #ffffff; color-scheme: light; border-color: #697f84; }
.dcl-copy-button-reveal a { color: #005c62; }
.dcl-copy-button-reveal button, .dcl-copy-button-reveal summary, .dcl-copy-button-reveal input, .dcl-copy-button-reveal select, .dcl-copy-button-reveal textarea { color: #11292f; background-color: #e4edef; border-color: #697f84; }
.dcl-copy-button-reveal .dcl-copy-button-reveal-primary, .dcl-copy-button-reveal button[aria-pressed="true"] { color: #ffffff; background-color: #00535a; }
.dcl-copy-button-reveal button:hover, .dcl-copy-button-reveal summary:hover { color: #ffffff; background-color: #00535a; }
.dcl-copy-button-reveal button[disabled], .dcl-copy-button-reveal button[aria-disabled="true"] { color: #405a60; background-color: #ffffff; }
.dcl-copy-button-reveal [data-panel], .dcl-copy-button-reveal dialog { color: #11292f; background-color: #e4edef; border-color: #697f84; }
.dcl-copy-button-reveal :focus-visible { outline: 3px solid #005c62; outline-offset: 2px; }
.dcl-copy-button-reveal[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; color-scheme: dark; border-color: #839fa5; }
.dcl-copy-button-reveal[data-dcl-scheme="dark"] a { color: #8fe1da; }
.dcl-copy-button-reveal[data-dcl-scheme="dark"] button, .dcl-copy-button-reveal[data-dcl-scheme="dark"] summary, .dcl-copy-button-reveal[data-dcl-scheme="dark"] input, .dcl-copy-button-reveal[data-dcl-scheme="dark"] select, .dcl-copy-button-reveal[data-dcl-scheme="dark"] textarea { color: #eff8f9; background-color: #1c3c44; border-color: #839fa5; }
.dcl-copy-button-reveal[data-dcl-scheme="dark"] .dcl-copy-button-reveal-primary, .dcl-copy-button-reveal[data-dcl-scheme="dark"] button[aria-pressed="true"] { color: #11292f; background-color: #8fe1da; }
.dcl-copy-button-reveal[data-dcl-scheme="dark"] button:hover, .dcl-copy-button-reveal[data-dcl-scheme="dark"] summary:hover { color: #11292f; background-color: #8fe1da; }
.dcl-copy-button-reveal[data-dcl-scheme="dark"] button[disabled], .dcl-copy-button-reveal[data-dcl-scheme="dark"] button[aria-disabled="true"] { color: #b0c9ce; background-color: #142f36; }
.dcl-copy-button-reveal[data-dcl-scheme="dark"] [data-panel], .dcl-copy-button-reveal[data-dcl-scheme="dark"] dialog { color: #eff8f9; background-color: #1c3c44; border-color: #839fa5; }
.dcl-copy-button-reveal[data-dcl-scheme="dark"] :focus-visible { outline: 3px solid #8fe1da; outline-offset: 2px; }
</style>
When to use
Let someone reveal and copy a sensitive value only when they choose to expose it.
Avoid
Do not use a hard-coded example as secure credential storage.
Accessibility
Expose the reveal state, keep the value hidden from the accessibility tree until shown, and announce Copy success only after the write resolves. Light/dark body text: 15.19:1 and 13.07:1, respectively.
Was this useful? Send feedback.