Skip to content

Commit ee1f62b

Browse files
committed
chores: refactor the ThemeToggle and the global.css for clarity
1 parent e90b4d3 commit ee1f62b

2 files changed

Lines changed: 178 additions & 532 deletions

File tree

src/components/ui/ThemeToggle.astro

Lines changed: 4 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ const t = useTranslations(lang);
88

99
<button
1010
id="theme-toggle"
11-
class="theme-switch"
11+
class="theme-toggle"
1212
role="switch"
1313
aria-checked="false"
1414
aria-label={t('theme.toggle')}
1515
title={t('theme.toggle')}
1616
>
1717
{/* Sun icon — fixed left */}
18-
<span class="theme-switch__icon theme-switch__icon--sun" aria-hidden="true">
18+
<span class="theme-toggle-icon theme-toggle-sun" aria-hidden="true">
1919
<svg class="h-5 w-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
2020
<path
2121
fill-rule="evenodd"
@@ -27,134 +27,16 @@ const t = useTranslations(lang);
2727
</span>
2828

2929
{/* Moon icon — fixed right */}
30-
<span class="theme-switch__icon theme-switch__icon--moon" aria-hidden="true">
30+
<span class="theme-toggle-icon theme-toggle-moon" aria-hidden="true">
3131
<svg class="h-5 w-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
3232
<path d="M17.293 13.293A8 8 0 016.707 2.707a8.001 8.001 0 1010.586 10.586z"></path>
3333
</svg>
3434
</span>
3535

3636
{/* Sliding knob */}
37-
<span class="theme-switch__knob" aria-hidden="true"></span>
37+
<span class="theme-toggle-knob" aria-hidden="true"></span>
3838
</button>
3939

40-
<style>
41-
.theme-switch {
42-
position: relative;
43-
display: inline-block;
44-
width: 48px;
45-
height: 24px;
46-
border-radius: 9999px;
47-
border: none;
48-
cursor: pointer;
49-
background: linear-gradient(135deg, #e0e7ff, #c7d2fe);
50-
transition:
51-
background 0.3s ease,
52-
box-shadow 0.3s ease;
53-
box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1);
54-
flex-shrink: 0;
55-
margin: 0 0.25rem;
56-
padding: 0;
57-
}
58-
59-
.theme-switch:hover {
60-
box-shadow:
61-
inset 0 1px 3px rgba(0, 0, 0, 0.15),
62-
0 0 0 3px rgba(99, 102, 241, 0.15);
63-
}
64-
65-
.theme-switch:focus-visible {
66-
outline: 2px solid var(--color-primary-500, #6366f1);
67-
outline-offset: 2px;
68-
}
69-
70-
/* Dark mode track */
71-
:global(.dark) .theme-switch {
72-
background: linear-gradient(135deg, #1e293b, #334155);
73-
box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3);
74-
}
75-
76-
:global(.dark) .theme-switch:hover {
77-
box-shadow:
78-
inset 0 1px 3px rgba(0, 0, 0, 0.3),
79-
0 0 0 3px rgba(99, 102, 241, 0.2);
80-
}
81-
82-
/* Icons — absolutely positioned, sized to match knob */
83-
.theme-switch__icon {
84-
position: absolute;
85-
top: 50%;
86-
transform: translateY(-50%);
87-
display: flex;
88-
align-items: center;
89-
justify-content: center;
90-
width: 20px;
91-
height: 20px;
92-
z-index: 1;
93-
transition:
94-
opacity 0.3s ease,
95-
color 0.3s ease;
96-
}
97-
98-
/* Sun icon — pinned left */
99-
.theme-switch__icon--sun {
100-
left: 2px;
101-
color: #374151;
102-
opacity: 0;
103-
}
104-
105-
/* Moon icon — pinned right */
106-
.theme-switch__icon--moon {
107-
right: 2px;
108-
color: #374151; /* matches search & language icons (gray-700) */
109-
opacity: 1;
110-
}
111-
112-
/* Dark mode: show sun (white), hide moon */
113-
:global(.dark) .theme-switch__icon--sun {
114-
opacity: 1;
115-
color: #ffffff;
116-
}
117-
118-
:global(.dark) .theme-switch__icon--moon {
119-
opacity: 0;
120-
}
121-
122-
/* Sliding knob */
123-
.theme-switch__knob {
124-
position: absolute;
125-
top: 2px;
126-
left: 2px;
127-
width: 20px;
128-
height: 20px;
129-
border-radius: 50%;
130-
background: white;
131-
box-shadow:
132-
0 1px 3px rgba(0, 0, 0, 0.2),
133-
0 1px 2px rgba(0, 0, 0, 0.1);
134-
transition:
135-
transform 0.3s cubic-bezier(0.4, 0, 0.2, 1),
136-
background 0.3s ease,
137-
box-shadow 0.3s ease;
138-
z-index: 2;
139-
}
140-
141-
/* Dark mode — knob slides right, white */
142-
:global(.dark) .theme-switch__knob {
143-
transform: translateX(24px);
144-
background: #ffffff;
145-
box-shadow:
146-
0 1px 3px rgba(0, 0, 0, 0.3),
147-
0 1px 2px rgba(0, 0, 0, 0.15);
148-
}
149-
150-
@media (prefers-reduced-motion: reduce) {
151-
.theme-switch__knob,
152-
.theme-switch__icon {
153-
transition: none;
154-
}
155-
}
156-
</style>
157-
15840
<script>
15941
document.addEventListener('astro:page-load', () => {
16042
const themeToggle = document.getElementById('theme-toggle');

0 commit comments

Comments
 (0)