-
-
Notifications
You must be signed in to change notification settings - Fork 259
/
Copy pathconditions.ts
201 lines (160 loc) · 5.39 KB
/
conditions.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { logger } from '@pandacss/logger'
import { capitalize, isBaseCondition, isObject, toRem, withoutSpace } from '@pandacss/shared'
import type {
ConditionDetails,
ConditionQuery,
ConditionType,
Conditions as ConditionsConfig,
ThemeVariantsMap,
} from '@pandacss/types'
import { Breakpoints } from './breakpoints'
import { parseCondition } from './parse-condition'
import { compareAtRuleOrMixed } from './sort-style-rules'
const order: ConditionType[] = ['at-rule', 'self-nesting', 'combinator-nesting', 'parent-nesting', 'mixed']
interface Options {
conditions?: ConditionsConfig
breakpoints?: Record<string, string>
containerNames?: string[]
containerSizes?: Record<string, string>
themes?: ThemeVariantsMap
}
const underscoreRegex = /^_/
const selectorRegex = /&|@/
export class Conditions {
values: Record<string, ConditionDetails>
breakpoints: Breakpoints
constructor(private options: Options) {
const { breakpoints: breakpointValues = {}, conditions = {} } = options
const breakpoints = new Breakpoints(breakpointValues)
this.breakpoints = breakpoints
const entries = Object.entries(conditions).map(([key, value]) => [`_${key}`, parseCondition(value)])
const containers = this.setupContainers()
const themes = this.setupThemes()
this.values = {
...Object.fromEntries(entries),
...breakpoints.conditions,
...containers,
...themes,
}
}
private setupContainers = () => {
const { containerNames = [], containerSizes = {} } = this.options
const containers: Record<string, ConditionDetails> = {}
containerNames.unshift('') // add empty container name for @/sm, @/md, etc.
containerNames.forEach((name) => {
Object.entries(containerSizes).forEach(([size, value]) => {
const _value = toRem(value) ?? value
containers[`@${name}/${size}`] = {
type: 'at-rule',
name: 'container',
value: _value,
raw: `@container ${name} (min-width: ${_value})`,
params: `${name} ${value}`,
}
})
})
return containers
}
private setupThemes = () => {
const { themes = {} } = this.options
const themeVariants: Record<string, ConditionDetails> = {}
Object.entries(themes).forEach(([theme, _themeVariant]) => {
const condName = this.getThemeName(theme)
const cond = parseCondition('& ' + this.getThemeSelector(theme))
if (!cond) return
themeVariants[condName] = cond
})
return themeVariants
}
getThemeSelector = (name: string) => {
return `[data-panda-theme=${name}]`
}
getThemeName = (theme: string) => {
return '_theme' + capitalize(theme)
}
finalize = (paths: string[]) => {
return paths.map((path) => {
if (this.has(path)) {
return path.replace(underscoreRegex, '')
}
if (selectorRegex.test(path)) {
return `[${withoutSpace(path.trim())}]`
}
return path
})
}
shift = (paths: string[]) => {
return paths
.map((path) => path.trim())
.sort((a, b) => {
const aIsCondition = this.isCondition(a)
const bIsCondition = this.isCondition(b)
if (aIsCondition && !bIsCondition) return 1
if (!aIsCondition && bIsCondition) return -1
if (!aIsCondition && !bIsCondition) return -1
return 0
})
}
segment = (paths: string[]): { condition: string[]; selector: string[] } => {
const condition: string[] = []
const selector: string[] = []
for (const path of paths) {
if (this.isCondition(path)) {
condition.push(path)
} else {
selector.push(path)
}
}
return { condition, selector }
}
has = (key: string) => {
return Object.prototype.hasOwnProperty.call(this.values, key)
}
isCondition = (key: string) => {
return this.has(key) || !!this.getRaw(key) || isBaseCondition(key)
}
isEmpty = () => {
return Object.keys(this.values).length === 0
}
get = (key: string): undefined | string | string[] => {
const details = this.values[key]
return details?.raw
}
getRaw = (condNameOrQuery: ConditionQuery): ConditionDetails | undefined => {
if (typeof condNameOrQuery === 'string' && this.values[condNameOrQuery]) return this.values[condNameOrQuery]
try {
return parseCondition(condNameOrQuery)
} catch (error) {
logger.error('core:condition', error)
}
}
sort = (conditions: string[]): ConditionDetails[] => {
const rawConditions = conditions.map(this.getRaw).filter(Boolean) as ConditionDetails[]
return rawConditions.sort((a, b) => order.indexOf(a.type) - order.indexOf(b.type))
}
normalize = (condition: ConditionQuery | ConditionDetails): ConditionDetails | undefined => {
return isObject(condition) ? (condition as ConditionDetails) : this.getRaw(condition)
}
keys = () => {
return Object.keys(this.values)
}
saveOne = (key: string, value: string) => {
const parsed = parseCondition(value)
if (!parsed) return
this.values[`_${key}`] = parsed
}
remove(key: string) {
delete this.values[`_${key}`]
}
getSortedKeys = () => {
return Object.keys(this.values).sort((a, b) => {
const aCondition = this.values[a]
const bCondition = this.values[b]
const score = compareAtRuleOrMixed(
{ entry: {} as any, conditions: [aCondition] },
{ entry: {} as any, conditions: [bCondition] },
)
return score
})
}
}