mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
refactor(web); align ThemeImage with next.js docs
This commit is contained in:
parent
e246932820
commit
3fd6756201
5 changed files with 42 additions and 39 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import CollinMBarrettIconLight from "./collinmbarrett-icon-light.png";
|
||||
import CollinMBarrettIconDark from "./collinmbarrett-icon-dark.png";
|
||||
import { ImageThemed } from "@/components/image-themed";
|
||||
import { ThemeImage } from "@/components/theme-image";
|
||||
|
||||
export function CollinMBarrettLink() {
|
||||
return (
|
||||
|
|
@ -10,7 +10,7 @@ export function CollinMBarrettLink() {
|
|||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
<ImageThemed
|
||||
<ThemeImage
|
||||
aria-hidden
|
||||
srcLight={CollinMBarrettIconLight}
|
||||
srcDark={CollinMBarrettIconDark}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import GitHubMarkLight from "./github-mark-light.svg";
|
||||
import GitHubMarkDark from "./github-mark-dark.svg";
|
||||
import { ImageThemed } from "@/components/image-themed";
|
||||
import { ThemeImage } from "@/components/theme-image";
|
||||
|
||||
export function GitHubLink() {
|
||||
return (
|
||||
|
|
@ -10,7 +10,7 @@ export function GitHubLink() {
|
|||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
<ImageThemed
|
||||
<ThemeImage
|
||||
aria-hidden
|
||||
srcLight={GitHubMarkLight}
|
||||
srcDark={GitHubMarkDark}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { ImageThemed } from "@/components/image-themed";
|
||||
import { ThemeImage } from "@/components/theme-image";
|
||||
import { ThemeToggle } from "../theme-toggle";
|
||||
import Link from "next/link";
|
||||
import LogoLight from "./logo-filterlists-light.png";
|
||||
|
|
@ -11,7 +11,7 @@ export function Header() {
|
|||
<div className="flex h-20 items-center justify-between px-6">
|
||||
<div className="flex items-center">
|
||||
<Link href="/" aria-label="Go to homepage">
|
||||
<ImageThemed
|
||||
<ThemeImage
|
||||
className={styles.logoHeader}
|
||||
srcLight={LogoLight}
|
||||
srcDark={logoDark}
|
||||
|
|
|
|||
|
|
@ -1,33 +0,0 @@
|
|||
"use client";
|
||||
|
||||
import { useState, useEffect } from "react";
|
||||
import Image, { type ImageProps, type StaticImageData } from "next/image";
|
||||
import { useTheme } from "next-themes";
|
||||
|
||||
type ThemedImageProps = Omit<ImageProps, "src" | "alt"> & {
|
||||
srcLight: string | StaticImageData;
|
||||
srcDark: string | StaticImageData;
|
||||
alt: string;
|
||||
};
|
||||
|
||||
export function ImageThemed({
|
||||
srcLight,
|
||||
srcDark,
|
||||
alt,
|
||||
...props
|
||||
}: ThemedImageProps) {
|
||||
const { resolvedTheme } = useTheme();
|
||||
const [mounted, setMounted] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
setMounted(true);
|
||||
}, []);
|
||||
|
||||
const src = resolvedTheme === "dark" ? srcDark : srcLight;
|
||||
|
||||
if (!mounted) {
|
||||
return <Image src={srcLight} alt={alt} {...props} />;
|
||||
}
|
||||
|
||||
return <Image src={src} alt={alt} {...props} />;
|
||||
}
|
||||
36
web/src/components/theme-image.tsx
Normal file
36
web/src/components/theme-image.tsx
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
"use client";
|
||||
|
||||
import { useTheme } from "next-themes";
|
||||
import Image, { ImageProps, StaticImageData } from "next/image";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
type Props = Omit<ImageProps, "src" | "loading"> & {
|
||||
srcLight: string | StaticImageData;
|
||||
srcDark: string | StaticImageData;
|
||||
};
|
||||
|
||||
export const ThemeImage = (props: Props) => {
|
||||
const { srcLight, srcDark, alt, priority, ...rest } = props;
|
||||
const { resolvedTheme } = useTheme();
|
||||
const [mounted, setMounted] = useState(false);
|
||||
|
||||
useEffect(() => setMounted(true), []);
|
||||
|
||||
if (!mounted) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (
|
||||
process.env.NODE_ENV === "development" &&
|
||||
priority === true &&
|
||||
alt !== "FilterLists logo"
|
||||
) {
|
||||
console.warn(
|
||||
`ThemeImage: [alt="${alt}"] Using priority will cause both light and dark images to be loaded. Only use for LCP images.`,
|
||||
);
|
||||
}
|
||||
|
||||
const src = resolvedTheme === "dark" ? srcDark : srcLight;
|
||||
|
||||
return <Image {...rest} src={src} alt={alt} priority={priority} />;
|
||||
};
|
||||
Loading…
Reference in a new issue