-
Notifications
You must be signed in to change notification settings - Fork 307
/
index.ts
147 lines (128 loc) · 3.6 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { createStitches } from "@stitches/core";
import { defaultLight, defaultDark, SANDPACK_THEMES } from "../themes";
import type { SandpackTheme, SandpackThemeProp } from "../types";
import { isDarkColor } from "../utils/stringUtils";
import { createStitchesMock } from "./stitches-mock";
/**
* @category Theme
*/
export const THEME_PREFIX = "sp";
const getNodeProcess = (): false | string | undefined => {
return (
process.env.SANDPACK_BARE_COMPONENTS ||
process.env.NEXT_PUBLIC_SANDPACK_BARE_COMPONENTS
);
};
/**
* @category Theme
*/
export const { createTheme, css, getCssText, keyframes } = getNodeProcess()
? createStitchesMock
: createStitches({
prefix: THEME_PREFIX,
});
const defaultVariables = {
space: new Array(11).fill(" ").reduce((acc, _, index) => {
return { ...acc, [index + 1]: `${(index + 1) * 4}px` };
}, {}),
border: { radius: "4px" },
layout: { height: "300px", headerHeight: "40px" },
transitions: { default: "150ms ease" },
zIndices: {
base: "1",
overlay: "2",
top: "3",
},
};
/**
* @category Theme
*/
export const standardizeStitchesTheme = (
theme: SandpackTheme
): Record<string, Record<string, string>> => {
// Flat values
const syntaxEntries = Object.entries(theme.syntax);
const syntax = syntaxEntries.reduce((tokenAcc, [tokenName, tokenValue]) => {
// Single property
let newValues = { [`color-${tokenName}`]: tokenValue };
// Multiples properties
if (typeof tokenValue === "object") {
newValues = Object.entries(tokenValue).reduce(
(valueAcc, [styleProp, styleValue]) => {
return {
...valueAcc,
[`${styleProp}-${tokenName}`]: styleValue,
};
},
{}
);
}
return { ...tokenAcc, ...newValues };
}, {});
return {
...defaultVariables,
colors: theme.colors,
font: theme.font,
syntax,
};
};
/**
* @category Theme
*/
export const standardizeTheme = (
inputTheme: SandpackThemeProp = "light"
): { id: string; theme: SandpackTheme; mode: "dark" | "light" } => {
const defaultLightThemeKey = "default";
/**
* Set a local theme: dark or light
*/
if (typeof inputTheme === "string") {
const predefinedTheme = SANDPACK_THEMES[inputTheme];
if (!predefinedTheme) {
throw new Error(
`[sandpack-react]: invalid theme '${inputTheme}' provided.`
);
}
return {
theme: predefinedTheme,
id: inputTheme,
mode: isDarkColor(predefinedTheme.colors.surface1) ? "dark" : "light",
};
}
/**
* Fullfill the colors key, in case it's missing any key
*/
const mode = isDarkColor(
inputTheme?.colors?.surface1 ?? defaultLight.colors.surface1
)
? "dark"
: "light";
/**
* Figure out what's the properly default colors it should be
* error, warning and success colors have different values between dark and light
*/
const baseTheme = mode === "dark" ? defaultDark : defaultLight;
const colorsByMode = { ...baseTheme.colors, ...(inputTheme?.colors ?? {}) };
const syntaxByMode = { ...baseTheme.syntax, ...(inputTheme?.syntax ?? {}) };
const fontByMode = { ...baseTheme.font, ...(inputTheme?.font ?? {}) };
const theme = {
colors: colorsByMode,
syntax: syntaxByMode,
font: fontByMode,
};
const id = inputTheme
? simpleHashFunction(JSON.stringify(theme))
: defaultLightThemeKey;
return {
theme,
id: `sp-${id}`,
mode,
};
};
const simpleHashFunction = (str: string): number => {
let hash = 0;
for (let i = 0; i < str.length; hash &= hash) {
hash = 31 * hash + str.charCodeAt(i++);
}
return Math.abs(hash);
};