This repository was archived by the owner on Mar 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Basic implementation of theme toggle * Change the toggle data attribute because data-theme created clashes with the toggle itself Although they were rather amusing * Add basic, temp UI to the toggle until we get some proper UI * Switch the theme toggle to use light dom and a switch control * Lean into the media query more and don't store that preference by default It makes more sense only to store the preference when the user interacts with the switch
- Loading branch information
1 parent
ed6c4a6
commit e242019
Showing
8 changed files
with
145 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright 2021 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Element that allows a user to toggle the current theme. | ||
* @extends {HTMLElement} | ||
* @final | ||
*/ | ||
class ThemeToggle extends HTMLElement { | ||
constructor() { | ||
super(); | ||
|
||
this.STORAGE_KEY = 'user-color-scheme'; | ||
this.COLOR_MODE_KEY = '--color-mode'; | ||
} | ||
|
||
connectedCallback() { | ||
this.toggleSwitch = this.querySelector('[role="switch"]'); | ||
|
||
if (this.toggleSwitch) { | ||
// On change, calculate the new setting, toggle state changes and store in storage | ||
this.toggleSwitch.addEventListener('change', () => { | ||
const setting = this.toggleSwitch.checked ? 'dark' : 'light'; | ||
this.applySetting(setting); | ||
localStorage.setItem(this.STORAGE_KEY, setting); | ||
}); | ||
|
||
this.applySetting(); | ||
} | ||
} | ||
|
||
applySetting(passedSetting) { | ||
// Attempts to load the setting from local storage | ||
const currentSetting = | ||
passedSetting || localStorage.getItem(this.STORAGE_KEY); | ||
|
||
if (currentSetting) { | ||
this.setToggleSwitchStatus(currentSetting); | ||
window.applyThemeSetting(currentSetting); | ||
} | ||
// If no storage setting, we set up media query-based state change | ||
else { | ||
// Set the checkbox to on if we're already in dark preference | ||
if (window.matchMedia('(prefers-color-scheme: dark)').matches) { | ||
this.setToggleSwitchStatus('dark'); | ||
} | ||
|
||
// Listen for changes to the preference and set checkbox state accordingly | ||
window | ||
.matchMedia('(prefers-color-scheme: dark)') | ||
.addEventListener('change', (evt) => { | ||
this.setToggleSwitchStatus(evt.matches ? 'dark' : 'light'); | ||
}); | ||
} | ||
} | ||
|
||
// Sets the correct aria checked role and checked state | ||
setToggleSwitchStatus(currentSetting) { | ||
const isDarkMode = currentSetting === 'dark'; | ||
this.toggleSwitch.setAttribute( | ||
'aria-checked', | ||
isDarkMode ? 'true' : 'false', | ||
); | ||
|
||
this.toggleSwitch.checked = isDarkMode; | ||
} | ||
} | ||
|
||
if ('customElements' in window) { | ||
customElements.define('theme-toggle', ThemeToggle); | ||
} | ||
|
||
export default ThemeToggle; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/// THIS IS VERY MUCH A TEMPORARY | ||
/// BIT OF CSS UNTIL WE SLING SOME | ||
/// PROPER UI DESIGN ON THE THEME TOGGLE | ||
.theme-toggle { | ||
display: flex; | ||
align-items: center; | ||
gap: 1rem; | ||
|
||
button { | ||
border: 2px solid var(--color-stroke); | ||
padding: 0.5rem 1rem; | ||
|
||
@include apply-utility('bg', 'action-bg'); | ||
@include apply-utility('color', 'action-text'); | ||
} | ||
|
||
[aria-pressed='true'] { | ||
border-color: get-color('core-primary-glare'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters