Skip to content

Commit 3e8b196

Browse files
committed
chore: linting fixes and added color changes to the chart, based on the user's theme
1 parent df6e788 commit 3e8b196

File tree

4 files changed

+64
-6
lines changed

4 files changed

+64
-6
lines changed

apps/insights/src/components/PythProDemoPriceChart/index.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
UTCTimestamp,
77
} from "lightweight-charts";
88
import { createChart, LineSeries } from "lightweight-charts";
9+
import { useTheme } from "next-themes";
910
import { useEffect, useLayoutEffect, useRef } from "react";
1011

1112
import type { AppStateContextVal } from "../../context/pyth-pro-demo";
@@ -29,6 +30,10 @@ export function PythProDemoPriceChartImpl({
2930
metrics,
3031
selectedSource,
3132
}: PythProDemoPriceChartImplProps) {
33+
/** hooks */
34+
const { theme } = useTheme();
35+
36+
/** refs */
3237
const containerRef = useRef<HTMLDivElement | null>(null);
3338
const chartRef = useRef<IChartApi>(undefined);
3439
const seriesMapRef = useRef<Record<string, ISeriesApi<"Line">>>({});
@@ -37,8 +42,14 @@ export function PythProDemoPriceChartImpl({
3742
useLayoutEffect(() => {
3843
if (!containerRef.current) return;
3944

40-
const grayColor = getThemeCssVar("--theme-palette-gray-800") ?? "#ccc";
41-
const grayText = getThemeCssVar("--theme-palette-gray-300") ?? "#f1f1f3";
45+
const grayColor =
46+
theme === "dark"
47+
? (getThemeCssVar("--theme-palette-gray-800") ?? "#ccc")
48+
: (getThemeCssVar("--theme-palette-gray-300") ?? "#f1f1f3");
49+
const grayText =
50+
theme === "dark"
51+
? (getThemeCssVar("--theme-palette-gray-300") ?? "#f1f1f3")
52+
: (getThemeCssVar("--theme-palette-gray-800") ?? "#ccc");
4253

4354
const chart = createChart(containerRef.current, {
4455
layout: {
@@ -69,7 +80,7 @@ export function PythProDemoPriceChartImpl({
6980
chartRef.current = undefined;
7081
seriesMapRef.current = {};
7182
};
72-
}, []);
83+
}, [theme]);
7384

7485
useLayoutEffect(() => {
7586
if (!chartRef.current || !containerRef.current) return;

apps/insights/src/components/Root/css-vars.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
:root {
44
@include theme.export-color-vars;
5-
}
5+
}

packages/component-library/src/theme.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ $header-height: var(--header-height);
923923
@each $key, $value in $map {
924924
$var-name: if($prefix != "", "#{$prefix}-#{$key}", "#{$key}");
925925

926-
@if type-of($value) == "map" {
926+
@if meta.type-of($value) == "map" {
927927
// recurse into nested maps
928928
@include export-css-vars($value, $var-name);
929929
} @else {
@@ -936,4 +936,4 @@ $header-height: var(--header-height);
936936
@mixin export-color-vars {
937937
@include export-css-vars($color-pallette, "theme-palette");
938938
@include export-css-vars($color, "theme-color");
939-
}
939+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"use client";
2+
3+
import { useEffect, useState } from "react";
4+
5+
/**
6+
* detects which color-scheme is active by checking
7+
* to see which color scheme is present on the body,
8+
* and if one isn't available, falling back to
9+
* the user's system preference
10+
*/
11+
export function useDetectThemePreference() {
12+
/** state */
13+
const [themePreference, setThemePreference] = useState<
14+
"dark" | "light" | undefined
15+
>(undefined);
16+
17+
/** effects */
18+
useEffect(() => {
19+
const prefersDarkQuery = globalThis.window.matchMedia(
20+
"(prefers-color-scheme: dark)",
21+
);
22+
const bodyColorScheme = (getComputedStyle(globalThis.document.body)
23+
.colorScheme || undefined) as typeof themePreference | undefined;
24+
25+
setThemePreference(
26+
bodyColorScheme ?? (prefersDarkQuery.matches ? "dark" : "light"),
27+
);
28+
29+
const mo = new MutationObserver(() => {
30+
debugger;
31+
const { colorScheme } = getComputedStyle(globalThis.document.body);
32+
if (!colorScheme) return;
33+
setThemePreference(colorScheme as typeof themePreference);
34+
});
35+
36+
mo.observe(globalThis.document.body, {
37+
attributeFilter: ["style"],
38+
attributes: true,
39+
});
40+
41+
return () => {
42+
mo.disconnect();
43+
};
44+
}, []);
45+
46+
return { themePreference };
47+
}

0 commit comments

Comments
 (0)