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
131 lines (125 loc) · 4.35 KB
/
uno-preset-rtl.ts
File metadata and controls
131 lines (125 loc) · 4.35 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
import type { CSSEntries, DynamicMatcher, Preset, RuleContext } from 'unocss'
import { cornerMap, directionSize, h } from '@unocss/preset-wind4/utils'
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
const defaultMap = { l: 'is', r: 'ie' }
const map = prefixMap || defaultMap
const replacement = map[direction as 'l' | 'r']
// oxlint-disable-next-line no-console -- warn logging
console.warn(
`[RTL] Avoid using '${match}'. Use '${match.replace(direction === 'l' ? 'l' : 'r', replacement)}' instead.`,
)
return matcher([match, replacement, size], context)
}
}
function handlerRounded(
[, a = '', s = 'DEFAULT']: string[],
{ theme }: RuleContext<any>,
): CSSEntries | undefined {
if (a in cornerMap) {
if (s === 'full') return cornerMap[a].map(i => [`border${i}-radius`, 'calc(infinity * 1px)'])
const _v = theme.radius?.[s] ?? h.bracket.cssvar.global.fraction.rem(s)
if (_v != null) {
return cornerMap[a].map(i => [`border${i}-radius`, _v])
}
}
}
function handlerBorderSize([, a = '', b = '1']: string[]): CSSEntries | undefined {
const v = h.bracket.cssvar.global.px(b)
if (a in directionMap && v != null) return directionMap[a].map(i => [`border${i}-width`, v])
}
/**
* CSS RTL support to detect, replace and warn wrong left/right usages.
* @public
*/
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) => {
const replacement = direction === 'left' ? 'inset-is' : 'inset-ie'
// oxlint-disable-next-line no-console -- warn logging
console.warn(
`[RTL] Avoid using '${direction}-${size}'. Use '${replacement}-${size}' instead.`,
)
return directionSize('inset')(['', direction === 'left' ? 'is' : 'ie', size], context)
},
{ autocomplete: '(left|right)-<num>' },
],
[
/^text-(left|right)$/,
([, direction]) => {
const replacement = direction === 'left' ? 'start' : 'end'
// oxlint-disable-next-line no-console -- warn logging
console.warn(`[RTL] Avoid using 'text-${direction}'. Use 'text-${replacement}' instead.`)
return { 'text-align': replacement }
},
{ autocomplete: 'text-(left|right)' },
],
[
/^rounded-([rl])(?:-(.+))?$/,
(args, context) => {
const [_, direction, size] = args
const replacementMap: Record<string, string> = {
l: 'is',
r: 'ie',
}
const replacement = replacementMap[direction]
// oxlint-disable-next-line no-console -- warn logging
console.warn(
`[RTL] Avoid using 'rounded-${direction}'. Use 'rounded-${replacement}' instead.`,
)
return handlerRounded(['', replacement, size], context)
},
],
[
/^border-([rl])(?:-(.+))?$/,
args => {
const [_, direction, size] = args
const replacement = direction === 'l' ? 'is' : 'ie'
// oxlint-disable-next-line no-console -- warn logging
console.warn(
`[RTL] Avoid using 'border-${direction}'. Use 'border-${replacement}' instead.`,
)
return handlerBorderSize(['', replacement, size || '1'])
},
],
],
}
}