Skip to content

Commit 4ea28a4

Browse files
authored
Merge pull request #94 from fastpok/pr-style-tags
Add support for style tags for theme styles
2 parents b6723cd + b050f00 commit 4ea28a4

File tree

4 files changed

+78
-3
lines changed

4 files changed

+78
-3
lines changed

README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Or, alternatively, use a `<script type="module">` tag (served from unpkg's CDN):
3232

3333
## Usage
3434

35-
There are two ways how you can use `<dark-mode-toggle>`:
35+
There are three ways how you can use `<dark-mode-toggle>`:
3636

3737
### ① Using different stylesheets per color scheme that are conditionally loaded
3838

@@ -168,6 +168,37 @@ toggle.addEventListener('colorschemechange', () => {
168168
});
169169
```
170170

171+
### ③ Using internal stylesheets for each color scheme
172+
173+
This approach allows you to define styles directly within your HTML using
174+
`<style>` tags, scoped to specific color schemes.
175+
176+
⚠️ **Warning**
177+
Internal stylesheets do not benefit from the loading optimizations provided by
178+
`<link>` elements, which may increase the page's initial load time.
179+
180+
```html
181+
<head>
182+
<style media="(prefers-color-scheme: light)">
183+
body {
184+
background-color: #ffffff;
185+
color: #000000;
186+
}
187+
</style>
188+
<style media="(prefers-color-scheme: dark)">
189+
body {
190+
background-color: #000000;
191+
color: #ffffff;
192+
}
193+
</style>
194+
<script
195+
type="module"
196+
src="https://googlechromelabs.github.io/dark-mode-toggle/src/dark-mode-toggle.mjs"
197+
></script>
198+
</head>
199+
<!-- ... -->
200+
```
201+
171202
## Demo
172203

173204
See the custom element in action in the

demo/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ <h1>Hi there!</h1>
106106
>example without flashing</a
107107
>).
108108
</p>
109+
<p>
110+
You can also check out the
111+
<a href="internal-stylesheets.html">demo with internal stylesheets</a>.
112+
</p>
109113
</main>
110114
<aside>
111115
<dark-mode-toggle id="dark-mode-toggle-1"></dark-mode-toggle>

demo/internal-stylesheets.html

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<style media="(prefers-color-scheme: light)">
5+
:root {
6+
--background-color: #fff;
7+
--text-color: #000;
8+
}
9+
</style>
10+
<style media="(prefers-color-scheme: dark)">
11+
:root {
12+
--background-color: #000;
13+
--text-color: #fff;
14+
}
15+
</style>
16+
<style>
17+
body {
18+
background-color: var(--background-color);
19+
color: var(--text-color);
20+
}
21+
</style>
22+
<script type="module" src="../src/dark-mode-toggle.mjs"></script>
23+
</head>
24+
<body>
25+
<h1>Demo with internal stylesheets</h1>
26+
<p>
27+
Lorem ipsum dolor sit amet, legere ancillae ne vis. Ne vim laudem accusam
28+
consectetuer, eu utinam integre abhorreant sea. Quo eius veri ei, nibh
29+
invenire democritum vel in, utamur vulputate id est. Possit ceteros vis
30+
an.
31+
</p>
32+
<div>
33+
Switch theme:
34+
<dark-mode-toggle permanent></dark-mode-toggle>
35+
</div>
36+
</body>
37+
</html>

src/dark-mode-toggle.mjs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const SYSTEM = 'system';
3030
const MQ_DARK = `(${PREFERS_COLOR_SCHEME}:${DARK})`;
3131
const MQ_LIGHT = `(${PREFERS_COLOR_SCHEME}:${LIGHT})`;
3232
const LINK_REL_STYLESHEET = 'link[rel=stylesheet]';
33+
const STYLE = 'style';
3334
const REMEMBER = 'remember';
3435
const LEGEND = 'legend';
3536
const TOGGLE = 'toggle';
@@ -126,10 +127,12 @@ export class DarkModeToggle extends HTMLElement {
126127
// We need to support `media="(prefers-color-scheme: dark)"` (with space)
127128
// and `media="(prefers-color-scheme:dark)"` (without space)
128129
this._darkCSS = doc.querySelectorAll(
129-
`${LINK_REL_STYLESHEET}[${MEDIA}*=${PREFERS_COLOR_SCHEME}][${MEDIA}*="${DARK}"]`,
130+
`${LINK_REL_STYLESHEET}[${MEDIA}*=${PREFERS_COLOR_SCHEME}][${MEDIA}*="${DARK}"],
131+
${STYLE}[${MEDIA}*=${PREFERS_COLOR_SCHEME}][${MEDIA}*="${DARK}"]`,
130132
);
131133
this._lightCSS = doc.querySelectorAll(
132-
`${LINK_REL_STYLESHEET}[${MEDIA}*=${PREFERS_COLOR_SCHEME}][${MEDIA}*="${LIGHT}"]`,
134+
`${LINK_REL_STYLESHEET}[${MEDIA}*=${PREFERS_COLOR_SCHEME}][${MEDIA}*="${LIGHT}"],
135+
${STYLE}[${MEDIA}*=${PREFERS_COLOR_SCHEME}][${MEDIA}*="${LIGHT}"]`,
133136
);
134137

135138
// Get DOM references.

0 commit comments

Comments
 (0)