Replacing svg images in dark mode #8001
-
I have an SVG image with a green tickmark and a * for conditions apply. So in dark mode, the * needs to change color, I assume we need to replace the image and I can create a new image. But how to replace the image in dark mode? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
If your SVG is an external import ThemedImage from '@theme/ThemedImage';
<ThemedImage
alt="Docusaurus themed image"
sources={{
light: useBaseUrl('/img/docusaurus_light.svg'),
dark: useBaseUrl('/img/docusaurus_dark.svg'),
}}
/>; https://docusaurus.io/docs/markdown-features/assets#github-style-themed-images If you don't want to have duplicate svg.mySvg .mySvgPath {
fill: red;
}
[data-theme='dark'] svg.mySvg .mySvgPath {
fill: blue;
} If your SVG is a React component you can do: import React from 'react';
import {useColorMode} from '@docusaurus/theme-common';
export default function IconArrow(props) {
const {colorMode} = useColorMode();
const mySvgColor = colorMode === "dark" ? "red" : "blue";
return (
<svg width="20" height="20" {...props}>
<g fill={mySvgColor}>
<path d="M9.992 10.023c0..." />
</g>
</svg>
);
} Note: I don't recommend much this one because it can produce "hydration FOUC": on first page load the |
Beta Was this translation helpful? Give feedback.
-
Sorry to not share my result. I tried @slorber's method and it worked. So I have also used another method because I needed this for a table. And my use case is You can see results here: https://docs.dojo.tech/payments/manage-payments/cancellation-payments/reversal Sorry I'm not too technical but know the basics that a technical writer must know and I'm still learning. Thanks! |
Beta Was this translation helpful? Give feedback.
If your SVG is an external
.svg
file, you can do:https://docusaurus.io/docs/markdown-features/assets#github-style-themed-images
If you don't want to have duplicate
.svg
files, you can target SVG elements with custom CSSIf your SVG is a React component you can do: