perf(a11y): add aria-checked attribute to switch

pull/644/head
Percy Ma 3 years ago
parent 9b7ccee3b6
commit acca3a81c3
No known key found for this signature in database
GPG Key ID: A1803D3315E6CCBC

@ -1,50 +1,19 @@
<script lang="ts" setup>
import { APPEARANCE_KEY } from '../../shared'
import VPSwitch from './VPSwitch.vue'
import VPIconSun from './icons/VPIconSun.vue'
import VPIconMoon from './icons/VPIconMoon.vue'
import { useDark, useToggle } from '@vueuse/core'
const toggle = typeof localStorage !== 'undefined' ? useAppearance() : () => {}
function useAppearance() {
const query = window.matchMedia('(prefers-color-scheme: dark)')
const classList = document.documentElement.classList
let userPreference = localStorage.getItem(APPEARANCE_KEY) || 'auto'
let isDark = userPreference === 'auto'
? query.matches
: userPreference === 'dark'
query.onchange = (e) => {
if (userPreference === 'auto') {
setClass((isDark = e.matches))
}
}
function toggle() {
setClass((isDark = !isDark))
userPreference = isDark
? query.matches ? 'auto' : 'dark'
: query.matches ? 'light' : 'auto'
localStorage.setItem(APPEARANCE_KEY, userPreference)
}
function setClass(dark: boolean): void {
classList[dark ? 'add' : 'remove']('dark')
}
return toggle
}
const isDark = useDark()
const toggleDark = useToggle(isDark)
</script>
<template>
<VPSwitch
class="VPSwitchAppearance"
aria-label="toggle dark mode"
@click="toggle"
@click="toggleDark()"
:aria-checked="isDark"
>
<VPIconSun class="sun" />
<VPIconMoon class="moon" />

Loading…
Cancel
Save