Repository navigation
Expand file tree
/
Copy pathaliases.ts
More file actions
266 lines (233 loc) · 6.53 KB
/
Copy pathaliases.ts
File metadata and controls
266 lines (233 loc) · 6.53 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* CSS property aliases for more concise syntax
*/
/**
* Converts camelCase or PascalCase to kebab-case
* @param str - The string to convert
* @returns kebab-case string
*
* @example
* toKebabCase('fontSize'); // Returns: 'font-size'
* toKebabCase('BackgroundColor'); // Returns: 'background-color'
* toKebabCase('borderTopLeftRadius'); // Returns: 'border-top-left-radius'
*/
export function toKebabCase(str: string): string {
// If already kebab-case or lowercase, return as-is
if (str === str.toLowerCase() && !str.match(/[A-Z]/)) {
return str;
}
return str
// Insert hyphen before uppercase letters
.replace(/([A-Z])/g, '-$1')
// Convert to lowercase
.toLowerCase()
// Remove leading hyphen if PascalCase
.replace(/^-/, '');
}
/**
* Built-in property aliases
* These common aliases are available by default
*/
export const DEFAULT_ALIASES: Record<string, string> = {
// Background
'bg': 'background',
'bg-color': 'background-color',
'bg-image': 'background-image',
'bg-size': 'background-size',
'bg-position': 'background-position',
'bg-repeat': 'background-repeat',
'bg-attachment': 'background-attachment',
'bg-clip': 'background-clip',
'bg-origin': 'background-origin',
// Text & Typography
'text': 'color',
'op': 'opacity',
// Layout & Sizing
'w': 'width',
'h': 'height',
'min-w': 'min-width',
'max-w': 'max-width',
'min-h': 'min-height',
'max-h': 'max-height',
// Spacing - Margin
'm': 'margin',
'mt': 'margin-top',
'mr': 'margin-right',
'mb': 'margin-bottom',
'ml': 'margin-left',
'mx': 'margin-inline',
'my': 'margin-block',
'ms': 'margin-inline-start',
'me': 'margin-inline-end',
// Spacing - Padding
'p': 'padding',
'pt': 'padding-top',
'pr': 'padding-right',
'pb': 'padding-bottom',
'pl': 'padding-left',
'px': 'padding-inline',
'py': 'padding-block',
'ps': 'padding-inline-start',
'pe': 'padding-inline-end',
// Space Between
'space-x': 'column-gap',
'space-y': 'row-gap',
// Border
'border-w': 'border-width',
'border-t': 'border-top',
'border-r': 'border-right',
'border-b': 'border-bottom',
'border-l': 'border-left',
'border-t-color': 'border-top-color',
'border-b-color': 'border-bottom-color',
'border-l-color': 'border-left-color',
'border-r-color': 'border-right-color',
'border-t-style': 'border-top-style',
'border-b-style': 'border-bottom-style',
'border-l-style': 'border-left-style',
'border-r-style': 'border-right-style',
'border-t-w': 'border-top-width',
'border-b-w': 'border-bottom-width',
'border-l-w': 'border-left-width',
'border-r-w': 'border-right-width',
'rounded': 'border-radius',
'rounded-tl': 'border-top-left-radius',
'rounded-tr': 'border-top-right-radius',
'rounded-bl': 'border-bottom-left-radius',
'rounded-br': 'border-bottom-right-radius',
// Z-Index
'z': 'z-index',
// Flexbox
'justify': 'justify-content',
'items': 'align-items',
'align': 'align-self',
'grow': 'flex-grow',
'shrink': 'flex-shrink',
'basis': 'flex-basis',
// Grid
'grid-cols': 'grid-template-columns',
'grid-rows': 'grid-template-rows',
'col': 'grid-column',
'col-start': 'grid-column-start',
'col-end': 'grid-column-end',
'row': 'grid-row',
'row-start': 'grid-row-start',
'row-end': 'grid-row-end',
'gap-x': 'column-gap',
'gap-y': 'row-gap',
// Effects
'shadow': 'box-shadow',
'mix-blend': 'mix-blend-mode',
'bg-blend': 'background-blend-mode',
// Transforms
'origin': 'transform-origin',
// Transitions & Animation
'duration': 'transition-duration',
'delay': 'transition-delay',
'animate': 'animation',
// Overflow
'overscroll': 'overscroll-behavior',
'overscroll-x': 'overscroll-behavior-x',
'overscroll-y': 'overscroll-behavior-y',
// Outline & Ring
'outline-w': 'outline-width',
// Aspect Ratio
'aspect': 'aspect-ratio',
// Object Fit
'object': 'object-fit',
// Lists
'list': 'list-style-type',
// Break
'break': 'word-break',
'break-words': 'overflow-wrap',
// Whitespace
'whitespace': 'white-space',
// SVG
'stroke-w': 'stroke-width',
// Fonts
'size': 'font-size',
'fw': 'font-weight'
};
/**
* User-registered custom aliases
* These override DEFAULT_ALIASES if there's a conflict
*/
let customAliases: Record<string, string> = {};
/**
* Register a single custom alias
*
* @param alias - The short alias name
* @param property - The full CSS property name
*
* @example
* registerAlias('txt', 'color');
* registerAlias('bgc', 'background-color');
*/
export function registerAlias(alias: string, property: string): void {
customAliases[alias] = property;
}
/**
* Register multiple custom aliases at once
*
* @param aliases - Object mapping alias names to CSS properties
*
* @example
* registerAliases({
* 'txt': 'color',
* 'bgc': 'background-color',
* 'fw': 'font-weight'
* });
*/
export function registerAliases(aliases: Record<string, string>): void {
Object.assign(customAliases, aliases);
}
/**
* Clear all custom aliases
*/
export function clearCustomAliases(): void {
customAliases = {};
}
/**
* Get all currently registered aliases (built-in + custom)
* Custom aliases override built-in ones
*
* @returns Combined object of all aliases
*/
export function getAllAliases(): Record<string, string> {
return { ...DEFAULT_ALIASES, ...customAliases };
}
/**
* Resolve a property name, converting alias to full property if applicable.
* Handles camelCase/PascalCase conversion to kebab-case.
* Priority: custom aliases > built-in aliases > kebab-case conversion
*
* @param property - Property name (could be an alias, camelCase, or PascalCase)
* @returns Full CSS property name in kebab-case
*
* @example
* resolveProperty('bg'); // Returns: 'background'
* resolveProperty('background'); // Returns: 'background'
* resolveProperty('fontSize'); // Returns: 'font-size'
* resolveProperty('BackgroundColor'); // Returns: 'background-color'
*/
export function resolveProperty(property: string): string {
// Check custom aliases first (highest priority)
if (customAliases[property]) {
return customAliases[property];
}
// Check built-in aliases
if (DEFAULT_ALIASES[property]) {
return DEFAULT_ALIASES[property];
}
// Convert camelCase/PascalCase to kebab-case
return toKebabCase(property);
}
/**
* Check if a property name is a registered alias
*
* @param property - Property name to check
* @returns True if it's a registered alias
*/
export function isAlias(property: string): boolean {
return property in customAliases || property in DEFAULT_ALIASES;
}