From dc4f7959b1aac746133dc475a4fdd7fd9063aec1 Mon Sep 17 00:00:00 2001 From: David W J Reid Date: Wed, 10 Apr 2024 16:58:44 +0100 Subject: [PATCH] feat: add mode updater to SaltTheme --- packages/core/src/salt-provider/SaltProvider.tsx | 14 ++++++++++---- .../salt-provider/salt-provider.stories.tsx | 11 ++++++++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/packages/core/src/salt-provider/SaltProvider.tsx b/packages/core/src/salt-provider/SaltProvider.tsx index 76a6412543b..d4ad019ccc6 100644 --- a/packages/core/src/salt-provider/SaltProvider.tsx +++ b/packages/core/src/salt-provider/SaltProvider.tsx @@ -6,6 +6,7 @@ import React, { ReactNode, useContext, useMemo, + useState, } from "react"; import { AriaAnnouncerProvider } from "../aria-announcer"; import { Breakpoints, DEFAULT_BREAKPOINTS } from "../breakpoints"; @@ -37,6 +38,10 @@ export interface ThemeContextProps { UNSTABLE_corner: UNSTABLE_Corner; } +export interface ThemeContextValue extends ThemeContextProps { + setMode: (mode: Mode) => void; +} + export const DensityContext = createContext(DEFAULT_DENSITY); export const ThemeContext = createContext({ @@ -185,9 +190,9 @@ function InternalSaltProvider({ const density = densityProp ?? inheritedDensity ?? DEFAULT_DENSITY; const themeName = themeProp ?? (inheritedTheme === "" ? DEFAULT_THEME_NAME : inheritedTheme); - const mode = modeProp ?? inheritedMode; const breakpoints = breakpointsProp ?? DEFAULT_BREAKPOINTS; const corner = cornerProp ?? inheritedCorner ?? DEFAULT_CORNER; + const [mode, setMode] = useState(modeProp ?? inheritedMode); const applyClassesTo = applyClassesToProp ?? (isRootProvider ? "root" : "scope"); @@ -199,13 +204,14 @@ function InternalSaltProvider({ window: targetWindow, }); - const themeContextValue = useMemo( + const themeContextValue = useMemo( () => ({ theme: themeName, mode, window: targetWindow, themeNext: Boolean(themeNext), UNSTABLE_corner: corner, + setMode, }), [themeName, mode, targetWindow, themeNext, corner] ); @@ -311,11 +317,11 @@ export function UNSTABLE_SaltProviderNext({ ); } -export const useTheme = (): ThemeContextProps => { +export const useTheme = (): ThemeContextValue => { // eslint-disable-next-line @typescript-eslint/no-unused-vars const { window, ...contextWithoutWindow } = useContext(ThemeContext); - return contextWithoutWindow; + return contextWithoutWindow as ThemeContextValue; }; /** diff --git a/packages/core/stories/salt-provider/salt-provider.stories.tsx b/packages/core/stories/salt-provider/salt-provider.stories.tsx index 62ebaa04309..735cb5f61cd 100644 --- a/packages/core/stories/salt-provider/salt-provider.stories.tsx +++ b/packages/core/stories/salt-provider/salt-provider.stories.tsx @@ -8,6 +8,7 @@ import { SaltProvider, ToggleButton, ToggleButtonGroup, + useTheme, } from "@salt-ds/core"; import "docs/story.css"; @@ -31,7 +32,7 @@ export const Default = () => { }; export const ToggleTheme = () => { - const [mode, setMode] = useState("light"); + const { mode, setMode } = useTheme(); const handleChangeTheme = (event: SyntheticEvent) => { setMode(event.currentTarget.value as Mode); @@ -50,6 +51,14 @@ export const ToggleTheme = () => { Dark + + + Light + + + Dark + +

{`This Card is wrapped with a SaltProvider, mode is ${mode}`}