-
-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathconditions.ts
More file actions
88 lines (73 loc) · 2.57 KB
/
conditions.ts
File metadata and controls
88 lines (73 loc) · 2.57 KB
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
import type { Context } from '@pandacss/core'
import outdent from 'outdent'
export function generateConditions(ctx: Context) {
const staticKeys = Object.keys(ctx.conditions.values)
const dynamicNames = ctx.conditions.getDynamicConditionNames()
const dynamicPrefixes = dynamicNames.map((name) => '_' + name)
const keys = [...staticKeys, ...dynamicPrefixes, 'base']
const dynamicPrefixesStr = JSON.stringify(dynamicPrefixes)
return {
js: outdent`
${ctx.file.import('withoutSpace', '../helpers')}
const conditionsStr = "${staticKeys.join(',')}"
const conditions = new Set(conditionsStr.split(','))
const dynamicConditionPrefixes = ${dynamicPrefixesStr}
const conditionRegex = /^@|&|&$/
export function isCondition(value){
return conditions.has(value) || conditionRegex.test(value) || dynamicConditionPrefixes.some(prefix => value === prefix || value.startsWith(prefix + '/'))
}
const underscoreRegex = /^_/
const conditionsSelectorRegex = /&|@/
export function finalizeConditions(paths){
return paths.map((path) => {
if (conditions.has(path) || dynamicConditionPrefixes.some(prefix => path === prefix || path.startsWith(prefix + '/'))){
return path.replace(underscoreRegex, '')
}
if (conditionsSelectorRegex.test(path)){
return \`[\${withoutSpace(path.trim())}]\`
}
return path
})}
export function sortConditions(paths){
return paths.sort((a, b) => {
const aa = isCondition(a)
const bb = isCondition(b)
if (aa && !bb) return 1
if (!aa && bb) return -1
return 0
})
}
`,
dts: outdent`
${ctx.file.importType('AnySelector, Selectors', './selectors')}
export interface Conditions {
${keys
.map(
(key) =>
`\t${
key === 'base'
? `/** The base (=no conditions) styles to apply */\n`
: ctx.conditions.get(key)
? `/** \`${([] as string[]).concat(ctx.conditions.get(key) ?? '').join(' ')}\` */\n`
: ''
}\t${JSON.stringify(key)}: string`,
)
.join('\n')}
${dynamicNames.map((name) => `\t\`_${name}/\${string}\`: string`).join('\n')}
}
export type ConditionalValue<V> =
| V
| Array<V | null>
| {
[K in keyof Conditions]?: ConditionalValue<V>
}
export type Nested<P> = P & {
[K in Selectors]?: Nested<P>
} & {
[K in AnySelector]?: Nested<P>
} & {
[K in keyof Conditions]?: Nested<P>
}
`,
}
}