Files
InvoiceShelf/resources/scripts/composables/use-theme.ts
Darko Gjorgjijoski 71388ec6a5 Rename resources/scripts-v2 to resources/scripts and drop @v2 alias
Now that the legacy v1 frontend (commit 064bdf53) is gone, the v2 directory is the only frontend and the v2 suffix is just noise. Renames resources/scripts-v2 to resources/scripts via git mv (so git records the move as renames, preserving blame and log --follow), then bulk-rewrites the 152 files that imported via @v2/... to use @/scripts/... instead. The existing @ alias (resources/) covers the new path with no extra config needed.

Drops the now-unused @v2 alias from vite.config.js and points the laravel-vite-plugin entry at resources/scripts/main.ts. Updates the only blade reference (resources/views/app.blade.php) to match. The package.json test script (eslint ./resources/scripts) automatically targets the right place after the rename without any edit.

Verified: npm run build exits clean and the Vite warning lines now reference resources/scripts/plugins/i18n.ts, confirming every import resolved through the new path. git log --follow on any moved file walks back through its scripts-v2 history.
2026-04-07 12:50:16 +02:00

93 lines
2.3 KiB
TypeScript

import { ref, onMounted, onUnmounted } from 'vue'
import type { Ref } from 'vue'
import { THEME, LS_KEYS } from '@/scripts/config/constants'
import type { Theme } from '@/scripts/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,
}
}