mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 15:09:33 +01:00
types: make theme types more strict
This commit is contained in:
@@ -1,22 +1,28 @@
|
|||||||
import { getColors, SchemeColors } from "./colorscheme";
|
import { getColors, SchemeColors } from "./colorscheme";
|
||||||
import { getVariants } from "./variants";
|
import { variants } from "./variants";
|
||||||
import { FontConfig, getFontConfig } from "./font";
|
import { FontConfig, getFontConfig } from "./font";
|
||||||
import { TransformerFactory, Transformers } from "./transformer";
|
import { TransformerFactory, Transformers } from "./transformer";
|
||||||
import { ThemeConfig } from "./types";
|
import { ThemeConfig } from "./types";
|
||||||
|
|
||||||
export type Theme = {
|
export type Theme = {
|
||||||
breakpoints: string[];
|
breakpoints: string[];
|
||||||
space: number[];
|
space: number[] & { small?: number };
|
||||||
sizes: Record<string, string | number>;
|
sizes: { full: "100%"; half: "50%" };
|
||||||
radii: Record<string, number>;
|
radii: {
|
||||||
shadows: Record<string, string>;
|
none: number;
|
||||||
|
default: number;
|
||||||
|
dialog: number;
|
||||||
|
small: number;
|
||||||
|
};
|
||||||
|
shadows: { menu: string };
|
||||||
colors: SchemeColors;
|
colors: SchemeColors;
|
||||||
iconSizes: {
|
iconSizes: {
|
||||||
small: number;
|
small: number;
|
||||||
medium: number;
|
medium: number;
|
||||||
big: number;
|
big: number;
|
||||||
};
|
};
|
||||||
} & FontConfig;
|
} & FontConfig &
|
||||||
|
typeof variants;
|
||||||
|
|
||||||
class ThemeFactory {
|
class ThemeFactory {
|
||||||
transform(type: Transformers, theme: any) {
|
transform(type: Transformers, theme: any) {
|
||||||
@@ -30,6 +36,8 @@ class ThemeFactory {
|
|||||||
space: [0, 5, 10, 15, 20, 25, 30, 35],
|
space: [0, 5, 10, 15, 20, 25, 30, 35],
|
||||||
sizes: { full: "100%", half: "50%" },
|
sizes: { full: "100%", half: "50%" },
|
||||||
radii: { none: 0, default: 5, dialog: 10, small: 2.5 },
|
radii: { none: 0, default: 5, dialog: 10, small: 2.5 },
|
||||||
|
iconSizes: { big: 18, medium: 16, small: 14 },
|
||||||
|
colors: getColors(config.theme, config.accent),
|
||||||
shadows:
|
shadows:
|
||||||
config.theme === "dark"
|
config.theme === "dark"
|
||||||
? {
|
? {
|
||||||
@@ -38,12 +46,10 @@ class ThemeFactory {
|
|||||||
: {
|
: {
|
||||||
menu: "0px 0px 10px 0px #00000022",
|
menu: "0px 0px 10px 0px #00000022",
|
||||||
},
|
},
|
||||||
iconSizes: { big: 18, medium: 16, small: 14 },
|
|
||||||
colors: getColors(config.theme, config.accent),
|
|
||||||
...getFontConfig(config.scale),
|
...getFontConfig(config.scale),
|
||||||
...getVariants(),
|
...variants,
|
||||||
};
|
};
|
||||||
(theme.space as any).small = 3;
|
theme.space.small = 3;
|
||||||
return theme;
|
return theme;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,21 +1,4 @@
|
|||||||
import { SxStyleProp } from "rebass";
|
import { SxStyleProp } from "rebass";
|
||||||
import { Variants } from ".";
|
|
||||||
|
|
||||||
export function getButtonVariants(): Variants {
|
|
||||||
return {
|
|
||||||
default: defaultVariant,
|
|
||||||
primary,
|
|
||||||
secondary,
|
|
||||||
tertiary,
|
|
||||||
list,
|
|
||||||
anchor,
|
|
||||||
tool,
|
|
||||||
icon,
|
|
||||||
dialog,
|
|
||||||
statusitem: statusItem,
|
|
||||||
menuitem: menuItem,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const defaultVariant: SxStyleProp = {
|
const defaultVariant: SxStyleProp = {
|
||||||
bg: "transparent",
|
bg: "transparent",
|
||||||
@@ -155,3 +138,17 @@ const menuItem: SxStyleProp = {
|
|||||||
backgroundColor: "border",
|
backgroundColor: "border",
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const buttonVariants = {
|
||||||
|
default: defaultVariant,
|
||||||
|
primary,
|
||||||
|
secondary,
|
||||||
|
tertiary,
|
||||||
|
list,
|
||||||
|
anchor,
|
||||||
|
tool,
|
||||||
|
icon,
|
||||||
|
dialog,
|
||||||
|
statusitem: statusItem,
|
||||||
|
menuitem: menuItem,
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,9 +1,22 @@
|
|||||||
import { SxStyleProp } from "rebass";
|
import { SxStyleProp } from "rebass";
|
||||||
import { Variants } from ".";
|
|
||||||
|
|
||||||
type FlexDirection = "row" | "column";
|
type FlexDirection = "row" | "column";
|
||||||
export function createFlexVariants(direction: FlexDirection): Variants {
|
export type FlexVariants<T extends FlexDirection> = T extends "row"
|
||||||
const variants: Variants = {
|
? {
|
||||||
|
rowCenter: SxStyleProp;
|
||||||
|
rowFill: SxStyleProp;
|
||||||
|
rowCenterFill: SxStyleProp;
|
||||||
|
}
|
||||||
|
: {
|
||||||
|
columnCenter: SxStyleProp;
|
||||||
|
columnFill: SxStyleProp;
|
||||||
|
columnCenterFill: SxStyleProp;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function createFlexVariants<T extends FlexDirection>(
|
||||||
|
direction: T
|
||||||
|
): FlexVariants<T> {
|
||||||
|
const variants = {
|
||||||
Center: createCenterVariant(direction),
|
Center: createCenterVariant(direction),
|
||||||
Fill: createFillVariant(direction),
|
Fill: createFillVariant(direction),
|
||||||
CenterFill: createCenterFillVariant(direction),
|
CenterFill: createCenterFillVariant(direction),
|
||||||
@@ -12,7 +25,7 @@ export function createFlexVariants(direction: FlexDirection): Variants {
|
|||||||
Object.entries(variants).map(([key, value]) => {
|
Object.entries(variants).map(([key, value]) => {
|
||||||
return [`${direction}${key}`, value];
|
return [`${direction}${key}`, value];
|
||||||
})
|
})
|
||||||
);
|
) as FlexVariants<T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
function createCenterVariant(direction: FlexDirection): SxStyleProp {
|
function createCenterVariant(direction: FlexDirection): SxStyleProp {
|
||||||
|
|||||||
@@ -1,19 +1,14 @@
|
|||||||
import { getButtonVariants } from "./button";
|
import { buttonVariants } from "./button";
|
||||||
import { getInputVariants } from "./input";
|
import { inputVariants } from "./input";
|
||||||
import { getTextVariants } from "./text";
|
import { textVariants } from "./text";
|
||||||
import { createFlexVariants } from "./flex";
|
import { createFlexVariants } from "./flex";
|
||||||
import { SxProps, SxStyleProp } from "rebass";
|
|
||||||
|
|
||||||
export function getVariants() {
|
export const variants = {
|
||||||
return {
|
buttons: buttonVariants,
|
||||||
buttons: getButtonVariants(),
|
forms: inputVariants,
|
||||||
forms: getInputVariants(),
|
text: textVariants,
|
||||||
text: getTextVariants(),
|
|
||||||
variants: {
|
variants: {
|
||||||
...createFlexVariants("row"),
|
...createFlexVariants("row"),
|
||||||
...createFlexVariants("column"),
|
...createFlexVariants("column"),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
||||||
export type Variants = Record<string, SxStyleProp>;
|
|
||||||
|
|||||||
@@ -1,14 +1,4 @@
|
|||||||
import { InputProps } from "@rebass/forms";
|
import { SxStyleProp } from "rebass";
|
||||||
import { SxProps, SxStyleProp } from "rebass";
|
|
||||||
import { Variants } from ".";
|
|
||||||
|
|
||||||
export function getInputVariants(): Variants {
|
|
||||||
return {
|
|
||||||
input: defaultVariant,
|
|
||||||
error,
|
|
||||||
clean,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const defaultVariant: SxStyleProp = {
|
const defaultVariant: SxStyleProp = {
|
||||||
borderRadius: "default",
|
borderRadius: "default",
|
||||||
@@ -51,3 +41,9 @@ const error: SxStyleProp = {
|
|||||||
boxShadow: "0px 0px 0px 1px var(--error) inset",
|
boxShadow: "0px 0px 0px 1px var(--error) inset",
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const inputVariants = {
|
||||||
|
input: defaultVariant,
|
||||||
|
error,
|
||||||
|
clean,
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,17 +1,4 @@
|
|||||||
import { SxStyleProp } from "rebass";
|
import { SxStyleProp } from "rebass";
|
||||||
import { Variants } from ".";
|
|
||||||
|
|
||||||
export function getTextVariants(): Variants {
|
|
||||||
return {
|
|
||||||
default: defaultVariant,
|
|
||||||
heading,
|
|
||||||
title,
|
|
||||||
subtitle,
|
|
||||||
body,
|
|
||||||
subBody,
|
|
||||||
error,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const defaultVariant: SxStyleProp = {
|
const defaultVariant: SxStyleProp = {
|
||||||
color: "text",
|
color: "text",
|
||||||
@@ -50,3 +37,13 @@ const error: SxStyleProp = {
|
|||||||
fontSize: "subBody",
|
fontSize: "subBody",
|
||||||
color: "error",
|
color: "error",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const textVariants = {
|
||||||
|
default: defaultVariant,
|
||||||
|
heading,
|
||||||
|
title,
|
||||||
|
subtitle,
|
||||||
|
body,
|
||||||
|
subBody,
|
||||||
|
error,
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user