This plugin allows you to change the theme of your application at runtime. It also allows you to watch for system theme changes and update the theme accordingly.
Install:
npm i vue-daisyui-theme-manager
As you install theme-manager.config.ts
will be automatically created and added to the root folder of your project.
You can set up the available themes in this file. It will be also used as type definition. But don't forget to specify the list of themes in tailwind.config.js
as well. More information about setting up the themes at DaisyUI Themes Setup.
As you insts
Initiate the plugin with the default theme and the dark theme. Theme options are from Daisiy UI themes as well as some custom added themes. Check all the built-in DaisyUI Themes.
Create your own custom daisy ui theme here and add it to the tailwind.config.js
file!
type DaisyThemes = "light" | "default" | "dark" | "cupcake" |
"bumblebee" | "emerald" | "corporate" | "synthwave" | "retro" | "cyberpunk" |
"valentine" | "halloween" | "garden" | 'forest' | 'aqua' | 'lofi' | 'pastel' |
'fantasy' | 'wireframe' | 'black' | 'luxury' | 'dracula' | 'cmyk' | 'autumn' |
'business' | 'acid' | 'lemonade' | 'night' | 'coffee' | | "winter"
export type ThemeOptions = {
light: DaisyThemes
dark: DaisyThemes
watchSystemTheme: boolean
}
createThemeManager(options?: ThemeOptions): (app: App) => void
The light
and dark
options are the default themes that will be used when "toggleDark()
" is called. Or when "set({theme:'default'})
" is being called.
// main.ts
import { createApp } from 'vue'
import App from './App.vue'
import { createThemeManager } from '@/plugins/themeManager'
const app = createApp(App)
app.use(
createThemeManager({
light: 'aqua',
dark: 'coffee',
watchSystemTheme: true,
})
)
app.mount('#app')
const themeManagerInstance = {
set,
get,
toggleDark,
setDefaults,
getDefaults,
watchSystemTheme,
isDark,
}
import { useThemeManager } from '@/plugins/themeManager'
const $theme = useThemeManager()
set
- Set a theme from the daisy theme options defined in thetailwind.config.js
Type definition:usage example:set({theme: DaisyThemes}): void
$theme.set({theme:'light'})
get
- Get the current active theme
Type definition:Usage example:get(): DaisyThemes
$theme.get() // ie: 'coffee'
toggleDark
- Toggle between the default light and dark themes that were defined in the plugin setup
Type definition:Usage example:toggleDark(): void
$theme.toggleDark()
setDefaults
- Set the default light and dark themes after the plugin has been initiated.
Type definition:Usage example:setDefaults(themes: { light?: DaisyThemes; dark?: DaisyThemes }): void
$theme.setDefaults({ light: 'aqua', dark: 'business' })
getDefaults
- Get the default light and dark themes
Type definition:Usage example:getDefaults(): { light: DaisyThemes; dark: DaisyThemes }
$theme.getDefaults() // ie: { light: 'aqua', dark: 'business' }
watchSystemTheme
- Watch for system theme changes, and set if you want it to update the themeimmediately
to the default theme that corresponds to the system mode (light/dark).updateTheme
is set totrue
by default.
Type definition:Usage example:watchSystemTheme(bool?: boolean, updateTheme: boolean = true): boolean
/* Returns if the theme manager watching the active system theme */ $theme.watchSystemTheme() // ie: true /* Theme will change to the default theme that corresponds to the system mode (light/dark) */ $theme.watchSystemTheme(true) /* theme will remain the same even if the system theme changes */ $theme.watchSystemTheme(false) /* theme will not change immidiatly to the default theme that corresponds to the system mode (light/dark) it will change if the theme of the system changes again */ $theme.watchSystemTheme(true, false)
isDark
- Get the current system theme
Type definition:Usage example:isDark(): boolean
$theme.isDark() // ie: true
Contributions to the project is highly appreciated. If you have any suggestions/questions/requests please consider opening an issue. If you want to contribute to the project, fixing an open issue is greatly recommended and appreciated. To see the all contribution rules please check the contribution rules.
This project is licensed under MIT License
if you want to see more, please check LICENSE for more information.
This project is created and actively maintained by kaandesu