Skip to content

Commit 800ae51

Browse files
committed
fix: support passing child elements in switch component
1 parent 48b93d9 commit 800ae51

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

lib/src/client/switch/switch.module.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.switch {
22
all: unset;
33
position: relative;
4+
display: inline-block;
45
color: currentColor;
56
border-radius: 50%;
67
border: 1px dashed currentColor;

lib/src/client/switch/switch.tsx

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import styles from "./switch.module.scss";
33
import { useStore } from "../../utils";
44
import { modes } from "../../constants";
55

6-
export interface SwitchProps extends HTMLProps<HTMLElement> {
6+
export interface SwitchProps extends HTMLProps<HTMLButtonElement> {
77
/** html tag @defaultValue 'button' */
88
tag?: "button" | "div";
99
/** Diameter of the color switch */
@@ -22,7 +22,13 @@ export interface SwitchProps extends HTMLProps<HTMLElement> {
2222
*
2323
* @source - Source code
2424
*/
25-
export const Switch = ({ tag: Tag = "button", size = 24, skipSystem, ...props }: SwitchProps) => {
25+
export const Switch = ({
26+
tag: Tag = "button",
27+
size = 24,
28+
skipSystem,
29+
children,
30+
...props
31+
}: SwitchProps) => {
2632
const [state, setState] = useStore();
2733
/** toggle mode */
2834
const handleModeSwitch = () => {
@@ -34,11 +40,27 @@ export const Switch = ({ tag: Tag = "button", size = 24, skipSystem, ...props }:
3440
m: modes[(index + 1) % n],
3541
});
3642
};
37-
const className = [props.className, styles["switch"]].filter(Boolean).join(" ");
43+
if (children) {
44+
return (
45+
// @ts-expect-error -- too complex types
46+
<Tag
47+
suppressHydrationWarning
48+
{...props}
49+
data-testid="switch"
50+
// skipcq: JS-0417
51+
onClick={handleModeSwitch}>
52+
{/* @ts-expect-error -> we are setting the CSS variable */}
53+
<div className={styles.switch} style={{ "--size": `${size}px` }} />
54+
{children}
55+
</Tag>
56+
);
57+
}
3858
return (
3959
<Tag
60+
// Hydration warning is caused when the data from localStorage differs from the default data provided while rendering on server
61+
suppressHydrationWarning
4062
{...props}
41-
className={className}
63+
className={[props.className, styles.switch].join(" ")}
4264
// @ts-expect-error -> we are setting the CSS variable
4365
style={{ "--size": `${size}px` }}
4466
data-testid="switch"

packages/shared/src/client/header/header.module.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
padding: 15px 20px;
4545
gap: 20px;
4646
display: flex;
47+
place-items: center;
4748
text-transform: capitalize;
4849
cursor: pointer;
4950
margin: 0;

packages/shared/src/client/header/theme-switch.tsx

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,18 @@
22

33
import { useMode } from "nextjs-darkmode/hooks";
44
import styles from "./header.module.scss";
5-
import { useCallback } from "react";
6-
import { ColorSchemePreference } from "nextjs-darkmode";
7-
import { Switch } from "nextjs-darkmode/switch";
85

9-
const modes: ColorSchemePreference[] = ["dark", "light", "system"];
6+
import { Switch } from "nextjs-darkmode/switch";
107

118
/** This is a wrapper around `nextjs-darkmode's ColorSwitch component to improve mobile view. */
129
export default function ThemeSwitch() {
13-
const { mode, setMode } = useMode();
14-
const toggle = useCallback(() => {
15-
const index = modes.indexOf(mode);
16-
setMode(modes[(index + 1) % modes.length]);
17-
}, [mode]);
10+
const { mode } = useMode();
1811

1912
return (
20-
<button className={styles.themeswitch} onClick={toggle}>
21-
<Switch tag="div" />
13+
<Switch className={styles.themeswitch}>
2214
<span className="mb" suppressHydrationWarning>
2315
{mode}
2416
</span>
25-
</button>
17+
</Switch>
2618
);
2719
}

0 commit comments

Comments
 (0)