document theme builder
This commit is contained in:
parent
667b210708
commit
693e5f23c7
|
@ -1,7 +1,8 @@
|
||||||
///////////////////////////////////////////////
|
///////////////////////////////////////////////
|
||||||
// Tweak Example Settings
|
// Grayscale Theme Builder
|
||||||
///////////////////////////////////////////////
|
///////////////////////////////////////////////
|
||||||
|
|
||||||
|
// query helper
|
||||||
const $ = document.querySelector.bind(document);
|
const $ = document.querySelector.bind(document);
|
||||||
|
|
||||||
function HEXToRGB(hex) {
|
function HEXToRGB(hex) {
|
||||||
|
@ -62,12 +63,15 @@ function RGBToHSL(r,g,b) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function update() {
|
// refresh current page theme
|
||||||
|
function refresh() {
|
||||||
const form = $('#settings');
|
const form = $('#settings');
|
||||||
|
|
||||||
|
// convert hex-to-rbg-to-hsl
|
||||||
const rgb = HEXToRGB(form.elements['theme'].value);
|
const rgb = HEXToRGB(form.elements['theme'].value);
|
||||||
const hsl = RGBToHSL(rgb.r, rgb.g, rgb.b);
|
const hsl = RGBToHSL(rgb.r, rgb.g, rgb.b);
|
||||||
|
|
||||||
|
// build theme string
|
||||||
const theme = String(`
|
const theme = String(`
|
||||||
:root {
|
:root {
|
||||||
--Hsl: ${hsl.h};
|
--Hsl: ${hsl.h};
|
||||||
|
@ -80,11 +84,13 @@ function update() {
|
||||||
--radius: ${form.elements['radius'].value}rem;
|
--radius: ${form.elements['radius'].value}rem;
|
||||||
}`).replace(/\s/g, '');
|
}`).replace(/\s/g, '');
|
||||||
|
|
||||||
|
// override :root to update page theme
|
||||||
$('#dynamic').innerHTML = theme;
|
$('#dynamic').innerHTML = theme;
|
||||||
|
|
||||||
return theme;
|
return theme;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// download custom grayscaled theme
|
||||||
function download(css) {
|
function download(css) {
|
||||||
const filename = `grayscaled.css`;
|
const filename = `grayscaled.css`;
|
||||||
|
|
||||||
|
@ -95,25 +101,34 @@ function download(css) {
|
||||||
el.click();
|
el.click();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// page download button
|
||||||
$('input[type="submit"]').addEventListener('click', (e) => {
|
$('input[type="submit"]').addEventListener('click', (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
const theme = update();
|
// get current theme
|
||||||
|
const theme = refresh();
|
||||||
|
|
||||||
function css_text(x) {
|
// get style rules as text
|
||||||
|
function getText(x) {
|
||||||
|
// exclude :root theme vars
|
||||||
if(x.selectorText !== ':root') {
|
if(x.selectorText !== ':root') {
|
||||||
return x.cssText;
|
return x.cssText;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var css = document.getElementById('grayscale');
|
// get grayscale stylesheet as dom object
|
||||||
var content = Array.prototype.map.call(css.sheet.cssRules, css_text).join('\n');
|
var css = $('#grayscale');
|
||||||
|
|
||||||
const grayscaled = `${theme}${content}`.replace(/\s/g, '');
|
// convert stylesheet dom object to string
|
||||||
|
var style = Array.prototype.map.call(css.sheet.cssRules, getText).join('\n');
|
||||||
|
|
||||||
|
// build custom grayscaled theme & minify
|
||||||
|
const grayscaled = `${theme}${style}`.replace(/\s/g, '');
|
||||||
|
|
||||||
download(grayscaled);
|
download(grayscaled);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// toggle light / dark mode
|
||||||
$('#mode').addEventListener('click', () => {
|
$('#mode').addEventListener('click', () => {
|
||||||
const attr = $('html').getAttribute('data-theme');
|
const attr = $('html').getAttribute('data-theme');
|
||||||
|
|
||||||
|
@ -124,9 +139,10 @@ $('#mode').addEventListener('click', () => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
$('#radius').addEventListener('click', update);
|
// refresh page theme on changes
|
||||||
$('#font').addEventListener('click', update);
|
$('#radius').addEventListener('click', refresh);
|
||||||
$('#size').addEventListener('click', update);
|
$('#font').addEventListener('click', refresh);
|
||||||
$('#spacing').addEventListener('click', update);
|
$('#size').addEventListener('click', refresh);
|
||||||
$('#line').addEventListener('click', update);
|
$('#spacing').addEventListener('click', refresh);
|
||||||
$('#theme').addEventListener('change', update);
|
$('#line').addEventListener('click', refresh);
|
||||||
|
$('#theme').addEventListener('change', refresh);
|
||||||
|
|
Loading…
Reference in New Issue