Skip to content

Instantly share code, notes, and snippets.

@amberlex78
Last active October 24, 2023 06:57
Show Gist options
  • Save amberlex78/532d9f95b3584f344b9d06fb8b4be0e6 to your computer and use it in GitHub Desktop.
Save amberlex78/532d9f95b3584f344b9d06fb8b4be0e6 to your computer and use it in GitHub Desktop.
dark-light switcher
(() => {
'use strict';
// Функция для работы с localStorage
const getStoredValue = (key, defaultValue) => localStorage.getItem(key) || defaultValue;
const setStoredValue = (key, value) => localStorage.setItem(key, value);
// Функции для установки фронт-темы и фронт-иконки
const setFrontTheme = theme => document.documentElement.setAttribute('data-bs-theme', theme);
const setFrontThemeIcon = themeIcon => document.querySelector('#themeIcon').setAttribute('xlink:href', themeIcon);
// Установка начальных значений
setFrontTheme(getStoredValue('theme', 'light'));
setFrontThemeIcon(getStoredValue('themeIcon', '#icoMoonFill'));
// Функция для обновления темы и иконки
const updateTheme = () => {
const currentTheme = getStoredValue('theme', 'light');
const newTheme = currentTheme === 'light' ? 'dark' : 'light';
setStoredValue('theme', newTheme);
setFrontTheme(newTheme);
const currentThemeIcon = getStoredValue('themeIcon', '#icoMoonFill');
const newThemeIcon = currentThemeIcon === '#icoMoonFill' ? '#icoSunFill' : '#icoMoonFill';
setStoredValue('themeIcon', newThemeIcon);
setFrontThemeIcon(newThemeIcon);
};
// Добавление обработчика события
document.querySelector('.change-theme').addEventListener('click', updateTheme);
})();
<svg xmlns="http://www.w3.org/2000/svg" class="d-none">
<symbol id="icoMoonFill" viewBox="0 0 16 16">
<path d="M6 .278a.768.768 0 0 1 .08.858 7.208 7.208 0 0 0-.878 3.46c0 4.021 3.278 7.277 7.318 7.277.527 0 1.04-.055 1.533-.16a.787.787 0 0 1 .81.316.733.733 0 0 1-.031.893A8.349 8.349 0 0 1 8.344 16C3.734 16 0 12.286 0 7.71 0 4.266 2.114 1.312 5.124.06A.752.752 0 0 1 6 .278z"/>
</symbol>
<symbol id="icoSunFill" viewBox="0 0 16 16">
<path d="M8 12a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM8 0a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 8 0zm0 13a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 8 13zm8-5a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1h2a.5.5 0 0 1 .5.5zM3 8a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1h2A.5.5 0 0 1 3 8zm10.657-5.657a.5.5 0 0 1 0 .707l-1.414 1.415a.5.5 0 1 1-.707-.708l1.414-1.414a.5.5 0 0 1 .707 0zm-9.193 9.193a.5.5 0 0 1 0 .707L3.05 13.657a.5.5 0 0 1-.707-.707l1.414-1.414a.5.5 0 0 1 .707 0zm9.193 2.121a.5.5 0 0 1-.707 0l-1.414-1.414a.5.5 0 0 1 .707-.707l1.414 1.414a.5.5 0 0 1 0 .707zM4.464 4.465a.5.5 0 0 1-.707 0L2.343 3.05a.5.5 0 1 1 .707-.707l1.414 1.414a.5.5 0 0 1 0 .708z"/>
</symbol>
</svg>
<nav class="navbar navbar-expand-lg" aria-label="Ninth navbar example">
<div class="container">
<a class="navbar-brand" href="/">App</a>
<button class="navbar-toggler" type="button"
data-bs-toggle="collapse"
data-bs-target="#navbarHeader"
aria-controls="navbarHeader"
aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarHeader">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link" href="#">Blog</a>
</li>
</ul>
<ul class="navbar-nav mb-2 mb-lg-0">
<li class="nav-item">
<a class="change-theme nav-link" href="#">
<svg class="bi" fill="currentColor" width="16" height="16">
<use id="themeIcon" xlink:href="#icoSunFill"></use>
</svg>
</a>
</li>
</ul>
</div>
</div>
</nav>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment