bug/fix os theme switch detection

This commit is contained in:
Triston Armstrong 2024-10-07 12:32:27 -04:00
parent 3bc92ecb7d
commit 4e86455c5d
Signed by: tristonarmstrong
GPG Key ID: A23B48AE45EB6EFE
6 changed files with 33 additions and 9 deletions

View File

@ -1,5 +1,8 @@
import InfiniteCanvas from "./components/InfinateCanvas" import InfiniteCanvas from "./components/InfinateCanvas"
import { useThemeDetector } from "./utils/useThemeDetector"
export function App() { export function App() {
useThemeDetector()
return <InfiniteCanvas /> return <InfiniteCanvas />
} }

View File

@ -4,7 +4,7 @@ import { useDebounce } from "../utils/useDebounce"
import { LayerEnum } from "../utils/enums" import { LayerEnum } from "../utils/enums"
import images, { ImageCardType } from "../signals/images" import images, { ImageCardType } from "../signals/images"
import { updateLocalStorage } from "../utils/localStorage" import { updateLocalStorage } from "../utils/localStorage"
import { isTheme } from "../utils/isTheme" import { useThemeDetector } from "../utils/useThemeDetector"
namespace ImageCard { namespace ImageCard {
export interface ImageCardProps { export interface ImageCardProps {
@ -121,6 +121,7 @@ export function ImageCard({ key: itemKey, data: item }: ImageCard.ImageCardProps
function ExpandIcon({ cb }: { function ExpandIcon({ cb }: {
cb: ((this: GlobalEventHandlers, ev: MouseEvent) => any) | null | undefined cb: ((this: GlobalEventHandlers, ev: MouseEvent) => any) | null | undefined
}) { }) {
const isDarkTheme = useThemeDetector()
return ( return (
<svg <svg
onmousedown={cb} onmousedown={cb}
@ -129,7 +130,7 @@ function ExpandIcon({ cb }: {
height="24" height="24"
viewBox="0 0 24 24" viewBox="0 0 24 24"
fill="none" fill="none"
stroke={isTheme('dark') ? "#777" : "#999"} stroke={isDarkTheme ? "#777" : "#999"}
stroke-width="1" stroke-width="1"
stroke-linecap="round" stroke-linecap="round"
stroke-linejoin="round" stroke-linejoin="round"

View File

@ -6,11 +6,12 @@ import { MiniMap } from "./MiniMap"
import { ImageCard } from "./ImageCard" import { ImageCard } from "./ImageCard"
import images from "../signals/images" import images from "../signals/images"
import { CardSelector } from "./cardSelector/CardSelector" import { CardSelector } from "./cardSelector/CardSelector"
import { isTheme } from "../utils/isTheme"
import { Logo } from "./Logo" import { Logo } from "./Logo"
import { useThemeDetector } from "../utils/useThemeDetector"
export default function InfiniteCanvas() { export default function InfiniteCanvas() {
const containerRef = useRef<HTMLDivElement>(null) const containerRef = useRef<HTMLDivElement>(null)
const isDarkTheme = useThemeDetector()
useEffect(() => { useEffect(() => {
window.scrollTo({ window.scrollTo({
@ -51,7 +52,7 @@ export default function InfiniteCanvas() {
width: `${canvasDimentsion.value.width}px`, width: `${canvasDimentsion.value.width}px`,
height: `${canvasDimentsion.value.width}px`, height: `${canvasDimentsion.value.width}px`,
backgroundSize: "30px 30px", backgroundSize: "30px 30px",
backgroundImage: `radial-gradient(circle, rgba(${isTheme('dark') ? '255, 255, 255, 0.2' : '0, 0, 0, 0.2'}) 1px, transparent 1px)`, backgroundImage: `radial-gradient(circle, rgba(${isDarkTheme ? '255, 255, 255, 0.2' : '0, 0, 0, 0.2'}) 1px, transparent 1px)`,
}}> }}>
{Object.keys(NotesSigal.default.notes.value).map((itemKey: string) => { {Object.keys(NotesSigal.default.notes.value).map((itemKey: string) => {
const item = NotesSigal.default.notes.value[itemKey] const item = NotesSigal.default.notes.value[itemKey]

View File

@ -3,7 +3,7 @@ import { NotesSigal, focusedItem } from "../signals"
import { useDebounce } from "../utils/useDebounce" import { useDebounce } from "../utils/useDebounce"
import notes, { NoteCardType } from "../signals/notes" import notes, { NoteCardType } from "../signals/notes"
import { LayerEnum } from "../utils/enums" import { LayerEnum } from "../utils/enums"
import { isTheme } from "../utils/isTheme" import { useThemeDetector } from "../utils/useThemeDetector"
namespace NoteCard { namespace NoteCard {
export interface NoteCardProps { export interface NoteCardProps {
@ -134,6 +134,8 @@ export function NoteCard({ key: itemKey, data: item }: NoteCard.NoteCardProps) {
function ExpandIcon({ cb }: { function ExpandIcon({ cb }: {
cb: ((this: GlobalEventHandlers, ev: MouseEvent) => any) | null | undefined cb: ((this: GlobalEventHandlers, ev: MouseEvent) => any) | null | undefined
}) { }) {
const isDarkTheme = useThemeDetector()
return ( return (
<svg <svg
onmousedown={cb} onmousedown={cb}
@ -142,7 +144,7 @@ function ExpandIcon({ cb }: {
height="24" height="24"
viewBox="0 0 24 24" viewBox="0 0 24 24"
fill="none" fill="none"
stroke={isTheme('dark') ? "#777" : "#999"} stroke={isDarkTheme ? "#777" : "#999"}
stroke-width="1" stroke-width="1"
stroke-linecap="round" stroke-linecap="round"
stroke-linejoin="round" stroke-linejoin="round"

View File

@ -1,3 +0,0 @@
export function isTheme(value: "light" | "dark") {
return window.matchMedia(`(prefers-color-scheme: ${value})`).matches
}

View File

@ -0,0 +1,20 @@
import { useEffect, useState } from "kaioken"
export function useThemeDetector() {
const [isDarkTheme, setIsDarkTheme] = useState<boolean>(getCurrentTheme())
function getCurrentTheme() {
return window.matchMedia("(prefers-color-scheme: dark)").matches
}
const mqListener = (e: MediaQueryListEvent) => {
setIsDarkTheme(e.matches)
}
useEffect(() => {
const darkThemeMq = window.matchMedia("(prefers-color-scheme: dark)")
darkThemeMq.addListener(mqListener)
return () => darkThemeMq.removeListener(mqListener)
}, [])
return isDarkTheme
}