2021-01-01 00:26:00 -07:00
|
|
|
import { extendTheme } from '@chakra-ui/react';
|
2021-01-02 12:52:35 -07:00
|
|
|
import { mode } from '@chakra-ui/theme-tools';
|
2021-02-26 11:15:35 -07:00
|
|
|
import { generateFontFamily, generatePalette } from 'palette-by-numbers';
|
2021-01-01 00:26:00 -07:00
|
|
|
|
2021-02-26 11:15:35 -07:00
|
|
|
import type { ChakraTheme } from '@chakra-ui/react';
|
2021-12-06 12:08:31 -07:00
|
|
|
import type { StyleFunctionProps } from '@chakra-ui/theme-tools';
|
2021-09-10 23:13:27 -07:00
|
|
|
import type { ThemeConfig, Theme } from '~/types';
|
2021-01-01 00:26:00 -07:00
|
|
|
|
2021-02-26 11:15:35 -07:00
|
|
|
function importFonts(userFonts: Theme.Fonts): ChakraTheme['fonts'] {
|
|
|
|
|
const { body: userBody, mono: userMono } = userFonts;
|
2021-01-02 12:52:35 -07:00
|
|
|
|
2021-01-01 00:26:00 -07:00
|
|
|
return {
|
2021-02-26 11:15:35 -07:00
|
|
|
body: generateFontFamily('sans-serif', userBody),
|
|
|
|
|
heading: generateFontFamily('sans-serif', userBody),
|
|
|
|
|
mono: generateFontFamily('monospace', userMono),
|
2021-01-01 00:26:00 -07:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-10 23:13:27 -07:00
|
|
|
function importColors(userColors: ThemeConfig['colors']): Theme.Colors {
|
2021-02-26 11:15:35 -07:00
|
|
|
const generatedColors = {} as Theme.Colors;
|
2021-09-10 23:13:27 -07:00
|
|
|
for (const [k, v] of Object.entries<string>(userColors)) {
|
2021-02-26 11:15:35 -07:00
|
|
|
generatedColors[k] = generatePalette(v);
|
2021-01-01 00:26:00 -07:00
|
|
|
}
|
|
|
|
|
|
2021-02-26 11:15:35 -07:00
|
|
|
generatedColors.blackSolid = {
|
2021-01-01 00:26:00 -07:00
|
|
|
50: '#444444',
|
|
|
|
|
100: '#3c3c3c',
|
|
|
|
|
200: '#353535',
|
|
|
|
|
300: '#2d2d2d',
|
|
|
|
|
400: '#262626',
|
|
|
|
|
500: '#1e1e1e',
|
|
|
|
|
600: '#171717',
|
|
|
|
|
700: '#0f0f0f',
|
|
|
|
|
800: '#080808',
|
|
|
|
|
900: '#000000',
|
|
|
|
|
};
|
2021-02-26 11:15:35 -07:00
|
|
|
generatedColors.whiteSolid = {
|
2021-01-01 00:26:00 -07:00
|
|
|
50: '#ffffff',
|
|
|
|
|
100: '#f7f7f7',
|
|
|
|
|
200: '#f0f0f0',
|
|
|
|
|
300: '#e8e8e8',
|
|
|
|
|
400: '#e1e1e1',
|
|
|
|
|
500: '#d9d9d9',
|
|
|
|
|
600: '#d2d2d2',
|
|
|
|
|
700: '#cacaca',
|
|
|
|
|
800: '#c3c3c3',
|
|
|
|
|
900: '#bbbbbb',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...generatedColors,
|
|
|
|
|
transparent: 'transparent',
|
|
|
|
|
current: 'currentColor',
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-02 16:53:41 -07:00
|
|
|
export function makeTheme(
|
2021-09-10 23:13:27 -07:00
|
|
|
userTheme: ThemeConfig,
|
2021-01-02 16:53:41 -07:00
|
|
|
defaultColorMode: 'dark' | 'light' | null,
|
|
|
|
|
): Theme.Full {
|
2021-02-26 11:15:35 -07:00
|
|
|
const fonts = importFonts(userTheme.fonts);
|
2021-03-03 15:56:23 -07:00
|
|
|
const { white, black, ...otherColors } = userTheme.colors;
|
|
|
|
|
const colors = importColors(otherColors);
|
2021-01-02 16:53:41 -07:00
|
|
|
const config = {} as Theme.Full['config'];
|
2021-02-26 11:15:35 -07:00
|
|
|
const fontWeights = {
|
|
|
|
|
hairline: 300,
|
|
|
|
|
thin: 300,
|
|
|
|
|
light: 300,
|
|
|
|
|
normal: 400,
|
|
|
|
|
medium: 400,
|
|
|
|
|
semibold: 700,
|
|
|
|
|
bold: 700,
|
|
|
|
|
extrabold: 700,
|
|
|
|
|
black: 700,
|
|
|
|
|
} as ChakraTheme['fontWeights'];
|
2021-01-02 16:53:41 -07:00
|
|
|
|
|
|
|
|
switch (defaultColorMode) {
|
|
|
|
|
case null:
|
|
|
|
|
config.useSystemColorMode = true;
|
|
|
|
|
break;
|
|
|
|
|
case 'light':
|
|
|
|
|
config.initialColorMode = 'light';
|
|
|
|
|
break;
|
|
|
|
|
case 'dark':
|
|
|
|
|
config.initialColorMode = 'dark';
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-01-01 00:26:00 -07:00
|
|
|
|
|
|
|
|
const defaultTheme = extendTheme({
|
|
|
|
|
fonts,
|
2021-01-02 16:53:41 -07:00
|
|
|
colors,
|
|
|
|
|
config,
|
2021-01-01 00:26:00 -07:00
|
|
|
fontWeights,
|
|
|
|
|
styles: {
|
2021-12-06 12:08:31 -07:00
|
|
|
global: (props: StyleFunctionProps) => ({
|
2021-01-02 12:52:35 -07:00
|
|
|
html: { scrollBehavior: 'smooth', height: '-webkit-fill-available' },
|
|
|
|
|
body: {
|
|
|
|
|
background: mode('light.500', 'dark.500')(props),
|
|
|
|
|
color: mode('black', 'white')(props),
|
2021-06-02 13:01:05 -07:00
|
|
|
overflowX: 'hidden',
|
2021-01-02 12:52:35 -07:00
|
|
|
},
|
|
|
|
|
}),
|
2021-01-01 00:26:00 -07:00
|
|
|
},
|
2021-12-06 12:08:31 -07:00
|
|
|
}) as Theme.Full;
|
2021-01-01 00:26:00 -07:00
|
|
|
|
|
|
|
|
return defaultTheme;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function googleFontUrl(fontFamily: string, weights: number[] = [300, 400, 700]): string {
|
|
|
|
|
const urlWeights = weights.join(',');
|
|
|
|
|
const fontName = fontFamily.split(/, /)[0].trim().replace(/'|"/g, '');
|
|
|
|
|
const urlFont = fontName.split(/ /).join('+');
|
|
|
|
|
return `https://fonts.googleapis.com/css?family=${urlFont}:${urlWeights}&display=swap`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export { theme as defaultTheme } from '@chakra-ui/react';
|