forked from npmx-dev/npmx.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuno-preset-rtl.ts
More file actions
154 lines (144 loc) · 4.78 KB
/
uno-preset-rtl.ts
File metadata and controls
154 lines (144 loc) · 4.78 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
153
154
import type { CSSEntries, DynamicMatcher, Preset, RuleContext } from 'unocss'
import { cornerMap, directionSize, h } from '@unocss/preset-wind4/utils'
// Track warnings to avoid duplicates
const warnedClasses = new Set<string>()
function warnOnce(message: string, key: string) {
if (!warnedClasses.has(key)) {
warnedClasses.add(key)
// oxlint-disable-next-line no-console -- warn logging
console.warn(message)
}
}
/** Reset warning state (for testing) */
export function resetRtlWarnings() {
warnedClasses.clear()
}
const directionMap: Record<string, string[]> = {
'l': ['-left'],
'r': ['-right'],
't': ['-top'],
'b': ['-bottom'],
's': ['-inline-start'],
'e': ['-inline-end'],
'x': ['-left', '-right'],
'y': ['-top', '-bottom'],
'': [''],
'bs': ['-block-start'],
'be': ['-block-end'],
'is': ['-inline-start'],
'ie': ['-inline-end'],
'block': ['-block-start', '-block-end'],
'inline': ['-inline-start', '-inline-end'],
}
function directionSizeRTL(
propertyPrefix: string,
prefixMap?: { l: string; r: string },
): DynamicMatcher {
const matcher = directionSize(propertyPrefix)
return (args, context) => {
const [match, direction, size] = args
if (!size) return undefined
const defaultMap = { l: 'is', r: 'ie' }
const map = prefixMap || defaultMap
const replacement = map[direction as 'l' | 'r']
warnOnce(
`[RTL] Avoid using '${match}'. Use '${match.replace(direction === 'l' ? 'l' : 'r', replacement)}' instead.`,
match,
)
return matcher([match, replacement, size], context)
}
}
function handlerRounded(
[, a = '', s = 'DEFAULT']: string[],
{ theme }: RuleContext<any>,
): CSSEntries | undefined {
const corners = cornerMap[a]
if (!corners) return undefined
if (s === 'full') return corners.map(i => [`border${i}-radius`, 'calc(infinity * 1px)'])
const _v = theme.radius?.[s] ?? h.bracket?.cssvar?.global?.fraction?.rem?.(s)
if (_v != null) {
return corners.map(i => [`border${i}-radius`, _v])
}
}
function handlerBorderSize([, a = '', b = '1']: string[]): CSSEntries | undefined {
const v = h.bracket?.cssvar?.global?.px?.(b)
const directions = directionMap[a]
if (directions && v != null) return directions.map(i => [`border${i}-width`, v])
}
/**
* CSS RTL support to detect, replace and warn wrong left/right usages.
*/
export function presetRtl(): Preset {
return {
name: 'rtl-preset',
rules: [
// RTL overrides
// We need to move the dash out of the capturing group to avoid capturing it in the direction
[
/^p([rl])-(.+)?$/,
directionSizeRTL('padding', { l: 's', r: 'e' }),
{ autocomplete: '(m|p)<directions>-<num>' },
],
[
/^m([rl])-(.+)?$/,
directionSizeRTL('margin', { l: 's', r: 'e' }),
{ autocomplete: '(m|p)<directions>-<num>' },
],
[
/^(?:position-|pos-)?(left|right)-(.+)$/,
([, direction, size], context) => {
if (!size) return undefined
const replacement = direction === 'left' ? 'inset-is' : 'inset-ie'
warnOnce(
`[RTL] Avoid using '${direction}-${size}'. Use '${replacement}-${size}' instead.`,
`${direction}-${size}`,
)
return directionSize('inset')(['', direction === 'left' ? 'is' : 'ie', size], context)
},
{ autocomplete: '(left|right)-<num>' },
],
[
/^text-(left|right)$/,
([, direction]) => {
const replacement = direction === 'left' ? 'start' : 'end'
warnOnce(
`[RTL] Avoid using 'text-${direction}'. Use 'text-${replacement}' instead.`,
`text-${direction}`,
)
return { 'text-align': replacement }
},
{ autocomplete: 'text-(left|right)' },
],
[
/^rounded-([rl])(?:-(.+))?$/,
(args, context) => {
const [_, direction, size] = args
if (!direction) return undefined
const replacementMap: Record<string, string> = {
l: 'is',
r: 'ie',
}
const replacement = replacementMap[direction]
if (!replacement) return undefined
warnOnce(
`[RTL] Avoid using 'rounded-${direction}'. Use 'rounded-${replacement}' instead.`,
`rounded-${direction}`,
)
return handlerRounded(['', replacement, size ?? 'DEFAULT'], context)
},
],
[
/^border-([rl])(?:-(.+))?$/,
args => {
const [_, direction, size] = args
const replacement = direction === 'l' ? 'is' : 'ie'
warnOnce(
`[RTL] Avoid using 'border-${direction}'. Use 'border-${replacement}' instead.`,
`border-${direction}`,
)
return handlerBorderSize(['', replacement, size || '1'])
},
],
],
}
}