-
-
Notifications
You must be signed in to change notification settings - Fork 184
/
twinConfig.ts
148 lines (136 loc) · 4.74 KB
/
twinConfig.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
148
import type { GetPackageUsed, TwinConfigAll } from 'core/types'
const TWIN_CONFIG_DEFAULTS = {
allowStyleProp: false,
autoCssProp: false,
config: undefined,
convertHtmlElementToStyled: false,
convertStyledDotToParam: false,
convertStyledDotToFunction: false,
css: { import: '', from: '' },
dataCsProp: false,
dataTwProp: false,
debug: false,
disableCsProp: true,
disableShortCss: true,
global: { import: '', from: '' },
hasLogColors: true,
includeClassNames: false,
moveTwPropToStyled: false,
moveKeyframesToGlobalStyles: false,
preset: undefined,
sassyPseudo: false,
stitchesConfig: undefined,
styled: { import: '', from: '' },
} as const
// Defaults for different css-in-js libraries
const configDefaultsStyledComponents = {
sassyPseudo: true, // Sets selectors like hover to &:hover
} as const
const configDefaultsGoober = {
sassyPseudo: true, // Sets selectors like hover to &:hover
} as const
const configDefaultsSolid = {
sassyPseudo: true, // Sets selectors like hover to &:hover
moveTwPropToStyled: true, // Move the tw prop to a styled definition
convertHtmlElementToStyled: true, // Add a styled definition on css prop elements
convertStyledDotToFunction: true, // Convert styled.[element] to a default syntax
} as const
const configDefaultsStitches = {
sassyPseudo: true, // Sets selectors like hover to &:hover
convertStyledDotToParam: true, // Convert styled.[element] to a default syntax
moveTwPropToStyled: true, // Move the tw prop to a styled definition
convertHtmlElementToStyled: true, // Add a styled definition on css prop elements
stitchesConfig: undefined, // Set the path to the stitches config
moveKeyframesToGlobalStyles: true, // Stitches doesn't support inline @keyframes
} as const
function configDefaultsTwin({
isSolid,
isStyledComponents,
isGoober,
isStitches,
isDev,
}: GetPackageUsed & { isDev: boolean }): TwinConfigAll {
return {
...TWIN_CONFIG_DEFAULTS,
...(isSolid && configDefaultsSolid),
...(isStyledComponents && configDefaultsStyledComponents),
...(isGoober && configDefaultsGoober),
...(isStitches && configDefaultsStitches),
dataTwProp: isDev,
dataCsProp: isDev,
}
}
function isBoolean(value: unknown): boolean {
return typeof value === 'boolean'
}
const allowedPresets = [
'styled-components',
'emotion',
'goober',
'stitches',
'solid',
]
type ConfigTwinValidators = Record<
keyof typeof TWIN_CONFIG_DEFAULTS & 'disableColorVariables',
[(value: unknown) => boolean, string]
>
const configTwinValidators: ConfigTwinValidators = {
preset: [
(value: unknown): boolean =>
value === undefined ||
(typeof value === 'string' && allowedPresets.includes(value)),
`The config “preset” can only be:\n${allowedPresets
.map(p => `'${p}'`)
.join(', ')}`,
],
allowStyleProp: [
isBoolean,
'The config “allowStyleProp” can only be a boolean',
],
autoCssProp: [
(value: unknown): boolean => !value,
'The “autoCssProp” feature has been removed from twin.macro@2.8.2+\nThis means the css prop must be added by styled-components instead.\nSetup info at https://twinredirect.page.link/auto-css-prop\n\nRemove the “autoCssProp” item from your config to avoid this message.',
],
convertStyledDot: [
(value: unknown): boolean => !value,
'The “convertStyledDot” feature was changed to “convertStyledDotParam”.',
],
disableColorVariables: [
(value: unknown): boolean => !value,
'The disableColorVariables feature has been removed from twin.macro@3+\n\nRemove the disableColorVariables item from your config to avoid this message.',
],
sassyPseudo: [isBoolean, 'The config “sassyPseudo” can only be a boolean'],
dataTwProp: [
(value: unknown): boolean => isBoolean(value) || value === 'all',
'The config “dataTwProp” can only be true, false or "all"',
],
dataCsProp: [
(value: unknown): boolean => isBoolean(value) || value === 'all',
'The config “dataCsProp” can only be true, false or "all"',
],
includeClassNames: [
isBoolean,
'The config “includeClassNames” can only be a boolean',
],
disableCsProp: [
isBoolean,
'The config “disableCsProp” can only be a boolean',
],
convertStyledDotToParam: [
isBoolean,
'The config “convertStyledDotToParam” can only be a boolean',
],
convertStyledDotToFunction: [
isBoolean,
'The config “convertStyledDotToFunction” can only be a boolean',
],
moveTwPropToStyled: [
isBoolean,
'The config “moveTwPropToStyled” can only be a boolean',
],
convertHtmlElementToStyled: [
isBoolean,
'The config “convertHtmlElementToStyled” can only be a boolean',
],
}
export { configDefaultsTwin, configTwinValidators, TWIN_CONFIG_DEFAULTS }