astro-viewtransition-theme 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Yusaku
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # astro-viewtransition-theme
2
+
3
+ Astro theme toggle components with view transitions support.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ npm install astro-viewtransition-theme
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```astro
14
+ ---
15
+ import { ThemeInit, ThemeToggle } from 'astro-viewtransition-theme';
16
+ ---
17
+
18
+ <html lang="en">
19
+ <head>
20
+ <ThemeInit />
21
+ </head>
22
+ <body>
23
+ <ThemeToggle />
24
+ </body>
25
+ </html>
26
+ ```
27
+
28
+ ### Custom button
29
+
30
+ ```astro
31
+ ---
32
+ import { ThemeInit, ThemeToggle, ThemeButton } from 'astro-viewtransition-theme';
33
+ ---
34
+
35
+ <ThemeInit />
36
+ <ThemeToggle>
37
+ <ThemeButton />
38
+ </ThemeToggle>
39
+ ```
40
+
41
+ ## Notes
42
+
43
+ - The current theme is stored in `localStorage` under the `theme` key.
44
+ - The active theme is applied via `data-theme` on the `<html>` element.
45
+ - `ThemeToggle` listens for `astro:before-swap` and `astro:page-load` to keep the theme during view transitions.
46
+ - Customize the button via CSS variables:
47
+ - `--toggle-track`
48
+ - `--toggle-thumb`
49
+ - `--toggle-icon`
50
+ - `--accent` (focus ring)
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "astro-viewtransition-theme",
3
+ "version": "0.0.1",
4
+ "description": "Astro theme toggle with view transitions support.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/Yusaku01/astro-viewtransition-theme.git"
9
+ },
10
+ "homepage": "https://github.com/Yusaku01/astro-viewtransition-theme#readme",
11
+ "bugs": {
12
+ "url": "https://github.com/Yusaku01/astro-viewtransition-theme/issues"
13
+ },
14
+ "keywords": [
15
+ "astro",
16
+ "theme",
17
+ "theme-toggle",
18
+ "dark-mode",
19
+ "color-scheme",
20
+ "view-transitions"
21
+ ],
22
+ "type": "module",
23
+ "exports": {
24
+ ".": "./src/index.js",
25
+ "./ThemeToggle": "./src/components/ThemeToggle.astro",
26
+ "./ThemeButton": "./src/components/ThemeButton.astro",
27
+ "./ThemeInit": "./src/components/ThemeInit.astro"
28
+ },
29
+ "files": [
30
+ "src/components/ThemeToggle.astro",
31
+ "src/components/ThemeButton.astro",
32
+ "src/components/ThemeInit.astro",
33
+ "src/index.js",
34
+ "README.md"
35
+ ],
36
+ "scripts": {
37
+ "dev": "astro dev",
38
+ "build": "astro build",
39
+ "preview": "astro preview",
40
+ "astro": "astro"
41
+ },
42
+ "peerDependencies": {
43
+ "astro": "^5.0.0"
44
+ },
45
+ "devDependencies": {
46
+ "astro": "^5.16.6",
47
+ "@astrojs/check": "^0.9.6",
48
+ "typescript": "^5.9.3"
49
+ }
50
+ }
@@ -0,0 +1,139 @@
1
+ <button
2
+ class="theme-toggle"
3
+ type="button"
4
+ data-theme-toggle
5
+ aria-label="Toggle color theme"
6
+ >
7
+ <span class="theme-toggle__thumb" aria-hidden="true">
8
+ <span class="theme-toggle__icon theme-toggle__icon--sun" aria-hidden="true">
9
+ <svg viewBox="0 0 24 24" width="14" height="14" aria-hidden="true">
10
+ <circle cx="12" cy="12" r="4" fill="currentColor" />
11
+ <circle cx="12" cy="2.5" r="1.5" fill="currentColor" />
12
+ <circle cx="12" cy="21.5" r="1.5" fill="currentColor" />
13
+ <circle cx="2.5" cy="12" r="1.5" fill="currentColor" />
14
+ <circle cx="21.5" cy="12" r="1.5" fill="currentColor" />
15
+ <circle cx="4.6" cy="4.6" r="1.3" fill="currentColor" />
16
+ <circle cx="19.4" cy="4.6" r="1.3" fill="currentColor" />
17
+ <circle cx="4.6" cy="19.4" r="1.3" fill="currentColor" />
18
+ <circle cx="19.4" cy="19.4" r="1.3" fill="currentColor" />
19
+ </svg>
20
+ </span>
21
+ <span class="theme-toggle__icon theme-toggle__icon--moon" aria-hidden="true">
22
+ <svg viewBox="0 0 24 24" width="14" height="14" aria-hidden="true">
23
+ <path
24
+ d="M16.5 14.5a6.5 6.5 0 0 1-7-7 7.5 7.5 0 1 0 7 7z"
25
+ fill="currentColor"
26
+ />
27
+ </svg>
28
+ </span>
29
+ </span>
30
+ <span class="theme-toggle__value sr-only" data-theme-label>Light</span>
31
+ </button>
32
+
33
+ <style>
34
+ :global(:root) {
35
+ --toggle-track: #1f2632;
36
+ --toggle-thumb: #f6f7f9;
37
+ --toggle-icon: #7b8494;
38
+ }
39
+
40
+ :global(html[data-theme='dark']) {
41
+ --toggle-track: #2a3342;
42
+ --toggle-thumb: #f2f4f7;
43
+ --toggle-icon: #6f798c;
44
+ }
45
+
46
+ .theme-toggle {
47
+ --toggle-width: 76px;
48
+ --toggle-height: 36px;
49
+ --toggle-padding: 4px;
50
+ --toggle-thumb-size: 28px;
51
+
52
+ position: relative;
53
+ display: inline-flex;
54
+ align-items: center;
55
+ justify-content: flex-start;
56
+ width: var(--toggle-width);
57
+ height: var(--toggle-height);
58
+ padding: 0;
59
+ border-radius: 999px;
60
+ border: none;
61
+ background: linear-gradient(
62
+ 180deg,
63
+ rgba(255, 255, 255, 0.08),
64
+ rgba(0, 0, 0, 0.25)
65
+ ),
66
+ var(--toggle-track);
67
+ box-shadow:
68
+ inset 0 1px 2px rgba(255, 255, 255, 0.08),
69
+ inset 0 -2px 4px rgba(0, 0, 0, 0.4);
70
+ cursor: pointer;
71
+ transition: background 200ms ease, box-shadow 200ms ease;
72
+ }
73
+
74
+ .theme-toggle:focus-visible {
75
+ outline: 2px solid var(--accent, #1b6c8c);
76
+ outline-offset: 3px;
77
+ }
78
+
79
+ .theme-toggle__thumb {
80
+ position: absolute;
81
+ top: var(--toggle-padding);
82
+ left: var(--toggle-padding);
83
+ width: var(--toggle-thumb-size);
84
+ height: var(--toggle-thumb-size);
85
+ border-radius: 50%;
86
+ background: var(--toggle-thumb);
87
+ box-shadow:
88
+ 0 4px 10px rgba(0, 0, 0, 0.35),
89
+ inset 0 1px 2px rgba(255, 255, 255, 0.8);
90
+ display: grid;
91
+ place-items: center;
92
+ transform: translateX(
93
+ calc(
94
+ var(--toggle-width) - (var(--toggle-padding) * 2) - var(--toggle-thumb-size)
95
+ )
96
+ );
97
+ transition: transform 200ms ease, box-shadow 200ms ease, background 200ms ease;
98
+ }
99
+
100
+ :global(html[data-theme='dark']) .theme-toggle__thumb {
101
+ transform: translateX(0);
102
+ }
103
+
104
+ .theme-toggle__icon {
105
+ position: absolute;
106
+ inset: 0;
107
+ display: grid;
108
+ place-items: center;
109
+ color: var(--toggle-icon);
110
+ transition: opacity 180ms ease, transform 180ms ease;
111
+ }
112
+
113
+ .theme-toggle__icon--moon {
114
+ opacity: 0;
115
+ transform: scale(0.6);
116
+ }
117
+
118
+ :global(html[data-theme='dark']) .theme-toggle__icon--sun {
119
+ opacity: 0;
120
+ transform: scale(0.6);
121
+ }
122
+
123
+ :global(html[data-theme='dark']) .theme-toggle__icon--moon {
124
+ opacity: 1;
125
+ transform: scale(1);
126
+ }
127
+
128
+ .sr-only {
129
+ position: absolute;
130
+ width: 1px;
131
+ height: 1px;
132
+ padding: 0;
133
+ margin: -1px;
134
+ overflow: hidden;
135
+ clip: rect(0, 0, 0, 0);
136
+ white-space: nowrap;
137
+ border: 0;
138
+ }
139
+ </style>
@@ -0,0 +1,12 @@
1
+ <script is:inline>
2
+ const stored = localStorage.getItem('theme');
3
+ const theme =
4
+ stored === 'dark' || stored === 'light'
5
+ ? stored
6
+ : window.matchMedia('(prefers-color-scheme: dark)').matches
7
+ ? 'dark'
8
+ : 'light';
9
+
10
+ document.documentElement.dataset.theme = theme;
11
+ document.documentElement.style.colorScheme = theme;
12
+ </script>
@@ -0,0 +1,99 @@
1
+ ---
2
+ import ThemeButton from './ThemeButton.astro';
3
+
4
+ const hasSlot = Astro.slots.has('default');
5
+ ---
6
+
7
+ {hasSlot ? <slot /> : <ThemeButton />}
8
+
9
+ <script>
10
+ const THEME_STORAGE_KEY = 'theme';
11
+ const THEMES = {
12
+ dark: 'dark',
13
+ light: 'light',
14
+ } as const;
15
+
16
+ type Theme = (typeof THEMES)[keyof typeof THEMES];
17
+
18
+ const getStoredTheme = (): Theme | null => {
19
+ const stored = localStorage.getItem(THEME_STORAGE_KEY);
20
+ if (stored === THEMES.dark || stored === THEMES.light) {
21
+ return stored;
22
+ }
23
+ return null;
24
+ };
25
+
26
+ const getPreferredTheme = (): Theme => {
27
+ const stored = getStoredTheme();
28
+ if (stored) {
29
+ return stored;
30
+ }
31
+ return window.matchMedia('(prefers-color-scheme: dark)').matches
32
+ ? THEMES.dark
33
+ : THEMES.light;
34
+ };
35
+
36
+ const isTheme = (value: string | undefined): value is Theme =>
37
+ value === THEMES.dark || value === THEMES.light;
38
+
39
+ const getActiveTheme = (): Theme => {
40
+ const datasetTheme = document.documentElement.dataset.theme;
41
+ return isTheme(datasetTheme) ? datasetTheme : getPreferredTheme();
42
+ };
43
+
44
+ const applyTheme = (targetDocument: Document, theme: Theme) => {
45
+ targetDocument.documentElement.dataset.theme = theme;
46
+ targetDocument.documentElement.style.colorScheme = theme;
47
+ };
48
+
49
+ const setTheme = (theme: Theme) => {
50
+ localStorage.setItem(THEME_STORAGE_KEY, theme);
51
+ applyTheme(document, theme);
52
+ };
53
+
54
+ const syncToggle = () => {
55
+ const buttons = document.querySelectorAll<HTMLButtonElement>('[data-theme-toggle]');
56
+ if (buttons.length === 0) {
57
+ return;
58
+ }
59
+
60
+ const theme = getActiveTheme();
61
+ if (!isTheme(document.documentElement.dataset.theme)) {
62
+ applyTheme(document, theme);
63
+ }
64
+ const isDark = theme === THEMES.dark;
65
+
66
+ buttons.forEach((button) => {
67
+ const label = button.querySelector<HTMLElement>('[data-theme-label]');
68
+
69
+ button.setAttribute('aria-pressed', String(isDark));
70
+ if (label) {
71
+ label.textContent = isDark ? 'Dark' : 'Light';
72
+ }
73
+
74
+ if (button.dataset.bound === 'true') {
75
+ return;
76
+ }
77
+
78
+ button.dataset.bound = 'true';
79
+ button.addEventListener('click', () => {
80
+ const nextTheme =
81
+ document.documentElement.dataset.theme === THEMES.dark
82
+ ? THEMES.light
83
+ : THEMES.dark;
84
+ setTheme(nextTheme);
85
+ syncToggle();
86
+ });
87
+ });
88
+ };
89
+
90
+ document.addEventListener('astro:before-swap', (event) => {
91
+ applyTheme(event.newDocument, getActiveTheme());
92
+ });
93
+
94
+ document.addEventListener('astro:page-load', () => {
95
+ syncToggle();
96
+ });
97
+
98
+ syncToggle();
99
+ </script>
package/src/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { default as ThemeToggle } from './components/ThemeToggle.astro';
2
+ export { default as ThemeButton } from './components/ThemeButton.astro';
3
+ export { default as ThemeInit } from './components/ThemeInit.astro';