@slimr/css 2.1.11 → 2.1.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +58 -50
- package/cjs/addCss.d.ts +23 -0
- package/cjs/addCss.js +38 -0
- package/cjs/addCss.js.map +1 -0
- package/cjs/addCss.ts +33 -0
- package/cjs/createClass.d.ts +43 -0
- package/cjs/{src/createClass.js → createClass.js} +78 -132
- package/cjs/createClass.js.map +1 -0
- package/cjs/createClass.ts +82 -211
- package/{esm/src → cjs}/index.d.ts +1 -0
- package/cjs/{src/index.js → index.js} +1 -0
- package/cjs/index.js.map +1 -0
- package/cjs/index.ts +1 -0
- package/cjs/{src/createClass.d.ts → shorthandProps.d.ts} +7 -47
- package/cjs/shorthandProps.js +51 -0
- package/cjs/shorthandProps.js.map +1 -0
- package/cjs/shorthandProps.ts +118 -0
- package/esm/addCss.d.ts +23 -0
- package/esm/addCss.js +34 -0
- package/esm/addCss.js.map +1 -0
- package/esm/addCss.ts +33 -0
- package/esm/createClass.d.ts +43 -0
- package/esm/{src/createClass.js → createClass.js} +76 -128
- package/esm/createClass.js.map +1 -0
- package/esm/createClass.ts +82 -211
- package/{cjs/src → esm}/index.d.ts +1 -0
- package/esm/{src/index.js → index.js} +1 -0
- package/esm/index.js.map +1 -0
- package/esm/index.ts +1 -0
- package/esm/{src/createClass.d.ts → shorthandProps.d.ts} +7 -47
- package/esm/shorthandProps.js +47 -0
- package/esm/shorthandProps.js.map +1 -0
- package/esm/shorthandProps.ts +118 -0
- package/package.json +4 -1
- package/src/addCss.ts +33 -0
- package/src/createClass.ts +82 -211
- package/src/index.ts +1 -0
- package/src/shorthandProps.ts +118 -0
- package/tsconfig.json +1 -1
- package/cjs/src/createClass.js.map +0 -1
- package/cjs/src/index.js.map +0 -1
- package/esm/src/createClass.js.map +0 -1
- package/esm/src/index.js.map +0 -1
package/cjs/createClass.ts
CHANGED
|
@@ -1,14 +1,88 @@
|
|
|
1
|
-
|
|
2
|
-
export const breakPoints = ['30em', '48em', '62em', '80em', '96em']
|
|
1
|
+
import {T2SProps, t2s} from '@slimr/util'
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
import {addCss} from './addCss.js'
|
|
4
|
+
import {expandShorthands} from './shorthandProps.js'
|
|
6
5
|
|
|
7
|
-
/**
|
|
8
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Joins class names and omits falsey props
|
|
8
|
+
*
|
|
9
|
+
* @param classes
|
|
10
|
+
* class names to be joined
|
|
11
|
+
*
|
|
12
|
+
* @returns a string of joined class names
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* classJoin('a', 'b', 'c') // 'a b c'
|
|
17
|
+
* classJoin('a', 0, 'b', null, 'c') // 'a b c'
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export function classJoin(...classes: (string | 0 | null | undefined)[]) {
|
|
9
21
|
return classes.filter(c => c && typeof c === 'string').join(' ')
|
|
10
22
|
}
|
|
11
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Upserts and returns a unique css class for a given css string
|
|
26
|
+
*
|
|
27
|
+
* @param cssString
|
|
28
|
+
* string or template string, to be injected. Shorthand props are supported (see Readme).
|
|
29
|
+
*
|
|
30
|
+
* @returns a unique class name
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* c1 = createClass('c: red;') // queues the create of new css class 's0'
|
|
35
|
+
* c2 = createClass('c: red;') // is duplicate so will return 's0'
|
|
36
|
+
* c3 = createClass`c: red;` // same
|
|
37
|
+
* c4 = css`c: red;` // same
|
|
38
|
+
* // c1 = c2 = c3 = c4
|
|
39
|
+
* <div className={css`c: red;`} /> // will resolve to 's0' like above
|
|
40
|
+
* c5 = css`c: blue;` // queues the create of new css class 's1'
|
|
41
|
+
* c6 = css`w: [100%, null, 400px]` // width = 100% on mobile and table, 400px on desktop
|
|
42
|
+
* ```
|
|
43
|
+
* ...and the queue will be executed next javascript tick
|
|
44
|
+
*/
|
|
45
|
+
export function createClass(...p: T2SProps) {
|
|
46
|
+
let css = t2s(...p)
|
|
47
|
+
if (!css) return ''
|
|
48
|
+
let className = createClass.history.get(css)
|
|
49
|
+
if (!className) {
|
|
50
|
+
className = 's' + createClass.count++
|
|
51
|
+
createClass.history.set(css, className)
|
|
52
|
+
|
|
53
|
+
css = deleteComments(css)
|
|
54
|
+
css = expandShorthands(css)
|
|
55
|
+
css = expandArrayValues(css)
|
|
56
|
+
const qs = findQueries(css)
|
|
57
|
+
|
|
58
|
+
for (const q of qs.reverse()) {
|
|
59
|
+
css = css.slice(0, q.start) + css.slice(q.end)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
css = `.${className}{\n${css}\n}\n`
|
|
63
|
+
css += qs
|
|
64
|
+
.map(q => {
|
|
65
|
+
if (q.query.startsWith('&')) {
|
|
66
|
+
return `.${className}${q.query.slice(1)}{\n${q.innerBody}\n}`
|
|
67
|
+
}
|
|
68
|
+
if (q.query.startsWith('@keyframes')) {
|
|
69
|
+
return q.outerBody
|
|
70
|
+
}
|
|
71
|
+
return `${q.query}{\n.${className}{${q.innerBody}\n}\n}`
|
|
72
|
+
})
|
|
73
|
+
.join('\n')
|
|
74
|
+
|
|
75
|
+
css = trimByLine(css) + '\n\n'
|
|
76
|
+
|
|
77
|
+
addCss(css)
|
|
78
|
+
}
|
|
79
|
+
return className
|
|
80
|
+
}
|
|
81
|
+
/** Breakpoints like chakra */
|
|
82
|
+
createClass.breakPoints = ['30em', '48em', '62em', '80em', '96em']
|
|
83
|
+
createClass.count = 0
|
|
84
|
+
createClass.history = new Map<string, string>()
|
|
85
|
+
|
|
12
86
|
/** delete css comments **/
|
|
13
87
|
function deleteComments(css: string) {
|
|
14
88
|
return css.replace(/{\/\*[\s\S]*?(?=\*\/})\*\/}/gm, '')
|
|
@@ -35,7 +109,7 @@ function expandArrayValues(css: string) {
|
|
|
35
109
|
if (i === 0) {
|
|
36
110
|
return `${prop}: ${val};`
|
|
37
111
|
}
|
|
38
|
-
return `@media (min-width: ${breakPoints[i - 1]}) { ${prop}: ${val}; }`
|
|
112
|
+
return `@media (min-width: ${createClass.breakPoints[i - 1]}) { ${prop}: ${val}; }`
|
|
39
113
|
})
|
|
40
114
|
.join('\n')
|
|
41
115
|
}
|
|
@@ -44,117 +118,6 @@ function expandArrayValues(css: string) {
|
|
|
44
118
|
.join('\n')
|
|
45
119
|
}
|
|
46
120
|
|
|
47
|
-
export interface ShorthandProps {
|
|
48
|
-
/** shorthand for css:align-items */
|
|
49
|
-
ai?: string
|
|
50
|
-
/** shorthand for css:border */
|
|
51
|
-
b?: string | number
|
|
52
|
-
/** shorthand for css:border-radius */
|
|
53
|
-
br?: string | number
|
|
54
|
-
/** shorthand for css:background */
|
|
55
|
-
bg?: string
|
|
56
|
-
/** shorthand for css:color */
|
|
57
|
-
c?: string
|
|
58
|
-
/** shorthand for css:display */
|
|
59
|
-
d?: string
|
|
60
|
-
/** shorthand for css:flex */
|
|
61
|
-
f?: string
|
|
62
|
-
/** shorthand for css:flex-direction */
|
|
63
|
-
fd?: string
|
|
64
|
-
/** shorthand for css:height */
|
|
65
|
-
h?: number | string
|
|
66
|
-
/** shorthand for css:inset */
|
|
67
|
-
i?: number | string
|
|
68
|
-
/** shorthand for css:justify-content */
|
|
69
|
-
jc?: string
|
|
70
|
-
/** shorthand for css:margin */
|
|
71
|
-
m?: number | string
|
|
72
|
-
/** shorthand for css:margin-left */
|
|
73
|
-
ml?: number | string
|
|
74
|
-
/** shorthand for css:margin-right */
|
|
75
|
-
mr?: number | string
|
|
76
|
-
/** shorthand for css:margin-top */
|
|
77
|
-
mt?: number | string
|
|
78
|
-
/** shorthand for css:margin-bottom */
|
|
79
|
-
mb?: number | string
|
|
80
|
-
/** shorthand for css:margin-top & margin-bottom */
|
|
81
|
-
my?: number | string
|
|
82
|
-
/** shorthand for css:margin-left & margin-right */
|
|
83
|
-
mx?: number | string
|
|
84
|
-
/** shorthand for css:max-width */
|
|
85
|
-
maxW?: number | string
|
|
86
|
-
/** shorthand for css:min-width */
|
|
87
|
-
minW?: number | string
|
|
88
|
-
/** shorthand for css:padding */
|
|
89
|
-
p?: number | string
|
|
90
|
-
/** shorthand for css:padding-left */
|
|
91
|
-
pl?: number | string
|
|
92
|
-
/** shorthand for css:padding-right */
|
|
93
|
-
pr?: number | string
|
|
94
|
-
/** shorthand for css:padding-top */
|
|
95
|
-
pt?: number | string
|
|
96
|
-
/** shorthand for css:padding-bottom */
|
|
97
|
-
pb?: number | string
|
|
98
|
-
/** shorthand for css:padding-top & padding-bottom */
|
|
99
|
-
py?: number | string
|
|
100
|
-
/** shorthand for css:padding-left & padding-right */
|
|
101
|
-
px?: number | string
|
|
102
|
-
/** shorthand for css:position */
|
|
103
|
-
pos?: number | string
|
|
104
|
-
/** shorthand for css:text-align */
|
|
105
|
-
ta?: string
|
|
106
|
-
/** shorthand for css:width */
|
|
107
|
-
w?: number | string
|
|
108
|
-
/** shorthand for css:z-index */
|
|
109
|
-
z?: number | string
|
|
110
|
-
}
|
|
111
|
-
export const shorthandPropsMap: Record<
|
|
112
|
-
keyof Omit<ShorthandProps, 'mx' | 'my' | 'px' | 'py'>,
|
|
113
|
-
string
|
|
114
|
-
> = {
|
|
115
|
-
ai: 'align-items',
|
|
116
|
-
b: 'border',
|
|
117
|
-
br: 'border-radius',
|
|
118
|
-
bg: 'background',
|
|
119
|
-
c: 'color',
|
|
120
|
-
d: 'display',
|
|
121
|
-
f: 'flex',
|
|
122
|
-
fd: 'flex-direction',
|
|
123
|
-
i: 'inset',
|
|
124
|
-
h: 'height',
|
|
125
|
-
jc: 'justify-content',
|
|
126
|
-
m: 'margin',
|
|
127
|
-
ml: 'margin-left',
|
|
128
|
-
mr: 'margin-right',
|
|
129
|
-
mt: 'margin-top',
|
|
130
|
-
mb: 'margin-bottom',
|
|
131
|
-
maxW: 'max-width',
|
|
132
|
-
minW: 'min-width',
|
|
133
|
-
p: 'padding',
|
|
134
|
-
pl: 'padding-left',
|
|
135
|
-
pr: 'padding-right',
|
|
136
|
-
pt: 'padding-top',
|
|
137
|
-
pb: 'padding-bottom',
|
|
138
|
-
pos: 'position',
|
|
139
|
-
ta: 'text-align',
|
|
140
|
-
w: 'width',
|
|
141
|
-
z: 'z-index',
|
|
142
|
-
}
|
|
143
|
-
export const shorthandProps = [...Object.keys(shorthandPropsMap), 'mx', 'my', 'px', 'py']
|
|
144
|
-
|
|
145
|
-
/** Expand short-hand css props to full */
|
|
146
|
-
function expandProps(css: string) {
|
|
147
|
-
css = '\n' + css // inject a newline to make the regex easier
|
|
148
|
-
// Handle 'mx', 'my', 'px', 'py'
|
|
149
|
-
css = css
|
|
150
|
-
.replace(/([mp])x:([^;]*)/g, '$1l:$2;\n$1r:$2')
|
|
151
|
-
.replace(/([mp])y:([^;]*);/g, '$1t:$2;\n$1b:$2')
|
|
152
|
-
Object.entries(shorthandPropsMap).forEach(([k, v]) => {
|
|
153
|
-
css = css.replace(new RegExp(`([ \n\t;])${k}:`, 'g'), `$1${v}:`)
|
|
154
|
-
})
|
|
155
|
-
return css.trim()
|
|
156
|
-
}
|
|
157
|
-
|
|
158
121
|
/** Find @keyframes, @media, @container queries in css **/
|
|
159
122
|
function findQueries(css: string) {
|
|
160
123
|
const queries = []
|
|
@@ -189,102 +152,10 @@ function findQueries(css: string) {
|
|
|
189
152
|
}
|
|
190
153
|
|
|
191
154
|
/** Trims whitespace for every line */
|
|
192
|
-
function
|
|
155
|
+
function trimByLine(css: string) {
|
|
193
156
|
return css
|
|
194
157
|
.split('\n')
|
|
195
158
|
.map(l => l.trim())
|
|
196
159
|
.filter(l => l)
|
|
197
160
|
.join('\n')
|
|
198
161
|
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Assemble a string from a template string array and a list of placeholders.
|
|
202
|
-
*
|
|
203
|
-
* - If the first argument is a string, it is returned as is.
|
|
204
|
-
*
|
|
205
|
-
* i.e.
|
|
206
|
-
* myFoo(...props: TemplateStringProps) => {
|
|
207
|
-
* const inputStr = t2s(...props);
|
|
208
|
-
* }
|
|
209
|
-
* myFoo`hello ${name}` => 'hello world'
|
|
210
|
-
* myFoo(`hello ${name}`) => 'hello world'
|
|
211
|
-
*/
|
|
212
|
-
export function t2s(...tsp: TemplateStringProps) {
|
|
213
|
-
const [s, ...p] = tsp
|
|
214
|
-
return typeof s === 'string' ? s : s.map((s, i) => s + (p?.[i] ?? '')).join('')
|
|
215
|
-
}
|
|
216
|
-
export type TemplateStringProps = [
|
|
217
|
-
strings: TemplateStringsArray | string,
|
|
218
|
-
...placeHolders: string[]
|
|
219
|
-
]
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* Injects css to the page
|
|
223
|
-
*
|
|
224
|
-
* - Batches adds for performance
|
|
225
|
-
*
|
|
226
|
-
* @param css - css to be injected
|
|
227
|
-
* @returns void
|
|
228
|
-
*/
|
|
229
|
-
export function addCss(css: string) {
|
|
230
|
-
addCss.que.add(css)
|
|
231
|
-
setTimeout(() => {
|
|
232
|
-
const css = [...addCss.que].join('\n')
|
|
233
|
-
if (css) {
|
|
234
|
-
addCss.que.clear()
|
|
235
|
-
const s = document.createElement('style')
|
|
236
|
-
s.id = `u${addCss.count++}`
|
|
237
|
-
s.innerHTML = css
|
|
238
|
-
document.head.appendChild(s)
|
|
239
|
-
}
|
|
240
|
-
}, 0)
|
|
241
|
-
}
|
|
242
|
-
addCss.que = new Set<string>()
|
|
243
|
-
addCss.count = 0
|
|
244
|
-
|
|
245
|
-
/**
|
|
246
|
-
* Injects css and creates unique class names
|
|
247
|
-
*
|
|
248
|
-
* - Skips if already added
|
|
249
|
-
*
|
|
250
|
-
* @param css - string or template string, to be injected
|
|
251
|
-
* @returns a unique class name
|
|
252
|
-
*/
|
|
253
|
-
export function createClass(...p: TemplateStringProps) {
|
|
254
|
-
let css = t2s(...p)
|
|
255
|
-
if (!css) return ''
|
|
256
|
-
let className = createClass.history.get(css)
|
|
257
|
-
if (!className) {
|
|
258
|
-
className = 's' + createClass.count++
|
|
259
|
-
createClass.history.set(css, className)
|
|
260
|
-
|
|
261
|
-
css = deleteComments(css)
|
|
262
|
-
css = expandProps(css)
|
|
263
|
-
css = expandArrayValues(css)
|
|
264
|
-
const qs = findQueries(css)
|
|
265
|
-
|
|
266
|
-
for (const q of qs.reverse()) {
|
|
267
|
-
css = css.slice(0, q.start) + css.slice(q.end)
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
css = `.${className}{\n${css}\n}\n`
|
|
271
|
-
css += qs
|
|
272
|
-
.map(q => {
|
|
273
|
-
if (q.query.startsWith('&')) {
|
|
274
|
-
return `.${className}${q.query.slice(1)}{\n${q.innerBody}\n}`
|
|
275
|
-
}
|
|
276
|
-
if (q.query.startsWith('@keyframes')) {
|
|
277
|
-
return q.outerBody
|
|
278
|
-
}
|
|
279
|
-
return `${q.query}{\n.${className}{${q.innerBody}\n}\n}`
|
|
280
|
-
})
|
|
281
|
-
.join('\n')
|
|
282
|
-
|
|
283
|
-
css = trim(css) + '\n\n'
|
|
284
|
-
|
|
285
|
-
addCss(css)
|
|
286
|
-
}
|
|
287
|
-
return className
|
|
288
|
-
}
|
|
289
|
-
createClass.count = 0
|
|
290
|
-
createClass.history = new Map<string, string>()
|
|
@@ -16,6 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
exports.css = void 0;
|
|
18
18
|
__exportStar(require("./createClass.js"), exports);
|
|
19
|
+
__exportStar(require("./shorthandProps.js"), exports);
|
|
19
20
|
var createClass_js_1 = require("./createClass.js");
|
|
20
21
|
Object.defineProperty(exports, "css", { enumerable: true, get: function () { return createClass_js_1.createClass; } });
|
|
21
22
|
//# sourceMappingURL=index.js.map
|
package/cjs/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,mDAAgC;AAChC,sDAAmC;AACnC,mDAAmD;AAA3C,qGAAA,WAAW,OAAO"}
|
package/cjs/index.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
/** Joins class names and filters out blanks */
|
|
5
|
-
export declare function classJoin(...classes: allowableAny[]): string;
|
|
1
|
+
/**
|
|
2
|
+
* All the shorthand props that can be used in the css prop
|
|
3
|
+
*/
|
|
6
4
|
export interface ShorthandProps {
|
|
7
5
|
/** shorthand for css:align-items */
|
|
8
6
|
ai?: string;
|
|
@@ -67,49 +65,11 @@ export interface ShorthandProps {
|
|
|
67
65
|
/** shorthand for css:z-index */
|
|
68
66
|
z?: number | string;
|
|
69
67
|
}
|
|
70
|
-
export declare const shorthandPropsMap: Record<keyof Omit<ShorthandProps, 'mx' | 'my' | 'px' | 'py'>, string>;
|
|
71
|
-
export declare const shorthandProps: string[];
|
|
72
|
-
/**
|
|
73
|
-
* Assemble a string from a template string array and a list of placeholders.
|
|
74
|
-
*
|
|
75
|
-
* - If the first argument is a string, it is returned as is.
|
|
76
|
-
*
|
|
77
|
-
* i.e.
|
|
78
|
-
* myFoo(...props: TemplateStringProps) => {
|
|
79
|
-
* const inputStr = t2s(...props);
|
|
80
|
-
* }
|
|
81
|
-
* myFoo`hello ${name}` => 'hello world'
|
|
82
|
-
* myFoo(`hello ${name}`) => 'hello world'
|
|
83
|
-
*/
|
|
84
|
-
export declare function t2s(...tsp: TemplateStringProps): string;
|
|
85
|
-
export type TemplateStringProps = [
|
|
86
|
-
strings: TemplateStringsArray | string,
|
|
87
|
-
...placeHolders: string[]
|
|
88
|
-
];
|
|
89
68
|
/**
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
* - Batches adds for performance
|
|
93
|
-
*
|
|
94
|
-
* @param css - css to be injected
|
|
95
|
-
* @returns void
|
|
69
|
+
* Expand short-hand css props to full
|
|
96
70
|
*/
|
|
97
|
-
export declare function
|
|
98
|
-
export declare namespace addCss {
|
|
99
|
-
var que: Set<string>;
|
|
100
|
-
var count: number;
|
|
101
|
-
}
|
|
71
|
+
export declare function expandShorthands(css: string): string;
|
|
102
72
|
/**
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
* - Skips if already added
|
|
106
|
-
*
|
|
107
|
-
* @param css - string or template string, to be injected
|
|
108
|
-
* @returns a unique class name
|
|
73
|
+
* Map shorthand props to their full css prop
|
|
109
74
|
*/
|
|
110
|
-
export declare
|
|
111
|
-
export declare namespace createClass {
|
|
112
|
-
var count: number;
|
|
113
|
-
var history: Map<string, string>;
|
|
114
|
-
}
|
|
115
|
-
export {};
|
|
75
|
+
export declare const shorthandPropsMap: Record<keyof Omit<ShorthandProps, 'mx' | 'my' | 'px' | 'py'>, string>;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.shorthandPropsMap = exports.expandShorthands = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Expand short-hand css props to full
|
|
6
|
+
*/
|
|
7
|
+
function expandShorthands(css) {
|
|
8
|
+
css = '\n' + css; // inject a newline to make the regex easier
|
|
9
|
+
// Handle 'mx', 'my', 'px', 'py'
|
|
10
|
+
css = css
|
|
11
|
+
.replace(/([mp])x:([^;]*)/g, '$1l:$2;\n$1r:$2')
|
|
12
|
+
.replace(/([mp])y:([^;]*);/g, '$1t:$2;\n$1b:$2');
|
|
13
|
+
Object.entries(exports.shorthandPropsMap).forEach(([k, v]) => {
|
|
14
|
+
css = css.replace(new RegExp(`([ \n\t;])${k}:`, 'g'), `$1${v}:`);
|
|
15
|
+
});
|
|
16
|
+
return css.trim();
|
|
17
|
+
}
|
|
18
|
+
exports.expandShorthands = expandShorthands;
|
|
19
|
+
/**
|
|
20
|
+
* Map shorthand props to their full css prop
|
|
21
|
+
*/
|
|
22
|
+
exports.shorthandPropsMap = {
|
|
23
|
+
ai: 'align-items',
|
|
24
|
+
b: 'border',
|
|
25
|
+
br: 'border-radius',
|
|
26
|
+
bg: 'background',
|
|
27
|
+
c: 'color',
|
|
28
|
+
d: 'display',
|
|
29
|
+
f: 'flex',
|
|
30
|
+
fd: 'flex-direction',
|
|
31
|
+
i: 'inset',
|
|
32
|
+
h: 'height',
|
|
33
|
+
jc: 'justify-content',
|
|
34
|
+
m: 'margin',
|
|
35
|
+
ml: 'margin-left',
|
|
36
|
+
mr: 'margin-right',
|
|
37
|
+
mt: 'margin-top',
|
|
38
|
+
mb: 'margin-bottom',
|
|
39
|
+
maxW: 'max-width',
|
|
40
|
+
minW: 'min-width',
|
|
41
|
+
p: 'padding',
|
|
42
|
+
pl: 'padding-left',
|
|
43
|
+
pr: 'padding-right',
|
|
44
|
+
pt: 'padding-top',
|
|
45
|
+
pb: 'padding-bottom',
|
|
46
|
+
pos: 'position',
|
|
47
|
+
ta: 'text-align',
|
|
48
|
+
w: 'width',
|
|
49
|
+
z: 'z-index',
|
|
50
|
+
};
|
|
51
|
+
//# sourceMappingURL=shorthandProps.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shorthandProps.js","sourceRoot":"","sources":["../src/shorthandProps.ts"],"names":[],"mappings":";;;AAoEA;;GAEG;AACH,SAAgB,gBAAgB,CAAC,GAAW;IAC1C,GAAG,GAAG,IAAI,GAAG,GAAG,CAAA,CAAC,4CAA4C;IAC7D,gCAAgC;IAChC,GAAG,GAAG,GAAG;SACN,OAAO,CAAC,kBAAkB,EAAE,iBAAiB,CAAC;SAC9C,OAAO,CAAC,mBAAmB,EAAE,iBAAiB,CAAC,CAAA;IAClD,MAAM,CAAC,OAAO,CAAC,yBAAiB,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE;QACnD,GAAG,GAAG,GAAG,CAAC,OAAO,CAAC,IAAI,MAAM,CAAC,aAAa,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,KAAK,CAAC,GAAG,CAAC,CAAA;IAClE,CAAC,CAAC,CAAA;IACF,OAAO,GAAG,CAAC,IAAI,EAAE,CAAA;AACnB,CAAC;AAVD,4CAUC;AAED;;GAEG;AACU,QAAA,iBAAiB,GAG1B;IACF,EAAE,EAAE,aAAa;IACjB,CAAC,EAAE,QAAQ;IACX,EAAE,EAAE,eAAe;IACnB,EAAE,EAAE,YAAY;IAChB,CAAC,EAAE,OAAO;IACV,CAAC,EAAE,SAAS;IACZ,CAAC,EAAE,MAAM;IACT,EAAE,EAAE,gBAAgB;IACpB,CAAC,EAAE,OAAO;IACV,CAAC,EAAE,QAAQ;IACX,EAAE,EAAE,iBAAiB;IACrB,CAAC,EAAE,QAAQ;IACX,EAAE,EAAE,aAAa;IACjB,EAAE,EAAE,cAAc;IAClB,EAAE,EAAE,YAAY;IAChB,EAAE,EAAE,eAAe;IACnB,IAAI,EAAE,WAAW;IACjB,IAAI,EAAE,WAAW;IACjB,CAAC,EAAE,SAAS;IACZ,EAAE,EAAE,cAAc;IAClB,EAAE,EAAE,eAAe;IACnB,EAAE,EAAE,aAAa;IACjB,EAAE,EAAE,gBAAgB;IACpB,GAAG,EAAE,UAAU;IACf,EAAE,EAAE,YAAY;IAChB,CAAC,EAAE,OAAO;IACV,CAAC,EAAE,SAAS;CACb,CAAA"}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* All the shorthand props that can be used in the css prop
|
|
3
|
+
*/
|
|
4
|
+
export interface ShorthandProps {
|
|
5
|
+
/** shorthand for css:align-items */
|
|
6
|
+
ai?: string
|
|
7
|
+
/** shorthand for css:border */
|
|
8
|
+
b?: string | number
|
|
9
|
+
/** shorthand for css:border-radius */
|
|
10
|
+
br?: string | number
|
|
11
|
+
/** shorthand for css:background */
|
|
12
|
+
bg?: string
|
|
13
|
+
/** shorthand for css:color */
|
|
14
|
+
c?: string
|
|
15
|
+
/** shorthand for css:display */
|
|
16
|
+
d?: string
|
|
17
|
+
/** shorthand for css:flex */
|
|
18
|
+
f?: string
|
|
19
|
+
/** shorthand for css:flex-direction */
|
|
20
|
+
fd?: string
|
|
21
|
+
/** shorthand for css:height */
|
|
22
|
+
h?: number | string
|
|
23
|
+
/** shorthand for css:inset */
|
|
24
|
+
i?: number | string
|
|
25
|
+
/** shorthand for css:justify-content */
|
|
26
|
+
jc?: string
|
|
27
|
+
/** shorthand for css:margin */
|
|
28
|
+
m?: number | string
|
|
29
|
+
/** shorthand for css:margin-left */
|
|
30
|
+
ml?: number | string
|
|
31
|
+
/** shorthand for css:margin-right */
|
|
32
|
+
mr?: number | string
|
|
33
|
+
/** shorthand for css:margin-top */
|
|
34
|
+
mt?: number | string
|
|
35
|
+
/** shorthand for css:margin-bottom */
|
|
36
|
+
mb?: number | string
|
|
37
|
+
/** shorthand for css:margin-top & margin-bottom */
|
|
38
|
+
my?: number | string
|
|
39
|
+
/** shorthand for css:margin-left & margin-right */
|
|
40
|
+
mx?: number | string
|
|
41
|
+
/** shorthand for css:max-width */
|
|
42
|
+
maxW?: number | string
|
|
43
|
+
/** shorthand for css:min-width */
|
|
44
|
+
minW?: number | string
|
|
45
|
+
/** shorthand for css:padding */
|
|
46
|
+
p?: number | string
|
|
47
|
+
/** shorthand for css:padding-left */
|
|
48
|
+
pl?: number | string
|
|
49
|
+
/** shorthand for css:padding-right */
|
|
50
|
+
pr?: number | string
|
|
51
|
+
/** shorthand for css:padding-top */
|
|
52
|
+
pt?: number | string
|
|
53
|
+
/** shorthand for css:padding-bottom */
|
|
54
|
+
pb?: number | string
|
|
55
|
+
/** shorthand for css:padding-top & padding-bottom */
|
|
56
|
+
py?: number | string
|
|
57
|
+
/** shorthand for css:padding-left & padding-right */
|
|
58
|
+
px?: number | string
|
|
59
|
+
/** shorthand for css:position */
|
|
60
|
+
pos?: number | string
|
|
61
|
+
/** shorthand for css:text-align */
|
|
62
|
+
ta?: string
|
|
63
|
+
/** shorthand for css:width */
|
|
64
|
+
w?: number | string
|
|
65
|
+
/** shorthand for css:z-index */
|
|
66
|
+
z?: number | string
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Expand short-hand css props to full
|
|
71
|
+
*/
|
|
72
|
+
export function expandShorthands(css: string) {
|
|
73
|
+
css = '\n' + css // inject a newline to make the regex easier
|
|
74
|
+
// Handle 'mx', 'my', 'px', 'py'
|
|
75
|
+
css = css
|
|
76
|
+
.replace(/([mp])x:([^;]*)/g, '$1l:$2;\n$1r:$2')
|
|
77
|
+
.replace(/([mp])y:([^;]*);/g, '$1t:$2;\n$1b:$2')
|
|
78
|
+
Object.entries(shorthandPropsMap).forEach(([k, v]) => {
|
|
79
|
+
css = css.replace(new RegExp(`([ \n\t;])${k}:`, 'g'), `$1${v}:`)
|
|
80
|
+
})
|
|
81
|
+
return css.trim()
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Map shorthand props to their full css prop
|
|
86
|
+
*/
|
|
87
|
+
export const shorthandPropsMap: Record<
|
|
88
|
+
keyof Omit<ShorthandProps, 'mx' | 'my' | 'px' | 'py'>,
|
|
89
|
+
string
|
|
90
|
+
> = {
|
|
91
|
+
ai: 'align-items',
|
|
92
|
+
b: 'border',
|
|
93
|
+
br: 'border-radius',
|
|
94
|
+
bg: 'background',
|
|
95
|
+
c: 'color',
|
|
96
|
+
d: 'display',
|
|
97
|
+
f: 'flex',
|
|
98
|
+
fd: 'flex-direction',
|
|
99
|
+
i: 'inset',
|
|
100
|
+
h: 'height',
|
|
101
|
+
jc: 'justify-content',
|
|
102
|
+
m: 'margin',
|
|
103
|
+
ml: 'margin-left',
|
|
104
|
+
mr: 'margin-right',
|
|
105
|
+
mt: 'margin-top',
|
|
106
|
+
mb: 'margin-bottom',
|
|
107
|
+
maxW: 'max-width',
|
|
108
|
+
minW: 'min-width',
|
|
109
|
+
p: 'padding',
|
|
110
|
+
pl: 'padding-left',
|
|
111
|
+
pr: 'padding-right',
|
|
112
|
+
pt: 'padding-top',
|
|
113
|
+
pb: 'padding-bottom',
|
|
114
|
+
pos: 'position',
|
|
115
|
+
ta: 'text-align',
|
|
116
|
+
w: 'width',
|
|
117
|
+
z: 'z-index',
|
|
118
|
+
}
|
package/esm/addCss.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Injects css to the page head
|
|
3
|
+
*
|
|
4
|
+
* - Lazy: Will not be added until the component mounts, if called from within a component
|
|
5
|
+
* - Batches dom changes for better performance
|
|
6
|
+
* - Has local cache to recall prior adds, to reduce duplicates and dom changes
|
|
7
|
+
*
|
|
8
|
+
* @param css
|
|
9
|
+
* css to be injected
|
|
10
|
+
*
|
|
11
|
+
* @returns void
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* addCss(`.myClass { color: red; }`) // queues css for injection
|
|
15
|
+
* addCss(`.myClass { color: red; }`) // is duplicate so will not be added
|
|
16
|
+
* addCss(`.myClass { color: blue; }`) // queues css for injection
|
|
17
|
+
* // ...and the queue will be executed next javascript tick
|
|
18
|
+
*/
|
|
19
|
+
export declare function addCss(css: string): void;
|
|
20
|
+
export declare namespace addCss {
|
|
21
|
+
var que: Set<string>;
|
|
22
|
+
var count: number;
|
|
23
|
+
}
|
package/esm/addCss.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Injects css to the page head
|
|
3
|
+
*
|
|
4
|
+
* - Lazy: Will not be added until the component mounts, if called from within a component
|
|
5
|
+
* - Batches dom changes for better performance
|
|
6
|
+
* - Has local cache to recall prior adds, to reduce duplicates and dom changes
|
|
7
|
+
*
|
|
8
|
+
* @param css
|
|
9
|
+
* css to be injected
|
|
10
|
+
*
|
|
11
|
+
* @returns void
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* addCss(`.myClass { color: red; }`) // queues css for injection
|
|
15
|
+
* addCss(`.myClass { color: red; }`) // is duplicate so will not be added
|
|
16
|
+
* addCss(`.myClass { color: blue; }`) // queues css for injection
|
|
17
|
+
* // ...and the queue will be executed next javascript tick
|
|
18
|
+
*/
|
|
19
|
+
export function addCss(css) {
|
|
20
|
+
addCss.que.add(css);
|
|
21
|
+
setTimeout(() => {
|
|
22
|
+
const css = [...addCss.que].join('\n');
|
|
23
|
+
if (css) {
|
|
24
|
+
addCss.que.clear();
|
|
25
|
+
const s = document.createElement('style');
|
|
26
|
+
s.id = `u${addCss.count++}`;
|
|
27
|
+
s.innerHTML = css;
|
|
28
|
+
document.head.appendChild(s);
|
|
29
|
+
}
|
|
30
|
+
}, 0);
|
|
31
|
+
}
|
|
32
|
+
addCss.que = new Set();
|
|
33
|
+
addCss.count = 0;
|
|
34
|
+
//# sourceMappingURL=addCss.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"addCss.js","sourceRoot":"","sources":["../src/addCss.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,MAAM,CAAC,GAAW;IAChC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IACnB,UAAU,CAAC,GAAG,EAAE;QACd,MAAM,GAAG,GAAG,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QACtC,IAAI,GAAG,EAAE;YACP,MAAM,CAAC,GAAG,CAAC,KAAK,EAAE,CAAA;YAClB,MAAM,CAAC,GAAG,QAAQ,CAAC,aAAa,CAAC,OAAO,CAAC,CAAA;YACzC,CAAC,CAAC,EAAE,GAAG,IAAI,MAAM,CAAC,KAAK,EAAE,EAAE,CAAA;YAC3B,CAAC,CAAC,SAAS,GAAG,GAAG,CAAA;YACjB,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,CAAA;SAC7B;IACH,CAAC,EAAE,CAAC,CAAC,CAAA;AACP,CAAC;AACD,MAAM,CAAC,GAAG,GAAG,IAAI,GAAG,EAAU,CAAA;AAC9B,MAAM,CAAC,KAAK,GAAG,CAAC,CAAA"}
|
package/esm/addCss.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Injects css to the page head
|
|
3
|
+
*
|
|
4
|
+
* - Lazy: Will not be added until the component mounts, if called from within a component
|
|
5
|
+
* - Batches dom changes for better performance
|
|
6
|
+
* - Has local cache to recall prior adds, to reduce duplicates and dom changes
|
|
7
|
+
*
|
|
8
|
+
* @param css
|
|
9
|
+
* css to be injected
|
|
10
|
+
*
|
|
11
|
+
* @returns void
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* addCss(`.myClass { color: red; }`) // queues css for injection
|
|
15
|
+
* addCss(`.myClass { color: red; }`) // is duplicate so will not be added
|
|
16
|
+
* addCss(`.myClass { color: blue; }`) // queues css for injection
|
|
17
|
+
* // ...and the queue will be executed next javascript tick
|
|
18
|
+
*/
|
|
19
|
+
export function addCss(css: string) {
|
|
20
|
+
addCss.que.add(css)
|
|
21
|
+
setTimeout(() => {
|
|
22
|
+
const css = [...addCss.que].join('\n')
|
|
23
|
+
if (css) {
|
|
24
|
+
addCss.que.clear()
|
|
25
|
+
const s = document.createElement('style')
|
|
26
|
+
s.id = `u${addCss.count++}`
|
|
27
|
+
s.innerHTML = css
|
|
28
|
+
document.head.appendChild(s)
|
|
29
|
+
}
|
|
30
|
+
}, 0)
|
|
31
|
+
}
|
|
32
|
+
addCss.que = new Set<string>()
|
|
33
|
+
addCss.count = 0
|