Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

refactor: migrate theming logic to factory pattern

thecodrr4a51c6b9eef3
diff --git a/apps/web/src/components/theme-provider/index.js b/apps/web/src/components/theme-provider/index.js
new file mode 100644
index 000000000..7f795ba74
--- /dev/null
+++ b/apps/web/src/components/theme-provider/index.js
@@ -0,0 +1,18 @@
+import React from "react";
+import { ThemeProvider as EmotionThemeProvider } from "emotion-theming";
+import { useStore } from "../../stores/theme-store";
+import ThemeFactory from "../../theme";
+import { injectCss } from "../../utils/css";
+
+function ThemeProvider(props) {
+ const theme = useStore(store => store.theme);
+ const accent = useStore(store => store.accent);
+ const factory = new ThemeFactory({ theme, accent, scale: 1 });
+ injectCss(factory.transform("css"));
+ return (
+ <EmotionThemeProvider theme={factory}>
+ {props.children}
+ </EmotionThemeProvider>
+ );
+}
+export default ThemeProvider;
diff --git a/apps/web/src/stores/app-store.js b/apps/web/src/stores/app-store.js
index c1946c7ac..8e519037a 100644
--- a/apps/web/src/stores/app-store.js
+++ b/apps/web/src/stores/app-store.js
@@ -13,7 +13,6 @@ function appStore(set, get) {
isSelectionMode: false,
shouldSelectAll: false,
selectedItems: [],
- theme: {},
colors: [],
refreshApp: function() {
noteStore.getState().refresh();
@@ -106,11 +105,6 @@ function appStore(set, get) {
.then(() => true)
.catch(() => false);
});
- },
- setTheme: function(theme) {
- set(state => {
- state.theme = theme;
- });
}
};
}
diff --git a/apps/web/src/stores/theme-store.js b/apps/web/src/stores/theme-store.js
new file mode 100644
index 000000000..0a7a0b6b9
--- /dev/null
+++ b/apps/web/src/stores/theme-store.js
@@ -0,0 +1,22 @@
+import createStore from "../common/store";
+
+function themeStore(set) {
+ return {
+ theme: "light",
+ accent: "#0560ff",
+ setTheme: function(theme) {
+ set(state => {
+ state.theme = theme;
+ });
+ },
+ setAccent: function(accent) {
+ set(state => {
+ state.accent = accent;
+ });
+ }
+ };
+}
+
+const [useStore, store] = createStore(themeStore);
+
+export { useStore, store };
diff --git a/apps/web/src/theme/colorscheme/dark.js b/apps/web/src/theme/colorscheme/dark.js
new file mode 100644
index 000000000..565711054
--- /dev/null
+++ b/apps/web/src/theme/colorscheme/dark.js
@@ -0,0 +1,24 @@
+import { hexToRGB } from "../../utils/color";
+import StaticColorSchemeFactory from "./static";
+
+class DarkColorSchemeFactory {
+ constructor(accent) {
+ return {
+ primary: accent,
+ shade: hexToRGB(accent, 0.1),
+ placeholder: hexToRGB("#ffffff", 0.6),
+ background: "#1f1f1f",
+ accent: "#000",
+ bgSecondary: "#2b2b2b",
+ border: "#2b2b2b",
+ hover: "#3b3b3b",
+ fontSecondary: "#000",
+ text: "#ffffff",
+ overlay: "rgba(255, 255, 255, 0.5)",
+ secondary: "black",
+ icon: "#dbdbdb",
+ ...new StaticColorSchemeFactory()
+ };
+ }
+}
+export default DarkColorSchemeFactory;
diff --git a/apps/web/src/theme/colorscheme/index.js b/apps/web/src/theme/colorscheme/index.js
new file mode 100644
index 000000000..da949d1d9
--- /dev/null
+++ b/apps/web/src/theme/colorscheme/index.js
@@ -0,0 +1,14 @@
+import DarkColorSchemeFactory from "./dark";
+import LightColorSchemeFactory from "./light";
+
+const colorSchemes = {
+ dark: DarkColorSchemeFactory,
+ light: LightColorSchemeFactory
+};
+
+class ColorSchemeFactory {
+ constructor(theme, accent) {
+ return colorSchemes[theme](accent);
+ }
+}
+export default ColorSchemeFactory;
diff --git a/apps/web/src/theme/colorscheme/light.js b/apps/web/src/theme/colorscheme/light.js
new file mode 100644
index 000000000..16ae7f9a4
--- /dev/null
+++ b/apps/web/src/theme/colorscheme/light.js
@@ -0,0 +1,24 @@
+import { hexToRGB } from "../../utils/color";
+import StaticColorSchemeFactory from "./static";
+
+class LightColorSchemeFactory {
+ constructor(accent) {
+ return {
+ primary: accent,
+ background: "white",
+ accent: "white",
+ bgSecondary: "#f0f0f0",
+ border: "#f0f0f0",
+ hover: "#e0e0e0",
+ fontSecondary: "white",
+ text: "#000000",
+ overlay: "rgba(0, 0, 0, 0.1)",
+ secondary: "white",
+ icon: "#3b3b3b",
+ shade: hexToRGB(accent, 0.1),
+ placeholder: hexToRGB("#000000", 0.6),
+ ...new StaticColorSchemeFactory()
+ };
+ }
+}
+export default LightColorSchemeFactory;
diff --git a/apps/web/src/theme/colorscheme/static.js b/apps/web/src/theme/colorscheme/static.js
new file mode 100644
index 000000000..2cd6ee339
--- /dev/null
+++ b/apps/web/src/theme/colorscheme/static.js
@@ -0,0 +1,12 @@
+class StaticColorSchemeFactory {
+ constructor() {
+ return {
+ fontTertiary: "gray",
+ transparent: "transparent",
+ static: "white",
+ error: "red",
+ favorite: "#ffd700"
+ };
+ }
+}
+export default StaticColorSchemeFactory;
diff --git a/apps/web/src/theme/font/fontsize.js b/apps/web/src/theme/font/fontsize.js
new file mode 100644
index 000000000..c1162c2c0
--- /dev/null
+++ b/apps/web/src/theme/font/fontsize.js
@@ -0,0 +1,14 @@
+class FontSizeFactory {
+ constructor(scaleFactor) {
+ return {
+ heading: 28 * scaleFactor,
+ input: 14 * scaleFactor,
+ title: 18 * scaleFactor,
+ subtitle: 16 * scaleFactor,
+ body: 14 * scaleFactor,
+ menu: 14 * scaleFactor,
+ subBody: 11 * scaleFactor
+ };
+ }
+}
+export default FontSizeFactory;
diff --git a/apps/web/src/theme/font/index.js b/apps/web/src/theme/font/index.js
new file mode 100644
index 000000000..210db6d8f
--- /dev/null
+++ b/apps/web/src/theme/font/index.js
@@ -0,0 +1,18 @@
+import FontSizeFactory from "./fontsize";
+
+class FontFactory {
+ constructor(scale) {
+ return {
+ fontSizes: new FontSizeFactory(scale),
+ fontWeights: {
+ body: 400,
+ bold: 700
+ },
+ fonts: {
+ body: "Noto Sans JP, sans-serif",
+ heading: "Noto Serif, serif"
+ }
+ };
+ }
+}
+export default FontFactory;
diff --git a/apps/web/src/theme/index.js b/apps/web/src/theme/index.js
new file mode 100644
index 000000000..b56885c7d
--- /dev/null
+++ b/apps/web/src/theme/index.js
@@ -0,0 +1,24 @@
+import ColorSchemeFactory from "./colorscheme";
+import VariantsFactory from "./variants";
+import FontFactory from "./font";
+import TransformerFactory from "./transformer";
+
+class ThemeFactory {
+ constructor(config) {
+ return {
+ breakpoints: ["480px", "1000px", "1000px"],
+ space: [0, 5, 10, 15, 20, 25, 30, 35],
+ sizes: { full: "100%", half: "50%" },
+ radii: { none: 0, default: 5 },
+ colors: new ColorSchemeFactory(config.theme, config.accent),
+ ...new FontFactory(config.scale),
+ ...new VariantsFactory()
+ };
+ }
+
+ transform(type) {
+ return new TransformerFactory(type, this);
+ }
+}
+
+export default ThemeFactory;
diff --git a/apps/web/src/theme/transformer/css.js b/apps/web/src/theme/transformer/css.js
new file mode 100644
index 000000000..71588bea3
--- /dev/null
+++ b/apps/web/src/theme/transformer/css.js
@@ -0,0 +1,8 @@
+function transform(theme) {
+ let root = ":root {";
+ for (let color in theme.colors) {
+ root += `--${color}: ${theme.colors[color]};`;
+ }
+ return root + "}";
+}
+export default transform;
diff --git a/apps/web/src/theme/transformer/index.js b/apps/web/src/theme/transformer/index.js
new file mode 100644
index 000000000..8b5e2424a
--- /dev/null
+++ b/apps/web/src/theme/transformer/index.js
@@ -0,0 +1,12 @@
+import css from "./css";
+
+const transformers = {
+ css: css
+};
+
+class TransformerFactory {
+ constructor(type, theme) {
+ return transformers[type](theme);
+ }
+}
+export default TransformerFactory;
diff --git a/apps/web/src/theme/variants/button.js b/apps/web/src/theme/variants/button.js
new file mode 100644
index 000000000..ef1b04ef3
--- /dev/null
+++ b/apps/web/src/theme/variants/button.js
@@ -0,0 +1,94 @@
+class ButtonFactory {
+ constructor() {
+ return {
+ default: new Default(),
+ primary: new Primary(),
+ secondary: new Secondary(),
+ tertiary: new Tertiary(),
+ list: new List(),
+ anchor: new Anchor(),
+ menu: new Menu()
+ };
+ }
+}
+export default ButtonFactory;
+
+class Default {
+ constructor() {
+ return {
+ fontFamily: "body",
+ fontWeight: "body",
+ borderRadius: "default",
+ ":focus": {
+ outline: "none"
+ },
+ ":hover": {
+ cursor: "pointer"
+ }
+ };
+ }
+}
+
+class Primary {
+ constructor() {
+ return {
+ variant: "buttons.default",
+ color: "fontSecondary",
+ bg: "primary"
+ };
+ }
+}
+
+class Secondary {
+ constructor() {
+ return { variant: "buttons.default", color: "text", bg: "bgSecondary" };
+ }
+}
+
+class Tertiary {
+ constructor() {
+ return {
+ variant: "buttons.default",
+ color: "text",
+ bg: "transparent",
+ border: "2px solid",
+ borderColor: "border"
+ };
+ }
+}
+
+class List {
+ constructor() {
+ return {
+ variant: "buttons.tertiary",
+ border: "0px solid",
+ borderBottom: "1px solid",
+ borderRadius: 0,
+ p: 2
+ };
+ }
+}
+
+class Anchor {
+ constructor() {
+ return {
+ variant: "button.default",
+ color: "primary",
+ fontSize: "subBody",
+ p: 0,
+ m: 0
+ };
+ }
+}
+
+class Menu {
+ constructor() {
+ return {
+ variant: "button.default",
+ cursor: "pointer",
+ ":hover": {
+ backgroundColor: "shade"
+ }
+ };
+ }
+}
diff --git a/apps/web/src/theme/variants/index.js b/apps/web/src/theme/variants/index.js
new file mode 100644
index 000000000..1b080939b
--- /dev/null
+++ b/apps/web/src/theme/variants/index.js
@@ -0,0 +1,14 @@
+import ButtonFactory from "./button";
+import InputFactory from "./input";
+import TextFactory from "./text";
+
+class VariantFactory {
+ constructor() {
+ return {
+ buttons: new ButtonFactory(),
+ forms: new InputFactory(),
+ text: new TextFactory()
+ };
+ }
+}
+export default VariantFactory;
diff --git a/apps/web/src/theme/variants/input.js b/apps/web/src/theme/variants/input.js
new file mode 100644
index 000000000..2d7eaf231
--- /dev/null
+++ b/apps/web/src/theme/variants/input.js
@@ -0,0 +1,45 @@
+class InputFactory {
+ constructor() {
+ return {
+ default: new Default(),
+ error: new Error()
+ };
+ }
+}
+export default InputFactory;
+
+class Default {
+ constructor() {
+ return {
+ borderRadius: "default",
+ border: "2px solid",
+ borderColor: "border",
+ fontFamily: "body",
+ fontWeight: "body",
+ fontSize: "input",
+ ":focus": {
+ outline: "none",
+ borderColor: "primary"
+ },
+ ":hover": {
+ borderColor: "shade"
+ }
+ };
+ }
+}
+
+class Error {
+ constructor() {
+ return {
+ variant: "forms.default",
+ borderColor: "red",
+ ":focus": {
+ outline: "none",
+ borderColor: "red"
+ },
+ ":hover": {
+ borderColor: "darkred"
+ }
+ };
+ }
+}
diff --git a/apps/web/src/theme/variants/text.js b/apps/web/src/theme/variants/text.js
new file mode 100644
index 000000000..d1fc1b3d8
--- /dev/null
+++ b/apps/web/src/theme/variants/text.js
@@ -0,0 +1,46 @@
+class TextFactory {
+ constructor() {
+ return {
+ default: new Default(),
+ heading: new Heading(),
+ title: new Title(),
+ body: new Body()
+ };
+ }
+}
+export default TextFactory;
+
+class Default {
+ constructor() {
+ return {
+ color: "text",
+ fontFamily: "body"
+ };
+ }
+}
+
+class Heading {
+ constructor() {
+ return {
+ variant: "text.default",
+ fontFamily: "heading",
+ fontWeight: "bold",
+ fontSize: "heading"
+ };
+ }
+}
+
+class Title {
+ constructor() {
+ return {
+ variant: "text.heading",
+ fontSize: "title"
+ };
+ }
+}
+
+class Body {
+ constructor() {
+ return { variant: "text.default", fontWeight: "body", fontSize: "body" };
+ }
+}
diff --git a/apps/web/src/utils/color.js b/apps/web/src/utils/color.js
new file mode 100644
index 000000000..dc5ee9803
--- /dev/null
+++ b/apps/web/src/utils/color.js
@@ -0,0 +1,18 @@
+function hexToRGB(hex, alpha = 1) {
+ let parseString = hex;
+ if (hex.startsWith("#")) {
+ parseString = hex.slice(1, 7);
+ }
+ if (parseString.length !== 6) {
+ return null;
+ }
+ const r = parseInt(parseString.slice(0, 2), 16);
+ const g = parseInt(parseString.slice(2, 4), 16);
+ const b = parseInt(parseString.slice(4, 6), 16);
+ if (isNaN(r) || isNaN(g) || isNaN(b)) {
+ return null;
+ }
+ return `rgba(${r}, ${g}, ${b}, ${alpha})`;
+}
+
+export { hexToRGB };
diff --git a/apps/web/src/utils/css.js b/apps/web/src/utils/css.js
index ba81acd54..78a7e5ab1 100644
--- a/apps/web/src/utils/css.js
+++ b/apps/web/src/utils/css.js
@@ -1,4 +1,4 @@
-export function addCss(rule) {
+export function injectCss(rule) {
let variableCss = document.getElementById("variables");
let head = document.getElementsByTagName("head")[0];
if (variableCss) {
diff --git a/apps/web/src/utils/theme.js b/apps/web/src/utils/theme.js
deleted file mode 100644
index d4814757f..000000000
--- a/apps/web/src/utils/theme.js
+++ /dev/null
@@ -1,322 +0,0 @@
-import React from "react";
-import { ThemeProvider as EmotionThemeProvider } from "emotion-theming";
-import { store, useStore } from "../stores/app-store";
-import { addCss } from "./css";
-
-function colorsLight(primary) {
- return makeTheme({
- primary,
- background: "white",
- accent: "white",
- navbg: "#f0f0f0",
- border: "#f0f0f0",
- hover: "#e0e0e0",
- fontSecondary: "white",
- text: "#000000",
- overlay: "rgba(0, 0, 0, 0.1)",
- secondary: "white",
- icon: "#3b3b3b"
- });
-}
-
-function colorsDark(primary) {
- return makeTheme({
- primary,
- background: "#1f1f1f",
- accent: "#000",
- navbg: "#2b2b2b",
- border: "#2b2b2b",
- hover: "#3b3b3b",
- fontSecondary: "#000",
- text: "#ffffff",
- overlay: "rgba(255, 255, 255, 0.5)",
- secondary: "black",
- icon: "#dbdbdb"
- });
-}
-
-const shadowsDark = {
- 1: "0 0 0px 0px #00000000",
- 2: "0 0 8px 0px #55555544",
- 3: "0 0 20px 0px #55555599"
-};
-
-const shadowsLight = {
- 1: "0 0 20px 0px #1790F3aa",
- 2: "0 0 8px 0px #00000047",
- 3: "0 0 20px 0px #aaaaaa77",
- 4: "0 0 5px 0px #00000017"
-};
-
-function theme(colors, shadows) {
- return {
- breakpoints: ["480px", "1000px", "1000px"],
- colors: colors,
- space: [0, 5, 10, 12, 15],
- fontSizes: {
- heading: 28,
- input: 14,
- title: 18,
- subtitle: 16,
- body: 14,
- menu: 14,
- subBody: 11
- },
- fontWeights: {
- body: 400,
- heading: 700,
- bold: 700
- },
- fonts: {
- body: "Noto Sans JP, sans-serif",
- heading: "Noto Serif, serif"
- },
- sizes: {
- full: "100%"
- },
- radii: {
- none: 0,
- default: 5
- },
- forms: {
- default: {
- borderWidth: 0,
- borderRadius: "default",
- border: "2px solid",
- borderColor: "border",
- fontFamily: "body",
- fontWeight: "body",
- fontSizes: "input",
- ":focus": {
- outline: "none",
- borderColor: "primary"
- },
- ":hover": {
- borderColor: "hover"
- }
- },
- search: {
- variant: "forms.default",
- ":focus": {
- outline: "none",
- boxShadow: 4
- }
- },
- error: {
- variant: "forms.default",
- borderColor: "red",
- ":focus": {
- outline: "none",
- borderColor: "red"
- },
- ":hover": {
- borderColor: "red"
- }
- }
- },
- text: {
- heading: {
- fontFamily: "heading",
- fontWeight: "heading",
- fontSize: "heading",
- color: "text"
- },
- title: {
- fontFamily: "heading",
- fontWeight: "bold",
- fontSize: "title"
- },
- body: {
- fontFamily: "body",
- fontWeight: "body",
- fontSize: "body"
- },
- menu: {
- pt: 1,
- pb: 2,
- px: 2,
- cursor: "pointer",
- ":hover": {
- backgroundColor: "shade"
- }
- }
- },
- buttons: {
- primary: {
- color: "fontSecondary",
- bg: "primary",
- borderRadius: "default",
- fontFamily: "body",
- fontWeight: "body",
- ":focus": {
- outline: "none"
- },
- ":hover": {
- cursor: "pointer"
- },
- ...ButtonPressedStyle
- },
- secondary: {
- variant: "buttons.primary",
- color: "text",
- bg: "navbg",
- ...ButtonPressedStyle
- },
- tertiary: {
- variant: "buttons.primary",
- color: "text",
- bg: "transparent",
- border: "2px solid",
- borderColor: "border",
- ":active": {
- color: "primary",
- opacity: 0.8
- }
- },
- nav: {
- bg: "transparent",
- fontFamily: "body",
- fontWeight: "body",
- ":focus": {
- outline: "none"
- }
- },
- links: {
- variant: "buttons.primary",
- bg: "transparent",
- color: "primary",
- fontSize: "subBody",
- fontFamily: "body",
- py: 0,
- px: 0,
- my: 0,
- mx: 0
- },
- setting: {
- bg: "transparent",
- borderBottom: "1px Solid",
- borderColor: "border",
- color: "text",
- textAlign: "left",
- fontSize: "body",
- borderRadius: 0,
- py: 2,
- px: 2,
- outline: "none",
- ":hover": { borderColor: "primary" },
- ":active": { color: "gray" }
- }
- },
- shadows: shadows
- };
-}
-
-function makeTheme({
- primary,
- background,
- accent,
- navbg,
- border,
- hover,
- fontSecondary,
- text,
- overlay,
- secondary,
- icon
-}) {
- return {
- background,
- primary,
- shade: hexToRGB(primary, 0.1),
- //secondary: "",
- accent,
- //custom
- navbg,
- border,
- hover,
- fontSecondary,
- fontTertiary: "gray",
- transparent: "transparent",
- text,
- placeholder: hexToRGB(text, 0.6),
- overlay,
- static: "white",
- secondary,
- icon,
- error: "red",
- favorite: "#ffd700"
- };
-}
-
-function getTheme(type, accent) {
- return type === "dark"
- ? theme(colorsDark(accent), shadowsDark)
- : theme(colorsLight(accent), shadowsLight);
-}
-
-var currentTheme = window.localStorage.getItem("theme") || "light";
-var currentAccent = window.localStorage.getItem("accent") || "#0560ff";
-
-export function ThemeProvider(props) {
- let theme = useStore(store => store.theme);
- theme = theme.colors ? theme : getTheme(currentTheme, currentAccent);
- addCss(cssTheme(theme));
- return (
- <EmotionThemeProvider theme={theme}>
- {props.children instanceof Function
- ? props.children(theme)
- : props.children}
- </EmotionThemeProvider>
- );
-}
-
-export function changeAccent(accent) {
- currentAccent = accent;
- window.localStorage.setItem("accent", accent);
- const theme = getTheme(currentTheme, currentAccent);
- addCss(cssTheme(theme));
- store.getState().setTheme(theme);
-}
-
-export function changeTheme() {
- currentTheme = currentTheme === "dark" ? "light" : "dark";
- window.localStorage.setItem("theme", currentTheme);
- const theme = getTheme(currentTheme, currentAccent);
- addCss(cssTheme(theme));
- store.getState().setTheme(theme);
-}
-
-export function isDarkTheme() {
- return currentTheme === "dark";
-}
-
-export const ButtonPressedStyle = {
- ":active": {
- opacity: "0.8"
- }
-};
-
-function cssTheme(theme) {
- let root = ":root {";
- for (let color in theme.colors) {
- root += `--${color}: ${theme.colors[color]};`;
- }
- return root + "}";
-}
-
-function hexToRGB(hex, alpha = 1) {
- let parseString = hex;
- if (hex.startsWith("#")) {
- parseString = hex.slice(1, 7);
- }
- if (parseString.length !== 6) {
- return null;
- }
- const r = parseInt(parseString.slice(0, 2), 16);
- const g = parseInt(parseString.slice(2, 4), 16);
- const b = parseInt(parseString.slice(4, 6), 16);
- if (isNaN(r) || isNaN(g) || isNaN(b)) {
- return null;
- }
- return `rgba(${r}, ${g}, ${b}, ${alpha})`;
-}