Skip to content

feat: add dark/light mode toggle demo#421

Open
Vaibhaav-Tiwari wants to merge 2 commits into
mainfrom
ao/ao-good-31/dark-mode-toggle
Open

feat: add dark/light mode toggle demo#421
Vaibhaav-Tiwari wants to merge 2 commits into
mainfrom
ao/ao-good-31/dark-mode-toggle

Conversation

@Vaibhaav-Tiwari

Copy link
Copy Markdown
Collaborator

Overview

Adds a self-contained dark/light mode toggle demo as a single HTML file.

Features

  • Single HTML file with embedded CSS and JavaScript (no external dependencies)
  • CSS variables for light/dark color themes
  • Pill-shaped toggle switch with smooth sliding animation
  • LocalStorage persistence for user preference
  • Smooth 0.3s transitions on all color changes
  • Centered card design with demo content
  • Keyboard accessible (Enter/Space keys)
  • Sun/moon icons for visual clarity

How to Test

Open index.html directly in any browser — no server needed. Click the toggle to switch between light and dark modes. Refresh to verify localStorage persistence.


🤖 Generated with Claude Code

@Vaibhaav-Tiwari Vaibhaav-Tiwari left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

This PR adds a standalone dark/light mode toggle demo as a single HTML file. The implementation is clean and well-structured for a demo file.

Verdict: Ready with optional improvement

Strengths

  • Self-contained with no external dependencies
  • Good use of CSS variables for theming
  • Smooth transitions (0.3s) on color changes
  • Proper accessibility features (role, aria-label, tabindex, keyboard support)
  • LocalStorage persistence for user preference
  • Clean, readable code structure

Optional Improvement

Missing error handling for localStorage operations

localStorage operations can throw exceptions in certain environments:

  • Safari private browsing mode when quota is exceeded
  • When storage is completely disabled
  • When storage quota is full

Currently, if localStorage.setItem throws, the toggle will still function but the preference won't persist silently. Consider wrapping the localStorage operations in a try-catch:

try {
    localStorage.setItem(STORAGE_KEY, isDark ? 'dark' : 'light');
} catch (e) {
    console.warn('Failed to persist theme preference:', e);
}

This is a minor issue for a demo file, but handling it gracefully would make the component more robust.

Comment thread index.html
<div class="demo-section">
<h2>Theme Demo Content</h2>
<p>
Notice how all colors smoothly transition when you switch themes. The preference is saved to localStorage.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider wrapping this in a try-catch. localStorage.setItem can throw in Safari private browsing mode, when storage is disabled, or when quota is exceeded. The toggle will still function without persistence, but the exception will be uncaught.

Comment thread index.html
class="icon"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same consideration here for localStorage.getItem — though less likely to throw, it could fail in some edge cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant