@codecademy/variance 0.25.1-alpha.ea0de1.0 → 0.25.1-alpha.ea1cbb.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
@@ -63,7 +63,8 @@ export const variance = {
63
63
  transform = identity,
64
64
  property,
65
65
  properties = [property],
66
- scale
66
+ scale,
67
+ resolveProperty
67
68
  } = config;
68
69
  const getScaleValue = createScaleLookup(scale);
69
70
  const alwaysTransform = scale === undefined || isArray(scale);
@@ -97,14 +98,23 @@ export const variance = {
97
98
  // for each property look up the scale value from theme if passed and apply any
98
99
  // final transforms to the value
99
100
  properties.forEach(property => {
101
+ // Resolve directional properties if resolveProperty hook is provided
102
+ let resolvedProperty;
103
+ if (resolveProperty && typeof property === 'object') {
104
+ const useLogicalProperties = props.theme?.useLogicalProperties ?? true;
105
+ const mode = resolveProperty(useLogicalProperties);
106
+ resolvedProperty = property[mode];
107
+ } else {
108
+ resolvedProperty = property;
109
+ }
100
110
  let styleValue = intermediateValue;
101
111
  if (useTransform && !isUndefined(styleValue)) {
102
- styleValue = transform(styleValue, property, props);
112
+ styleValue = transform(styleValue, resolvedProperty, props);
103
113
  }
104
114
  switch (typeof styleValue) {
105
115
  case 'number':
106
116
  case 'string':
107
- return styles[property] = styleValue;
117
+ return styles[resolvedProperty] = styleValue;
108
118
  case 'object':
109
119
  return Object.assign(styles, styleValue);
110
120
  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 @@
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,21 @@
1
1
  import { Theme } from '@emotion/react';
2
- import { DefaultCSSPropertyValue, PropertyTypes } from './properties';
2
+ import { DefaultCSSPropertyValue, 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[];
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
+ /** Hook to resolve directional properties (physical/logical) based on theme setting */
18
+ resolveProperty?: (useLogicalProperties: boolean) => PropertyMode;
16
19
  }
17
20
  export interface AbstractPropTransformer extends Prop {
18
21
  prop: string;
@@ -24,7 +27,8 @@ export interface AbstractParser {
24
27
  config: Record<string, AbstractPropTransformer>;
25
28
  }
26
29
  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>;
30
+ type BasePropertyKey<P> = P extends DirectionalProperty ? P['physical'] : P;
31
+ 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
32
  export type Scale<Config extends Prop> = ResponsiveProp<ScaleValue<Config> | ((theme: Theme) => ScaleValue<Config>)>;
29
33
  export interface TransformFn<P extends string, Config extends Prop> {
30
34
  (value: Scale<Config> | Scale<Config>, prop: P, props: ThemeProps<{
@@ -69,3 +73,4 @@ export type SystemProps<P extends AbstractParser> = {
69
73
  export type VariantProps<T extends string, V> = {
70
74
  [Key in T]?: V;
71
75
  };
76
+ export {};
@@ -19,4 +19,9 @@ 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
+ }
22
27
  export {};
@@ -9,6 +9,8 @@ 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;
12
14
 
13
15
  /**
14
16
  * Orders all properties by the most dependent props
@@ -27,13 +29,13 @@ export const orderPropNames = config => Object.keys(config).sort((a, b) => {
27
29
  property: bProp,
28
30
  properties: bProperties = []
29
31
  } = bConf;
30
- const aIsShorthand = SHORTHAND_PROPERTIES.includes(aProp);
31
- const bIsShorthand = SHORTHAND_PROPERTIES.includes(bProp);
32
+ const aIsShorthand = isShorthand(aProp);
33
+ const bIsShorthand = isShorthand(bProp);
32
34
  if (aIsShorthand && bIsShorthand) {
33
35
  const aNum = aProperties.length;
34
36
  const bNum = bProperties.length;
35
37
  if (aProp !== bProp) {
36
- return compare(SHORTHAND_PROPERTIES.indexOf(aProp), SHORTHAND_PROPERTIES.indexOf(bProp));
38
+ return compare(getShorthandIndex(aProp), getShorthandIndex(bProp));
37
39
  }
38
40
  if (aProp === bProp) {
39
41
  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.1-alpha.ea0de1.0",
4
+ "version": "0.25.1-alpha.ea1cbb.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": "7de0fe0615a8424530043aa5122010579e7c0e3c"
35
+ "gitHead": "c2969b8bc6d5586862a0c05b228db55a08c1c94a"
36
36
  }