JM Tech Power

Theme Switcher Feature Update

👁 9 views 👍 1 likes 💬 1 comments
Back to Articles
Theme Switcher Feature Update

Theme Switcher Feature Update

Overview

We've introduced a comprehensive theme switching system that allows users to customize their website appearance by selecting and combining multiple themes. This feature provides a flexible and user-friendly way to personalize the visual experience.

Key Features

🎨 Multi-Theme Support

  • Users can select and combine multiple themes simultaneously using checkboxes
  • Each theme can be toggled independently, allowing for creative theme combinations
  • Themes are applied dynamically without page reload

💾 Persistent Theme Selection

  • User theme preferences are automatically saved to browser's localStorage
  • Selected themes are restored automatically on page load
  • Settings persist across browser sessions

🔧 Easy Theme Management

  • Theme configuration is centralized in config.py
  • Simple JSON-like structure for adding new themes
  • Each theme requires only three properties: id, name, and css_file

🎯 User Interface

  • Intuitive theme selector button (🎨) in the navigation bar
  • Clean dropdown panel with checkbox interface
  • Click outside to close the panel
  • Smooth CSS-based animations for panel display

Files Modified

  1. config.py

    • Added THEMES configuration list
    • Centralized theme definitions with metadata
  2. main.py

    • Imported THEMES from config
    • Injected THEMES into template context via inject_conf_vars()
  3. templates/base.html

    • Added theme selector UI component in navigation
    • Implemented JavaScript theme switching logic
    • Removed inline styles in favor of CSS classes
    • Added theme styles container for dynamic CSS loading
  4. static/style.css

    • Added comprehensive theme selector styles:
      • .theme-selector - Container for theme controls
      • .theme-toggle-btn - Theme button styling
      • .theme-panel - Dropdown panel with show/hide states
      • .theme-panel-header - Panel header styling
      • .theme-panel-body - Panel content area
      • .theme-checkbox-label - Checkbox label styling

File Structure Changes

  • Created static/themes/ directory for theme organization
  • Moved and renamed style2.cssstatic/themes/cyber.css
  • Base style.css remains always loaded as the foundation

How It Works

  1. Theme Loading: The base style.css is always loaded first
  2. Dynamic Loading: Selected theme CSS files are dynamically injected after the base stylesheet
  3. Theme Combination: Multiple theme CSS files can be loaded simultaneously, with later themes overriding earlier ones
  4. Persistence: Selected theme IDs are stored in localStorage and restored on page load

Adding New Themes

To add a new theme, simply add an entry to the THEMES list in config.py:

THEMES = [
    {
        'id': 'your_theme_id',
        'name': 'Your Theme Name',
        'css_file': 'themes/your_theme.css'
    },
]

Then create the corresponding CSS file in static/themes/your_theme.css.

Default Themes Included

  • Dark Theme (themes/dark.css) - A dark color scheme
  • Cyber Theme (themes/cyber.css) - A cyberpunk/futuristic theme with neon colors and tech fonts

Browser Compatibility

  • Modern browsers with localStorage support
  • CSS Grid and Flexbox support required
  • JavaScript must be enabled

Code Quality Improvements

  • Removed inline styles in favor of CSS classes
  • Clean separation of concerns (HTML, CSS, JavaScript)
  • No linter errors or warnings
  • Template syntax properly isolated from JavaScript code

Future Enhancements

  • Theme preview functionality
  • Theme categories/tags
  • User-uploaded custom themes
  • Theme import/export feature

Date: December 19, 2025
Version: 1.0.0
Status: ✅ Production Ready

Comments (1)

KingsonMo
KingsonMo 2025-12-26 08:04
new theme is cool!