slidev icon indicating copy to clipboard operation
slidev copied to clipboard

Check if dark theme is enabled to conditionally render images

Open dserv-nh opened this issue 3 years ago • 5 comments

Is your feature request related to a problem? Please describe. When I have an image with black text, it only looks good in light mode and vice-versa.

Describe the solution you'd like Something along the lines of $slidev.isDark

Describe alternatives you've considered Helper image component that has two sources for the light and dark theme

dserv-nh avatar Jun 13 '22 12:06 dserv-nh

You can do that programmatically already 😋

See eg. https://github.com/slidevjs/slidev/discussions/85#discussioncomment-727923

From there, you can build your own image wrapper to switch out images depending on the set mode.

TheAlexLichter avatar Jun 13 '22 14:06 TheAlexLichter

@manniL Thanks, that worked.

I've created this simple component

<script setup lang="ts">
import { isDark } from '@slidev/client/logic/dark';

const props = defineProps<{
  dark: string;
  light: string;
}>();
</script>

<template>
  <img :src="isDark ? props.dark : props.light" v-bind="$attrs" />
</template>

But I still think something like this should be a native feature, as it's a quite common problem when using images with black/white text.

dserv-nh avatar Jun 15 '22 10:06 dserv-nh

If we find a good name for it, this component might probably be integrated in slidev. What name did you use?

twitwi avatar Jun 23 '22 15:06 twitwi

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Sep 14 '22 16:09 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Nov 14 '22 08:11 stale[bot]

Not stale ☺️

TheAlexLichter avatar Nov 14 '22 09:11 TheAlexLichter

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jan 13 '23 09:01 stale[bot]

Nooooo

TheAlexLichter avatar Jan 13 '23 13:01 TheAlexLichter

Hello, I push a commit contaning a component named LightOrDark. You can used it to render something different between the light and dark modes (it can be anything, not only images).

Simple usage:

<LightOrDark>
  <template #dark>Dark mode is on</template>
  <template #light>Light mode is on</template>
</LightOrDark>

Render some images:

<LightOrDark width="100" alt="some image">
  <template #dark="props">
    <img src="https://cdn-icons-png.flaticon.com/512/7645/7645197.png" v-bind="props"/>
  </template>
  <template #light="props">
    <img src="https://cdn-icons-png.flaticon.com/512/1687/1687686.png" v-bind="props"/>
  </template>
</LightOrDark>

You can get the props passed to the LightOrDark component and reuse them inside the slot by using scoped slot props: https://vuejs.org/guide/components/slots.html#scoped-slots

With markdown (we need blank spaces between the template and the markdown):

<LightOrDark>
  <template #dark>
  
![dark](https://cdn-icons-png.flaticon.com/512/7645/7645197.png)

  </template>
  <template #light>
  
![light](https://cdn-icons-png.flaticon.com/512/1687/1687686.png)

  </template>
</LightOrDark>

Example: light dark

This is available on newly published 0.38.4 version

tonai avatar Jan 20 '23 13:01 tonai