/
container.ts
39 lines (36 loc) · 1.12 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
import type { 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, { variantHandlers }) => {
let width = '100%'
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
}
}
return { 'max-width': width }
},
{ 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
}],
]