Skip to content

Latest commit

 

History

History
114 lines (91 loc) · 4.21 KB

File metadata and controls

114 lines (91 loc) · 4.21 KB

Frontend Changes - Theme Toggle Feature

Overview

Added a dark/light theme toggle feature to the Course Materials Assistant application. Users can now switch between dark and light themes with a smooth transition animation.

Files Modified

1. frontend/index.html

Changes:

  • Added theme toggle button with sun and moon SVG icons
  • Button positioned at the top-right of the page
  • Includes proper ARIA labels for accessibility
  • Button is keyboard-navigable

Location: Lines 13-29

2. frontend/style.css

Changes:

Light Theme Variables (Lines 27-43)

  • Added [data-theme="light"] selector with light theme color variables:
    • Background: #f8fafc (light gray)
    • Surface: #ffffff (white)
    • Text primary: #0f172a (dark)
    • Text secondary: #475569 (medium gray)
    • Border color: #e2e8f0 (light gray)
    • Assistant message background: #f1f5f9 (very light gray)

Smooth Transitions (Lines 58-66)

  • Added transition properties to all elements for smooth theme switching
  • Transitions apply to: background-color, color, border-color, box-shadow
  • Transition duration: 0.3s with ease timing

Theme Toggle Button Styles (Lines 744-804)

  • Fixed position in top-right corner (1.5rem from top and right)
  • Circular button (48px diameter)
  • Hover effects: scale up, border color change
  • Focus states for accessibility
  • Icon rotation animation on hover
  • Conditional icon display based on theme (sun for light, moon for dark)

3. frontend/script.js

Changes:

Theme Functions (Lines 27-56)

  • initializeTheme(): Initializes theme on page load

    • Checks localStorage for saved preference
    • Falls back to system preference (prefers-color-scheme)
    • Defaults to dark theme if no preference found
  • toggleTheme(): Switches between light and dark themes

  • setTheme(theme): Sets the theme by updating data-theme attribute

    • Saves preference to localStorage for persistence

Event Listeners (Lines 59-69)

  • Added click listener for theme toggle button
  • Added keyboard navigation (Enter/Space keys)
  • Theme preference persists across sessions

DOM Elements (Line 8)

  • Added themeToggle to global DOM elements

Features Implemented

1. Toggle Button Design

  • Icon-based design with sun (light mode) and moon (dark mode) icons
  • Positioned in top-right corner (fixed position)
  • Smooth rotation animation on hover
  • Accessible and keyboard-navigable (Tab, Enter, Space)
  • Visual feedback on hover, focus, and active states

2. Light Theme Colors

  • Light background colors for improved readability in bright environments
  • Dark text for good contrast (WCAG AA compliant)
  • Adjusted border and surface colors
  • Maintains visual hierarchy and design language

3. JavaScript Functionality

  • Click to toggle between themes
  • Keyboard support (Enter and Space keys)
  • Smooth 0.3s transitions for all color changes
  • Theme preference saved to localStorage
  • Respects system theme preference on first visit

4. Implementation Details

  • Uses CSS custom properties (CSS variables) for theme switching
  • data-theme="light" attribute on <html> element for light theme
  • Dark theme is default (no attribute needed)
  • All existing elements work well in both themes
  • Maintains current visual hierarchy and design language

Testing

To test the theme toggle:

  1. Open the application in a browser
  2. Click the theme toggle button in the top-right corner
  3. Verify smooth transition between dark and light themes
  4. Test keyboard navigation (Tab to button, Enter/Space to toggle)
  5. Refresh the page - theme preference should persist
  6. Check that all UI elements are visible and readable in both themes

Accessibility Features

  • Proper ARIA labels (aria-label="Toggle theme")
  • Keyboard navigation support
  • Focus indicators for keyboard users
  • Good color contrast in both themes
  • Smooth transitions don't trigger motion sickness (0.3s is within acceptable range)

Browser Compatibility

  • Works in all modern browsers (Chrome, Firefox, Safari, Edge)
  • CSS custom properties are widely supported
  • localStorage is universally available
  • SVG icons are supported in all modern browsers