Skip to content

Commit

Permalink
Create switch styles
Browse files Browse the repository at this point in the history
  • Loading branch information
mayank1513 committed Jun 5, 2024
1 parent 4124e4d commit 5fc733b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 7 deletions.
7 changes: 5 additions & 2 deletions lib/src/client/core/core.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ export const Core = ({ t }: CoreProps) => {
const clsList = documentEl.classList;
modes.forEach(mode => clsList.remove(mode));
clsList.add(resolvedMode);
clsList.add(mode);
documentEl.setAttribute("data-system", systemMode);
[
["sm", systemMode],
["rm", resolvedMode],
["m", mode],
].forEach(([dataLabel, value]) => documentEl.setAttribute(`data-${dataLabel}`, value));
localStorage.setItem(key, JSON.stringify({ mode, systemMode }));
}, [resolvedMode]);

Expand Down
53 changes: 51 additions & 2 deletions lib/src/client/switch/switch.module.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,52 @@
.switch {
/* create your container styles here */
}
all: unset;
position: relative;
color: currentColor;
border-radius: 50%;
border: 1px dashed currentColor;
cursor: pointer;
--size: 24px;
height: var(--size);
width: var(--size);
transition: all 0.3s ease-in-out 0s !important;
}

[data-m="system"] .switch::after {
position: absolute;
height: 100%;
width: 100%;
top: 0;
left: 0;
font-weight: 600;
font-size: calc(var(--size) / 2);
display: flex;
align-items: center;
justify-content: center;
content: "A";
}

[data-rm="light"] .switch {
box-shadow: 0 0 50px 10px yellow;
background-color: yellow;
border: 1px solid orangered;
}

[data-rm="dark"] .switch {
box-shadow: calc(var(--size) / 4) calc(var(--size) / -4) calc(var(--size) / 8) inset #fff;
border: none;
background: transparent;
animation: swing linear 0.5s;
}

@keyframes swing {
40% {
transform: rotate(-15deg);
}
80% {
transform: rotate(10deg);
}
0%,
100% {
transform: rotate(0deg);
}
}
15 changes: 12 additions & 3 deletions lib/src/client/switch/switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { modes, useStore } from "../../utils";

export interface SwitchProps extends HTMLProps<HTMLElement> {
/** html tag @defaultValue 'button' */
tag: "button" | "div";
tag?: "button" | "div";
/** Diameter of the color switch */
size?: number;
/** Skip system colorScheme while toggling */
Expand All @@ -21,7 +21,7 @@ export interface SwitchProps extends HTMLProps<HTMLElement> {
*
* @source - Source code
*/
export const Switch = ({ tag: Tag = "button", size, skipSystem, ...props }: SwitchProps) => {
export const Switch = ({ tag: Tag = "button", size = 24, skipSystem, ...props }: SwitchProps) => {
const [state, setState] = useStore();
const handleModeSwitch = useCallback(() => {
let index = modes.indexOf(state.mode);
Expand All @@ -32,5 +32,14 @@ export const Switch = ({ tag: Tag = "button", size, skipSystem, ...props }: Swit
});
}, []);
const className = [props.className, styles["switch"]].filter(Boolean).join(" ");
return <Tag {...props} className={className} data-testid="switch" onClick={handleModeSwitch} />;
return (
<Tag
{...props}
className={className}
// @ts-expect-error -> we are setting the CSS variable
style={{ "--size": `${size}px` }}
data-testid="switch"
onClick={handleModeSwitch}
/>
);
};
1 change: 1 addition & 0 deletions packages/shared/src/server/layout/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ReactNode } from "react";
import styles from "./layout.module.scss";
import { ForkMe } from "@mayank1513/fork-me/server";
import config from "@repo/scripts/rebrand.config.json";
import "nextjs-darkmode/dist/index.css";

const { owner, repo } = config;
interface LayoutProps {
Expand Down

0 comments on commit 5fc733b

Please sign in to comment.