Improve toggle contrast in monochrome mode

This commit is contained in:
Eason Li 2026-01-06 10:15:37 +08:00
parent ad14fc0dd6
commit 87f17bcf61
4 changed files with 113 additions and 34 deletions

View file

@ -1,14 +1,26 @@
<script setup>
import { Switch } from '@headlessui/vue'
import { ref } from 'vue'
<script setup lang="ts">
import { Switch as HeadlessSwitch } from '@headlessui/vue'
const enabled = ref(false)
const props = defineProps<{
modelValue: boolean
disabled?: boolean
}>()
const emit = defineEmits<{
(event: 'update:modelValue', value: boolean): void
}>()
</script>
<template>
<Switch v-model="enabled" class="switch" :class="{ enabled }">
<HeadlessSwitch
:model-value="props.modelValue"
:disabled="props.disabled"
class="switch"
:class="{ enabled: props.modelValue, disabled: props.disabled }"
@update:modelValue="emit('update:modelValue', $event)"
>
<span class="thumb" />
</Switch>
</HeadlessSwitch>
</template>
<style>
@ -31,10 +43,20 @@ const enabled = ref(false)
}
.switch.disabled {
opacity: 0.5;
opacity: 1;
pointer-events: none;
background-color: var(--vp-c-bg-soft);
border-color: var(--vp-c-divider);
background-color: #2f2f2f;
border-color: #666;
}
.switch.disabled .thumb {
background-color: #000;
box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.1), var(--vp-shadow-1);
}
.dark .switch.disabled {
background-color: #2f2f2f;
border-color: #7d7d7d;
}
</style>
@ -45,12 +67,12 @@ const enabled = ref(false)
.thumb {
display: inline-block;
background-color: #fff;
background-color: #000;
transition: transform 0.25s;
width: 20px;
height: 20px;
border-radius: 50%;
box-shadow: var(--vp-shadow-1);
box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.08), var(--vp-shadow-1);
}
.switch.enabled .thumb {

View file

@ -1,19 +1,53 @@
<script setup lang="ts">
import { onBeforeUnmount, onMounted, ref } from 'vue'
import Switch from './Switch.vue'
const toggleIndexes = () => {
const root = document.documentElement
const enabling = !root.classList.contains('indexes-only')
root.classList.toggle('indexes-only')
const isOn = ref(false)
if (enabling && root.classList.contains('starred-only')) {
root.classList.remove('starred-only')
const syncState = () => {
isOn.value = document.documentElement.classList.contains('indexes-only')
}
let observer: MutationObserver | undefined
onMounted(() =>
(observer = new MutationObserver(syncState)).observe(document.documentElement, {
attributes: true,
attributeFilter: ['class']
})
)
onMounted(syncState)
onBeforeUnmount(() => observer?.disconnect())
const toggleIndexes = (value: boolean) => {
const root = document.documentElement
const enabling = value
const wasStarred = root.classList.contains('starred-only')
root.classList.toggle('indexes-only', enabling)
if (enabling) {
root.dataset.starredWasOn = wasStarred ? 'true' : 'false'
if (wasStarred) {
root.classList.remove('starred-only')
}
} else {
if (root.dataset.starredWasOn === 'true') {
root.classList.add('starred-only')
}
delete root.dataset.starredWasOn
}
isOn.value = enabling
}
</script>
<template>
<Switch @click="toggleIndexes()" />
<Switch v-model="isOn" @update:modelValue="toggleIndexes" />
</template>
<style>

View file

@ -3,43 +3,46 @@ import { onBeforeUnmount, onMounted, ref } from 'vue'
import Switch from './Switch.vue'
const isDisabled = ref(false)
const switchKey = ref(0)
const isOn = ref(false)
const syncDisabled = () => {
const syncState = () => {
const root = document.documentElement
const disabled = root.classList.contains('indexes-only')
isDisabled.value = disabled
if (disabled && root.classList.contains('starred-only')) {
root.classList.remove('starred-only')
switchKey.value += 1
}
isDisabled.value = root.classList.contains('indexes-only')
isOn.value = root.classList.contains('starred-only')
}
let observer: MutationObserver | undefined
onMounted(() =>
(observer = new MutationObserver(syncDisabled)).observe(document.documentElement, {
(observer = new MutationObserver(syncState)).observe(document.documentElement, {
attributes: true,
attributeFilter: ['class']
})
)
onMounted(syncDisabled)
onMounted(syncState)
onBeforeUnmount(() => observer?.disconnect())
const toggleStarred = () => {
if (isDisabled.value) return
document.documentElement.classList.toggle('starred-only')
const toggleStarred = (value: boolean) => {
if (isDisabled.value) {
isOn.value = document.documentElement.classList.contains('starred-only')
return
}
const root = document.documentElement
root.classList.toggle('starred-only', value)
root.dataset.starredWasOn = value ? 'true' : 'false'
isOn.value = value
}
</script>
<template>
<Switch
:key="switchKey"
v-model="isOn"
:disabled="isDisabled"
:class="{ disabled: isDisabled }"
@click="toggleStarred()"
@update:modelValue="toggleStarred"
/>
</template>

View file

@ -88,6 +88,26 @@
img:not(.VPImage) {
filter: grayscale(100%);
}
.switch,
.switch * {
filter: none;
}
.switch .thumb {
background-color: #000 !important;
}
/* Keep sidebar toggles legible in monochrome mode */
.switch,
.switch * {
filter: none;
}
/* Ensure toggle thumb stays black in grayscale */
.switch .thumb {
background-color: #000 !important;
}
}
.vp-doc a {