0.1.1 • Published 2 years ago

@petegi/pi-color-scheme-switcher v0.1.1

Weekly downloads
-
License
-
Repository
github
Last release
2 years ago

PiColorSchemeSwitcher

Simple color scheme switcher for your site. When connected to the DOM, checks if LocalStorage has the value with current scheme (by given key, look at the available attributes). If no value is found in the LocalStorage, checks the media prefers-color-scheme.

Default styling is just button with specified min-width and height. To style the button on your own, look at the Parts and slots section to see how the element is structured. By default, if you place the code in your HTML, the start of your document will look like:

<html color-scheme="dark"></html>`

Requirements

Node v16+ if you'd like to build the component.

CustomElement doesn't have any dependencies other than vite used to build things up (devDep only).

Instalation

npm install @petegi/pi-color-scheme-switcher

Usage

// Include the script
import "path-to-@petegi/pi-color-scheme-switcher"
// or
<script src="path-to-@petegi/pi-color-scheme-switcher"></script>

// Use in your HTML
<pi-color-scheme-switcher></pi-color-scheme-switcher>

Elements

NameDescription
pi-color-scheme-switcherMain and only element

Attributes

NameTypeDefaultDescription
elementstring'head'Selector for element that should get attribute with value of current color scheme
attrnamestring'color-scheme'Name of te attribute that will be given to the elemenet with the value of color scheme
storagekeystring'color-scheme'Name of the field to be used in LocalStorage
requesturl*string''URL to make request with currently selected theme (sends { [attrname]: "light" or "dark" })

*CustomElement will not send anything if requesturl is not specified

Special attributes

CustomElements spec doesn't give the ability to specify custom pseudoclasses, so these attributes show what current scheme is.

They should not be used in any other way than in CSS to style the element.

NameDescription
lightCurrent color scheme is light
darkCurrent color scheme is dark

Properties

NameTypeDefaultDescription
additionalHeadersObject{}Additional headers to be sent with each request

To use these properties, grab the element (e.g. using querySelector) and do it like:

const switcher = document.querySelector('.color-switcher');
switcher.additionalHeaders = { /* your custom headers */ }

Parts and slots

Custom Element parts are useful for styling the components's inner elements. Slots on the other hand, are used to place your content inside them.

PiColorSchemeSwitcher allows you to style all of the elements on your own. Below you can see how the component is built, with all of it's parts and slots.

pi-select

<button part="button" type="button">
  <span part="marker">
    <slot></slot>
  </span>
</button>

Licence

MIT