Color switch button to quickly set user preference. Use same targetId for corresponding components and hooks if you are using themes for specific container only.
<ColorSwitch /> Copy
<ColorSwitch />
Custom size & skipSystem
<ColorSwitch size={20} skipSystem /> Copy
<ColorSwitch size={20} skipSystem />
export const ColorSwitch = ({ targetId, skipSystem, size, ...props }: ColorSwitchProps) => { const { setColorSchemePreference, resolvedColorScheme: rcs, colorSchemePreference: csp, } = useTheme(targetId); const toggleColorScheme = () => { switch (csp) { case "dark": setColorSchemePreference("light"); break; case "light": setColorSchemePreference(skipSystem ? "dark" : "system"); break; case "system": default: setColorSchemePreference("dark"); break; } }; return ( <button className={["nthul--color-switch", rcs, csp === "system" ? "system" : ""].join(" ")} data-testid="color-switch" {...props} onClick={toggleColorScheme} // @ts-expect-error -- setting custom attribute style={{ "--size": `${size}px` }} type="button" /> );}; Copy
export const ColorSwitch = ({ targetId, skipSystem, size, ...props }: ColorSwitchProps) => { const { setColorSchemePreference, resolvedColorScheme: rcs, colorSchemePreference: csp, } = useTheme(targetId); const toggleColorScheme = () => { switch (csp) { case "dark": setColorSchemePreference("light"); break; case "light": setColorSchemePreference(skipSystem ? "dark" : "system"); break; case "system": default: setColorSchemePreference("dark"); break; } }; return ( <button className={["nthul--color-switch", rcs, csp === "system" ? "system" : ""].join(" ")} data-testid="color-switch" {...props} onClick={toggleColorScheme} // @ts-expect-error -- setting custom attribute style={{ "--size": `${size}px` }} type="button" /> );};
Color switch button to quickly set user preference. Use same targetId for corresponding components and hooks if you are using themes for specific container only.
Example
Custom size & skipSystem
Source