Skip to content

Commit

Permalink
feat: use css to show dark images
Browse files Browse the repository at this point in the history
  • Loading branch information
djdjz7 committed Feb 9, 2025
1 parent a289013 commit 80ab016
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions src/components/AutoDarkImage.vue
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
<script setup lang="ts">
import { usePreferredDark } from '@vueuse/core'
import { onMounted, ref, watch } from 'vue'
const props = defineProps<{
src: string
srcDark?: string
alt?: string
}>()
const url = ref('')
const isDark = usePreferredDark()
onMounted(() => {
watch(
isDark,
(newVal) => {
const src = newVal ? (props.srcDark ?? props.src) : props.src
url.value = src
},
{ immediate: true },
)
})
const srcDarkValue = `url(${props.srcDark || props.src})`
</script>

<template>
<img :src="url" :alt="alt" />
<img class="auto-dark-img" :src="props.src" :alt="alt" />
</template>

<style lang="css" scoped>
@media (prefers-color-scheme: dark) {
.auto-dark-img {
content: v-bind(srcDarkValue);
}
}
</style>

0 comments on commit 80ab016

Please sign in to comment.