@codecademy/variance 0.25.2 → 0.25.3-alpha.4aeb6d.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/dist/core.js CHANGED
@@ -62,11 +62,13 @@ export const variance = {
62
62
  const {
63
63
  transform = identity,
64
64
  property,
65
- properties = [property],
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
- properties.forEach(property => {
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, property, props);
119
+ styleValue = transform(styleValue, resolvedProperty, props);
103
120
  }
104
121
  switch (typeof styleValue) {
105
122
  case 'number':
106
123
  case 'string':
107
- return styles[property] = styleValue;
124
+ return styles[resolvedProperty] = styleValue;
108
125
  case 'object':
109
126
  return Object.assign(styles, styleValue);
110
127
  default:
@@ -0,0 +1,2 @@
1
+ import { PropertyMode } from '../types/properties';
2
+ export declare const getPropertyMode: (useLogicalProperties: boolean) => PropertyMode;
@@ -0,0 +1,3 @@
1
+ export const getPropertyMode = useLogicalProperties => {
2
+ return useLogicalProperties ? 'logical' : 'physical';
3
+ };
@@ -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
@@ -3,3 +3,4 @@ export * from './createTheme';
3
3
  export * from './types/props';
4
4
  export * from './transforms';
5
5
  export * from './scales/createScale';
6
+ export * from './getPropertyMode';
package/dist/index.js CHANGED
@@ -2,4 +2,5 @@ export { variance } from './core';
2
2
  export * from './createTheme';
3
3
  export * from './types/props';
4
4
  export * from './transforms';
5
- export * from './scales/createScale';
5
+ export * from './scales/createScale';
6
+ export * from './getPropertyMode';
@@ -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: keyof PropertyTypes;
11
- properties?: readonly (keyof PropertyTypes)[];
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
- export type ScaleValue<Config extends Prop> = Config['scale'] extends keyof Theme ? keyof Theme[Config['scale']] | PropertyValues<Config['property']> : Config['scale'] extends MapScale ? keyof Config['scale'] | PropertyValues<Config['property']> : Config['scale'] extends ArrayScale ? Config['scale'][number] | PropertyValues<Config['property']> : PropertyValues<Config['property'], true>;
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 {};
@@ -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 = SHORTHAND_PROPERTIES.includes(aProp);
31
- const bIsShorthand = SHORTHAND_PROPERTIES.includes(bProp);
38
+ const aIsShorthand = isShorthand(aProp);
39
+ const bIsShorthand = isShorthand(bProp);
32
40
  if (aIsShorthand && bIsShorthand) {
33
- const aNum = aProperties.length;
34
- const bNum = bProperties.length;
41
+ const aNum = getPropertiesCount(aProperties);
42
+ const bNum = getPropertiesCount(bProperties);
35
43
  if (aProp !== bProp) {
36
- return compare(SHORTHAND_PROPERTIES.indexOf(aProp), SHORTHAND_PROPERTIES.indexOf(bProp));
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.2",
4
+ "version": "0.25.3-alpha.4aeb6d.0",
5
5
  "author": "codecaaron <aaron@codecademy.com>",
6
6
  "dependencies": {
7
7
  "csstype": "^3.0.7",
@@ -32,5 +32,5 @@
32
32
  "build": "nx build @codecademy/variance"
33
33
  },
34
34
  "types": "dist/index.d.ts",
35
- "gitHead": "dba147a168af471002f2f97b74eea97204bd843f"
35
+ "gitHead": "c3bacad29ba252b1df9328296dd76c713cd2df16"
36
36
  }