Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds dark and light toggle #1

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules
/build
/public/build
.env
traffic.csv
2 changes: 1 addition & 1 deletion app/entry.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ export default function handleRequest(

return new Response("<!DOCTYPE html>" + markup, {
status: responseStatusCode,
headers: responseHeaders
headers: responseHeaders,
});
}
93 changes: 89 additions & 4 deletions app/root.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {
json,
Links,
LinksFunction,
LiveReload,
LoaderFunction,
Meta,
NavLink,
Outlet,
Expand Down Expand Up @@ -38,7 +40,11 @@ export const links: LinksFunction = () => [
];

export const meta: MetaFunction = () => {
return { title: "Hacker News | Remix", charset: "utf-8" };
return {
title: "Hacker News | Remix",
charset: "utf-8",
viewport: "width=device-width,initial-scale=1",
};
};

function highlightFirstStoryLink(e: KeyboardEvent<HTMLBodyElement>) {
Expand All @@ -51,10 +57,8 @@ export default function App() {
const matches = useMatches();
const transition = useTransition();
return (
<html lang="en">
<html lang="en" data-theme="light">
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<Meta />
<Links />
</head>
Expand Down Expand Up @@ -90,6 +94,87 @@ export default function App() {
})}
</h5>
</section>
{/* Moon icon */}
<svg
xmlns="http://www.w3.org/2000/svg"
className="theme-toggle dark"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={2}
onClick={() => {
document
.querySelector("html")
?.setAttribute("data-theme", "dark");
}}
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M20.354 15.354A9 9 0 018.646 3.646 9.003 9.003 0 0012 21a9.003 9.003 0 008.354-5.646z"
/>
</svg>
{/* Sun icon */}
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
className="theme-toggle light"
onClick={() => {
document
.querySelector("html")
?.setAttribute("data-theme", "light");
}}
>
<circle cx="12" cy="12" r="5"></circle>
<line x1="12" y1="1" x2="12" y2="3"></line>
<line x1="12" y1="21" x2="12" y2="23"></line>
<line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line>
<line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line>
<line x1="1" y1="12" x2="3" y2="12"></line>
<line x1="21" y1="12" x2="23" y2="12"></line>
<line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line>
<line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line>
</svg>
{/* <svg
xmlns="http://www.w3.org/2000/svg"
className="theme-toggle light"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={2}
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M12 3v1m0 16v1m9-9h-1M4 12H3m15.364 6.364l-.707-.707M6.343 6.343l-.707-.707m12.728 0l-.707.707M6.343 17.657l-.707.707M16 12a4 4 0 11-8 0 4 4 0 018 0z"
/>
</svg>
<svg
xmlns="http://www.w3.org/2000/svg"
className="theme-toggle light"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={2}
onClick={() => {
document
.querySelector("html")
?.setAttribute("data-theme", "light");
}}
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M12 3v1m0 16v1m9-9h-1M4 12H3m15.364 6.364l-.707-.707M6.343 6.343l-.707-.707m12.728 0l-.707.707M6.343 17.657l-.707.707M16 12a4 4 0 11-8 0 4 4 0 018 0z"
/>
</svg> */}
</nav>
<svg
xmlns="http://www.w3.org/2000/svg"
Expand Down
5 changes: 3 additions & 2 deletions app/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { getItem, getTopStories } from "~/utils/api.server";
import stylesUrl from "~/styles/index.css";
import { getRelativeTimeString } from "~/utils/time";
import { logTraffic } from "~/utils/traffic-logger.server";

export const links: LinksFunction = () => [
{
Expand All @@ -30,6 +31,7 @@ type StoryType = {
};

export const loader: LoaderFunction = async ({ request }) => {
logTraffic(request);
const storiesPerPage = 30;
const allStoryIds = await getTopStories(storiesPerPage);

Expand Down Expand Up @@ -112,8 +114,7 @@ export default function Index() {
<section className="grid author-line">
<p>
<span>
By {story.by}{" "}
{getRelativeTimeString(story.time * 1_000)}
By {story.by} {getRelativeTimeString(story.time * 1_000)}
</span>
</p>
<Link to={`/item/${story.id}`} prefetch="intent">
Expand Down
2 changes: 2 additions & 0 deletions app/routes/item/$id.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { getItem } from "~/utils/api.server";
import stylesUrl from "~/styles/item.css";
import { getRelativeTimeString } from "~/utils/time";
import { logTraffic } from "~/utils/traffic-logger.server";

export const links: LinksFunction = () => [
{
Expand Down Expand Up @@ -41,6 +42,7 @@ const fetchAllKids = async (id: string) => {
};

export const loader: LoaderFunction = async ({ request, params }) => {
logTraffic(request);
const { id } = params;

if (!id) return redirect("/");
Expand Down
33 changes: 33 additions & 0 deletions app/styles/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,39 @@ html {
--shadow-strength-dim: 0.2;
}

html[data-theme="light"] {
color-scheme: light;
--surface-1: var(--surface1-light);
--surface-2: var(--surface2-light);
--surface-3: var(--surface3-light);
--surface-4: var(--surface4-light);
--surface-shadow: var(--surface-shadow-light);
--shadow-strength: var(--shadow-strength-light);
--text-1: var(--text1-light);
--text-2: var(--text2-light);
--brand: var(--brand-light);
}

.theme-toggle {
display: none;
position: fixed;
top: 0.75rem;
right: 0.75rem;
width: var(--size-6);
height: var(--size-6);
cursor: pointer;
stroke: currentColor;
fill: none;
}

html[data-theme="light"] .theme-toggle.dark {
display: block;
}

html[data-theme="dark"] .theme-toggle.light {
display: block;
}

:root {
--brand: var(--orange-7);
--font-serif: "Charter", serif;
Expand Down