@codecademy/variance 0.25.3-alpha.c2d955.0 → 0.25.3-alpha.d8b528.0
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/LICENSE +21 -0
- package/dist/core.js +22 -5
- package/dist/getPropertyMode/getPropertyMode.d.ts +2 -0
- package/dist/getPropertyMode/getPropertyMode.js +3 -0
- package/dist/getPropertyMode/getPropertyMode.test.js +15 -0
- package/dist/getPropertyMode/index.d.ts +1 -0
- package/dist/getPropertyMode/index.js +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -1
- package/dist/types/config.d.ts +8 -4
- package/dist/types/properties.d.ts +9 -0
- package/dist/utils/propNames.js +15 -7
- package/package.json +3 -2
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Codecademy LLC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/core.js
CHANGED
|
@@ -62,11 +62,13 @@ export const variance = {
|
|
|
62
62
|
const {
|
|
63
63
|
transform = identity,
|
|
64
64
|
property,
|
|
65
|
-
properties
|
|
66
|
-
scale
|
|
65
|
+
properties: configProperties,
|
|
66
|
+
scale,
|
|
67
|
+
resolveProperty
|
|
67
68
|
} = config;
|
|
68
69
|
const getScaleValue = createScaleLookup(scale);
|
|
69
70
|
const alwaysTransform = scale === undefined || isArray(scale);
|
|
71
|
+
const isDirectionalProperties = props => props !== undefined && !isArray(props) && 'physical' in props && 'logical' in props;
|
|
70
72
|
return {
|
|
71
73
|
...config,
|
|
72
74
|
prop,
|
|
@@ -93,18 +95,33 @@ export const variance = {
|
|
|
93
95
|
default:
|
|
94
96
|
return styles;
|
|
95
97
|
}
|
|
98
|
+
const useLogicalProperties = props.theme?.useLogicalProperties ?? true;
|
|
99
|
+
let resolvedProperties;
|
|
100
|
+
if (isDirectionalProperties(configProperties)) {
|
|
101
|
+
const mode = resolveProperty ? resolveProperty(useLogicalProperties) : useLogicalProperties ? 'logical' : 'physical';
|
|
102
|
+
resolvedProperties = configProperties[mode];
|
|
103
|
+
} else {
|
|
104
|
+
resolvedProperties = configProperties ?? [property];
|
|
105
|
+
}
|
|
96
106
|
|
|
97
107
|
// for each property look up the scale value from theme if passed and apply any
|
|
98
108
|
// final transforms to the value
|
|
99
|
-
|
|
109
|
+
resolvedProperties.forEach(property => {
|
|
110
|
+
let resolvedProperty;
|
|
111
|
+
if (resolveProperty && typeof property === 'object') {
|
|
112
|
+
const mode = resolveProperty(useLogicalProperties);
|
|
113
|
+
resolvedProperty = property[mode];
|
|
114
|
+
} else {
|
|
115
|
+
resolvedProperty = property;
|
|
116
|
+
}
|
|
100
117
|
let styleValue = intermediateValue;
|
|
101
118
|
if (useTransform && !isUndefined(styleValue)) {
|
|
102
|
-
styleValue = transform(styleValue,
|
|
119
|
+
styleValue = transform(styleValue, resolvedProperty, props);
|
|
103
120
|
}
|
|
104
121
|
switch (typeof styleValue) {
|
|
105
122
|
case 'number':
|
|
106
123
|
case 'string':
|
|
107
|
-
return styles[
|
|
124
|
+
return styles[resolvedProperty] = styleValue;
|
|
108
125
|
case 'object':
|
|
109
126
|
return Object.assign(styles, styleValue);
|
|
110
127
|
default:
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { getPropertyMode } from './getPropertyMode';
|
|
2
|
+
describe('getPropertyMode', () => {
|
|
3
|
+
it.each([{
|
|
4
|
+
useLogicalProperties: true,
|
|
5
|
+
expected: 'logical'
|
|
6
|
+
}, {
|
|
7
|
+
useLogicalProperties: false,
|
|
8
|
+
expected: 'physical'
|
|
9
|
+
}])('returns "$expected" when useLogicalProperties is $useLogicalProperties', ({
|
|
10
|
+
useLogicalProperties,
|
|
11
|
+
expected
|
|
12
|
+
}) => {
|
|
13
|
+
expect(getPropertyMode(useLogicalProperties)).toBe(expected);
|
|
14
|
+
});
|
|
15
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './getPropertyMode';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './getPropertyMode';
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/types/config.d.ts
CHANGED
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
import { Theme } from '@emotion/react';
|
|
2
|
-
import { DefaultCSSPropertyValue, PropertyTypes } from './properties';
|
|
2
|
+
import { DefaultCSSPropertyValue, DirectionalProperties, DirectionalProperty, PropertyMode, PropertyTypes } from './properties';
|
|
3
3
|
import { AbstractProps, CSSObject, CSSPropMap, CSSProps, ResponsiveProp, ThemeProps } from './props';
|
|
4
4
|
import { AllUnionKeys, Key, KeyFromUnion } from './utils';
|
|
5
5
|
export type MapScale = Record<string | number, string | number>;
|
|
6
6
|
export type ArrayScale = readonly (string | number)[] & {
|
|
7
7
|
length: 0;
|
|
8
8
|
};
|
|
9
|
+
export type PropertyValue = keyof PropertyTypes | DirectionalProperty;
|
|
9
10
|
export interface BaseProperty {
|
|
10
|
-
property:
|
|
11
|
-
properties?: readonly
|
|
11
|
+
property: PropertyValue;
|
|
12
|
+
properties?: readonly PropertyValue[] | DirectionalProperties;
|
|
12
13
|
}
|
|
13
14
|
export interface Prop extends BaseProperty {
|
|
14
15
|
scale?: keyof Theme | MapScale | ArrayScale;
|
|
15
16
|
transform?: (val: string | number, prop?: string, props?: AbstractProps) => string | number | CSSObject;
|
|
17
|
+
resolveProperty?: (useLogicalProperties: boolean) => PropertyMode;
|
|
16
18
|
}
|
|
17
19
|
export interface AbstractPropTransformer extends Prop {
|
|
18
20
|
prop: string;
|
|
@@ -24,7 +26,8 @@ export interface AbstractParser {
|
|
|
24
26
|
config: Record<string, AbstractPropTransformer>;
|
|
25
27
|
}
|
|
26
28
|
export type PropertyValues<Property extends keyof PropertyTypes, All extends boolean = false> = Exclude<PropertyTypes<All extends true ? DefaultCSSPropertyValue : never>[Property], All extends true ? never : object | any[]>;
|
|
27
|
-
|
|
29
|
+
type BasePropertyKey<P> = P extends DirectionalProperty ? P['physical'] : P;
|
|
30
|
+
export type ScaleValue<Config extends Prop> = Config['scale'] extends keyof Theme ? keyof Theme[Config['scale']] | PropertyValues<BasePropertyKey<Config['property']>> : Config['scale'] extends MapScale ? keyof Config['scale'] | PropertyValues<BasePropertyKey<Config['property']>> : Config['scale'] extends ArrayScale ? Config['scale'][number] | PropertyValues<BasePropertyKey<Config['property']>> : PropertyValues<BasePropertyKey<Config['property']>, true>;
|
|
28
31
|
export type Scale<Config extends Prop> = ResponsiveProp<ScaleValue<Config> | ((theme: Theme) => ScaleValue<Config>)>;
|
|
29
32
|
export interface TransformFn<P extends string, Config extends Prop> {
|
|
30
33
|
(value: Scale<Config> | Scale<Config>, prop: P, props: ThemeProps<{
|
|
@@ -69,3 +72,4 @@ export type SystemProps<P extends AbstractParser> = {
|
|
|
69
72
|
export type VariantProps<T extends string, V> = {
|
|
70
73
|
[Key in T]?: V;
|
|
71
74
|
};
|
|
75
|
+
export {};
|
|
@@ -19,4 +19,13 @@ export interface VendorPropertyTypes<Overrides = DefaultCSSPropertyValue> extend
|
|
|
19
19
|
}
|
|
20
20
|
export interface CSSPropertyTypes<Overrides = DefaultCSSPropertyValue> extends PropertyTypes<Overrides>, VendorPropertyTypes<Overrides> {
|
|
21
21
|
}
|
|
22
|
+
export type PropertyMode = 'logical' | 'physical';
|
|
23
|
+
export interface DirectionalProperty {
|
|
24
|
+
physical: keyof PropertyTypes;
|
|
25
|
+
logical: keyof PropertyTypes;
|
|
26
|
+
}
|
|
27
|
+
export interface DirectionalProperties {
|
|
28
|
+
physical: readonly (keyof PropertyTypes)[];
|
|
29
|
+
logical: readonly (keyof PropertyTypes)[];
|
|
30
|
+
}
|
|
22
31
|
export {};
|
package/dist/utils/propNames.js
CHANGED
|
@@ -9,6 +9,14 @@ const compare = (a, b) => {
|
|
|
9
9
|
if (b < a) return SORT.B_BEFORE_A;
|
|
10
10
|
return SORT.EQUAL;
|
|
11
11
|
};
|
|
12
|
+
const isShorthand = prop => typeof prop === 'string' && SHORTHAND_PROPERTIES.includes(prop);
|
|
13
|
+
const getShorthandIndex = prop => typeof prop === 'string' ? SHORTHAND_PROPERTIES.indexOf(prop) : -1;
|
|
14
|
+
const getPropertiesCount = properties => {
|
|
15
|
+
if (!properties) return 0;
|
|
16
|
+
if (Array.isArray(properties)) return properties.length;
|
|
17
|
+
// DirectionalProperties object - using physical array length as representative, since the length for logical is the same
|
|
18
|
+
return properties.physical?.length ?? 0;
|
|
19
|
+
};
|
|
12
20
|
|
|
13
21
|
/**
|
|
14
22
|
* Orders all properties by the most dependent props
|
|
@@ -21,19 +29,19 @@ export const orderPropNames = config => Object.keys(config).sort((a, b) => {
|
|
|
21
29
|
} = config;
|
|
22
30
|
const {
|
|
23
31
|
property: aProp,
|
|
24
|
-
properties: aProperties
|
|
32
|
+
properties: aProperties
|
|
25
33
|
} = aConf;
|
|
26
34
|
const {
|
|
27
35
|
property: bProp,
|
|
28
|
-
properties: bProperties
|
|
36
|
+
properties: bProperties
|
|
29
37
|
} = bConf;
|
|
30
|
-
const aIsShorthand =
|
|
31
|
-
const bIsShorthand =
|
|
38
|
+
const aIsShorthand = isShorthand(aProp);
|
|
39
|
+
const bIsShorthand = isShorthand(bProp);
|
|
32
40
|
if (aIsShorthand && bIsShorthand) {
|
|
33
|
-
const aNum = aProperties
|
|
34
|
-
const bNum = bProperties
|
|
41
|
+
const aNum = getPropertiesCount(aProperties);
|
|
42
|
+
const bNum = getPropertiesCount(bProperties);
|
|
35
43
|
if (aProp !== bProp) {
|
|
36
|
-
return compare(
|
|
44
|
+
return compare(getShorthandIndex(aProp), getShorthandIndex(bProp));
|
|
37
45
|
}
|
|
38
46
|
if (aProp === bProp) {
|
|
39
47
|
if (aNum === 0) return SORT.A_BEFORE_B;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codecademy/variance",
|
|
3
3
|
"description": "Constraint based CSS in JS for building scalable design systems",
|
|
4
|
-
"version": "0.25.3-alpha.
|
|
4
|
+
"version": "0.25.3-alpha.d8b528.0",
|
|
5
5
|
"author": "codecaaron <aaron@codecademy.com>",
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"csstype": "^3.0.7",
|
|
@@ -31,5 +31,6 @@
|
|
|
31
31
|
"scripts": {
|
|
32
32
|
"build": "nx build @codecademy/variance"
|
|
33
33
|
},
|
|
34
|
-
"types": "dist/index.d.ts"
|
|
34
|
+
"types": "dist/index.d.ts",
|
|
35
|
+
"gitHead": "fe5dff414ecab26d29ead6252dccd7e4c5c00f64"
|
|
35
36
|
}
|