@dcl/react-ecs 7.4.3-7935604720.commit-e16c799 → 7.4.3-7976826769.commit-bfde8e1

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.
@@ -1,5 +1,5 @@
1
1
  import { ReactEcs } from '../../react-ecs';
2
- import { getFont, getTextAlign } from '../Label/utils';
2
+ import { getFont, getFontSize, getTextAlign } from '../Label/utils';
3
3
  import { parseUiBackground } from '../uiBackground';
4
4
  import { parseUiTransform } from '../uiTransform';
5
5
  function getButtonProps(props) {
@@ -35,12 +35,13 @@ export function Button(props) {
35
35
  ...buttonProps.uiBackground,
36
36
  ...uiBackground
37
37
  });
38
- const { font, textAlign, ...uiTexProps } = otherProps;
38
+ const { font, textAlign, fontSize, ...uiTexProps } = otherProps;
39
39
  const textProps = {
40
40
  ...buttonProps.uiText,
41
41
  ...uiTexProps,
42
42
  ...getFont(font),
43
- ...getTextAlign(textAlign)
43
+ ...getTextAlign(textAlign),
44
+ ...getFontSize(fontSize)
44
45
  };
45
46
  const uiTransformProps = parseUiTransform({
46
47
  height: 36,
@@ -1,8 +1,8 @@
1
1
  import { parseProps } from '../utils';
2
2
  import { ReactEcs } from '../../react-ecs';
3
- import { getFont, getTextAlign } from '../Label/utils';
3
+ import { getFont, getFontSize, getTextAlign } from '../Label/utils';
4
4
  function parseUiDropdown(props) {
5
- const { textAlign, font, ...otherProps } = props;
5
+ const { textAlign, font, fontSize, ...otherProps } = props;
6
6
  return {
7
7
  acceptEmpty: false,
8
8
  options: [],
@@ -10,7 +10,8 @@ function parseUiDropdown(props) {
10
10
  disabled: false,
11
11
  ...otherProps,
12
12
  ...getTextAlign(textAlign),
13
- ...getFont(font)
13
+ ...getFont(font),
14
+ ...getFontSize(fontSize)
14
15
  };
15
16
  }
16
17
  /**
@@ -1,12 +1,13 @@
1
1
  import { PBUiDropdown } from '@dcl/ecs';
2
2
  import { TextAlignType, UiFontType } from '../Label/types';
3
- import { EntityPropTypes } from '../types';
3
+ import { EntityPropTypes, ScaleUnit } from '../types';
4
4
  /**
5
5
  * @public
6
6
  * Dropdown Props
7
7
  */
8
- export interface UiDropdownProps extends EntityPropTypes, Omit<Partial<PBUiDropdown>, 'textAlign' | 'font'> {
8
+ export interface UiDropdownProps extends EntityPropTypes, Omit<Partial<PBUiDropdown>, 'textAlign' | 'font' | 'fontSize'> {
9
9
  onChange?(value: number): void;
10
10
  font?: UiFontType;
11
11
  textAlign?: TextAlignType;
12
+ fontSize?: ScaleUnit;
12
13
  }
@@ -1,14 +1,15 @@
1
1
  import { parseProps } from '../utils';
2
2
  import { ReactEcs } from '../../react-ecs';
3
- import { getTextAlign, getFont } from '../Label/utils';
3
+ import { getTextAlign, getFont, getFontSize } from '../Label/utils';
4
4
  function parseUiInput(props) {
5
- const { textAlign, font, ...otherProps } = props;
5
+ const { textAlign, font, fontSize, ...otherProps } = props;
6
6
  return {
7
7
  disabled: false,
8
8
  placeholder: '',
9
9
  ...otherProps,
10
10
  ...getTextAlign(textAlign),
11
- ...getFont(font)
11
+ ...getFont(font),
12
+ ...getFontSize(fontSize)
12
13
  };
13
14
  }
14
15
  /**
@@ -1,13 +1,15 @@
1
1
  import { PBUiInput } from '@dcl/ecs';
2
2
  import { TextAlignType, UiFontType } from '../Label/types';
3
+ import { ScaleUnit } from '../types';
3
4
  /**
4
5
  * @public
5
6
  */
6
- export interface UiInputProps extends Omit<PBUiInput, 'font' | 'textAlign'> {
7
+ export interface UiInputProps extends Omit<PBUiInput, 'font' | 'textAlign' | 'fontSize'> {
7
8
  /** function to be called on value change */
8
9
  onChange?(value: string): void;
9
10
  /** function to be called on text field submit */
10
11
  onSubmit?(value: string): void;
11
12
  font?: UiFontType;
12
13
  textAlign?: TextAlignType;
14
+ fontSize?: ScaleUnit;
13
15
  }
@@ -1,6 +1,7 @@
1
1
  import { ReactEcs } from '../../react-ecs';
2
2
  import { EntityPropTypes } from '../types';
3
3
  import { UiLabelProps } from './types';
4
+ export { scaleFontSize } from './utils';
4
5
  /**
5
6
  *
6
7
  * @public
@@ -1,6 +1,7 @@
1
1
  import { parseProps } from '../utils';
2
2
  import { ReactEcs } from '../../react-ecs';
3
- import { getFont, getTextAlign } from './utils';
3
+ import { getFont, getFontSize, getTextAlign } from './utils';
4
+ export { scaleFontSize } from './utils';
4
5
  /**
5
6
  *
6
7
  * @public
@@ -22,11 +23,12 @@ export function Label(props) {
22
23
  onMouseDown,
23
24
  onMouseUp
24
25
  });
25
- const { font, textAlign, ...textProps } = uiTextProps;
26
+ const { font, textAlign, fontSize, ...textProps } = uiTextProps;
26
27
  const uiText = {
27
28
  ...textProps,
28
29
  ...getFont(font),
29
- ...getTextAlign(textAlign)
30
+ ...getTextAlign(textAlign),
31
+ ...getFontSize(fontSize)
30
32
  };
31
33
  return ReactEcs.createElement("entity", { ...commonProps, uiText: uiText });
32
34
  }
@@ -1,4 +1,5 @@
1
1
  import { Color4 } from '@dcl/ecs/dist/components/generated/pb/decentraland/common/colors.gen';
2
+ import { ScaleUnit } from '../types';
2
3
  /**
3
4
  * Label component props
4
5
  * @public
@@ -9,7 +10,7 @@ export interface UiLabelProps {
9
10
  /** Color of the label. @defaultValue `{ r: 1, g: 1, b: 1, a: 1 }` */
10
11
  color?: Color4 | undefined;
11
12
  /** Label font size. @defaultValue 10 */
12
- fontSize?: number | undefined;
13
+ fontSize?: ScaleUnit | undefined;
13
14
  /** Label align position. @defaultValue 'middle-center' */
14
15
  textAlign?: TextAlignType | undefined;
15
16
  /** Label font type. @defaultValue 'sans-serif' */
@@ -1 +1,11 @@
1
- export {};
1
+ import { ScaleContext, ScaleUnit } from '../types';
2
+ /**
3
+ * Scales a font size depending on a context's width/height
4
+ * @param {number} fontSize size of the font to scale
5
+ * @param {ScaleUnit} [scaleUnit=0.39] the scaling unit (uses "width" as unit if a number is supplied)
6
+ * @param {ScaleContext} [ctx=viewport] the context where to apply the scaling
7
+ * @returns {number} the fontSize scaled
8
+ * @see https://matthewjamestaylor.com/responsive-font-size#fluid
9
+ * @public
10
+ */
11
+ export declare function scaleFontSize(fontSize: number, scaleUnit?: ScaleUnit, ctx?: ScaleContext | undefined): number;
@@ -1,3 +1,4 @@
1
+ import { calcOnViewport, getScaleCtx } from '../utils';
1
2
  const parseFont = {
2
3
  'sans-serif': 0 /* Font.F_SANS_SERIF */,
3
4
  serif: 1 /* Font.F_SERIF */,
@@ -30,3 +31,27 @@ export function getTextAlign(textAlign) {
30
31
  return undefined;
31
32
  return { textAlign: parseTextAlign[textAlign] };
32
33
  }
34
+ /**
35
+ * @internal
36
+ */
37
+ export function getFontSize(fontSize) {
38
+ if (!fontSize)
39
+ return undefined;
40
+ if (typeof fontSize === 'string')
41
+ return { fontSize: calcOnViewport(fontSize) };
42
+ return { fontSize };
43
+ }
44
+ /**
45
+ * Scales a font size depending on a context's width/height
46
+ * @param {number} fontSize size of the font to scale
47
+ * @param {ScaleUnit} [scaleUnit=0.39] the scaling unit (uses "width" as unit if a number is supplied)
48
+ * @param {ScaleContext} [ctx=viewport] the context where to apply the scaling
49
+ * @returns {number} the fontSize scaled
50
+ * @see https://matthewjamestaylor.com/responsive-font-size#fluid
51
+ * @public
52
+ */
53
+ export function scaleFontSize(fontSize, scaleUnit = 0.39, ctx = getScaleCtx()) {
54
+ if (!ctx)
55
+ return fontSize;
56
+ return fontSize + calcOnViewport(scaleUnit, ctx);
57
+ }
@@ -11,7 +11,7 @@ export * from './Label/types';
11
11
  export * from './Button/types';
12
12
  export { Dropdown } from './Dropdown';
13
13
  export { Input } from './Input';
14
- export { Label } from './Label';
14
+ export { Label, scaleFontSize } from './Label';
15
15
  export { Button } from './Button';
16
16
  /**
17
17
  * @public
@@ -1,5 +1,5 @@
1
1
  import { ReactEcs } from '../react-ecs';
2
- import { getFont, getTextAlign } from './Label/utils';
2
+ import { getFont, getFontSize, getTextAlign } from './Label/utils';
3
3
  import { parseProps } from './utils';
4
4
  export * from './types';
5
5
  export * from './uiTransform/types';
@@ -11,7 +11,7 @@ export * from './Label/types';
11
11
  export * from './Button/types';
12
12
  export { Dropdown } from './Dropdown';
13
13
  export { Input } from './Input';
14
- export { Label } from './Label';
14
+ export { Label, scaleFontSize } from './Label';
15
15
  export { Button } from './Button';
16
16
  /**
17
17
  * @public
@@ -23,7 +23,8 @@ export function UiEntity(props) {
23
23
  uiText: {
24
24
  ...props.uiText,
25
25
  ...getFont(props.uiText.font),
26
- ...getTextAlign(props.uiText.textAlign)
26
+ ...getTextAlign(props.uiText.textAlign),
27
+ ...getFontSize(props.uiText.fontSize)
27
28
  }
28
29
  };
29
30
  return ReactEcs.createElement("entity", { ...parseProps(props), ...uiText });
@@ -20,3 +20,22 @@ export interface EntityPropTypes extends Listeners {
20
20
  */
21
21
  export type Key = number | string;
22
22
  export type Children = unknown;
23
+ /**
24
+ * unit value type. i.e. 'vw' || 'vh'
25
+ * @public
26
+ */
27
+ export type ScaleUnits = 'vw' | 'vh';
28
+ /**
29
+ * unit value specified. i.e. 10 || '10vw' || '10vh'
30
+ * @public
31
+ */
32
+ export type ScaleUnit = `${number}${ScaleUnits}` | number;
33
+ /**
34
+ * context for applying a scale
35
+ * @public
36
+ */
37
+ export type ScaleContext = {
38
+ width: number;
39
+ height: number;
40
+ ratio: number;
41
+ };
@@ -1,8 +1,9 @@
1
+ import { ScaleUnit } from '../types';
1
2
  /**
2
- * unit value specified. i.e. 1 || '100%' || '1px'
3
+ * unit value specified. i.e. 1 || '100%' || '1px' || '10vw'
3
4
  * @public
4
5
  */
5
- export type PositionUnit = `${number}px` | `${number}%` | number | `${number}`;
6
+ export type PositionUnit = `${number}px` | `${number}%` | number | `${number}` | ScaleUnit;
6
7
  /**
7
8
  * The values are in clockwise order, beginning at the top: top, right, bottom, then left
8
9
 
@@ -1,3 +1,4 @@
1
+ import { calcOnViewport } from '../utils';
1
2
  function capitalize(value) {
2
3
  return `${value[0].toUpperCase()}${value.slice(1, value.length)}`;
3
4
  }
@@ -7,6 +8,12 @@ function isPercent(val) {
7
8
  function isPoint(val) {
8
9
  return typeof val === 'string' && val.endsWith('px');
9
10
  }
11
+ function isVw(val) {
12
+ return typeof val === 'string' && val.endsWith('vw');
13
+ }
14
+ function isVh(val) {
15
+ return typeof val === 'string' && val.endsWith('vh');
16
+ }
10
17
  function parsePositionUnit(val) {
11
18
  function getValue(key, value) {
12
19
  return Number(value.slice(0, value.indexOf(key)));
@@ -26,6 +33,9 @@ function parsePositionUnit(val) {
26
33
  if (isPoint(val)) {
27
34
  return [getValue('px', val), 1 /* YGUnit.YGU_POINT */];
28
35
  }
36
+ if (isVw(val) || isVh(val)) {
37
+ return [calcOnViewport(val), 1 /* YGUnit.YGU_POINT */];
38
+ }
29
39
  return [undefined, 0 /* YGUnit.YGU_UNDEFINED */];
30
40
  }
31
41
  export function parsePosition(position = {}, prop) {
@@ -1,3 +1,4 @@
1
+ import { engine, UiCanvasInformation as engineUiCanvasInformation } from '@dcl/ecs';
1
2
  import { parseUiBackground } from './uiBackground';
2
3
  import { parseUiTransform } from './uiTransform';
3
4
  /**
@@ -13,3 +14,47 @@ export function parseProps(props) {
13
14
  ...uiBackgroundProps
14
15
  };
15
16
  }
17
+ /**
18
+ * @internal
19
+ */
20
+ export function getScaleAndUnit(scaleUnit) {
21
+ if (typeof scaleUnit === 'number') {
22
+ return [scaleUnit, 'vw'];
23
+ }
24
+ const value = Number(scaleUnit.slice(0, -2));
25
+ if (scaleUnit.endsWith('vh'))
26
+ return [value, 'vh'];
27
+ if (scaleUnit.endsWith('vw'))
28
+ return [value, 'vw'];
29
+ return [NaN, 'vw'];
30
+ }
31
+ /**
32
+ * @internal
33
+ */
34
+ export function scaleOnDim(scale, dim, pxRatio) {
35
+ return (dim / 100) * (scale / pxRatio);
36
+ }
37
+ /**
38
+ * @internal
39
+ */
40
+ export function getScaleCtx(_engine = engine) {
41
+ const UiCanvasInformation = _engine.getComponent(engineUiCanvasInformation.componentId);
42
+ const canvasInfo = UiCanvasInformation.getOrNull(_engine.RootEntity);
43
+ if (!canvasInfo)
44
+ return undefined;
45
+ const { width, height, devicePixelRatio } = canvasInfo;
46
+ return { width, height, ratio: devicePixelRatio };
47
+ }
48
+ /**
49
+ * @internal
50
+ */
51
+ export function calcOnViewport(value, ctx = getScaleCtx()) {
52
+ const [scale, unit] = getScaleAndUnit(value);
53
+ if (!ctx)
54
+ return scale;
55
+ const { height, width, ratio } = ctx;
56
+ if (unit === 'vh')
57
+ return scaleOnDim(scale, height, ratio);
58
+ // by default, we scale by 'vw' (width)
59
+ return scaleOnDim(scale, width, ratio);
60
+ }
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@dcl/react-ecs",
3
3
  "description": "Decentraland ECS",
4
- "version": "7.4.3-7935604720.commit-e16c799",
4
+ "version": "7.4.3-7976826769.commit-bfde8e1",
5
5
  "author": "DCL",
6
6
  "bugs": "https://github.com/decentraland/js-sdk-toolchain/issues",
7
7
  "dependencies": {
8
- "@dcl/ecs": "7.4.3-7935604720.commit-e16c799",
8
+ "@dcl/ecs": "7.4.3-7976826769.commit-bfde8e1",
9
9
  "react": "^18.2.0",
10
10
  "react-reconciler": "^0.29.0"
11
11
  },
@@ -39,5 +39,5 @@
39
39
  "tsconfig": "./tsconfig.json"
40
40
  },
41
41
  "types": "./dist/index.d.ts",
42
- "commit": "e16c7990a84c14a83e410262a8912cf478229b6f"
42
+ "commit": "bfde8e1d46e4becfa46148c293f74be9daabf1a3"
43
43
  }