2021-06-18 12:57:08 -04:00
|
|
|
import { isTouchscreenDevice } from "../lib/isTouchscreenDevice";
|
2021-06-18 10:35:35 -04:00
|
|
|
import { createGlobalStyle } from "styled-components";
|
2021-06-19 17:37:12 -04:00
|
|
|
import { connectState } from "../redux/connector";
|
2021-06-18 12:57:08 -04:00
|
|
|
import { Children } from "../types/Preact";
|
2021-07-03 10:23:29 -04:00
|
|
|
import { useEffect } from "preact/hooks";
|
2021-06-18 15:21:54 -04:00
|
|
|
import { createContext } from "preact";
|
2021-06-18 12:57:08 -04:00
|
|
|
import { Helmet } from "react-helmet";
|
2021-06-18 10:35:35 -04:00
|
|
|
|
2021-06-18 12:57:08 -04:00
|
|
|
export type Variables =
|
|
|
|
| "accent"
|
|
|
|
| "background"
|
|
|
|
| "foreground"
|
|
|
|
| "block"
|
|
|
|
| "message-box"
|
|
|
|
| "mention"
|
|
|
|
| "success"
|
|
|
|
| "warning"
|
|
|
|
| "error"
|
|
|
|
| "hover"
|
|
|
|
| "scrollbar-thumb"
|
|
|
|
| "scrollbar-track"
|
|
|
|
| "primary-background"
|
|
|
|
| "primary-header"
|
|
|
|
| "secondary-background"
|
|
|
|
| "secondary-foreground"
|
|
|
|
| "secondary-header"
|
|
|
|
| "tertiary-background"
|
|
|
|
| "tertiary-foreground"
|
|
|
|
| "status-online"
|
|
|
|
| "status-away"
|
|
|
|
| "status-busy"
|
|
|
|
| "status-streaming"
|
|
|
|
| "status-invisible";
|
|
|
|
|
2021-07-04 12:56:18 -04:00
|
|
|
export type Fonts = 'Open Sans' | 'Inter' | 'Atkinson Hyperlegible' | 'Roboto' | 'Noto Sans' | 'Lato' | 'Bree Serif' | 'Montserrat' | 'Poppins' | 'Raleway' | 'Ubuntu' | 'Comic Neue';
|
|
|
|
export type MonoscapeFonts = 'Fira Code' | 'Roboto Mono' | 'Source Code Pro' | 'Space Mono' | 'Ubuntu Mono';
|
|
|
|
|
2021-06-18 12:57:08 -04:00
|
|
|
export type Theme = {
|
|
|
|
[variable in Variables]: string;
|
|
|
|
} & {
|
|
|
|
light?: boolean;
|
2021-07-04 12:56:18 -04:00
|
|
|
font?: Fonts;
|
2021-06-18 12:57:08 -04:00
|
|
|
css?: string;
|
2021-07-04 12:56:18 -04:00
|
|
|
monoscapeFont?: MonoscapeFonts;
|
2021-06-18 12:57:08 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
export interface ThemeOptions {
|
|
|
|
preset?: string;
|
2021-07-04 12:56:18 -04:00
|
|
|
ligatures?: boolean;
|
2021-06-18 12:57:08 -04:00
|
|
|
custom?: Partial<Theme>;
|
|
|
|
}
|
|
|
|
|
2021-07-04 15:46:30 -04:00
|
|
|
// import aaa from "@fontsource/open-sans/300.css?raw";
|
|
|
|
// console.info(aaa);
|
|
|
|
|
2021-07-04 12:56:18 -04:00
|
|
|
export const FONTS: Record<Fonts, { name: string, load: () => void }> = {
|
|
|
|
"Open Sans": {
|
|
|
|
name: "Open Sans",
|
|
|
|
load: async () => {
|
|
|
|
await import("@fontsource/open-sans/300.css");
|
|
|
|
await import("@fontsource/open-sans/400.css");
|
|
|
|
await import("@fontsource/open-sans/600.css");
|
|
|
|
await import("@fontsource/open-sans/700.css");
|
|
|
|
await import("@fontsource/open-sans/400-italic.css");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Inter: {
|
|
|
|
name: "Inter",
|
|
|
|
load: async () => {
|
|
|
|
await import("@fontsource/inter/300.css");
|
|
|
|
await import("@fontsource/inter/400.css");
|
|
|
|
await import("@fontsource/inter/600.css");
|
|
|
|
await import("@fontsource/inter/700.css");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"Atkinson Hyperlegible": {
|
|
|
|
name: "Atkinson Hyperlegible",
|
|
|
|
load: async () => {
|
|
|
|
await import("@fontsource/atkinson-hyperlegible/400.css");
|
|
|
|
await import("@fontsource/atkinson-hyperlegible/700.css");
|
|
|
|
await import("@fontsource/atkinson-hyperlegible/400-italic.css");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"Roboto": {
|
|
|
|
name: "Roboto",
|
|
|
|
load: async () => {
|
|
|
|
await import("@fontsource/roboto/400.css");
|
|
|
|
await import("@fontsource/roboto/700.css");
|
|
|
|
await import("@fontsource/roboto/400-italic.css");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"Noto Sans": {
|
|
|
|
name: "Noto Sans",
|
|
|
|
load: async () => {
|
|
|
|
await import("@fontsource/noto-sans/400.css");
|
|
|
|
await import("@fontsource/noto-sans/700.css");
|
|
|
|
await import("@fontsource/noto-sans/400-italic.css");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"Bree Serif": {
|
|
|
|
name: "Bree Serif",
|
|
|
|
load: () => import("@fontsource/bree-serif/400.css")
|
|
|
|
},
|
|
|
|
"Lato": {
|
|
|
|
name: "Lato",
|
|
|
|
load: async () => {
|
|
|
|
await import("@fontsource/lato/300.css");
|
|
|
|
await import("@fontsource/lato/400.css");
|
|
|
|
await import("@fontsource/lato/700.css");
|
|
|
|
await import("@fontsource/lato/400-italic.css");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"Montserrat": {
|
|
|
|
name: "Montserrat",
|
|
|
|
load: async () => {
|
|
|
|
await import("@fontsource/montserrat/300.css");
|
|
|
|
await import("@fontsource/montserrat/400.css");
|
|
|
|
await import("@fontsource/montserrat/600.css");
|
|
|
|
await import("@fontsource/montserrat/700.css");
|
|
|
|
await import("@fontsource/montserrat/400-italic.css");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"Poppins": {
|
|
|
|
name: "Poppins",
|
|
|
|
load: async () => {
|
|
|
|
await import("@fontsource/poppins/300.css");
|
|
|
|
await import("@fontsource/poppins/400.css");
|
|
|
|
await import("@fontsource/poppins/600.css");
|
|
|
|
await import("@fontsource/poppins/700.css");
|
|
|
|
await import("@fontsource/poppins/400-italic.css");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"Raleway": {
|
|
|
|
name: "Raleway",
|
|
|
|
load: async () => {
|
|
|
|
await import("@fontsource/raleway/300.css");
|
|
|
|
await import("@fontsource/raleway/400.css");
|
|
|
|
await import("@fontsource/raleway/600.css");
|
|
|
|
await import("@fontsource/raleway/700.css");
|
|
|
|
await import("@fontsource/raleway/400-italic.css");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"Ubuntu": {
|
|
|
|
name: "Ubuntu",
|
|
|
|
load: async () => {
|
|
|
|
await import("@fontsource/ubuntu/300.css");
|
|
|
|
await import("@fontsource/ubuntu/400.css");
|
|
|
|
await import("@fontsource/ubuntu/500.css");
|
|
|
|
await import("@fontsource/ubuntu/700.css");
|
|
|
|
await import("@fontsource/ubuntu/400-italic.css");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"Comic Neue": {
|
|
|
|
name: "Comic Neue",
|
|
|
|
load: async () => {
|
|
|
|
await import("@fontsource/comic-neue/300.css");
|
|
|
|
await import("@fontsource/comic-neue/400.css");
|
|
|
|
await import("@fontsource/comic-neue/700.css");
|
|
|
|
await import("@fontsource/comic-neue/400-italic.css");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const MONOSCAPE_FONTS: Record<MonoscapeFonts, { name: string, load: () => void }> = {
|
|
|
|
"Fira Code": {
|
|
|
|
name: "Fira Code",
|
|
|
|
load: () => import("@fontsource/fira-code/400.css")
|
|
|
|
},
|
|
|
|
"Roboto Mono": {
|
|
|
|
name: "Roboto Mono",
|
|
|
|
load: () => import("@fontsource/roboto-mono/400.css")
|
|
|
|
},
|
|
|
|
"Source Code Pro": {
|
|
|
|
name: "Source Code Pro",
|
|
|
|
load: () => import("@fontsource/source-code-pro/400.css")
|
|
|
|
},
|
|
|
|
"Space Mono": {
|
|
|
|
name: "Space Mono",
|
|
|
|
load: () => import("@fontsource/space-mono/400.css")
|
|
|
|
},
|
|
|
|
"Ubuntu Mono": {
|
|
|
|
name: "Ubuntu Mono",
|
|
|
|
load: () => import("@fontsource/ubuntu-mono/400.css")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const FONT_KEYS = Object.keys(FONTS).sort();
|
|
|
|
export const MONOSCAPE_FONT_KEYS = Object.keys(MONOSCAPE_FONTS).sort();
|
|
|
|
|
2021-06-18 12:57:08 -04:00
|
|
|
// Generated from https://gitlab.insrt.uk/revolt/community/themes
|
|
|
|
export const PRESETS: { [key: string]: Theme } = {
|
|
|
|
light: {
|
|
|
|
light: true,
|
|
|
|
accent: "#FD6671",
|
|
|
|
background: "#F6F6F6",
|
|
|
|
foreground: "#101010",
|
|
|
|
block: "#414141",
|
|
|
|
"message-box": "#F1F1F1",
|
|
|
|
mention: "rgba(251, 255, 0, 0.40)",
|
|
|
|
success: "#65E572",
|
|
|
|
warning: "#FAA352",
|
|
|
|
error: "#F06464",
|
|
|
|
hover: "rgba(0, 0, 0, 0.2)",
|
|
|
|
"scrollbar-thumb": "#CA525A",
|
|
|
|
"scrollbar-track": "transparent",
|
|
|
|
"primary-background": "#FFFFFF",
|
|
|
|
"primary-header": "#F1F1F1",
|
|
|
|
"secondary-background": "#F1F1F1",
|
|
|
|
"secondary-foreground": "#888888",
|
|
|
|
"secondary-header": "#F1F1F1",
|
|
|
|
"tertiary-background": "#4D4D4D",
|
|
|
|
"tertiary-foreground": "#646464",
|
|
|
|
"status-online": "#3ABF7E",
|
|
|
|
"status-away": "#F39F00",
|
|
|
|
"status-busy": "#F84848",
|
|
|
|
"status-streaming": "#977EFF",
|
|
|
|
"status-invisible": "#A5A5A5",
|
|
|
|
},
|
|
|
|
dark: {
|
|
|
|
light: false,
|
|
|
|
accent: "#FD6671",
|
|
|
|
background: "#191919",
|
|
|
|
foreground: "#F6F6F6",
|
|
|
|
block: "#2D2D2D",
|
|
|
|
"message-box": "#363636",
|
|
|
|
mention: "rgba(251, 255, 0, 0.06)",
|
|
|
|
success: "#65E572",
|
|
|
|
warning: "#FAA352",
|
|
|
|
error: "#F06464",
|
|
|
|
hover: "rgba(0, 0, 0, 0.1)",
|
|
|
|
"scrollbar-thumb": "#CA525A",
|
|
|
|
"scrollbar-track": "transparent",
|
|
|
|
"primary-background": "#242424",
|
|
|
|
"primary-header": "#363636",
|
|
|
|
"secondary-background": "#1E1E1E",
|
|
|
|
"secondary-foreground": "#C8C8C8",
|
|
|
|
"secondary-header": "#2D2D2D",
|
|
|
|
"tertiary-background": "#4D4D4D",
|
|
|
|
"tertiary-foreground": "#848484",
|
|
|
|
"status-online": "#3ABF7E",
|
|
|
|
"status-away": "#F39F00",
|
|
|
|
"status-busy": "#F84848",
|
|
|
|
"status-streaming": "#977EFF",
|
|
|
|
"status-invisible": "#A5A5A5",
|
|
|
|
},
|
2021-06-18 10:57:08 -04:00
|
|
|
};
|
2021-06-18 10:35:35 -04:00
|
|
|
|
2021-07-01 14:46:50 -04:00
|
|
|
const keys = Object.keys(PRESETS.dark);
|
2021-06-18 12:57:08 -04:00
|
|
|
const GlobalTheme = createGlobalStyle<{ theme: Theme }>`
|
2021-06-18 10:35:35 -04:00
|
|
|
:root {
|
2021-06-18 12:57:08 -04:00
|
|
|
${(props) =>
|
|
|
|
(Object.keys(props.theme) as Variables[]).map((key) => {
|
2021-07-01 14:46:50 -04:00
|
|
|
if (!keys.includes(key)) return;
|
2021-06-18 12:57:08 -04:00
|
|
|
return `--${key}: ${props.theme[key]};`;
|
|
|
|
})}
|
2021-06-18 10:35:35 -04:00
|
|
|
}
|
|
|
|
`;
|
2021-06-18 12:57:08 -04:00
|
|
|
|
2021-06-18 15:21:54 -04:00
|
|
|
export const ThemeContext = createContext<Theme>({} as any);
|
|
|
|
|
2021-06-18 12:57:08 -04:00
|
|
|
interface Props {
|
|
|
|
children: Children;
|
2021-06-19 17:37:12 -04:00
|
|
|
options?: ThemeOptions;
|
2021-06-18 12:57:08 -04:00
|
|
|
}
|
|
|
|
|
2021-07-04 12:56:18 -04:00
|
|
|
function Theme({ children, options }: Props) {
|
2021-06-19 17:37:12 -04:00
|
|
|
const theme: Theme = {
|
|
|
|
...PRESETS["dark"],
|
2021-07-04 12:56:18 -04:00
|
|
|
...(PRESETS as any)[options?.preset as any],
|
|
|
|
...options?.custom
|
2021-06-19 17:37:12 -04:00
|
|
|
};
|
2021-06-18 12:57:08 -04:00
|
|
|
|
2021-07-04 12:56:18 -04:00
|
|
|
const root = document.documentElement.style;
|
|
|
|
useEffect(() => {
|
|
|
|
const font = theme.font ?? 'Inter';
|
|
|
|
root.setProperty('--font', `"${font}"`);
|
|
|
|
FONTS[font].load();
|
|
|
|
}, [ theme.font ]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const font = theme.monoscapeFont ?? 'Fira Code';
|
|
|
|
root.setProperty('--monoscape-font', `"${font}"`);
|
|
|
|
MONOSCAPE_FONTS[font].load();
|
|
|
|
}, [ theme.monoscapeFont ]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
root.setProperty('--ligatures', options?.ligatures ? 'normal' : 'none');
|
|
|
|
}, [ options?.ligatures ]);
|
|
|
|
|
2021-07-03 10:23:29 -04:00
|
|
|
useEffect(() => {
|
2021-07-04 12:56:18 -04:00
|
|
|
const resize = () => root.setProperty('--app-height', `${window.innerHeight}px`);
|
2021-07-03 10:23:29 -04:00
|
|
|
resize();
|
|
|
|
|
|
|
|
window.addEventListener('resize', resize);
|
|
|
|
return () => window.removeEventListener('resize', resize);
|
|
|
|
}, []);
|
|
|
|
|
2021-06-18 12:57:08 -04:00
|
|
|
return (
|
2021-06-18 15:21:54 -04:00
|
|
|
<ThemeContext.Provider value={theme}>
|
2021-06-18 12:57:08 -04:00
|
|
|
<Helmet>
|
|
|
|
<meta
|
|
|
|
name="theme-color"
|
|
|
|
content={
|
|
|
|
isTouchscreenDevice
|
|
|
|
? theme["primary-header"]
|
2021-07-03 09:27:57 -04:00
|
|
|
: theme["background"]
|
2021-06-18 12:57:08 -04:00
|
|
|
}
|
|
|
|
/>
|
|
|
|
</Helmet>
|
|
|
|
<GlobalTheme theme={theme} />
|
2021-06-19 17:37:12 -04:00
|
|
|
{theme.css && (
|
|
|
|
<style dangerouslySetInnerHTML={{ __html: theme.css }} />
|
|
|
|
)}
|
2021-07-04 12:56:18 -04:00
|
|
|
{ children }
|
2021-06-18 15:21:54 -04:00
|
|
|
</ThemeContext.Provider>
|
2021-06-18 12:57:08 -04:00
|
|
|
);
|
|
|
|
}
|
2021-06-19 17:37:12 -04:00
|
|
|
|
2021-06-19 17:39:30 -04:00
|
|
|
export default connectState<{ children: Children }>(Theme, state => {
|
2021-06-19 17:37:12 -04:00
|
|
|
return {
|
|
|
|
options: state.settings.theme
|
|
|
|
};
|
|
|
|
});
|