Next.js Discord

Discord Forum

How to properly handle logo color mode when switching themes

Answered
Rhinelander posted this in #help-forum
Open in Discord
RhinelanderOP
I don't like approach bellow as it makes logo blink whenever I switch theme - due to not being mounted immedieatly.

But if I remove mounted logo goes to darkmode automaticlly when i refresh no matter theme i select, after refresh it always does the same thing. It works when switching themes but not after refreshing.

"use client";
import { useTheme } from "next-themes";
import Image from "next/image";
import Link from "next/link";
import { useEffect, useState } from "react";

const Logo = () => {
  const { theme } = useTheme();
  const [mounted, setMounted] = useState(false);

  useEffect(() => {
    setMounted(true);
  }, []);

  return (
    <div className="h-10 overflow-hidden">
      <Link href="/">
        {mounted && (
          <Image
            src={
              theme === "dark"
                ? "/vector/logo-light.svg"
                : "/vector/logo-dark.svg"
            }
            alt="Venya"
            width={100}
            height={30}
            priority
            className="h-auto w-auto"
          />
        )}
      </Link>
    </div>
  );
};

export default Logo;
Answered by Lewis's Woodpecker
this. I have a logo of mine in SVG format, and I adjust the fill of the path with CSS variables
View full answer

10 Replies

The best solution is to make a vector based logo if im not mistaken.
@Jboncz The best solution is to make a vector based logo if im not mistaken.
Lewis's Woodpecker
this. I have a logo of mine in SVG format, and I adjust the fill of the path with CSS variables
Answer
@Lewis's Woodpecker this. I have a logo of mine in SVG format, and I adjust the fill of the path with CSS variables
Sometimes it can be really difficult depending on the logo but its always doable 🙂
@Jboncz Sometimes it can be really difficult depending on the logo but its always doable 🙂
Lewis's Woodpecker
my suggestion is to make sure all of your paths are closed, and then union everything into one path. it preserves your shapes while making it so you only have one item to recolor
(this is of course assuming you have a monochromatic logo)
Yeah 🙂 its a good suggestion and I think the correct one at that.
Lewis's Woodpecker
outside of that, if you give each of the paths in the svg an id, you can target the ids and set the fill that way
@Lewis's Woodpecker this. I have a logo of mine in SVG format, and I adjust the fill of the path with CSS variables
RhinelanderOP
Yeah thats the first thing i tought but solution is not pretty as svg in long as f***... i was looking for cleaner solution but if nothing works i will sure do that
RhinelanderOP
I removed fill for now and added id