Change CSS variable names + create a themer (#109)
continuous-integration/drone/push Build is passing Details

Try it out at https://csclub.uwaterloo.ca/~a3thakra/website-demo/themer/

- Themes persist across website reloads
- Individual color reset buttons
- Reset all colors button

Reviewed-on: #109
Reviewed-by: n3parikh <n3parikh@localhost>
Reviewed-by: b38peng <b38peng@uwaterloo.ca>
Co-authored-by: Aditya Thakral <a3thakra@csclub.uwaterloo.ca>
Co-committed-by: Aditya Thakral <a3thakra@csclub.uwaterloo.ca>
This commit is contained in:
Aditya Thakral 2021-08-03 19:37:02 -04:00
parent b281572116
commit af8c750c0c
27 changed files with 398 additions and 161 deletions

View File

@ -3,5 +3,5 @@
}
.bubble:nth-child(odd) {
background-color: var(--blue-1-20);
background-color: var(--primary-accent-light);
}

View File

@ -2,8 +2,8 @@
.link {
font-family: "Poppins", "sans-serif";
border-radius: calc(20rem / 16);
background-color: var(--blue-2);
color: white;
background-color: var(--primary-accent);
color: var(--primary-background);
border: none;
outline: none;
transition-duration: 0.3s;
@ -13,7 +13,7 @@
.button:hover,
.link:hover {
background-color: var(--teal-2);
background-color: var(--secondary-accent);
cursor: pointer;
}

View File

@ -1,5 +1,5 @@
.header {
color: var(--blue-2);
color: var(--primary-accent);
font-weight: 600;
font-size: calc(36rem / 16);
}

View File

@ -3,7 +3,7 @@
}
.header {
color: var(--blue-2);
color: var(--primary-accent);
font-weight: 600;
font-size: calc(36rem / 16);
}

View File

@ -41,7 +41,7 @@
}
.content > h2 {
color: var(--purple-2);
color: var(--primary-heading);
font-size: 1rem;
margin-bottom: calc(14rem / 16);
}

View File

@ -4,7 +4,7 @@
max-width: calc(540rem / 16);
padding: calc(24rem / 16);
border-radius: calc(20rem / 16);
background-color: white;
background-color: var(--primary-background);
}
.poster {
@ -35,7 +35,7 @@
.setting {
margin: 0;
color: var(--blue-2);
color: var(--primary-accent);
font-size: calc(14rem / 16);
font-weight: 600;
}
@ -67,7 +67,7 @@
.poster {
width: calc(95rem / 16);
height: calc(95rem / 16);
border: 1px solid var(--purple-2);
border: 1px solid var(--primary-heading);
box-sizing: border-box;
margin-right: calc(14rem / 16);
}

View File

@ -1,6 +1,6 @@
.footer {
box-sizing: border-box;
background: var(--purple-2);
background: var(--primary-heading);
height: calc(66rem / 16);
padding: calc(14rem / 16) 0;
width: 100%;
@ -18,7 +18,7 @@
}
.text {
color: var(--white);
color: var(--primary-background);
font-style: normal;
text-align: center;
}

View File

@ -8,20 +8,20 @@
border: 0;
border-radius: calc(20rem / 16);
background-color: var(--grey-1-24);
background-color: var(--input-background);
font-size: calc(18rem / 16);
line-height: calc(30rem / 16);
color: var(--grey-3);
color: var(--input-text);
}
.input::placeholder {
color: var(--grey-2);
color: var(--input-placeholder-text);
font-weight: 700;
}
.input:is(:active, :hover, :focus) {
box-sizing: border-box;
border: calc(3rem / 16) solid var(--blue-1);
border: calc(3rem / 16) solid var(--primary-accent-soft);
border-radius: calc(20rem / 16);
outline: none;
padding: calc(7rem / 16) calc(28rem / 16);

View File

@ -1,10 +1,10 @@
.link {
color: var(--blue-2);
color: var(--primary-accent);
transition-duration: 0.3s;
text-decoration: none;
white-space: normal;
}
.link:hover {
color: var(--teal-2);
color: var(--secondary-accent);
}

View File

@ -5,7 +5,7 @@
}
.card:nth-child(odd) {
background-color: var(--teal-2-20);
background-color: var(--secondary-accent-light);
}
.name {
@ -19,7 +19,7 @@
}
.info {
color: var(--purple-2);
color: var(--primary-heading);
margin-bottom: calc(12rem / 16);
}
@ -29,7 +29,7 @@
right: 0;
cursor: pointer;
margin: calc(20rem / 16);
color: var(--blue-2);
color: var(--primary-accent);
font-size: calc(14rem / 16);
}
@ -46,5 +46,5 @@
}
.dropDownIcon {
fill: var(--blue-2);
fill: var(--primary-accent);
}

View File

@ -4,7 +4,7 @@
align-items: center;
z-index: 10;
background-color: var(--white);
background-color: var(--primary-background);
}
.navContent {
@ -63,16 +63,16 @@
}
.navMenu a {
color: var(--purple-2);
color: var(--primary-heading);
text-decoration: none;
}
.navMenu a.currentPage {
color: var(--blue-2);
color: var(--primary-accent);
}
.navMenu > li > a:hover {
color: var(--blue-2);
color: var(--primary-accent);
font-weight: 600;
}
@ -116,8 +116,8 @@
padding: 0;
border-radius: calc(8rem / 16);
background-color: var(--white);
box-shadow: 0 calc(8rem / 16) 1rem var(--blue-1-20);
background-color: var(--primary-background);
box-shadow: 0 calc(8rem / 16) 1rem var(--primary-accent-light);
font-size: calc(14rem / 16);
}
@ -134,7 +134,7 @@
.dropdown li:hover a,
.dropdown li a:focus {
background-color: var(--blue-1-20);
background-color: var(--primary-accent-light);
}
.dropdown li:first-child a {
@ -224,7 +224,7 @@
left: 0;
z-index: 20;
background-color: var(--navbar-gray);
background-color: var(--navbar-page-overlay);
opacity: 0;
transition: 0.5s;
@ -244,7 +244,7 @@
padding: calc(calc(64rem / 16) - 1rem);
padding-left: calc(calc(78rem / 16) - 1rem);
background-color: var(--off-white);
background-color: var(--secondary-background);
transform: translateX(100vw);
transition: 0.5s;

View File

@ -1,7 +1,7 @@
.card {
padding: calc(30rem / 16) calc(40rem / 16);
max-width: calc(524rem / 16);
background-color: var(--white);
background-color: var(--primary-background);
border-radius: calc(20rem / 16);
}
@ -11,7 +11,7 @@
}
.author {
color: var(--purple-1);
color: var(--secondary-heading);
font-style: normal;
}

View File

@ -6,7 +6,7 @@
margin: 1rem 0;
font-size: calc(24rem / 16);
font-weight: 600;
color: var(--blue-2);
color: var(--primary-accent);
}
.content {
@ -17,7 +17,7 @@
.nav {
margin: calc(8rem / 16) calc(32rem / 16) 0 0;
color: var(--purple-2);
color: var(--primary-heading);
font-weight: 500;
}
@ -26,7 +26,7 @@
overflow: hidden;
white-space: nowrap;
font-size: calc(14rem / 16);
border-bottom: calc(1rem / 16) solid var(--blue-2-25);
border-bottom: calc(1rem / 16) solid var(--primary-accent-light);
align-items: center;
height: calc(40rem / 16);
width: calc(284rem / 16);
@ -41,8 +41,8 @@
}
.selected {
background-color: var(--blue-1-05);
color: var(--blue-2);
background-color: var(--primary-accent-dim);
color: var(--primary-accent);
font-weight: 700;
}
@ -56,7 +56,7 @@
.selected .marker {
display: inline;
background-color: var(--blue-2);
background-color: var(--primary-accent);
height: calc(24rem / 16);
width: calc(4rem / 16);
margin-right: 1rem;
@ -73,7 +73,7 @@
flex: 1;
display: flex;
cursor: pointer;
color: var(--purple-2);
color: var(--primary-heading);
font-size: calc(12rem / 16);
}
@ -88,15 +88,15 @@
}
.arrowHeading {
color: var(--blue-2);
color: var(--primary-accent);
font-size: calc(14rem / 16);
font-weight: 700;
border-bottom: calc(2rem / 16) solid var(--blue-2);
border-bottom: calc(2rem / 16) solid var(--primary-accent);
padding-bottom: calc(4rem / 16);
}
.arrow {
fill: var(--blue-2);
fill: var(--primary-accent);
margin-top: calc(27rem / 16);
}

View File

@ -18,9 +18,9 @@
}
.blue {
fill: var(--blue-1);
fill: var(--primary-accent-soft);
}
.white {
fill: var(--white);
fill: var(--primary-background);
}

View File

@ -18,10 +18,10 @@
.name {
font-weight: 700;
color: var(--blue-2);
color: var(--primary-accent);
}
.role {
font-weight: 600;
color: var(--purple-2);
color: var(--primary-heading);
}

View File

@ -29,7 +29,7 @@
grid-area: name;
margin: 0;
color: var(--blue-2);
color: var(--primary-accent);
font-size: calc(36rem / 16);
font-weight: 600;
}

178
components/Theme.tsx Normal file
View File

@ -0,0 +1,178 @@
import React, {
createContext,
ReactElement,
useContext,
useEffect,
useState,
} from "react";
type BuiltInThemes = "light" | "dark";
export interface Theme {
name: BuiltInThemes | "custom";
palette: Palette;
}
export type SetThemeInput = BuiltInThemes | Partial<Palette>;
export const PALETTE_NAMES = [
"--primary-background",
"--secondary-background",
"--primary-accent",
"--primary-accent-soft",
"--primary-accent-light",
"--primary-accent-dim",
"--secondary-accent",
"--secondary-accent-light",
"--primary-heading",
"--secondary-heading",
"--text",
"--form-invalid",
"--input-background",
"--input-placeholder-text",
"--input-text",
"--navbar-page-overlay",
] as const;
export const emptyPalette = PALETTE_NAMES.reduce(
(partial, varName) => ({
...partial,
[varName]: "#c4e0f8",
}),
{} as Palette
);
const ThemeContext =
createContext<
| {
theme: Theme;
setTheme(input: SetThemeInput): void;
save(): void;
clearSaved(): void;
}
| undefined
>(undefined);
export interface Props {
children?: ReactElement;
}
export function ThemeProvider(props: Props) {
const update = useForcedUpdate();
const [themeName, setThemeName] =
useState<Theme["name"] | undefined>(undefined);
const setTheme = (input: SetThemeInput) => {
if (typeof input === "string") {
PALETTE_NAMES.forEach((name) =>
document.body.style.setProperty(name, "")
);
if (input === "light") {
document.body.classList.remove("dark");
} else if (input === "dark") {
document.body.classList.add("dark");
}
setThemeName(input);
} else {
const properties = Object.keys(input) as PaletteNames[];
properties.forEach((property) =>
document.body.style.setProperty(
property,
input[property]?.trim() ?? null
)
);
setThemeName("custom");
}
update();
};
useEffect(() => {
const customPalette = getSavedPalette();
if (customPalette == null) {
setThemeName("light");
} else {
setTheme(customPalette);
setThemeName("custom");
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return (
<ThemeContext.Provider
value={
themeName == null
? undefined
: {
theme: {
name: themeName,
get palette() {
return getCurrentPalette();
},
},
setTheme,
save: () => savePalette(getCurrentPalette()),
clearSaved: clearSavedPalette,
}
}
>
{props.children}
</ThemeContext.Provider>
);
}
export function useThemeContext() {
return useContext(ThemeContext);
}
export type PaletteNames = typeof PALETTE_NAMES[number];
export type Palette = {
[key in PaletteNames]: string;
};
function getCurrentPalette() {
const styles = getComputedStyle(document.body);
return PALETTE_NAMES.reduce(
(partial, varName) => ({
...partial,
[varName]: styles.getPropertyValue(varName).trim(),
}),
{} as Palette
);
}
function useForcedUpdate() {
const [fakeState, setFakeState] = useState(true);
return () => setFakeState(!fakeState);
}
const STORAGE_KEY = "csc-theme-palette";
function getSavedPalette() {
const raw = localStorage.getItem(STORAGE_KEY);
return raw == null ? undefined : (JSON.parse(raw) as Palette);
}
function savePalette(palette: Palette) {
localStorage.setItem(STORAGE_KEY, JSON.stringify(palette));
}
function clearSavedPalette() {
localStorage.removeItem(STORAGE_KEY);
}

View File

@ -1,13 +1,13 @@
.newsDemo {
padding: calc(50rem / 16);
background-color: var(--off-white);
background-color: var(--secondary-background);
display: inline-block;
}
.newsTitle {
font-style: normal;
font-weight: bold;
color: var(--purple-2);
color: var(--primary-heading);
font-size: calc(24rem / 16);
line-height: calc(36 / 24);
margin-bottom: calc(14rem / 16);
@ -19,20 +19,20 @@
font-size: calc(14rem / 16);
line-height: calc(21 / 14);
white-space: pre-line;
color: var(--purple-2);
color: var(--primary-heading);
vertical-align: baseline;
}
.news > hr {
border: none;
height: calc(1rem / 16);
background-color: var(--purple-2);
background-color: var(--primary-heading);
margin: 0 0 calc(13rem / 16) 0;
}
.eventDescriptionCardDemo {
padding: calc(50rem / 16) 0;
background-color: var(--off-white);
background-color: var(--secondary-background);
display: inline-block;
}
@ -54,7 +54,7 @@
.committee {
margin: 0;
color: var(--purple-2);
color: var(--primary-heading);
font-weight: 600;
font-size: calc(24rem / 16);
line-height: calc(36 / 24);
@ -63,7 +63,7 @@
.teamMemberDemo > hr {
border: none;
height: calc(1rem / 16);
background-color: var(--blue-2);
background-color: var(--primary-accent);
width: 100%;
margin-top: calc(24rem / 16);
margin-bottom: calc(46rem / 16);
@ -78,24 +78,24 @@
.linkDemo {
padding: calc(50rem / 16);
background-color: var(--off-white);
background-color: var(--secondary-background);
}
.linkTitle {
font-weight: bold;
color: var(--purple-2);
color: var(--primary-heading);
font-size: calc(24rem / 16);
}
.miniTechTalkDemo > *:nth-child(odd) {
background: var(--background-teal-2);
background: var(--secondary-accent-light);
}
@media only screen and (max-width: calc(768rem / 16)) {
.newsDemo,
.eventDescriptionCardDemo {
padding: calc(20rem / 16);
background-color: var(--off-white);
background-color: var(--secondary-background);
}
.eventDescriptionCardDemo > * {

View File

@ -1,36 +0,0 @@
import React, { createContext, ReactElement, useEffect, useState } from "react";
type Theme = "light" | "dark";
const ThemeContext = createContext<{
theme: Theme;
setTheme(newTheme: Theme): void;
}>({
theme: "light",
setTheme: () => {
throw new Error("Use ThemeProvider instead.");
},
});
interface Props {
theme: Theme;
children?: ReactElement;
}
export function ThemeProvider({ children, theme }: Props) {
const [currentTheme, setTheme] = useState(theme);
useEffect(() => {
if (currentTheme === "light") {
document.body.classList.remove("dark");
} else if (currentTheme === "dark") {
document.body.classList.add("dark");
}
}, [currentTheme]);
return (
<ThemeContext.Provider value={{ theme: currentTheme, setTheme }}>
{children}
</ThemeContext.Provider>
);
}

View File

@ -1,35 +1,31 @@
body {
/* Default is light theme */
--white: #ffffff;
--off-white: #fdf8f5;
--teal-1: #76ffdc;
--teal-2: #4ed4b2;
--teal-2-20: #4ed4b234;
--blue-1: #5caff9;
--blue-1-05: #5caff90d;
--blue-1-20: #5caff934;
--blue-2: #1482e3;
--blue-2-25: #1482e340;
--purple-1: #525284;
--purple-2: #2a2a62;
--black: #000000;
--grey-1: #c4c4c4;
--grey-1-24: #c4c4c440;
--grey-2: #bbbbbb;
--grey-3: #6b6b6b;
--red: #9f616a;
--gradient-blue-green: linear-gradient(
99.94deg,
#1481e3 -17.95%,
#4ed4b2 172.82%
);
--background-teal-2: rgb(78, 212, 178, 0.2);
/* used in mobile navbar background */
--navbar-gray: #787878b2;
/* used in home page */
--home-title-purple: #27153e;
--primary-background: #ffffff;
--secondary-background: #fdf8f5;
color: var(--black);
--primary-accent: #1482e3;
--primary-accent-soft: #5caff9;
--primary-accent-light: #c4e0f8;
--primary-accent-dim: #f7fbff;
--secondary-accent: #4ed4b2;
--secondary-accent-light: #dcf6f0;
--primary-heading: #2a2a62;
--secondary-heading: #525284;
--text: #000000;
--form-invalid: #9f616a;
--input-background: #f0f0f0;
--input-placeholder-text: #bbbbbb;
--input-text: #6b6b6b;
--navbar-page-overlay: #787878b2;
background-color: var(--primary-background);
color: var(--text);
font-family: "Poppins", "sans-serif";
font-size: 1rem;
font-weight: 400;
@ -42,25 +38,8 @@ input {
font-family: "Poppins", "sans-serif";
}
/* FIXME: Dark theme is the same as light theme right now */
.dark {
--white: #ffffff;
--off-white: #fdf8f5;
--teal-1: #76ffdc;
--teal-2: #4ed4b2;
--blue-1: #5caff9;
--blue-1-05: #5caff90d;
--blue-1-20: #5caff934;
--blue-2: #1482e3;
--purple-1: #525284;
--purple-2: #2a2a62;
--gradient-blue-green: linear-gradient(
99.94deg,
#1481e3 -17.95%,
#4ed4b2 172.82%
);
/* used in mobile navbar background */
--navbar-gray: #787878b2;
/* FIXME: Implement dark theme */
}
h1,
@ -71,7 +50,7 @@ h5,
h6 {
font-style: normal;
line-height: 1.5;
color: var(--purple-2);
color: var(--primary-heading);
}
h1,

View File

@ -2,7 +2,7 @@ import React, { ComponentType, ReactNode } from "react";
import { NextComponentType, NextPageContext } from "next";
import { AppProps as DefaultAppProps } from "next/app";
import { MDXProvider } from "@mdx-js/react";
import { ThemeProvider } from "../components/theme";
import { ThemeProvider } from "../components/Theme";
import { Navbar } from "../components/Navbar";
import { Footer } from "../components/Footer";
import { Link } from "../components/Link";
@ -16,7 +16,7 @@ export default function App({ Component, pageProps }: AppProps): JSX.Element {
const Layout = Component.Layout ?? DefaultLayout;
return (
<ThemeProvider theme="light">
<ThemeProvider>
<MDXProvider components={{ a: Link }}>
<div className={styles.appContainer}>
<Navbar />

View File

@ -9,12 +9,12 @@
}
.content {
color: black;
background-color: white;
color: var(--text);
background-color: var(--primary-background);
}
.content span {
color: var(--blue-2);
color: var(--primary-accent);
}
.titleContainer {

View File

@ -3,18 +3,18 @@
flex-direction: row;
align-items: flex-end;
padding-bottom: 1rem;
border-bottom: calc(1rem / 16) solid var(--purple-2);
border-bottom: calc(1rem / 16) solid var(--primary-heading);
}
.header {
color: var(--purple-2);
color: var(--primary-heading);
margin: 0 1rem 0 0;
text-align: center;
}
.content h2 {
font-size: calc(24rem / 16);
color: var(--blue-2);
color: var(--primary-accent);
}
@media only screen and (max-width: calc(768rem / 16)) {

View File

@ -5,16 +5,16 @@
.page > header {
display: flex;
flex-direction: row;
border-bottom: calc(1rem / 16) solid var(--purple-2);
border-bottom: calc(1rem / 16) solid var(--primary-heading);
}
.headerText > h1 {
color: var(--purple-2);
color: var(--primary-heading);
margin: 0 0 calc(8rem / 16) 0;
}
.headerText > p {
color: var(--black);
color: var(--text);
margin: 0 0 calc(22rem / 16) 0;
}
@ -27,7 +27,7 @@
}
.content h2 {
color: var(--blue-2);
color: var(--primary-accent);
}
.content strong {
@ -60,7 +60,7 @@
}
.headerText > h1 {
color: var(--purple-2);
color: var(--primary-heading);
font-size: calc(24rem / 16);
margin-bottom: (12rem / 16);
}

View File

@ -47,13 +47,13 @@
font-family: "Futura", "sans-serif";
font-size: calc(33rem / 16);
text-align: center;
color: var(--home-title-purple);
color: var(--primary-heading);
}
.clubDescription {
margin-bottom: calc(50rem / 16);
text-align: center;
color: var(--purple-2);
color: var(--primary-heading);
}
.clubDescription br {
@ -71,7 +71,7 @@
margin: 0;
width: 100%;
background-color: var(--off-white);
background-color: var(--secondary-background);
}
.cards {
@ -154,7 +154,7 @@
.clubDescription {
margin: calc(8rem / 16) 0 calc(26rem / 16);
color: var(--purple-2);
color: var(--primary-heading);
}
.clubDescription br {
@ -196,6 +196,6 @@
margin: 1rem 0 calc(34rem / 16);
height: calc(1rem / 16);
border: none;
background-color: var(--purple-2);
background-color: var(--primary-heading);
}
}

38
pages/themer.module.css Normal file
View File

@ -0,0 +1,38 @@
.page {
margin-bottom: calc(60rem / 16);
}
.controls {
display: flex;
justify-content: space-between;
}
.controls > *:first-child {
margin-right: calc(20rem / 16);
}
.palette {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(calc(150rem / 16), 1fr));
row-gap: calc(20rem / 16);
column-gap: calc(40rem / 16);
}
.palette > * {
display: flex;
flex-direction: column;
align-items: center;
}
.colorSelector {
height: calc(75rem / 16);
}
.colorReset {
margin-bottom: calc(10rem / 16);
transition-duration: unset;
}
.colorName {
text-align: center;
}

78
pages/themer.tsx Normal file
View File

@ -0,0 +1,78 @@
import React from "react";
import { useThemeContext, emptyPalette } from "components/Theme";
import { Input } from "components/Input";
import { Button } from "components/Button";
import styles from "./themer.module.css";
export default function Themer() {
const context = useThemeContext();
const palette = context?.theme.palette ?? emptyPalette;
return (
<main className={styles.page}>
<h1>Themer</h1>
<form onSubmit={(event) => event.preventDefault()}>
<div className={styles.controls}>
<Button
type="reset"
onClick={() => {
context?.clearSaved();
context?.setTheme("light");
}}
>
Reset to light mode
</Button>
<Button type="submit" onClick={() => context?.save()}>
Save
</Button>
</div>
<div className={styles.palette}>
{Object.entries(palette).map(([key, value]) => {
const color =
value.length === 4 && value.startsWith("#")
? `#${value[1].repeat(2)}${value[2].repeat(2)}${value[3].repeat(
2
)}`
: value;
const isValidColor = color.startsWith("#") && color.length === 7;
return (
<div key={key}>
<Input
id={`color-${key}`}
type="color"
className={styles.colorSelector}
value={color}
onChange={(event) =>
context?.setTheme({ [key]: event.target.value })
}
/>
<Button
type="button"
className={styles.colorReset}
size="small"
onClick={() => context?.setTheme({ [key]: "" })}
>
Reset
</Button>
<code className={styles.colorName}>
<label htmlFor={`color-${key}`}>
{!isValidColor && "🚧 "}
{key
.slice(2)
.split("-")
.map((word) => word[0].toUpperCase() + word.slice(1))
.join(" ")}
{!isValidColor && " 🚧"}
</label>
</code>
</div>
);
})}
</div>{" "}
</form>
</main>
);
}