Skip to content

Commit

Permalink
feat: theme switch design
Browse files Browse the repository at this point in the history
  • Loading branch information
EATSTEAK committed Dec 1, 2023
1 parent 738a31e commit f4da994
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/components/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import "./header.css";
import { LightSwitch } from "./LightSwitch";
import { Switch } from "./Switch";

export const Header = (props: { page: string }) => {
const selected = (page: string) =>
Expand All @@ -23,8 +25,7 @@ export const Header = (props: { page: string }) => {
</a>
</nav>
<aside class="shrink-0 flex justify-end items-center gap-2 order-2 lg:order-3">
<input type="checkbox"></input>
<span class="align-center">LIGHT</span>
<LightSwitch />
</aside>
</div>
</header>
Expand Down
26 changes: 26 additions & 0 deletions src/components/LightSwitch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { createSignal, type Component, Show } from "solid-js";
import { Switch } from "./Switch";

export const LightSwitch: Component<{}> = () => {
const [isDarkTheme, setDarkTheme] = createSignal(false);
return (
<div class="flex items-center justify-start">
<Switch
enabled={isDarkTheme()}
onChange={(e) => {
setDarkTheme(e.target.checked);
console.log(e.target.checked);
}}
>
<Show when={isDarkTheme()} fallback="L">
D
</Show>
</Switch>
<span class="align-center font-mono w-16">
<Show when={isDarkTheme()} fallback="LIGHT">
DARK
</Show>
</span>
</div>
);
};
50 changes: 50 additions & 0 deletions src/components/Switch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { JSX, Component } from "solid-js";
import { createSignal, splitProps } from "solid-js";
import "./switch.css";

interface SwitchProps {
enabled: boolean;
ref?: HTMLInputElement;
onChange?: JSX.ChangeEventHandler<HTMLInputElement, Event>;
children?: JSX.Element;
}

export const Switch: Component<
SwitchProps & JSX.InputHTMLAttributes<HTMLInputElement>
> = (props) => {
const [isEnabled, setIsEnabled] = createSignal(props.enabled);

const [self, inputAttributes] = splitProps(props, [
"enabled",
"ref",
"onChange",
"children",
]);

const toggle = () => {
setIsEnabled((c) => !c);
const event = new Event("change");
props.ref?.dispatchEvent(event);
};

const handleClick = () => {
toggle();
};
return (
<>
<div onClick={handleClick} class="switch-background">
<div class={`switch-display${isEnabled() ? " enabled" : ""}`}>
{self.children}
</div>
<input
ref={props.ref}
type="checkbox"
class="invisible"
checked={isEnabled()}
onChange={(e) => self?.onChange?.(e)}
{...inputAttributes}
></input>
</div>
</>
);
};
38 changes: 38 additions & 0 deletions src/components/switch.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
.switch-background {
margin: 0rem 1.5rem;
width: 2.5rem;
height: 1rem;
border-color: black;
border-style: solid;
border-width: 2px;
cursor: pointer;
transition: border-color 0.5s ease-out;
}

.switch-display {
font-size: 1.25rem;
text-align: center;
line-height: 2rem;
display: block;
width: 2rem;
height: 2rem;
background-color: white;
border-color: black;
border-style: solid;
border-width: 2px;
transition:
border-color 0.5s ease-out,
background-color 0.5s ease-out,
content 0.5s ease-out,
transform 0.5s cubic-bezier(0.22, 0.61, 0.36, 1);
}

.switch-display:not(.enabled) {
content: "☀";
transform: translate(-1rem, -0.65rem) rotate(-135deg);
}

.switch-display.enabled {
content: "🌙";
transform: translate(1.25rem, -0.65rem) rotate(0deg);
}
2 changes: 1 addition & 1 deletion src/layouts/Layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const page = pathname.slice(1);
<BaseHead title={title} description={description} image={image} />
</head>
<body class="flex flex-col">
<Header page={page} />
<Header page={page} client:load />
<slot />
<script is:inline>
const theme = (() => {
Expand Down

0 comments on commit f4da994

Please sign in to comment.