Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
152 lines (140 loc) · 4.55 KB
/
Copy pathindex.js
File metadata and controls
152 lines (140 loc) · 4.55 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
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
// Squircle with 1.715 bigger radius cuts the same area as round corner
const SQUIRCLE_SCALE = 1.715
function roundTo3Digits(number) {
return Math.round(number * 1000) / 1000
}
function isSquircleFallback(node) {
return node?.type === 'atrule' && node.params.includes('corner-shape')
}
function increaseForSquircle(decl, { AtRule }) {
if (isSquircleFallback(decl.next())) return
let value = decl.value.replace(
/(\d*\.?\d+)(rem|em|px|%)/g,
(_, number, unit) => {
let size = parseFloat(number) * SQUIRCLE_SCALE
// Round pixels to keep CSS clean, other units keep 3 digits
if (unit === 'px') return `${Math.round(size)}px`
return `${roundTo3Digits(size)}${unit}`
}
)
if (value !== decl.value) {
decl.after(
new AtRule({
name: 'supports',
nodes: [decl.clone({ value })],
params: '(corner-shape:squircle)'
})
)
}
}
function isCircle(value) {
return [...value.matchAll(/(\d*\.?\d+)%/g)].some(
([, number]) => Number(number) >= 50
)
}
function isTooSmall(value, minSize) {
let lengths = [...value.matchAll(/(\d*\.?\d+)(rem|em|px|%)?/g)]
return (
lengths.length > 0 &&
lengths.every(
([, number, unit]) =>
unit !== '%' && Number(number) * (unit === 'px' ? 1 : 16) < minSize
)
)
}
function toPixels(value) {
let match = value.trim().match(/^(\d*\.?\d+)(rem|em|px)?$/)
if (!match) return undefined
let number = Number(match[1])
if (!match[2] || match[2] === 'px') return number
return number * 16
}
function resolveVars(value, vars) {
return value.replace(
/var\(\s*(--[\w-]+)\s*\)/g,
(full, name) => vars[name] ?? full
)
}
function getRadiusPixels(value) {
let sizes = []
for (let part of value.split(/[\s/]+/).filter(Boolean)) {
let size = toPixels(part)
if (size === undefined) return undefined
sizes.push(size)
}
return Math.max(...sizes)
}
function isPill(radius, sizes, vars) {
let known = false
for (let size of sizes) {
if (size.includes('var(') && size.trim() === radius.trim()) return true
let radiusPx = getRadiusPixels(resolveVars(radius, vars))
let sizePx = toPixels(resolveVars(size, vars))
if (radiusPx === undefined || sizePx === undefined) continue
known = true
if (2 * radiusPx * SQUIRCLE_SCALE > sizePx) return true
}
return known ? false : undefined
}
const SIZE_PROPS = new Set(['block-size', 'height', 'inline-size', 'width'])
let plugin = (opts = {}) => {
return {
Declaration(decl, helpers) {
let parent = decl.parent
if (isSquircleFallback(parent)) return
if (decl.prop.startsWith('--')) {
let isRadius = opts.props && decl.prop.search(opts.props) !== -1
if (isRadius && !decl.value.includes('var(')) {
increaseForSquircle(decl, helpers)
}
} else if (/^border(-[a-z]+)*-radius$/.test(decl.prop)) {
let shape
let sizes = []
parent.each(node => {
if (node.type === 'decl') {
if (node.prop === 'corner-shape') shape = node.value
if (SIZE_PROPS.has(node.prop)) sizes.push(node.value)
}
})
let hasVar = decl.value.includes('var(')
if (!shape && decl.value === 'inherit') {
decl.cloneBefore({ prop: 'corner-shape', value: 'inherit' })
} else if (!shape && opts.auto && isCircle(decl.value)) {
// Explicit round to override squircle from other rules
decl.cloneBefore({ prop: 'corner-shape', value: 'round' })
} else if (
!shape &&
opts.auto &&
isPill(decl.value, sizes, opts.vars ?? {})
) {
decl.cloneBefore({ prop: 'corner-shape', value: 'round' })
} else if (
!shape &&
opts.auto &&
(hasVar || !isTooSmall(decl.value, opts.autoMinSize ?? 5))
) {
decl.cloneBefore({ prop: 'corner-shape', value: 'squircle' })
shape = 'squircle'
if (
hasVar &&
sizes.some(size => !size.includes('var(') && toPixels(size)) &&
isPill(decl.value, sizes, opts.vars ?? {}) === undefined
) {
decl.warn(
helpers.result,
'Can’t detect pill shape, because radius uses unknown ' +
'variable. Set corner-shape explicitly or add variable ' +
'value to the vars option.'
)
}
}
if (shape === 'squircle' && !hasVar) {
increaseForSquircle(decl, helpers)
}
}
},
postcssPlugin: 'postcss-smooth-corners'
}
}
plugin.postcss = true
export default plugin