Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useDark.md
1---2category: Browser3related:4- useColorMode5- usePreferredDark6- useStorage7---89# useDark1011Reactive dark mode with auto data persistence.1213<CourseLink href="https://vueschool.io/lessons/theming-with-vueuse-usedark-and-usecolormode?friend=vueuse">Learn useDark with this FREE video lesson from Vue School!</CourseLink>1415## Basic Usage1617```ts18import { useDark, useToggle } from '@vueuse/core'1920const isDark = useDark()21const toggleDark = useToggle(isDark)22```2324## Behavior2526`useDark` combines with `usePreferredDark` and `useStorage`. On start up, it reads the value from localStorage/sessionStorage (the key is configurable) to see if there is a user configured color scheme, if not, it will use users' system preferences. When you change the `isDark` ref, it will update the corresponding element's attribute and then store the preference to storage (default key: `vueuse-color-scheme`) for persistence.2728> Please note `useDark` only handles the DOM attribute changes for you to apply proper selector in your CSS. It does NOT handle the actual style, theme or CSS for you.2930## Configuration3132By default, it uses [Tailwind CSS favored dark mode](https://tailwindcss.com/docs/dark-mode#toggling-dark-mode-manually), which enables dark mode when class `dark` is applied to the `html` tag, for example:3334```html35<!--light-->36<html>37...38</html>3940<!--dark-->41<html class="dark">42...43</html>44```4546Still, you can also customize it to make it work with most CSS frameworks.4748For example:4950```ts51import { useDark } from '@vueuse/core'52// ---cut---53const isDark = useDark({54selector: 'body',55attribute: 'color-scheme',56valueDark: 'dark',57valueLight: 'light',58})59```6061will work like6263```html64<!--light-->65<html>66<body color-scheme="light">67...68</body>69</html>7071<!--dark-->72<html>73<body color-scheme="dark">74...75</body>76</html>77```7879If the configuration above still does not fit your needs, you can use the`onChanged` option to take full control over how you handle updates.8081```ts82import { useDark } from '@vueuse/core'83// ---cut---84const isDark = useDark({85onChanged(dark) {86// update the dom, call the API or something87},88})89```9091## Component Usage9293```vue94<template>95<UseDark v-slot="{ isDark, toggleDark }">96<button @click="toggleDark()">97Is Dark: {{ isDark }}98</button>99</UseDark>100</template>101```102103## Type Declarations104105```ts106export interface UseDarkOptions extends Omit<107UseColorModeOptions<BasicColorSchema>,108"modes" | "onChanged"109> {110/**111* Value applying to the target element when isDark=true112*113* @default 'dark'114*/115valueDark?: string116/**117* Value applying to the target element when isDark=false118*119* @default ''120*/121valueLight?: string122/**123* A custom handler for handle the updates.124* When specified, the default behavior will be overridden.125*126* @default undefined127*/128onChanged?: (129isDark: boolean,130defaultHandler: (mode: BasicColorSchema) => void,131mode: BasicColorSchema,132) => void133}134export type UseDarkReturn = WritableComputedRef<boolean>135/**136* Reactive dark mode with auto data persistence.137*138* @see https://vueuse.org/useDark139* @param options140*/141export declare function useDark(options?: UseDarkOptions): UseDarkReturn142```143