From 3da727667b725e91402863f449228ab2d43325f5 Mon Sep 17 00:00:00 2001 From: DarkWiiPlayer Date: Tue, 11 Oct 2022 09:15:53 +0200 Subject: [PATCH] Refactor color palette code --- templates/palette.html | 104 +++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 66 deletions(-) diff --git a/templates/palette.html b/templates/palette.html index beb476d..9c0ce3e 100644 --- a/templates/palette.html +++ b/templates/palette.html @@ -15,15 +15,6 @@

Primary

- - - - - - - - -
@@ -32,17 +23,7 @@

Secondary

- - - - - - - - - - - +
You can copy any colour to the clipboard by just clicking on its box. @@ -51,17 +32,7 @@

Tertiary

- - - - - - - - - - - +
By making use of calc and var, most of the colour palette can be automated. @@ -69,17 +40,7 @@

Neutral

- - - - - - - - - - - +
By making use of calc and var, most of the colour palette can be automated. @@ -176,13 +137,6 @@ /* === Popup notification shenanigans === */ - @keyframes fade { - 0% { opacity: 0; transform: translate(-50%, -10%)} - 20% { opacity: 1;} - 50% { opacity: 1;} - 100% { opacity: 0; transform: translate(-50%, -150%)} - } - div.notification { top: 0em; border: 1px solid currentcolor; @@ -263,26 +217,17 @@ document.querySelector("body").appendChild(box); - box.style.setProperty('animation', 'fade 1s'); - box.style.setProperty('animation-fill-mode', 'forwards'); + box.animate([ + { offset: 0, opacity: 1, transform: "translate(-50%, -10%)"}, + { offset: .3, opacity: 1 }, + { offset: 1, opacity: 0, transform: "translate(-50%, -200%)"}, + ], { + duration: 1e3 + }).finished.then(() => box.remove()) + box.style.setProperty('color', color); - - box.addEventListener("animationend", ({box: target}) => box.parentElement.removeChild(box)) } - document.querySelectorAll('colour-palette button').forEach(button => { - button.addEventListener('click', event => { - const prop = name => getComputedStyle(button) - .getPropertyValue(name) - .replace(/^ *| *$/g, "") - const colour = `hsl(${prop("--hue")}, ${prop("--saturation")}, ${prop("--lightness")})` - navigator.clipboard.writeText(colour).then(() => { - const box = button.getBoundingClientRect() - showMessage(`Copied to Clipboard:
${colour}`, colour, box.x+box.width/2, box.y+box.height/2); - }); - }); - }); - const register = (type, name, initial=undefined) => CSS.registerProperty({ name: `--${name}`, syntax: `<${type}>`, @@ -295,6 +240,7 @@ register("percentage", "lightness", "50%") const backgroundColourButton = document.getElementById("background-color") + backgroundColourButton.addEventListener("input", event => { const rgb = backgroundColourButton.value document.body.style.background = rgb @@ -317,6 +263,32 @@ .map(colour => colour.match(/\d+/g)) .map(match => `${match[0]} ${match[1]} ${match[2]}`) } + + connectedCallback() { + this.innerHTML = ` + + + + + + + + + + ` + this.querySelectorAll('button').forEach(button => { + button.addEventListener('click', event => { + const prop = name => getComputedStyle(button) + .getPropertyValue(name) + .replace(/^ *| *$/g, "") + const colour = `hsl(${prop("--hue")}, ${prop("--saturation")}, ${prop("--lightness")})` + navigator.clipboard.writeText(colour).then(() => { + const box = button.getBoundingClientRect() + showMessage(`Copied to Clipboard:
${colour}`, colour, box.x+box.width/2, box.y+box.height/2); + }); + }); + }); + } })