02/01/2023

PicoCSS Dynamic Templates

Pico CSS has the ability to customize the dark and light templates here is how to change the template colors with JS.

The Pico CSS template utilizes CSS variables to dynamically alter the appearance of the website. In this scenario, the intention was to extract the color information from a database and dynamically customize the visual presentation of the site. All CSS variables are defined at the :root level and can be manipulated using JavaScript's setProperty method, as demonstrated below.

let color = "#00A1FE"

let root = document.querySelector(':root');
root.style.setProperty('--primary', color);
root.style.setProperty('--primary-hover', color);
root.style.setProperty('--primary-focus', color + '40');
root.style.setProperty('--primary-inverse', '#fff');

To ensure that the template colors are not pre-loaded prior to dynamically setting them, it is recommended to set the template colors to transparent, as demonstrated below:

/* Deep-purple Light scheme (Default) */
/* Can be forced with data-theme="light" */
:root:not([data-theme=dark]),
[data-theme=light] {
    --primary: #00000000;
    --primary-hover: #00000000;
    --primary-focus: #00000000;
    --primary-inverse: #FFF;
    --logo-text: #000;
}

/* Deep-purple Dark scheme (Auto) */
/* Automatically enabled if user has Dark mode enabled */
@media only screen and (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --primary: #00000000;
        --primary-hover: #00000000;
        --primary-focus: #00000000;
        --primary-inverse: #FFF;
        --logo-text: #FFF;
    }
}

/* Deep-purple Dark scheme (Forced) */
/* Enabled if forced with data-theme="dark" */
[data-theme="dark"] {
    --primary: #00000000;
    --primary-hover: #00000000;
    --primary-focus: #00000000;
    --primary-inverse: #FFF;
    --logo-text: #FFF;
}

/* Deep-purple (Common styles) */
:root {
    --form-element-active-border-color: var(--primary);
    --form-element-focus-color: var(--primary-focus);
    --switch-color: var(--primary-inverse);
    --switch-checked-background-color: var(--primary);
}
Back

Comments


Be the first to comment!

Read More

Making sticky elements in HTML and CSS.

Sticky elements are a useful feature in modern web design, as they allow certain elements on a webpage to remain visible even when the user scrolls. This can be useful for elements such as the n...

pocketbase
pocketbase

PocketBase JS API

Unlock the full potential of PocketBase's JS SDK with this comprehensive guide covering everything you need to know to avoid confusion.

Node JS Express Server

Even though Node JS has a built in HTTP request handler using a web server framework can reduce the amount of code and increase the reliability of your site. There are many options to ...