-
Notifications
You must be signed in to change notification settings - Fork 617
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee61ead
commit fb9e7a0
Showing
5 changed files
with
91 additions
and
10 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
// Copyright © SixtyFPS GmbH <[email protected]> | ||
// SPDX-License-Identifier: MIT | ||
import type { Props } from "@astrojs/starlight/props"; | ||
import { Icon } from "@astrojs/starlight/components"; | ||
--- | ||
|
||
<script> | ||
class ThemeSwitcher extends HTMLElement { | ||
constructor() { | ||
super(); | ||
const storedTheme = | ||
typeof localStorage !== 'undefined' && localStorage.getItem('starlight-theme'); | ||
const theme = | ||
storedTheme || | ||
(window.matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark'); | ||
document.documentElement.dataset.theme = theme === 'light' ? 'light' : 'dark'; | ||
this.handleMouseDown = this.handleMouseDown.bind(this); | ||
} | ||
connectedCallback() { | ||
this.addEventListener('click', this.handleMouseDown); | ||
} | ||
disconnectedCallback() { | ||
this.removeEventListener('click', this.handleMouseDown); | ||
} | ||
private handleMouseDown(e: MouseEvent) { | ||
const theme = document.documentElement.dataset.theme === 'light' ? 'dark' : 'light'; | ||
document.documentElement.dataset.theme = theme; | ||
localStorage.setItem('starlight-theme', theme); | ||
} | ||
} | ||
customElements.define('theme-switcher', ThemeSwitcher); | ||
</script> | ||
|
||
<theme-switcher class="sl-flex"> | ||
<Icon name="sun" class="theme-selector-light" /> | ||
<Icon name="moon" class="theme-selector-dark" /> | ||
</theme-switcher> |
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,34 @@ | ||
theme-switcher { | ||
align-items: center; | ||
} | ||
.theme-selector-light, | ||
.theme-selector-dark { | ||
user-select: none; | ||
z-index: 999999; | ||
position: relative; | ||
cursor: pointer; | ||
} | ||
.theme-selector-light:hover, | ||
.theme-selector-dark:hover { | ||
color: var(--sl-color-accent-high); | ||
} | ||
:root { | ||
.theme-selector-light { | ||
display: none; | ||
} | ||
.theme-selector-dark { | ||
display: inline-block; | ||
} | ||
} | ||
:root[data-theme="light"] { | ||
.theme-selector-light { | ||
display: inline-block; | ||
} | ||
.theme-selector-dark { | ||
display: none; | ||
} | ||
.theme-selector-light:hover, | ||
.theme-selector-dark:hover { | ||
color: var(--sl-color-accent); | ||
} | ||
} |