/
container.ts
72 lines (59 loc) · 1.92 KB
/
container.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
import type { CSSObject, Rule, Shortcut, VariantHandlerContext } from '@unocss/core'
import type { Theme } from '@unocss/preset-mini'
import { resolveBreakpoints } from '@unocss/preset-mini/utils'
const queryMatcher = /@media \(min-width: (.+)\)/
export const container: Rule<Theme>[] = [
[
/^__container$/,
(m, context) => {
const { theme, variantHandlers } = context
let width = '100%'
const themePadding = theme.container?.padding
let padding: string | undefined
if (typeof themePadding === 'string')
padding = themePadding
else
padding = themePadding?.DEFAULT
for (const v of variantHandlers) {
const query = v.handle?.({} as VariantHandlerContext, x => x)?.parent
if (typeof query === 'string') {
const match = query.match(queryMatcher)?.[1]
if (match) {
width = match
const bp = resolveBreakpoints(context) ?? {}
const matchBp = Object.keys(bp).find(key => bp[key] === match)
if (matchBp && typeof themePadding !== 'string')
padding = themePadding?.[matchBp] ?? padding
}
}
}
const css: CSSObject = {
'max-width': width,
}
if (theme.container?.center) {
css['margin-left'] = 'auto'
css['margin-right'] = 'auto'
}
if (themePadding) {
css['padding-left'] = padding
css['padding-right'] = padding
}
return css
},
{ internal: true },
],
]
export const containerShortcuts: Shortcut<Theme>[] = [
[/^(?:(\w+)[:-])?container$/, ([, bp], context) => {
let points = Object.keys(resolveBreakpoints(context) ?? {})
if (bp) {
if (!points.includes(bp))
return
points = points.slice(points.indexOf(bp))
}
const shortcuts = points.map(p => `${p}:__container`)
if (!bp)
shortcuts.unshift('__container')
return shortcuts
}],
]