Skip to content

Commit e9d0034

Browse files
committed
make toggle less obtrusive
1 parent 96da859 commit e9d0034

3 files changed

Lines changed: 20 additions & 5 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ An existing template that uses only `@media (prefers-color-scheme: dark)` needs
108108
body { background: var(--page-background); color: var(--page-ink); }
109109
```
110110

111-
Apply the same pattern to all of the template's dark palette variables, including inherited `--jamstacks-*` component fallbacks. With Tailwind, configure its dark variant to use the `.dark` class rather than a media query. The toggle carries its own inline styles and needs no stylesheet import or global CSS rules. Its colours can be customized through `--jamstacks-toggle-background`, `--jamstacks-toggle-dark-background`, `--jamstacks-toggle-border`, `--jamstacks-toggle-color`, and `--jamstacks-toggle-dark-color`. The `style` prop overrides the button's defaults; `className` remains available for template selectors. It preserves the browser's native keyboard-focus outline. Theme changes are instantaneous, with no animation that needs reduced-motion handling.
111+
Apply the same pattern to all of the template's dark palette variables, including inherited `--jamstacks-*` component fallbacks. With Tailwind, configure its dark variant to use the `.dark` class rather than a media query. The toggle shows only its icon on a transparent background; its border and background appear while a mouse or pen hovers over it. It carries its own inline styles and needs no stylesheet import or global CSS rules. Its colours can be customized through `--jamstacks-toggle-background`, `--jamstacks-toggle-dark-background`, `--jamstacks-toggle-border`, `--jamstacks-toggle-color`, and `--jamstacks-toggle-dark-color`. The `style` prop overrides the button's defaults; `className` remains available for template selectors. It preserves the browser's native keyboard-focus outline. Theme changes are instantaneous, with no animation that needs reduced-motion handling.
112112

113113
## Theme API
114114

‎src/components/DarkModeToggle.tsx‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import { forwardRef } from 'react';
3+
import { forwardRef, useState } from 'react';
44
import type { ButtonHTMLAttributes, CSSProperties } from 'react';
55
import { useTheme } from '../theme/ThemeProvider.js';
66
import type { ResolvedTheme, Theme } from '../theme/types.js';
@@ -17,7 +17,8 @@ const buttonStyle: CSSProperties = {
1717
width: '2rem',
1818
height: '2rem',
1919
padding: 0,
20-
border: '1px solid var(--jamstacks-toggle-border, #788579)',
20+
borderWidth: 1,
21+
borderStyle: 'solid',
2122
borderRadius: 999,
2223
outlineOffset: 3,
2324
};
@@ -40,12 +41,15 @@ function nextTheme(theme: Theme, device: ResolvedTheme): Theme {
4041

4142
/** A single tri-state light / dark / system button with encapsulated inline styles; no stylesheet import is needed. */
4243
export const DarkModeToggle = forwardRef<HTMLButtonElement, DarkModeToggleProps>(function DarkModeToggle(
43-
{ className, style, onClick, disabled, title, 'aria-label': label = 'Theme', ...props }, ref,
44+
{ className, style, onClick, onPointerEnter, onPointerLeave, disabled, title, 'aria-label': label = 'Theme', ...props }, ref,
4445
) {
4546
const { theme, resolvedTheme, setTheme, mounted } = useTheme();
4647
const isDark = resolvedTheme === 'dark';
4748
const isDisabled = disabled || !mounted;
4849
const name = `${label}: ${names[theme]}`;
50+
// Inline styles cannot target :hover, so track it. Touch is ignored to avoid a hover state that sticks after a tap.
51+
const [hovered, setHovered] = useState(false);
52+
const showChrome = hovered && !isDisabled;
4953
return (
5054
<button
5155
{...props}
@@ -54,7 +58,9 @@ export const DarkModeToggle = forwardRef<HTMLButtonElement, DarkModeToggleProps>
5458
className={['jamstacks-dark-mode-toggle', className].filter(Boolean).join(' ')}
5559
style={{
5660
...buttonStyle,
57-
background: isDark ? 'var(--jamstacks-toggle-dark-background, #334a3c)' : 'var(--jamstacks-toggle-background, #e5e7eb)',
61+
background: !showChrome ? 'transparent'
62+
: isDark ? 'var(--jamstacks-toggle-dark-background, #334a3c)' : 'var(--jamstacks-toggle-background, #e5e7eb)',
63+
borderColor: showChrome ? 'var(--jamstacks-toggle-border, #788579)' : 'transparent',
5864
color: isDark ? 'var(--jamstacks-toggle-dark-color, #f3f4f6)' : 'var(--jamstacks-toggle-color, #243c2d)',
5965
cursor: isDisabled ? 'default' : 'pointer',
6066
opacity: isDisabled ? .55 : 1,
@@ -64,6 +70,8 @@ export const DarkModeToggle = forwardRef<HTMLButtonElement, DarkModeToggleProps>
6470
title={title ?? name}
6571
data-theme-preference={theme}
6672
disabled={isDisabled}
73+
onPointerEnter={event => { onPointerEnter?.(event); if (event.pointerType !== 'touch') setHovered(true); }}
74+
onPointerLeave={event => { onPointerLeave?.(event); setHovered(false); }}
6775
onClick={event => {
6876
onClick?.(event);
6977
if (!event.defaultPrevented) setTheme(nextTheme(theme, theme === 'system' ? resolvedTheme : deviceTheme()));

‎tests/theme.spec.mjs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,14 @@ test('component styling needs no stylesheet and accepts template customizations'
108108
await ready(page);
109109
const toggle = page.getByTestId('toggle');
110110
await page.evaluate(() => document.documentElement.style.setProperty('--jamstacks-toggle-background', 'rgb(255, 0, 0)'));
111+
await expect(toggle).toHaveCSS('background-color', 'rgba(0, 0, 0, 0)');
112+
await expect(toggle).toHaveCSS('border-top-color', 'rgba(0, 0, 0, 0)');
113+
await toggle.hover();
111114
await expect(toggle).toHaveCSS('background-color', 'rgb(255, 0, 0)');
115+
await expect(toggle).toHaveCSS('border-top-color', 'rgb(120, 133, 121)');
116+
await page.mouse.move(0, 0);
117+
await expect(toggle).toHaveCSS('background-color', 'rgba(0, 0, 0, 0)');
118+
await expect(toggle).toHaveCSS('border-top-color', 'rgba(0, 0, 0, 0)');
112119
const custom = page.getByRole('button', { name: /^Custom toggle: / });
113120
await expect(custom).toHaveCSS('background-color', 'rgb(128, 0, 128)');
114121
await custom.click();

0 commit comments

Comments
 (0)