Files
InvoiceShelf/resources/scripts-v2/composables/use-theme.ts
Darko Gjorgjijoski a46cca5cd8 Complete scripts-v2 TypeScript migration — all imports resolved,
build passes

Create all missing components (modals, dropdowns, icons, tabs, mail
drivers, customer partials), fix all @/scripts/ imports to @v2/,
wire up vite entry point and blade template. 382 files, 48883 lines.

- 27 settings components: modals (tax, payment, custom field, note,
  category, role, exchange rate, unit, mail test), dropdowns (6),
  customization tabs (4), mail driver forms (4)
- 22 icon components: 5 utility icons, 4 dashboard icons, 13 editor
  toolbar icons with typed barrel export
- 3 customer components: info, chart placeholder, custom fields single
- Fixed usePopper composable, client/format-money import patterns
- Zero remaining @/scripts/ imports in scripts-v2/

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-04 09:30:00 +02:00

93 lines
2.3 KiB
TypeScript

import { ref, onMounted, onUnmounted } from 'vue'
import type { Ref } from 'vue'
import { THEME, LS_KEYS } from '@v2/config/constants'
import type { Theme } from '@v2/config/constants'
import * as ls from '../utils/local-storage'
export interface UseThemeReturn {
currentTheme: Ref<Theme>
setTheme: (theme: Theme) => void
applyTheme: (theme?: Theme) => void
}
const currentTheme = ref<Theme>(
(ls.get<string>(LS_KEYS.THEME) as Theme) ?? THEME.SYSTEM
)
let mediaQueryCleanup: (() => void) | null = null
/**
* Apply the correct data-theme attribute to the document element.
*
* @param theme - The theme to apply (light, dark, or system)
*/
function applyThemeToDocument(theme: Theme): void {
const prefersDark =
theme === THEME.DARK ||
(theme === THEME.SYSTEM &&
window.matchMedia('(prefers-color-scheme: dark)').matches)
if (prefersDark) {
document.documentElement.setAttribute('data-theme', 'dark')
} else {
document.documentElement.removeAttribute('data-theme')
}
}
/**
* Composable for managing the application theme (light/dark/system).
* Extracted from TheSiteHeader. Listens for system preference changes
* when set to "system" mode.
*/
export function useTheme(): UseThemeReturn {
/**
* Set and persist the current theme.
*
* @param theme - The theme to set
*/
function setTheme(theme: Theme): void {
currentTheme.value = theme
ls.set(LS_KEYS.THEME, theme)
applyThemeToDocument(theme)
}
/**
* Apply the given or current theme to the document.
*
* @param theme - Optional theme override; uses currentTheme if not provided
*/
function applyTheme(theme?: Theme): void {
applyThemeToDocument(theme ?? currentTheme.value)
}
function handleSystemThemeChange(): void {
if (currentTheme.value === THEME.SYSTEM) {
applyThemeToDocument(THEME.SYSTEM)
}
}
onMounted(() => {
applyThemeToDocument(currentTheme.value)
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)')
mediaQuery.addEventListener('change', handleSystemThemeChange)
mediaQueryCleanup = () => {
mediaQuery.removeEventListener('change', handleSystemThemeChange)
}
})
onUnmounted(() => {
if (mediaQueryCleanup) {
mediaQueryCleanup()
mediaQueryCleanup = null
}
})
return {
currentTheme,
setTheme,
applyTheme,
}
}