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

View file

@ -1,19 +1,53 @@
<script setup lang="ts"> <script setup lang="ts">
import { onBeforeUnmount, onMounted, ref } from 'vue'
import Switch from './Switch.vue' import Switch from './Switch.vue'
const toggleIndexes = () => { const isOn = ref(false)
const root = document.documentElement
const enabling = !root.classList.contains('indexes-only')
root.classList.toggle('indexes-only')
if (enabling && root.classList.contains('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') root.classList.remove('starred-only')
} }
} else {
if (root.dataset.starredWasOn === 'true') {
root.classList.add('starred-only')
}
delete root.dataset.starredWasOn
}
isOn.value = enabling
} }
</script> </script>
<template> <template>
<Switch @click="toggleIndexes()" /> <Switch v-model="isOn" @update:modelValue="toggleIndexes" />
</template> </template>
<style> <style>

View file

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

View file

@ -88,6 +88,26 @@
img:not(.VPImage) { img:not(.VPImage) {
filter: grayscale(100%); 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 { .vp-doc a {