@weser/styling 0.0.12 → 0.0.13
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/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/plugins/extend.d.ts +15 -0
- package/dist/plugins/extend.js +59 -0
- package/dist/plugins/unit.d.ts +4 -0
- package/dist/plugins/unit.js +36 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -5,5 +5,7 @@ export { default as prefixerPlugin, fallbacks as prefixerFallbacks, } from './pl
|
|
|
5
5
|
export { default as debugPlugin } from './plugins/debug.js';
|
|
6
6
|
export { default as enforceLonghandPlugin } from './plugins/enforceLonghand.js';
|
|
7
7
|
export { default as sortPropertyPlugin } from './plugins/sortProperty.js';
|
|
8
|
+
export { default as unitPlugin } from './plugins/unit.js';
|
|
9
|
+
export { default as extendPlugin, T_ExtendStyle } from './plugins/extend.js';
|
|
8
10
|
export { default as useCSSVariable } from './helpers/useCSSVariable.js';
|
|
9
11
|
export { T_Fallback, T_Style } from './types.js';
|
package/dist/index.js
CHANGED
|
@@ -5,4 +5,6 @@ export { default as prefixerPlugin, fallbacks as prefixerFallbacks, } from './pl
|
|
|
5
5
|
export { default as debugPlugin } from './plugins/debug.js';
|
|
6
6
|
export { default as enforceLonghandPlugin } from './plugins/enforceLonghand.js';
|
|
7
7
|
export { default as sortPropertyPlugin } from './plugins/sortProperty.js';
|
|
8
|
+
export { default as unitPlugin } from './plugins/unit.js';
|
|
9
|
+
export { default as extendPlugin } from './plugins/extend.js';
|
|
8
10
|
export { default as useCSSVariable } from './helpers/useCSSVariable.js';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { WithHooks } from '@css-hooks/core';
|
|
2
|
+
type Extension<T, Hooks> = {
|
|
3
|
+
condition: boolean | undefined;
|
|
4
|
+
style: T_ExtendStyle<T, Hooks>;
|
|
5
|
+
};
|
|
6
|
+
export type T_ExtendStyle<T, Hooks> = WithHooks<keyof Hooks & string, T> & {
|
|
7
|
+
extend?: Extension<T, Hooks> | T_ExtendStyle<T, Hooks> | Array<Extension<T, Hooks>>;
|
|
8
|
+
};
|
|
9
|
+
type T_Extend = Record<string, any> & {
|
|
10
|
+
condition?: never;
|
|
11
|
+
style?: never;
|
|
12
|
+
};
|
|
13
|
+
export default function extendPlugin(): typeof extend;
|
|
14
|
+
declare function extend<T extends T_Extend>(style: T): T;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import isPlainObject from 'isobject';
|
|
2
|
+
import { assignStyle } from 'css-in-js-utils';
|
|
3
|
+
export default function extendPlugin() {
|
|
4
|
+
return extend;
|
|
5
|
+
}
|
|
6
|
+
function extend(style) {
|
|
7
|
+
for (const property in style) {
|
|
8
|
+
const value = style[property];
|
|
9
|
+
if (property === 'extend') {
|
|
10
|
+
// @ts-ignore
|
|
11
|
+
const extensions = [].concat(value);
|
|
12
|
+
extensions.forEach((extension) => extendStyle(style, extension));
|
|
13
|
+
delete style[property];
|
|
14
|
+
// support nested extend as well
|
|
15
|
+
}
|
|
16
|
+
else if (isPlainObject(value)) {
|
|
17
|
+
// @ts-ignore
|
|
18
|
+
style[property] = extend(value);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return style;
|
|
22
|
+
}
|
|
23
|
+
function extendStyle(style, extension) {
|
|
24
|
+
// extend conditional style objects
|
|
25
|
+
if ('condition' in extension) {
|
|
26
|
+
if (extension.condition) {
|
|
27
|
+
assignStyle(style, extend(extension.style));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
// extend basic style objects
|
|
32
|
+
assignStyle(style, removeUndefined(extend(extension)));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function removeUndefined(style) {
|
|
36
|
+
for (const property in style) {
|
|
37
|
+
const value = style[property];
|
|
38
|
+
if (isPlainObject(value)) {
|
|
39
|
+
// @ts-ignore
|
|
40
|
+
style[property] = removeUndefined(value);
|
|
41
|
+
}
|
|
42
|
+
else if (Array.isArray(value)) {
|
|
43
|
+
style[property] = value.filter((item) => !isUndefinedValue(item));
|
|
44
|
+
}
|
|
45
|
+
else if (isUndefinedValue(value)) {
|
|
46
|
+
delete style[property];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return style;
|
|
50
|
+
}
|
|
51
|
+
const FALSY_REGEX = /undefined|null/;
|
|
52
|
+
const URL_REGEX = /url/;
|
|
53
|
+
function isUndefinedValue(value) {
|
|
54
|
+
return (value === undefined ||
|
|
55
|
+
value === null ||
|
|
56
|
+
(typeof value === 'string' &&
|
|
57
|
+
FALSY_REGEX.test(value) &&
|
|
58
|
+
!URL_REGEX.test(value)));
|
|
59
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { T_RawStyle, T_Style } from '../types.js';
|
|
2
|
+
type PropertyMap<T = T_RawStyle> = Partial<Record<keyof T, string>>;
|
|
3
|
+
export default function unitPlugin<T = T_Style>(defaultUnit?: string, propertyMap?: PropertyMap, isUnitlessProperty?: (property: string) => boolean): (style: T) => T;
|
|
4
|
+
export {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { isUnitlessProperty as defaultIsUnitlessProperty } from 'css-in-js-utils';
|
|
2
|
+
import isPlainObject from 'isobject';
|
|
3
|
+
export default function unitPlugin(defaultUnit, propertyMap, isUnitlessProperty) {
|
|
4
|
+
return function unit(style) {
|
|
5
|
+
return addUnit(style, defaultUnit, propertyMap, isUnitlessProperty);
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
function addUnit(style, defaultUnit = 'px', propertyMap = {}, isUnitlessProperty = defaultIsUnitlessProperty) {
|
|
9
|
+
for (const property in style) {
|
|
10
|
+
if (!isUnitlessProperty(property)) {
|
|
11
|
+
const cssValue = style[property];
|
|
12
|
+
const propertyUnit = propertyMap[property] || defaultUnit;
|
|
13
|
+
if (isPlainObject(cssValue)) {
|
|
14
|
+
// @ts-ignore
|
|
15
|
+
style[property] = addUnit(cssValue, defaultUnit, propertyMap, isUnitlessProperty);
|
|
16
|
+
}
|
|
17
|
+
else if (Array.isArray(cssValue)) {
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
style[property] = cssValue.map((val) => addUnitIfNeeded(val, propertyUnit));
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
style[property] = addUnitIfNeeded(cssValue, propertyUnit);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return style;
|
|
27
|
+
}
|
|
28
|
+
function addUnitIfNeeded(value, propertyUnit) {
|
|
29
|
+
const valueType = typeof value;
|
|
30
|
+
if ((valueType === 'number' ||
|
|
31
|
+
(valueType === 'string' && value == parseFloat(value))) &&
|
|
32
|
+
value != 0) {
|
|
33
|
+
return value + propertyUnit;
|
|
34
|
+
}
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"root":["../src/index.ts","../src/types.ts","../src/core/createrenderer.ts","../src/core/fallbackvalue.ts","../src/core/fallbackvalueplugin.ts","../src/core/getfallbackcss.ts","../src/core/getfallbackvariable.ts","../src/helpers/usecssvariable.ts","../src/modules/css-in-js-utils.d.ts","../src/modules/styles-debugger.d.ts","../src/plugins/debug.ts","../src/plugins/enforcelonghand.ts","../src/plugins/prefixer.ts","../src/plugins/responsivevalue.ts","../src/plugins/sortproperty.ts"],"version":"5.9.2"}
|
|
1
|
+
{"root":["../src/index.ts","../src/types.ts","../src/core/createrenderer.ts","../src/core/fallbackvalue.ts","../src/core/fallbackvalueplugin.ts","../src/core/getfallbackcss.ts","../src/core/getfallbackvariable.ts","../src/helpers/usecssvariable.ts","../src/modules/css-in-js-utils.d.ts","../src/modules/styles-debugger.d.ts","../src/plugins/debug.ts","../src/plugins/enforcelonghand.ts","../src/plugins/extend.ts","../src/plugins/prefixer.ts","../src/plugins/responsivevalue.ts","../src/plugins/sortproperty.ts","../src/plugins/unit.ts"],"version":"5.9.2"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weser/styling",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.13",
|
|
4
4
|
"description": "Utils for styling with CSS hooks",
|
|
5
5
|
"author": "Robin Weser <robin@weser.io>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -57,5 +57,5 @@
|
|
|
57
57
|
"styles-debugger": "^1.0.0",
|
|
58
58
|
"typescript": "^5.9.2"
|
|
59
59
|
},
|
|
60
|
-
"gitHead": "
|
|
60
|
+
"gitHead": "2e5430bd1d2f421a9ab85dff0918b89023f8039b"
|
|
61
61
|
}
|