Skip to content

Commit

Permalink
Released first version.
Browse files Browse the repository at this point in the history
  • Loading branch information
DZ authored and DZ committed Jan 5, 2022
0 parents commit 7f9556a
Show file tree
Hide file tree
Showing 80 changed files with 575 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.vscode
node_modules
package-lock.json
.history
.DS_Store
1 change: 1 addition & 0 deletions CSS/app.dz.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions CSS/dark.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
body {
background-color: #333;
}

i.Icon {
color: #f2f2f2;
}
1 change: 1 addition & 0 deletions CSS/desktop.dz.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

7 changes: 7 additions & 0 deletions CSS/light.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
body {
background-color: #fff;
}

i.Icon {
color: #333;
}
1 change: 1 addition & 0 deletions CSS/mobile.dz.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

20 changes: 20 additions & 0 deletions Dist/CSS/CustomThemeSwitcher.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Dist/CSS/CustomThemeSwitcher.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Dist/CSS/CustomThemeSwitcher.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 91 additions & 0 deletions Dist/JS/CustomThemeSwitcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ThemeSwitcher = void 0;
class ThemeSwitcher {
constructor(el, params) {
this.elementsClass = 'ThemeSwitcher__item';
this.elementActiveClass = `${this.elementsClass}--active`;
this.el = el;
this.elements = [...el.querySelectorAll(`.${this.elementsClass}`)];
this.linkEl = document.head.querySelector(`#${params.linkId}`);
this.lightSchemeRoute = params.routes.light;
this.darkSchemeRoute = params.routes.dark;
this.schemeKey = params.schemeKey;
this.defaultScheme = params.defaultScheme;
this.name = el.dataset.name || '';
this.init();
}
init() {
try {
this.check();
this.listen();
let currentScheme = this.getScheme();
this.setScheme(currentScheme);
this.el.onclick = this.clickHandler.bind(this);
}
catch (err) {
console.warn(err.message);
}
}
clickHandler() {
let allSchemes = ['light', 'dark', 'system'];
let currentScheme = this.getScheme();
let currentSchemeIndex = allSchemes.findIndex((item) => item == currentScheme);
if ((allSchemes.length - 1) == currentSchemeIndex)
currentSchemeIndex = 0;
else
currentSchemeIndex++;
let nextScheme = allSchemes[currentSchemeIndex];
this.setScheme(nextScheme);
}
listen() {
window.addEventListener('storage', () => {
let currentScheme = this.getScheme();
for (let item of this.elements) {
item.classList.remove(this.elementActiveClass);
}
(this.elements.find((el) => el.dataset.value == currentScheme)).classList.add(this.elementActiveClass);
switch (currentScheme) {
case 'light':
this.setLink(this.lightSchemeRoute);
break;
case 'dark':
this.setLink(this.darkSchemeRoute);
break;
case 'system':
this.systemSchemeListener();
break;
}
});
}
systemSchemeListener() {
let matchMedia = window.matchMedia('(prefers-color-scheme: light)');
matchMedia.matches ? this.setLink(this.lightSchemeRoute) : this.setLink(this.darkSchemeRoute);
matchMedia.addListener((e) => {
e.matches ? this.setLink(this.lightSchemeRoute) : this.setLink(this.darkSchemeRoute);
});
}
setLink(route) {
this.linkEl.setAttribute('href', route);
}
getScheme() {
let allSchemes = ['light', 'dark', 'system'];
let scheme = localStorage.getItem(this.schemeKey) || this.defaultScheme;
if (!allSchemes.includes(scheme) || !scheme.length) {
scheme = this.defaultScheme;
}
return scheme;
}
setScheme(val) {
localStorage.setItem(this.schemeKey, val);
window.dispatchEvent(new Event('storage'));
}
check() {
if (!this.el)
throw new Error(`Component "${this.name}" not found!`);
if (!this.linkEl)
throw new Error('Link element for change theme is not found!');
console.info(`Component "${this.name}" is ready.`);
}
}
exports.ThemeSwitcher = ThemeSwitcher;
1 change: 1 addition & 0 deletions Dist/JS/CustomThemeSwitcher.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions Dist/SASS/CustomThemeSwitcher.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.ThemeSwitcher
cursor: pointer
&__item
display: none
&--active
display: flex
justify-content: center
&__system
> *:first-child
position: relative
right: -6px
> *:last-child
position: relative
left: -6px

133 changes: 133 additions & 0 deletions Dist/TS/CustomThemeSwitcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
type Scheme = 'light' | 'dark' | 'system';

interface IParams {
linkId: string,
schemeKey: string,
defaultScheme: Scheme,
routes: {
light: string,
dark: string
}
}

export class ThemeSwitcher {
public readonly elementsClass: string = 'ThemeSwitcher__item';
public readonly elementActiveClass: string = `${this.elementsClass}--active`;
public readonly name: string;
public readonly schemeKey: string;
public readonly defaultScheme: Scheme;
public linkEl: HTMLLinkElement | null;

protected el: HTMLDivElement | null;
protected elements: HTMLDivElement[];
protected lightSchemeRoute: string;
protected darkSchemeRoute: string;

constructor(el: HTMLDivElement | null, params: IParams) {
this.el = el;
this.elements = [...el!.querySelectorAll<HTMLDivElement>(`.${this.elementsClass}`)];
this.linkEl = document.head.querySelector(`#${params.linkId}`);
this.lightSchemeRoute = params.routes.light;
this.darkSchemeRoute = params.routes.dark;
this.schemeKey = params.schemeKey;
this.defaultScheme = params.defaultScheme;
this.name = el!.dataset.name || '';

this.init();
}

protected init(): void {
try {
this.check();

this.listen();
let currentScheme = this.getScheme();
this.setScheme(currentScheme);

this.el!.onclick = this.clickHandler.bind(this);
} catch (err: Error | any) {
console.warn(err.message);
}
}

protected clickHandler(): void {
let allSchemes: Scheme[] = ['light', 'dark', 'system'];
let currentScheme: Scheme = this.getScheme();

let currentSchemeIndex: number = allSchemes.findIndex((item: Scheme): boolean => item == currentScheme);
if ((allSchemes.length - 1) == currentSchemeIndex)
currentSchemeIndex = 0;
else
currentSchemeIndex++;

let nextScheme: Scheme = allSchemes[currentSchemeIndex];
this.setScheme(nextScheme);
}

protected listen(): void {
window.addEventListener('storage', () => {
let currentScheme: Scheme = this.getScheme();

for (let item of this.elements) {
item.classList.remove(this.elementActiveClass);
}

(this.elements.find((el: HTMLDivElement): boolean => el.dataset.value == currentScheme))!.classList.add(this.elementActiveClass);

switch (currentScheme) {
case 'light':
this.setLink(this.lightSchemeRoute);

break;
case 'dark':
this.setLink(this.darkSchemeRoute);

break;
case 'system':
this.systemSchemeListener();

break;
}
});
}

protected systemSchemeListener(): void {
let matchMedia = window.matchMedia('(prefers-color-scheme: light)');
matchMedia.matches ? this.setLink(this.lightSchemeRoute) : this.setLink(this.darkSchemeRoute);

matchMedia.addListener((e: MediaQueryListEvent): void => {
e.matches ? this.setLink(this.lightSchemeRoute) : this.setLink(this.darkSchemeRoute);
});
}

protected setLink(route: string): void {
this.linkEl!.setAttribute('href', route);
}

protected getScheme(): Scheme {
let allSchemes: Scheme[] = ['light', 'dark', 'system'];
let scheme: string | Scheme = localStorage.getItem(this.schemeKey) || this.defaultScheme;

if (!allSchemes.includes(scheme as Scheme) || !scheme.length) {
scheme = this.defaultScheme;
}

return scheme as Scheme;
}

public setScheme(val: Scheme): void {
localStorage.setItem(this.schemeKey, val);

window.dispatchEvent(new Event('storage'));
}

protected check(): void {
if (!this.el)
throw new Error(`Component "${this.name}" not found!`);

if (!this.linkEl)
throw new Error('Link element for change theme is not found!');

console.info(`Component "${this.name}" is ready.`);
}
}
1 change: 1 addition & 0 deletions Fonts/0d058f9b0545f5710695b5e5bb78c5cd.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Fonts/4c4141063352e1c8a0384e793cdb38ce.ttf
Binary file not shown.
Binary file added Fonts/a80d8446178520ebafd590eef41b4f39.eot
Binary file not shown.
Binary file added Fonts/dc05e74f7dbfa773026a348871be8c18.woff
Binary file not shown.
12 changes: 12 additions & 0 deletions Img/7a9b25e1b8ffdb7365d6c41ab8970317.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Img/Favicon/Dark/android-icon-144x144.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Img/Favicon/Dark/android-icon-192x192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Img/Favicon/Dark/android-icon-36x36.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Img/Favicon/Dark/android-icon-48x48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Img/Favicon/Dark/android-icon-72x72.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Img/Favicon/Dark/android-icon-96x96.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Img/Favicon/Dark/apple-icon-114x114.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Img/Favicon/Dark/apple-icon-120x120.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Img/Favicon/Dark/apple-icon-144x144.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Img/Favicon/Dark/apple-icon-152x152.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Img/Favicon/Dark/apple-icon-180x180.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Img/Favicon/Dark/apple-icon-57x57.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Img/Favicon/Dark/apple-icon-60x60.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Img/Favicon/Dark/apple-icon-72x72.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Img/Favicon/Dark/apple-icon-76x76.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Img/Favicon/Dark/apple-icon-precomposed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Img/Favicon/Dark/apple-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions Img/Favicon/Dark/browserconfig.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<browserconfig><msapplication><tile><square70x70logo src="/ms-icon-70x70.png"/><square150x150logo src="/ms-icon-150x150.png"/><square310x310logo src="/ms-icon-310x310.png"/><TileColor>#ffffff</TileColor></tile></msapplication></browserconfig>
Binary file added Img/Favicon/Dark/favicon-16x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Img/Favicon/Dark/favicon-32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Img/Favicon/Dark/favicon-96x96.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Img/Favicon/Dark/favicon.ico
Binary file not shown.
41 changes: 41 additions & 0 deletions Img/Favicon/Dark/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "App",
"icons": [
{
"src": "\/android-icon-36x36.png",
"sizes": "36x36",
"type": "image\/png",
"density": "0.75"
},
{
"src": "\/android-icon-48x48.png",
"sizes": "48x48",
"type": "image\/png",
"density": "1.0"
},
{
"src": "\/android-icon-72x72.png",
"sizes": "72x72",
"type": "image\/png",
"density": "1.5"
},
{
"src": "\/android-icon-96x96.png",
"sizes": "96x96",
"type": "image\/png",
"density": "2.0"
},
{
"src": "\/android-icon-144x144.png",
"sizes": "144x144",
"type": "image\/png",
"density": "3.0"
},
{
"src": "\/android-icon-192x192.png",
"sizes": "192x192",
"type": "image\/png",
"density": "4.0"
}
]
}
Binary file added Img/Favicon/Dark/ms-icon-144x144.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Img/Favicon/Dark/ms-icon-150x150.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Img/Favicon/Dark/ms-icon-310x310.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Img/Favicon/Dark/ms-icon-70x70.png
Binary file added Img/Favicon/Light/android-icon-144x144.png
Binary file added Img/Favicon/Light/android-icon-192x192.png
Binary file added Img/Favicon/Light/android-icon-36x36.png
Binary file added Img/Favicon/Light/android-icon-48x48.png
Binary file added Img/Favicon/Light/android-icon-72x72.png
Binary file added Img/Favicon/Light/android-icon-96x96.png
Binary file added Img/Favicon/Light/apple-icon-114x114.png
Binary file added Img/Favicon/Light/apple-icon-120x120.png
Binary file added Img/Favicon/Light/apple-icon-144x144.png
Binary file added Img/Favicon/Light/apple-icon-152x152.png
Binary file added Img/Favicon/Light/apple-icon-180x180.png
Binary file added Img/Favicon/Light/apple-icon-57x57.png
Binary file added Img/Favicon/Light/apple-icon-60x60.png
Binary file added Img/Favicon/Light/apple-icon-72x72.png
Binary file added Img/Favicon/Light/apple-icon-76x76.png
Binary file added Img/Favicon/Light/apple-icon-precomposed.png
Binary file added Img/Favicon/Light/apple-icon.png
2 changes: 2 additions & 0 deletions Img/Favicon/Light/browserconfig.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<browserconfig><msapplication><tile><square70x70logo src="/ms-icon-70x70.png"/><square150x150logo src="/ms-icon-150x150.png"/><square310x310logo src="/ms-icon-310x310.png"/><TileColor>#ffffff</TileColor></tile></msapplication></browserconfig>
Binary file added Img/Favicon/Light/favicon-16x16.png
Binary file added Img/Favicon/Light/favicon-32x32.png
Binary file added Img/Favicon/Light/favicon-96x96.png
Binary file added Img/Favicon/Light/favicon.ico
Binary file not shown.
Loading

0 comments on commit 7f9556a

Please sign in to comment.