惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
博客园 - 三生石上(FineUI控件)
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
MongoDB | Blog
MongoDB | Blog
小众软件
小众软件
Y
Y Combinator Blog
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
D
Docker
罗磊的独立博客
Microsoft Security Blog
Microsoft Security Blog
D
DataBreaches.Net
B
Blog
Vercel News
Vercel News
Recent Announcements
Recent Announcements
GbyAI
GbyAI
阮一峰的网络日志
阮一峰的网络日志
T
The Blog of Author Tim Ferriss
H
Hackread – Cybersecurity News, Data Breaches, AI and More
P
Proofpoint News Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
宝玉的分享
宝玉的分享

Stonecharioteer on Tech

I Traced My Traffic Through a Home Tailscale Exit Node What Was I Reading Last? In Three Not-So-Easy Pieces Dogfooding Is Hard Code blocks in your books, finally GoForGo v0.9.0 Merrilin - We built an app to read books I use a Macbook now Data Structures & Algorithms - Preparing for Interviews Using a local DNS namespace for local service discovery Direction KOllector - Publishing KOReader Highlights gbt: branches touched in the last 24 hours A Soiree into Symbols in Ruby Some Smalltalk about Ruby Loops Ruby Blocks Returning from Ruby Blocks, Procs and Lambdas My Linux Laptop Finally Works: How Claude Helped Me Fix Years of Annoyances TIL: Watchexec - Modern File Watching for Development Workflows A Less Busy Mind GoForGo - Learn Go through live examples Migrating My Old Blog to Hugo with Claude The Qtile Window Manager: A Python-Powered Tiling Experience Read the RFCs that Built the Internet Py-x-Protobuf - Or How I Learned to Stop Worrying and Love Protocol Buffers Python Reverse a List New Beginnings Leaving ChainSafe Systems Screen Lock for Cinnamon Desktop using Zenity and Terminal Commands Crews Not Teams A System for Getting Better at LeetCode
TIL: Dark Mode Toggle Web Component by Google Chrome Labs
2021-03-15 · via Stonecharioteer on Tech

Web Components and Dark Mode

Dark Mode Toggle Custom Element

  • GitHub - GoogleChromeLabs/dark-mode-toggle
  • Custom HTML element for adding dark mode toggles to websites
  • Developed by Google Chrome Labs for easy dark mode implementation
  • Standards-based web component that works across frameworks

Key Features

Easy Integration

  • Custom Element: Use as simple HTML tag <dark-mode-toggle>
  • Framework Agnostic: Works with any framework or vanilla HTML
  • No Dependencies: Self-contained web component
  • Accessibility: Built with accessibility best practices

User Experience

  • Persistent Preference: Remembers user’s dark mode choice
  • System Integration: Respects OS-level dark mode preferences
  • Smooth Transitions: Provides smooth visual transitions
  • Customizable: Configurable appearance and behavior

Technical Implementation

  • CSS Custom Properties: Uses CSS variables for theming
  • Local Storage: Persists user preferences across sessions
  • Media Queries: Integrates with prefers-color-scheme
  • Progressive Enhancement: Works without JavaScript as fallback

Dark Mode Best Practices

Design Considerations

  • Color Contrast: Ensure sufficient contrast in both modes
  • Image Handling: Consider image visibility in dark themes
  • Brand Colors: Adapt brand colors for dark backgrounds
  • User Choice: Always allow user override of system preferences

Implementation Patterns

  • CSS Variables: Use custom properties for easy theme switching
  • Semantic Colors: Define colors by purpose, not appearance
  • Component Isolation: Ensure components work in both modes
  • Testing: Test thoroughly in both light and dark modes

Key Takeaways

  • User Experience: Dark mode is becoming an expected feature
  • Web Standards: Custom elements provide reusable, standards-based solutions
  • Accessibility: Consider user preferences and system settings
  • Developer Experience: Well-designed components simplify implementation
  • Progressive Enhancement: Build features that work with or without JavaScript

This component demonstrates how web standards and thoughtful design can make complex features (like theme switching) simple to implement while maintaining excellent user experience and accessibility.