# devloker/theme-switcher architecture

> Architecture diagram of the GitHub repository devloker/theme-switcher, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-07-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/devloker/theme-switcher
- Repository: https://github.com/devloker/theme-switcher

## Overview

##### Project type and purpose
- This is a browser extension (specifically a Chrome extension) named “Dark Light Mode Switcher.”

##### File structure
- Top-level files:
- package.json, package-lock.json, tsconfig*.json, vite.config.ts (build & config)
- index.html (extension’s popup host)
- src/: main code
- src/Popup.tsx + src/main.tsx — React-based popup UI
- src/scripts/injection/content.ts — content script injected into every page
- src/scripts/service-worker/background.ts — persistent background/service worker script
- src/constants, src/types, src/utils, src/styles — shared assets & type definitions
- Build output (dist/) is generated by Vite via `npm run build`.

##### README for additional insights
- Architecture overview is implicit:
- React popup for user interaction
- Background script to coordinate state or messaging
- Key tech: Vite, React, TypeScript, Chrome extension APIs, CSS filter hack.

a. Main components
- Browser (Chrome)
- Extension Popup (React UI)
- Background Service Worker (event-driven script)
- Content Script (injected into web pages)
- Chrome Extension APIs (messaging, storage)
b. Relationships & interactions
2. Popup UI sends a message to Background (chrome.runtime.sendMessage)
3. Background script receives toggle request → stores state (optional) → broadcasts to all tabs or current tab
4. Content script listens for messages → applies or removes CSS `filter: invert` on document.body
c. Architectural patterns & design principles
- Event-driven / message-passing between components
- Separation of concerns: UI logic (popup) vs page logic (content script) vs orchestration (background)
- Modular code: shared constants/types/utils imported by multiple components
d. Relevant technologies & libraries
- Chrome Extension Manifest v3 (service worker)
- React + TypeScript + Vite for building the popup
- CSS filter inversion hack
- Chrome runtime messaging & storage APIs

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Main Browser Extension Components
    subgraph "Chrome Browser"
        direction TB
        User["User"]:::external
        Popup["Extension Popup (React UI)"]:::ui
        Background["Background Service Worker"]:::logic
        Content["Content Script"]:::script
        API["Chrome Extension APIs"]:::api
        Manifest["Manifest & Permissions"]:::api
        Icons["Extension Icons"]:::assets
    end

    %% Shared Assets
    subgraph "Shared Assets"
        direction TB
        Constants["Theme Constants"]:::assets
        TypesMsg["Message Types"]:::assets
        TypesTheme["Theme Types"]:::assets
        Utils["Utilities"]:::assets
        Styles["Popup Styles"]:::assets
    end

    %% Build Pipeline
    subgraph "Build Pipeline"
        direction TB
        Source["Source Code (src/)"]:::build
        Vite["Vite Build"]:::build
        Dist["Build Output (dist/)"]:::build
        LoadUnpacked["Chrome Load Unpacked"]:::build
    end

    %% Page Context
    Page["Page DOM"]:::script

    %% Connections
    User -->|"click icon"| Popup
    Popup -->|"sendMessage(toggle)"| Background
    Background -->|"broadcast toggle"| Content
    Content -->|"apply/remove CSS filter"| Page

    Popup -->|"storage.get/set"| API
    Background -->|"storage.set/get"| API
    Content -->|"tabs.sendMessage"| API

    Manifest -->|loads| Popup
    Manifest -->|loads| Background
    Manifest -->|loads| Content
    Icons -->|declared in| Manifest

    Popup -->|imports| Constants
    Popup -->|imports| TypesMsg
    Popup -->|imports| TypesTheme
    Popup -->|imports| Utils
    Popup -->|imports| Styles

    Background -->|imports| Constants
    Background -->|imports| TypesMsg
    Background -->|imports| Utils

    Content -->|imports| Constants
    Content -->|imports| TypesMsg
    Content -->|imports| Utils

    %% Build pipeline connections
    Source -->|"npm run build"| Vite
    Vite -->|outputs| Dist
    Dist -->|load unpacked| LoadUnpacked
    LoadUnpacked -->|installs extension| User

    %% Click Events
    click Popup "https://github.com/devloker/theme-switcher/blob/main/src/Popup.tsx"
    click Background "https://github.com/devloker/theme-switcher/blob/main/src/scripts/service-worker/background.ts"
    click Content "https://github.com/devloker/theme-switcher/blob/main/src/scripts/injection/content.ts"
    click Manifest "https://github.com/devloker/theme-switcher/blob/main/public/manifest.json"
    click Icons "https://github.com/devloker/theme-switcher/tree/main/public/icons/"
    click Source "https://github.com/devloker/theme-switcher/tree/main/src/"
    click Constants "https://github.com/devloker/theme-switcher/blob/main/src/constants/theme.ts"
    click TypesMsg "https://github.com/devloker/theme-switcher/blob/main/src/types/TMessage.ts"
    click TypesTheme "https://github.com/devloker/theme-switcher/blob/main/src/types/TTheme.ts"
    click Utils "https://github.com/devloker/theme-switcher/blob/main/src/utils/helpers.ts"
    click Styles "https://github.com/devloker/theme-switcher/blob/main/src/styles/index.css"
    click Vite "https://github.com/devloker/theme-switcher/blob/main/vite.config.ts"
    click Vite "https://github.com/devloker/theme-switcher/blob/main/tsconfig.app.json"
    click Vite "https://github.com/devloker/theme-switcher/blob/main/tsconfig.json"
    click Vite "https://github.com/devloker/theme-switcher/blob/main/tsconfig.node.json"
    click Vite "https://github.com/devloker/theme-switcher/blob/main/package.json"
    click LoadUnpacked "https://github.com/devloker/theme-switcher/tree/main/screenshots/"

    %% Styles
    classDef ui fill:#AEDFF7,stroke:#333,stroke-width:1px
    classDef logic fill:#C2F0C2,stroke:#333,stroke-width:1px
    classDef script fill:#E5C1F7,stroke:#333,stroke-width:1px
    classDef api fill:#F9E79F,stroke:#333,stroke-width:1px
    classDef assets fill:#F5B7B1,stroke:#333,stroke-width:1px
    classDef external fill:#D5DBDB,stroke:#333,stroke-width:1px
    classDef build fill:#D6EAF8,stroke:#333,stroke-width:1px
```
