Skip to content

Latest commit

 

History

History
135 lines (100 loc) · 4.32 KB

File metadata and controls

135 lines (100 loc) · 4.32 KB

Time-Based Theme Implementation

Overview

Implemented automatic theme switching based on the current browser time to provide users with the most appropriate theme for their current time of day.

Time Schedule

  • Light Theme: 7:00 AM to 6:59 PM (7am - 7pm)
  • Dark Theme: 7:00 PM to 6:59 AM (7pm - 7am)

Implementation Details

Core Logic

The theme switching uses the browser's current time (new Date().getHours()) to determine whether it's daytime (7am-7pm) or nighttime (7pm-7am).

Key Features

1. Automatic Theme Detection

  • New Users: Auto theme is enabled by default
  • Time-Based Logic: Uses new Date().getHours() to determine current time
  • Smart Fallback: Falls back to system preference if auto theme is disabled

2. User Control

  • Explicit Theme Setting: If user manually sets a theme, it overrides auto theme
  • Auto Theme Toggle: Users can enable/disable auto theme switching
  • Persistent Settings: All preferences saved in localStorage

3. Real-Time Updates

  • Minute-by-Minute Checking: Checks for theme changes every minute
  • Automatic Switching: Seamlessly switches themes when time changes
  • Console Logging: Detailed logging for debugging

Functions Added

// Enable auto theme switching
enableAutoTheme()

// Disable auto theme switching  
disableAutoTheme()

// Check if auto theme is enabled
isAutoThemeEnabled()

// Get current time-based theme
getTimeBasedTheme()

// Apply time-based theme if enabled
applyTimeBasedTheme()

// Initialize auto theme checking
initializeAutoTheme()

// Get time info for debugging
getTimeInfo()

Files Modified

src/lib/stores/themeStore.ts

  • Updated getInitialTheme() function to include time-based logic
  • Added auto theme enable/disable functions
  • Added time-based theme calculation functions
  • Added periodic theme checking with setInterval()
  • Added debugging functions for time information

src/routes/+page.svelte

  • Imported initializeAutoTheme function
  • Added initializeAutoTheme() call in onMount() lifecycle

How It Works

  1. App Startup: initializeAutoTheme() is called on page load
  2. Time Check: Every minute, checks if current time requires theme change
  3. Automatic Switch: If time has changed (e.g., 7pm → dark theme), switches automatically
  4. User Override: Manual theme selection takes precedence over auto theme
  5. Persistence: Settings are saved and restored on page reload

User Experience

  • Seamless: Theme changes happen automatically without user intervention
  • Respectful: Manual theme selection is preserved
  • Smart: New users get the optimal theme for their current time
  • Consistent: Works across all pages and browser sessions

Technical Implementation

Theme Priority Order

  1. User explicitly set theme (highest priority)
  2. Auto theme based on current time
  3. System preference (light/dark mode)
  4. Default dark theme (fallback)

localStorage Keys

  • holmes-theme: User's explicitly set theme
  • holmes-auto-theme: Auto theme setting ("enabled" or null)

Time Calculation

const currentHour = new Date().getHours();
const isDaytime = currentHour >= 7 && currentHour < 19; // 7am to 7pm
const timeBasedTheme = isDaytime ? "light" : "dark";

Benefits

  1. Improved UX: Users get appropriate themes for their current time
  2. Reduced Eye Strain: Light theme during day, dark theme at night
  3. Automatic: No manual intervention required
  4. Flexible: Users can still override if desired
  5. Performance: Efficient checking with 1-minute intervals

Future Enhancements

  • Geolocation: Use user's location for more accurate sunrise/sunset times
  • Customizable Hours: Allow users to set their preferred light/dark transition times
  • Smooth Transitions: Add CSS transitions for theme changes
  • System Integration: Better integration with OS-level dark mode settings

Testing

The implementation includes comprehensive console logging for debugging:

  • Theme selection decisions
  • Time-based calculations
  • Auto theme status
  • Theme change events

Commit Information

Date: December 19, 2024 Feature: Time-based automatic theme switching Files Modified:

  • src/lib/stores/themeStore.ts
  • src/routes/+page.svelte New Files:
  • docs/TIME_BASED_THEME_IMPLEMENTATION.md