css-in-props 3.4.4 → 3.4.6

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.
@@ -0,0 +1,15 @@
1
+ const MISC_PROPS = {
2
+ overflow: (value) => ({
3
+ overflow: value,
4
+ scrollBehavior: "smooth"
5
+ }),
6
+ cursor: (value, el, s, ctx) => {
7
+ if (!value) return;
8
+ const file = ctx.files && ctx.files[value];
9
+ if (file && file.content) value = `url(${file.content.src})`;
10
+ return { cursor: value };
11
+ }
12
+ };
13
+ export {
14
+ MISC_PROPS
15
+ };
@@ -0,0 +1,31 @@
1
+ import { getSpacingByKey } from "@symbo.ls/scratch";
2
+ const POSITION_PROPS = {
3
+ inset: (val, el) => {
4
+ if (el.call("isNumber", val)) return { inset: val };
5
+ if (!el.call("isString", val)) return;
6
+ return { inset: val.split(" ").map((v) => getSpacingByKey(v, "k").k).join(" ") };
7
+ },
8
+ left: (val) => getSpacingByKey(val, "left"),
9
+ top: (val) => getSpacingByKey(val, "top"),
10
+ right: (val) => getSpacingByKey(val, "right"),
11
+ bottom: (val) => getSpacingByKey(val, "bottom"),
12
+ verticalInset: (val) => {
13
+ if (typeof val !== "string") return;
14
+ const vi = val.split(" ").map((v) => getSpacingByKey(v, "k").k);
15
+ return {
16
+ top: vi[0],
17
+ bottom: vi[1] || vi[0]
18
+ };
19
+ },
20
+ horizontalInset: (val) => {
21
+ if (typeof val !== "string") return;
22
+ const vi = val.split(" ").map((v) => getSpacingByKey(v, "k").k);
23
+ return {
24
+ left: vi[0],
25
+ right: vi[1] || vi[0]
26
+ };
27
+ }
28
+ };
29
+ export {
30
+ POSITION_PROPS
31
+ };
@@ -0,0 +1,139 @@
1
+ import {
2
+ getMediaTheme,
3
+ getMediaColor,
4
+ transformTextStroke,
5
+ transformShadow,
6
+ transformBoxShadow,
7
+ transformBorder,
8
+ transformBackgroundImage,
9
+ resolveColorsInGradient,
10
+ transformSizeRatio
11
+ } from "@symbo.ls/scratch";
12
+ import { isDefined, isString } from "@domql/utils";
13
+ const getSystemGlobalTheme = ({ context, state }) => {
14
+ const theme = state?.root?.globalTheme || context.designSystem?.globalTheme;
15
+ return theme === "auto" ? null : theme;
16
+ };
17
+ const THEME_PROPS = {
18
+ theme: (val, element) => {
19
+ const { props } = element;
20
+ const globalTheme = getSystemGlobalTheme(element);
21
+ if (!val) return;
22
+ const hasSubtheme = val.includes(" ") && !val.includes("@");
23
+ const globalThemeForced = `@${props.themeModifier || globalTheme}`;
24
+ if (hasSubtheme) {
25
+ const themeAppliedInVal = val.split(" ");
26
+ themeAppliedInVal.splice(1, 0, globalThemeForced);
27
+ return getMediaTheme(themeAppliedInVal);
28
+ } else if (val.includes("@{globalTheme}")) val.replace("@{globalTheme}", globalThemeForced);
29
+ return getMediaTheme(val, `@${props.themeModifier || globalTheme}`);
30
+ },
31
+ color: (val, element) => {
32
+ const globalTheme = getSystemGlobalTheme(element);
33
+ if (!val) return;
34
+ return {
35
+ color: getMediaColor(val, globalTheme)
36
+ };
37
+ },
38
+ background: (val, element) => {
39
+ const globalTheme = getSystemGlobalTheme(element);
40
+ if (!val) return;
41
+ if (isString(val) && val.includes("gradient")) {
42
+ return { background: resolveColorsInGradient(val, globalTheme) };
43
+ }
44
+ return {
45
+ background: getMediaColor(val, globalTheme)
46
+ };
47
+ },
48
+ backgroundColor: (val, element) => {
49
+ const globalTheme = getSystemGlobalTheme(element);
50
+ if (!val) return;
51
+ return {
52
+ backgroundColor: getMediaColor(val, globalTheme)
53
+ };
54
+ },
55
+ backgroundImage: (val, element, s, ctx) => {
56
+ const globalTheme = getSystemGlobalTheme(element);
57
+ if (!val) return;
58
+ const file = ctx.files && ctx.files[val];
59
+ if (file && file.content) val = file.content.src;
60
+ return {
61
+ backgroundImage: transformBackgroundImage(val, globalTheme)
62
+ };
63
+ },
64
+ textStroke: (val) => ({
65
+ WebkitTextStroke: transformTextStroke(val)
66
+ }),
67
+ outline: (val) => ({
68
+ outline: transformBorder(val)
69
+ }),
70
+ outlineOffset: (val, { props }) => transformSizeRatio("outlineOffset", val, props),
71
+ border: (val) => ({
72
+ border: transformBorder(val)
73
+ }),
74
+ borderColor: (val, element) => {
75
+ const globalTheme = getSystemGlobalTheme(element);
76
+ if (!val) return;
77
+ return {
78
+ borderColor: getMediaColor(val, globalTheme)
79
+ };
80
+ },
81
+ borderTopColor: (val, element) => {
82
+ const globalTheme = getSystemGlobalTheme(element);
83
+ if (!val) return;
84
+ return { borderTopColor: getMediaColor(val, globalTheme) };
85
+ },
86
+ borderBottomColor: (val, element) => {
87
+ const globalTheme = getSystemGlobalTheme(element);
88
+ if (!val) return;
89
+ return { borderBottomColor: getMediaColor(val, globalTheme) };
90
+ },
91
+ borderLeftColor: (val, element) => {
92
+ const globalTheme = getSystemGlobalTheme(element);
93
+ if (!val) return;
94
+ return { borderLeftColor: getMediaColor(val, globalTheme) };
95
+ },
96
+ borderRightColor: (val, element) => {
97
+ const globalTheme = getSystemGlobalTheme(element);
98
+ if (!val) return;
99
+ return { borderRightColor: getMediaColor(val, globalTheme) };
100
+ },
101
+ borderLeft: (val) => ({
102
+ borderLeft: transformBorder(val)
103
+ }),
104
+ borderTop: (val) => ({
105
+ borderTop: transformBorder(val)
106
+ }),
107
+ borderRight: (val) => ({
108
+ borderRight: transformBorder(val)
109
+ }),
110
+ borderBottom: (val) => ({
111
+ borderBottom: transformBorder(val)
112
+ }),
113
+ shadow: (val, element) => {
114
+ const globalTheme = getSystemGlobalTheme(element);
115
+ if (!val) return;
116
+ return {
117
+ boxShadow: transformShadow(val, globalTheme)
118
+ };
119
+ },
120
+ boxShadow: (val, element) => {
121
+ if (!isString(val)) return;
122
+ const [value, hasImportant] = val.split("!importan");
123
+ const globalTheme = getSystemGlobalTheme(element);
124
+ const important = hasImportant ? " !important" : "";
125
+ return {
126
+ boxShadow: transformBoxShadow(value.trim(), globalTheme) + important
127
+ };
128
+ },
129
+ textShadow: (val, { context }) => ({
130
+ textShadow: transformBoxShadow(val, context.designSystem.globalTheme)
131
+ }),
132
+ columnRule: (val) => ({
133
+ columnRule: transformBorder(val)
134
+ })
135
+ };
136
+ export {
137
+ THEME_PROPS,
138
+ getSystemGlobalTheme
139
+ };
@@ -0,0 +1,26 @@
1
+ import {
2
+ getTimingFunction,
3
+ splitTransition,
4
+ transformDuration
5
+ } from "@symbo.ls/scratch";
6
+ const TIMING_PROPS = {
7
+ transition: (val) => ({
8
+ transition: splitTransition(val)
9
+ }),
10
+ transitionDuration: (val) => ({
11
+ transitionDuration: transformDuration(val)
12
+ }),
13
+ transitionDelay: (val) => ({
14
+ transitionDelay: transformDuration(val)
15
+ }),
16
+ transitionTimingFunction: (val) => ({
17
+ transitionTimingFunction: getTimingFunction(val)
18
+ }),
19
+ transitionProperty: (val) => ({
20
+ transitionProperty: val,
21
+ willChange: val
22
+ })
23
+ };
24
+ export {
25
+ TIMING_PROPS
26
+ };
@@ -0,0 +1,91 @@
1
+ import { exec, isArray, isFunction, isObject, isProduction, merge, overwrite, overwriteDeep } from "@domql/utils";
2
+ import { CSS_PROPS_REGISTRY } from "../props";
3
+ import { DEFAULT_CSS_PROPERTIES_LIST } from "../props/defaults";
4
+ import { applyTrueProps, transformersByPrefix } from "./transformers";
5
+ const isProd = isProduction();
6
+ const usePropsAsCSS = (sourceObj, element, opts) => {
7
+ let obj = {};
8
+ const rest = {};
9
+ const setToObj = (key, val) => {
10
+ if (opts.unpack) {
11
+ obj = { ...obj, ...val };
12
+ return;
13
+ }
14
+ obj[key] = val;
15
+ };
16
+ for (const key in sourceObj) {
17
+ const value = sourceObj[key];
18
+ if (key === "class" && element.call("isString", sourceObj.class)) {
19
+ const val = value.split(" ");
20
+ if (val.length) {
21
+ const CLASS = element.context.designSystem.CLASS;
22
+ const result = val.reduce((acc, curr) => merge(acc, CLASS[curr]), {});
23
+ obj.designSystemClass = result;
24
+ }
25
+ } else if (key === "true") {
26
+ const val = exec(value, element);
27
+ merge(obj, applyTrueProps(val, element));
28
+ } else if (element.classlist[key]) {
29
+ const originalFromClass = element.classlist[key];
30
+ const result = isFunction(originalFromClass) ? originalFromClass(element, element.state, element.context) : originalFromClass;
31
+ if (result) setToObj(key, result);
32
+ if (!isProd && isObject(obj[key])) obj[key].label = key;
33
+ } else if (CSS_PROPS_REGISTRY[key]) {
34
+ let val = exec(value, element);
35
+ if (isArray(val)) {
36
+ val = val.reduce((a, c) => merge(a, c), {});
37
+ }
38
+ let result = CSS_PROPS_REGISTRY[key](val, element, element.state, element.context);
39
+ if (isArray(result)) result = result.reduce((a, c) => merge(a, c), {});
40
+ if (result) setToObj(key, result);
41
+ if (!isProd && isObject(obj[key])) obj[key].label = key;
42
+ } else if (DEFAULT_CSS_PROPERTIES_LIST.has(key)) {
43
+ const result = exec(value, element);
44
+ setToObj(key, { [key]: result });
45
+ if (!isProd && isObject(obj[key])) obj[key].label = key;
46
+ } else {
47
+ rest[key] = value;
48
+ }
49
+ }
50
+ return [obj, rest];
51
+ };
52
+ const useSelectorsAsCSS = (sourceObj, element) => {
53
+ const obj = {};
54
+ for (const key in sourceObj) {
55
+ const selectroSetter = transformersByPrefix[key.slice(0, 1)];
56
+ if (selectroSetter) {
57
+ const result = selectroSetter(key, sourceObj[key], element);
58
+ if (result) overwriteDeep(obj, result);
59
+ }
60
+ }
61
+ return obj;
62
+ };
63
+ const useCssInProps = (selectorProps, element, opts = { unpack: true }) => {
64
+ const [cssObj, restProps] = usePropsAsCSS(selectorProps, element, opts);
65
+ const selectorsObj = useSelectorsAsCSS(restProps, element);
66
+ let hasSelectors = false;
67
+ for (const _k in selectorsObj) {
68
+ hasSelectors = true;
69
+ break;
70
+ }
71
+ if (hasSelectors) {
72
+ if (opts.unpack) return overwrite(cssObj, selectorsObj);
73
+ cssObj._selectors = selectorsObj;
74
+ }
75
+ return cssObj;
76
+ };
77
+ const exetuteClassPerComponent = (component, element) => {
78
+ const classObj = {};
79
+ if (component.class) {
80
+ for (const classProp in component.class) {
81
+ classObj[classProp] = component.class[classProp](element);
82
+ }
83
+ }
84
+ return classObj;
85
+ };
86
+ export {
87
+ exetuteClassPerComponent,
88
+ useCssInProps,
89
+ usePropsAsCSS,
90
+ useSelectorsAsCSS
91
+ };
@@ -0,0 +1 @@
1
+ export * from "./executors";
@@ -0,0 +1,61 @@
1
+ import { useCssInProps } from "./executors";
2
+ const applyMediaProps = (key, selectorProps, element) => {
3
+ const { context } = element;
4
+ if (!context.designSystem?.MEDIA) return;
5
+ const mediaValue = context.designSystem.MEDIA[key.slice(1)];
6
+ const generatedClass = useCssInProps(selectorProps, element);
7
+ const mediaKey = mediaValue ? "@media " + (mediaValue === "print" ? mediaValue : `screen and ${mediaValue}`) : key;
8
+ return { [mediaKey]: generatedClass };
9
+ };
10
+ const applyAndProps = (key, selectorProps, element) => {
11
+ return { [key]: useCssInProps(selectorProps, element) };
12
+ };
13
+ const applySelectorProps = (key, selectorProps, element) => {
14
+ const selectorKey = `&${key}`;
15
+ return { [selectorKey]: useCssInProps(selectorProps, element) };
16
+ };
17
+ const applyCaseProps = (key, selectorProps, element) => {
18
+ const { CASES } = element.context?.designSystem || {};
19
+ const caseKey = key.slice(1);
20
+ const isCaseTrue = !CASES?.[caseKey] && !element.props[caseKey];
21
+ if (!isCaseTrue) return;
22
+ return useCssInProps(selectorProps, element);
23
+ };
24
+ const applyVariableProps = (key, selectorVal, element) => {
25
+ return { [key]: selectorVal };
26
+ };
27
+ const applyConditionalCaseProps = (key, selectorProps, element) => {
28
+ const caseKey = key.slice(1);
29
+ const isCaseTrue = element.props[caseKey] === true || element.state[caseKey] || element[caseKey];
30
+ if (!isCaseTrue) return;
31
+ return useCssInProps(selectorProps, element);
32
+ };
33
+ const applyConditionalFalsyProps = (key, selectorProps, element) => {
34
+ const caseKey = key.slice(1);
35
+ const isCaseTrue = element.props[caseKey] === true || element.state[caseKey] || element[caseKey];
36
+ if (isCaseTrue) return;
37
+ return useCssInProps(selectorProps, element);
38
+ };
39
+ const applyTrueProps = (selectorProps, element) => {
40
+ return useCssInProps(selectorProps, element);
41
+ };
42
+ const transformersByPrefix = {
43
+ "@": applyMediaProps,
44
+ // Selector handlers
45
+ ":": applySelectorProps,
46
+ "[": applySelectorProps,
47
+ "*": applySelectorProps,
48
+ "+": applySelectorProps,
49
+ "~": applySelectorProps,
50
+ "&": applyAndProps,
51
+ ">": applyAndProps,
52
+ // Conditional and variable handlers
53
+ $: applyCaseProps,
54
+ "-": applyVariableProps,
55
+ ".": applyConditionalCaseProps,
56
+ "!": applyConditionalFalsyProps
57
+ };
58
+ export {
59
+ applyTrueProps,
60
+ transformersByPrefix
61
+ };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "css-in-props",
3
3
  "description": "Utilize props as CSS methods",
4
4
  "author": "symbo.ls",
5
- "version": "3.4.4",
5
+ "version": "3.4.6",
6
6
  "repository": "https://github.com/symbo-ls/smbls",
7
7
  "type": "module",
8
8
  "module": "./dist/esm/index.js",
@@ -13,7 +13,7 @@
13
13
  ".": {
14
14
  "import": "./dist/esm/index.js",
15
15
  "require": "./dist/cjs/index.js",
16
- "browser": "./dist/iife/index.js",
16
+ "browser": "./dist/esm/index.js",
17
17
  "default": "./dist/esm/index.js"
18
18
  }
19
19
  },
@@ -21,8 +21,8 @@
21
21
  "publishConfig": {},
22
22
  "scripts": {
23
23
  "copy:package:cjs": "cp ../../build/package-cjs.json dist/cjs/package.json",
24
- "build:esm": "cross-env NODE_ENV=$NODE_ENV esbuild src/*.js --target=es2020 --format=esm --outdir=dist/esm --define:process.env.NODE_ENV=process.env.NODE_ENV",
25
- "build:cjs": "cross-env NODE_ENV=$NODE_ENV esbuild src/*.js --target=node18 --format=cjs --outdir=dist/cjs --define:process.env.NODE_ENV=process.env.NODE_ENV",
24
+ "build:esm": "cross-env NODE_ENV=$NODE_ENV esbuild $(find src -name '*.js') --target=es2020 --format=esm --outdir=dist/esm --define:process.env.NODE_ENV=process.env.NODE_ENV",
25
+ "build:cjs": "cross-env NODE_ENV=$NODE_ENV esbuild $(find src -name '*.js') --target=node18 --format=cjs --outdir=dist/cjs --define:process.env.NODE_ENV=process.env.NODE_ENV",
26
26
  "build:iife": "cross-env NODE_ENV=$NODE_ENV esbuild src/index.js --bundle --target=es2020 --format=iife --global-name=CssInProps --outfile=dist/iife/index.js --define:process.env.NODE_ENV=process.env.NODE_ENV --external:@symbo.ls/* --external:@domql/* --external:smbls",
27
27
  "build": "node ../../build/build.js",
28
28
  "prepublish": "npm run build && npm run copy:package:cjs"
@@ -33,14 +33,14 @@
33
33
  "src"
34
34
  ],
35
35
  "dependencies": {
36
- "@domql/utils": "^3.4.4",
37
- "@symbo.ls/atoms": "^3.4.4",
38
- "@symbo.ls/emotion": "^3.4.4",
39
- "@symbo.ls/scratch": "^3.4.4",
40
- "smbls": "^3.4.4"
36
+ "@domql/utils": "^3.4.6",
37
+ "@symbo.ls/atoms": "^3.4.6",
38
+ "@symbo.ls/emotion": "^3.4.6",
39
+ "@symbo.ls/scratch": "^3.4.6",
40
+ "smbls": "^3.4.6"
41
41
  },
42
42
  "gitHead": "9fc1b79b41cdc725ca6b24aec64920a599634681",
43
- "browser": "./dist/iife/index.js",
43
+ "browser": "./dist/esm/index.js",
44
44
  "sideEffects": false,
45
45
  "devDependencies": {
46
46
  "@babel/core": "^7.29.0"