@dcl-regenesislabs/react-ecs 0.0.0-stage → 7.0.0-42707159203.commit-ff138cd

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.
Files changed (62) hide show
  1. package/README.md +130 -2
  2. package/dist/components/Button/index.d.ts +15 -0
  3. package/dist/components/Button/index.js +62 -0
  4. package/dist/components/Button/types.d.ts +16 -0
  5. package/dist/components/Button/types.js +1 -0
  6. package/dist/components/Dropdown/index.d.ts +21 -0
  7. package/dist/components/Dropdown/index.js +48 -0
  8. package/dist/components/Dropdown/types.d.ts +13 -0
  9. package/dist/components/Dropdown/types.js +1 -0
  10. package/dist/components/Input/index.d.ts +25 -0
  11. package/dist/components/Input/index.js +49 -0
  12. package/dist/components/Input/types.d.ts +15 -0
  13. package/dist/components/Input/types.js +1 -0
  14. package/dist/components/InteractableArea/index.d.ts +23 -0
  15. package/dist/components/InteractableArea/index.js +34 -0
  16. package/dist/components/InteractableArea/types.d.ts +23 -0
  17. package/dist/components/InteractableArea/types.js +1 -0
  18. package/dist/components/Label/index.d.ts +17 -0
  19. package/dist/components/Label/index.js +37 -0
  20. package/dist/components/Label/types.d.ts +34 -0
  21. package/dist/components/Label/types.js +1 -0
  22. package/dist/components/Label/utils.d.ts +11 -0
  23. package/dist/components/Label/utils.js +69 -0
  24. package/dist/components/ScreenInsetArea/index.d.ts +22 -0
  25. package/dist/components/ScreenInsetArea/index.js +33 -0
  26. package/dist/components/ScreenInsetArea/types.d.ts +22 -0
  27. package/dist/components/ScreenInsetArea/types.js +1 -0
  28. package/dist/components/index.d.ts +26 -0
  29. package/dist/components/index.js +36 -0
  30. package/dist/components/listeners/types.d.ts +19 -0
  31. package/dist/components/listeners/types.js +11 -0
  32. package/dist/components/types.d.ts +45 -0
  33. package/dist/components/types.js +1 -0
  34. package/dist/components/uiBackground/index.d.ts +6 -0
  35. package/dist/components/uiBackground/index.js +16 -0
  36. package/dist/components/uiBackground/types.d.ts +61 -0
  37. package/dist/components/uiBackground/types.js +1 -0
  38. package/dist/components/uiBackground/utils.d.ts +1 -0
  39. package/dist/components/uiBackground/utils.js +51 -0
  40. package/dist/components/uiTransform/index.d.ts +6 -0
  41. package/dist/components/uiTransform/index.js +91 -0
  42. package/dist/components/uiTransform/types.d.ts +134 -0
  43. package/dist/components/uiTransform/types.js +1 -0
  44. package/dist/components/uiTransform/utils.d.ts +42 -0
  45. package/dist/components/uiTransform/utils.js +246 -0
  46. package/dist/components/utils.d.ts +1 -0
  47. package/dist/components/utils.js +195 -0
  48. package/dist/index.d.ts +14 -0
  49. package/dist/index.js +15 -0
  50. package/dist/platform.d.ts +12 -0
  51. package/dist/platform.js +26 -0
  52. package/dist/react-ecs.d.ts +87 -0
  53. package/dist/react-ecs.js +10 -0
  54. package/dist/reconciler/index.d.ts +6 -0
  55. package/dist/reconciler/index.js +322 -0
  56. package/dist/reconciler/types.d.ts +31 -0
  57. package/dist/reconciler/types.js +1 -0
  58. package/dist/reconciler/utils.d.ts +40 -0
  59. package/dist/reconciler/utils.js +174 -0
  60. package/dist/system.d.ts +92 -0
  61. package/dist/system.js +264 -0
  62. package/package.json +41 -3
@@ -0,0 +1,134 @@
1
+ import { Color4 } from '@dcl/ecs/dist/components/generated/pb/decentraland/common/colors.gen';
2
+ import { ScaleUnit } from '../types';
3
+ /**
4
+ * unit value specified. i.e. 1 || '100%' || '1px' || '10vw'
5
+ * @public
6
+ */
7
+ export type PositionUnit = `${number}px` | `${number}%` | number | `${number}` | ScaleUnit;
8
+ /**
9
+ * The values are in clockwise order, beginning at the top: top, right, bottom, then left
10
+
11
+ * When one value is specified, it applies the same margin to all four sides.
12
+ *
13
+ * When two values are specified, the first margin applies to the top and bottom, the second to the left and right.
14
+ *
15
+ * When three values are specified, the first margin applies to the top, the second to the left and right, the third to the bottom.
16
+ *
17
+ When four values are specified, the margins apply to the top, right, bottom, and left in that order (clockwise).
18
+ * @public
19
+ */
20
+ export type PositionShorthand = PositionUnit | `${PositionUnit} ${PositionUnit}` | `${PositionUnit} ${PositionUnit} ${PositionUnit}` | `${PositionUnit} ${PositionUnit} ${PositionUnit} ${PositionUnit}`;
21
+ /**
22
+ * Type used for defining the position of the element. i.e. margin, padding
23
+ * @public
24
+ */
25
+ export interface Position {
26
+ top: PositionUnit;
27
+ right: PositionUnit;
28
+ bottom: PositionUnit;
29
+ left: PositionUnit;
30
+ }
31
+ /**
32
+ * Type used for defining the border radius of the element
33
+ * @public
34
+ */
35
+ export interface BorderRadius {
36
+ topLeft: PositionUnit;
37
+ topRight: PositionUnit;
38
+ bottomLeft: PositionUnit;
39
+ bottomRight: PositionUnit;
40
+ }
41
+ /**
42
+ * @public
43
+ */
44
+ export type DisplayType = 'flex' | 'none';
45
+ /**
46
+ * @public
47
+ */
48
+ export type JustifyType = 'flex-start' | 'center' | 'flex-end' | 'space-between' | 'space-around' | 'space-evenly';
49
+ /**
50
+ * @public
51
+ */
52
+ export type AlignType = 'auto' | 'flex-start' | 'center' | 'flex-end' | 'stretch' | 'baseline' | 'space-between' | 'space-around';
53
+ /**
54
+ * @public
55
+ */
56
+ export type FlexDirectionType = 'row' | 'column' | 'column-reverse' | 'row-reverse';
57
+ /**
58
+ * @public
59
+ */
60
+ export type FlexWrapType = 'wrap' | 'nowrap' | 'wrap-reverse';
61
+ /**
62
+ * @public
63
+ * The overflow property controls what happens to content that is too big to fit into an area
64
+ */
65
+ export type OverflowType = 'hidden' | 'scroll' | 'visible';
66
+ /**
67
+ * @public
68
+ * The position property specifies the type of positioning method used for an element
69
+ */
70
+ export type PositionType = 'absolute' | 'relative';
71
+ /**
72
+ * @public
73
+ * The pointer filter property determines if the ui element blocks the pointer or not (elements with pointer events always block the pointer regardless of this property)
74
+ */
75
+ export type PointerFilterType = 'none' | 'block';
76
+ /**
77
+ * Layout props to position things in the canvas
78
+ * @public
79
+ */
80
+ export interface UiTransformProps {
81
+ /** The display property controls if a item is going to be displayed or not. */
82
+ display?: DisplayType;
83
+ /** The flex shorthand property sets how a flex item will grow or shrink to fit the space available in its flex container. */
84
+ flex?: number;
85
+ /** Justify content describes how to align children within the main axis of their container. */
86
+ justifyContent?: JustifyType;
87
+ /** The position type of an element defines how it is positioned within its parent. */
88
+ positionType?: PositionType;
89
+ /** The align-items property controls the alignment of items on the Cross Axis. */
90
+ alignItems?: AlignType;
91
+ /** The align-self property has the same options and effect as align items but instead of affecting the children within a container, you can apply this property to a single child to change its alignment within its parent */
92
+ alignSelf?: AlignType;
93
+ /** The align-content property sets the distribution of space between and around content items along a flexbox's cross-axis or a grid's block axis. */
94
+ alignContent?: AlignType;
95
+ /** The flex-direction property sets how flex items are placed in the flex container defining the main axis and the direction (normal or reversed). */
96
+ flexDirection?: FlexDirectionType;
97
+ /** The position property sets how an element is positioned in a document. The top, right, bottom, and left properties determine the final location of positioned elements. */
98
+ position?: Partial<Position> | PositionShorthand;
99
+ /** The padding shorthand property sets the padding area on all four sides of an element at once. */
100
+ padding?: Partial<Position> | PositionShorthand;
101
+ /** The margin shorthand property sets the margin area on all four sides of an element. */
102
+ margin?: Partial<Position> | PositionShorthand;
103
+ /** The width property specifies the width of an element. Using 'auto' as value makes it adapt to its content. */
104
+ width?: PositionUnit | 'auto';
105
+ /** The height property specifies the height of an element. Using 'auto' as value makes it adapt to its content. */
106
+ height?: PositionUnit | 'auto';
107
+ /** The min-width property sets the minimum width of an element. */
108
+ minWidth?: PositionUnit;
109
+ /** The max-width property sets the maximum width of an element. */
110
+ maxWidth?: PositionUnit;
111
+ /** The min-height CSS property sets the minimum height of an element. */
112
+ minHeight?: PositionUnit;
113
+ /** The max-height property sets the maximum height of an element */
114
+ maxHeight?: PositionUnit;
115
+ /** The flex-wrap property sets whether flex items are forced onto one line or can wrap onto multiple lines. If wrapping is allowed, it sets the direction that lines are stacked*/
116
+ flexWrap?: FlexWrapType;
117
+ /** The flex-basis property sets the initial main size of a flex item. It sets the size of the content box.*/
118
+ flexBasis?: number;
119
+ /** The flex-grow property sets the flex grow factor of a flex item's main size. */
120
+ flexGrow?: number;
121
+ /** The flex-shrink property sets the flex shrink factor of a flex item. If the size of all flex items is larger than the flex container, items shrink to fit according to flex-shrink. */
122
+ flexShrink?: number;
123
+ /** The overflow property controls what happens to content that is too big to fit into an area */
124
+ overflow?: OverflowType;
125
+ /** The pointer filter property determines if the ui element blocks the pointer or not (elements with pointer events always block the pointer regardless of this property) **/
126
+ pointerFilter?: PointerFilterType;
127
+ borderColor?: Record<keyof Partial<Position>, Color4> | Color4 | undefined;
128
+ borderRadius?: Partial<BorderRadius> | PositionUnit;
129
+ borderWidth?: Partial<Position> | PositionUnit;
130
+ /** The opacity property sets the opacity level for an element, it's accumulated across children @defaultValue 1 */
131
+ opacity?: number;
132
+ /** default 0 */
133
+ zIndex?: number;
134
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,42 @@
1
+ import { YGUnit } from '@dcl/ecs';
2
+ import { Position, PositionUnit, PositionShorthand, BorderRadius, UiTransformProps } from './types';
3
+ import { Color4 } from '@dcl/ecs/dist/components/generated/pb/decentraland/common/colors.gen';
4
+ type PropName = 'position' | 'margin' | 'padding' | 'borderWidth';
5
+ type PropKey = `${PropName}${Capitalize<keyof Position>}`;
6
+ type PropKeyUnit = `${PropName}${Capitalize<keyof Position>}Unit`;
7
+ type PositionParsed = {
8
+ [key in PropKey]?: number;
9
+ } & {
10
+ [key in PropKeyUnit]?: YGUnit;
11
+ };
12
+ type BorderProp = `border${Capitalize<keyof BorderRadius>}Radius`;
13
+ type BorderPropUnit = `${BorderProp}Unit`;
14
+ type BorderProps = {
15
+ [key in BorderProp]?: number;
16
+ } & {
17
+ [key in BorderPropUnit]?: YGUnit;
18
+ };
19
+ export declare function parseBorderRadius(radius: Partial<BorderRadius> | PositionUnit): BorderProps | undefined;
20
+ type BorderKey = Record<keyof Position, number>;
21
+ type BorderWidthProp = `border${Capitalize<keyof BorderKey>}Width`;
22
+ type BorderWidthPropUnit = `${BorderWidthProp}Unit`;
23
+ type BorderWidthProps = {
24
+ [key in BorderWidthProp]?: number;
25
+ } & {
26
+ [key in BorderWidthPropUnit]?: YGUnit;
27
+ };
28
+ export declare function parseBorderWidth(borderWidth: Partial<Position> | PositionUnit): BorderWidthProps | undefined;
29
+ type BorderColor = Record<keyof Position, Color4>;
30
+ type BorderColorKey = `border${Capitalize<keyof BorderColor>}Color`;
31
+ export declare function parseBorderColor(borderColor: UiTransformProps['borderColor']): Record<BorderColorKey, Color4> | undefined;
32
+ export declare function parsePosition<T extends PropName>(position: PositionShorthand | Partial<Position> | undefined, prop: T): Partial<PositionParsed>;
33
+ type HeightWidth = 'height' | 'width';
34
+ type SizePropName = HeightWidth | `max${Capitalize<HeightWidth>}` | `min${Capitalize<HeightWidth>}`;
35
+ type SizePropKeyUnit = `${SizePropName}Unit`;
36
+ type SizeReturnType = {
37
+ [key in SizePropName]: number;
38
+ } & {
39
+ [key in SizePropKeyUnit]: YGUnit;
40
+ };
41
+ export declare function parseSize(val: PositionUnit | 'auto' | undefined, key: SizePropName): Partial<SizeReturnType>;
42
+ export {};
@@ -0,0 +1,246 @@
1
+ import { calcOnViewport, getUiScaleFactor } from '../utils';
2
+ function capitalize(value) {
3
+ return `${value[0].toUpperCase()}${value.slice(1, value.length)}`;
4
+ }
5
+ function isPercent(val) {
6
+ return typeof val === 'string' && val.endsWith('%');
7
+ }
8
+ function isPoint(val) {
9
+ return typeof val === 'string' && val.endsWith('px');
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
+ }
17
+ function scalePixelValue(value) {
18
+ return value * getUiScaleFactor();
19
+ }
20
+ function parsePositionUnit(val) {
21
+ function getValue(key, value) {
22
+ return Number(value.slice(0, value.indexOf(key)));
23
+ }
24
+ if (val === undefined || val === null) {
25
+ return [undefined, 0 /* YGUnit.YGU_UNDEFINED */];
26
+ }
27
+ if (val === 'auto') {
28
+ return [0, 3 /* YGUnit.YGU_AUTO */];
29
+ }
30
+ if (typeof val === 'number' || (typeof val === 'string' && !isNaN(Number(val)))) {
31
+ return [scalePixelValue(Number(val)), 1 /* YGUnit.YGU_POINT */];
32
+ }
33
+ if (isPercent(val)) {
34
+ return [getValue('%', val), 2 /* YGUnit.YGU_PERCENT */];
35
+ }
36
+ if (isPoint(val)) {
37
+ return [scalePixelValue(getValue('px', val)), 1 /* YGUnit.YGU_POINT */];
38
+ }
39
+ if (isVw(val) || isVh(val)) {
40
+ return [calcOnViewport(val), 1 /* YGUnit.YGU_POINT */];
41
+ }
42
+ return [undefined, 0 /* YGUnit.YGU_UNDEFINED */];
43
+ }
44
+ export function parseBorderRadius(radius) {
45
+ if (typeof radius === 'object') {
46
+ const obj = {};
47
+ for (const key in radius) {
48
+ const typedKey = key;
49
+ const propKey = `border${capitalize(typedKey)}Radius`;
50
+ const propKeyUnit = `${propKey}Unit`;
51
+ const [value, unit] = parsePositionUnit(radius[typedKey]);
52
+ if (value === undefined)
53
+ continue;
54
+ obj[propKeyUnit] = unit;
55
+ obj[propKey] = value;
56
+ }
57
+ return obj;
58
+ }
59
+ if (typeof radius === 'number') {
60
+ return parseBorderRadius({ topLeft: radius, topRight: radius, bottomLeft: radius, bottomRight: radius });
61
+ }
62
+ }
63
+ export function parseBorderWidth(borderWidth) {
64
+ if (typeof borderWidth === 'object') {
65
+ const obj = {};
66
+ for (const key in borderWidth) {
67
+ const typedKey = key;
68
+ const propKey = `border${capitalize(typedKey)}Width`;
69
+ const propKeyUnit = `${propKey}Unit`;
70
+ const [value, unit] = parsePositionUnit(borderWidth[typedKey]);
71
+ if (value === undefined)
72
+ continue;
73
+ obj[propKeyUnit] = unit;
74
+ obj[propKey] = value;
75
+ }
76
+ return obj;
77
+ }
78
+ if (typeof borderWidth === 'number') {
79
+ return parseBorderWidth({ top: borderWidth, left: borderWidth, right: borderWidth, bottom: borderWidth });
80
+ }
81
+ }
82
+ const isColor = (v) => {
83
+ if (v.b !== undefined)
84
+ return true;
85
+ return false;
86
+ };
87
+ export function parseBorderColor(borderColor) {
88
+ if (isColor(borderColor)) {
89
+ return parseBorderColor({ top: borderColor, left: borderColor, right: borderColor, bottom: borderColor });
90
+ }
91
+ if (typeof borderColor === 'object') {
92
+ const obj = {};
93
+ for (const key in borderColor) {
94
+ const typedKey = key;
95
+ const propKey = `border${capitalize(typedKey)}Color`;
96
+ obj[propKey] = borderColor[typedKey];
97
+ }
98
+ return obj;
99
+ }
100
+ }
101
+ export function parsePosition(position = {}, prop) {
102
+ if (typeof position === 'object') {
103
+ const obj = {};
104
+ for (const key in position) {
105
+ const typedKey = key;
106
+ const propKey = `${prop}${capitalize(typedKey)}`;
107
+ const propKeyUnit = `${prop}${capitalize(typedKey)}Unit`;
108
+ const [value, unit] = parsePositionUnit(position[typedKey]);
109
+ if (value === undefined)
110
+ continue;
111
+ obj[propKeyUnit] = unit;
112
+ obj[propKey] = value;
113
+ }
114
+ return obj;
115
+ }
116
+ if (typeof position === 'number') {
117
+ return parsePosition({ top: position, left: position, right: position, bottom: position }, prop);
118
+ }
119
+ const values = position.split(' ').filter((a) => a !== '');
120
+ if (values.length === 1) {
121
+ const [value] = values;
122
+ return parsePosition({ top: value, left: value, right: value, bottom: value }, prop);
123
+ }
124
+ if (values.length === 2) {
125
+ const [topBottom, rigthLeft] = values;
126
+ return parsePosition({ top: topBottom, left: rigthLeft, right: rigthLeft, bottom: topBottom }, prop);
127
+ }
128
+ if (values.length === 3) {
129
+ const [top, rigthLeft, bottom] = values;
130
+ return parsePosition({ top, left: rigthLeft, right: rigthLeft, bottom }, prop);
131
+ }
132
+ const [top, right, bottom, left] = values;
133
+ return parsePosition({ top, right, bottom, left }, prop);
134
+ }
135
+ export function parseSize(val, key) {
136
+ const unitKey = `${key}Unit`;
137
+ const [value, unit] = parsePositionUnit(val);
138
+ if (value === undefined)
139
+ return {};
140
+ return {
141
+ [key]: value,
142
+ [unitKey]: unit
143
+ };
144
+ }
145
+ /**
146
+ * @internal
147
+ */
148
+ export function getDisplay(display) {
149
+ const value = display ? parseDisplay[display] : 0 /* YGDisplay.YGD_FLEX */;
150
+ return { display: value };
151
+ }
152
+ const parseDisplay = {
153
+ flex: 0 /* YGDisplay.YGD_FLEX */,
154
+ none: 1 /* YGDisplay.YGD_NONE */
155
+ };
156
+ /**
157
+ * @internal
158
+ */
159
+ export function getJustify(justify) {
160
+ const value = justify ? parseJustify[justify] : 0 /* YGJustify.YGJ_FLEX_START */;
161
+ return { justifyContent: value };
162
+ }
163
+ const parseJustify = {
164
+ center: 1 /* YGJustify.YGJ_CENTER */,
165
+ 'flex-end': 2 /* YGJustify.YGJ_FLEX_END */,
166
+ 'flex-start': 0 /* YGJustify.YGJ_FLEX_START */,
167
+ 'space-around': 4 /* YGJustify.YGJ_SPACE_AROUND */,
168
+ 'space-between': 3 /* YGJustify.YGJ_SPACE_BETWEEN */,
169
+ 'space-evenly': 5 /* YGJustify.YGJ_SPACE_EVENLY */
170
+ };
171
+ /**
172
+ * @internal
173
+ */
174
+ export function getAlign(prop, align) {
175
+ const value = parseAligns[align];
176
+ return { [prop]: value };
177
+ }
178
+ const parseAligns = {
179
+ auto: 0 /* YGAlign.YGA_AUTO */,
180
+ baseline: 5 /* YGAlign.YGA_BASELINE */,
181
+ center: 2 /* YGAlign.YGA_CENTER */,
182
+ 'flex-end': 3 /* YGAlign.YGA_FLEX_END */,
183
+ 'flex-start': 1 /* YGAlign.YGA_FLEX_START */,
184
+ stretch: 4 /* YGAlign.YGA_STRETCH */,
185
+ 'space-between': 6 /* YGAlign.YGA_SPACE_BETWEEN */,
186
+ 'space-around': 7 /* YGAlign.YGA_SPACE_AROUND */
187
+ };
188
+ /**
189
+ * @internal
190
+ */
191
+ export function getFlexDirection(flexDirection) {
192
+ const value = flexDirection ? parseFlexDirection[flexDirection] : 0 /* YGFlexDirection.YGFD_ROW */;
193
+ return { flexDirection: value };
194
+ }
195
+ const parseFlexDirection = {
196
+ row: 0 /* YGFlexDirection.YGFD_ROW */,
197
+ column: 1 /* YGFlexDirection.YGFD_COLUMN */,
198
+ 'row-reverse': 3 /* YGFlexDirection.YGFD_ROW_REVERSE */,
199
+ 'column-reverse': 2 /* YGFlexDirection.YGFD_COLUMN_REVERSE */
200
+ };
201
+ /**
202
+ * @internal
203
+ */
204
+ export function getFlexWrap(flexWrap) {
205
+ const value = parseFlexWrap[flexWrap];
206
+ return { flexWrap: value };
207
+ }
208
+ const parseFlexWrap = {
209
+ wrap: 1 /* YGWrap.YGW_WRAP */,
210
+ nowrap: 0 /* YGWrap.YGW_NO_WRAP */,
211
+ 'wrap-reverse': 2 /* YGWrap.YGW_WRAP_REVERSE */
212
+ };
213
+ /**
214
+ * @internal
215
+ */
216
+ export function getOverflow(overflow) {
217
+ const value = overflow ? parseOverflow[overflow] : 0 /* YGOverflow.YGO_VISIBLE */;
218
+ return { overflow: value };
219
+ }
220
+ const parseOverflow = {
221
+ visible: 0 /* YGOverflow.YGO_VISIBLE */,
222
+ scroll: 2 /* YGOverflow.YGO_SCROLL */,
223
+ hidden: 1 /* YGOverflow.YGO_HIDDEN */
224
+ };
225
+ /**
226
+ * @internal
227
+ */
228
+ export function getPositionType(position) {
229
+ const value = position ? parsePositionType[position] : 0 /* YGPositionType.YGPT_RELATIVE */;
230
+ return { positionType: value };
231
+ }
232
+ const parsePositionType = {
233
+ relative: 0 /* YGPositionType.YGPT_RELATIVE */,
234
+ absolute: 1 /* YGPositionType.YGPT_ABSOLUTE */
235
+ };
236
+ /**
237
+ * @internal
238
+ */
239
+ export function getPointerFilter(pointerFilter) {
240
+ const value = pointerFilter ? parsePointerFilter[pointerFilter] : 0 /* PointerFilterMode.PFM_NONE */;
241
+ return { pointerFilter: value };
242
+ }
243
+ const parsePointerFilter = {
244
+ none: 0 /* PointerFilterMode.PFM_NONE */,
245
+ block: 1 /* PointerFilterMode.PFM_BLOCK */
246
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,195 @@
1
+ import { engine, UiCanvasInformation as engineUiCanvasInformation } from '@dcl/ecs';
2
+ import { parseUiBackground } from './uiBackground';
3
+ import { parseUiTransform } from './uiTransform';
4
+ let uiScaleFactor = 1;
5
+ let uiScaleOwner = undefined;
6
+ const ZERO_INSETS = { top: 0, left: 0, right: 0, bottom: 0 };
7
+ let screenInsetArea = { ...ZERO_INSETS };
8
+ let screenInsetAreaOwner = undefined;
9
+ let interactableArea = { ...ZERO_INSETS };
10
+ let interactableAreaOwner = undefined;
11
+ /**
12
+ * @internal
13
+ */
14
+ export function parseProps(props) {
15
+ const { uiTransform, uiBackground, ...otherProps } = props;
16
+ const uiTransformProps = parseUiTransform(uiTransform);
17
+ const uiBackgroundProps = uiBackground ? { uiBackground: parseUiBackground(uiBackground) } : undefined;
18
+ return {
19
+ ...otherProps,
20
+ uiTransform: uiTransformProps,
21
+ ...uiBackgroundProps
22
+ };
23
+ }
24
+ /**
25
+ * @internal
26
+ */
27
+ export function getScaleAndUnit(scaleUnit) {
28
+ if (typeof scaleUnit === 'number') {
29
+ return [scaleUnit, 'vw'];
30
+ }
31
+ const value = Number(scaleUnit.slice(0, -2));
32
+ if (scaleUnit.endsWith('vh'))
33
+ return [value, 'vh'];
34
+ if (scaleUnit.endsWith('vw'))
35
+ return [value, 'vw'];
36
+ return [NaN, 'vw'];
37
+ }
38
+ /**
39
+ * @internal
40
+ */
41
+ export function scaleOnDim(scale, dim) {
42
+ return (dim / 100) * scale;
43
+ }
44
+ /**
45
+ * @internal
46
+ */
47
+ export function getScaleCtx(_engine = engine) {
48
+ const UiCanvasInformation = _engine.getComponent(engineUiCanvasInformation.componentId);
49
+ const canvasInfo = UiCanvasInformation.getOrNull(_engine.RootEntity);
50
+ if (!canvasInfo)
51
+ return undefined;
52
+ const { width, height, devicePixelRatio } = canvasInfo;
53
+ return { width, height, ratio: devicePixelRatio };
54
+ }
55
+ /**
56
+ * @internal
57
+ */
58
+ export function getUiScaleFactor() {
59
+ return uiScaleFactor;
60
+ }
61
+ /**
62
+ * Sets the global UI scale factor.
63
+ *
64
+ * The `owner` symbol implements a cooperative reset-protection scheme shared
65
+ * with {@link resetUiScaleFactor}:
66
+ * - Writes always succeed — last writer claims ownership (the most recent
67
+ * `owner` passed to `set` is the one allowed to `reset`).
68
+ * - Resets from a non-matching owner are ignored, so a stale system can't
69
+ * stomp the active scale while another system is driving it.
70
+ * - A reset called without an owner always wins (used by tests / teardown).
71
+ *
72
+ * @internal
73
+ */
74
+ export function setUiScaleFactor(nextScale, owner) {
75
+ if (!Number.isFinite(nextScale) || nextScale < 0)
76
+ return;
77
+ if (owner) {
78
+ uiScaleOwner = owner;
79
+ }
80
+ uiScaleFactor = nextScale;
81
+ }
82
+ /**
83
+ * @internal
84
+ */
85
+ export function resetUiScaleFactor(owner) {
86
+ // No-op for non-owners (see ownership rules on `setUiScaleFactor`).
87
+ // A reset with no owner always wins — used by tests and teardown.
88
+ if (owner && uiScaleOwner !== owner)
89
+ return;
90
+ uiScaleOwner = undefined;
91
+ uiScaleFactor = 1;
92
+ }
93
+ /**
94
+ * Divides an inset area by the current UI scale factor.
95
+ *
96
+ * Inset areas are reported by the renderer in canvas pixels, but raw pixel
97
+ * values in `uiTransform` props are multiplied by the UI scale factor when
98
+ * parsed. Pre-dividing cancels that multiplication out, so the values sent to
99
+ * the renderer stay in canvas pixels regardless of the virtual screen.
100
+ *
101
+ * @internal
102
+ */
103
+ export function compensateInsetForUiScale(area) {
104
+ const scale = getUiScaleFactor();
105
+ const factor = scale > 0 ? scale : 1;
106
+ return { top: area.top / factor, left: area.left / factor, right: area.right / factor, bottom: area.bottom / factor };
107
+ }
108
+ /**
109
+ * @internal
110
+ */
111
+ export function getScreenInsetArea() {
112
+ return { ...screenInsetArea };
113
+ }
114
+ /**
115
+ * Sets the global screen inset area.
116
+ *
117
+ * The `owner` symbol implements a cooperative reset-protection scheme shared
118
+ * with {@link resetScreenInsetArea}:
119
+ * - Writes always succeed — last writer claims ownership (the most recent
120
+ * `owner` passed to `set` is the one allowed to `reset`).
121
+ * - Resets from a non-matching owner are ignored, so a stale system can't
122
+ * stomp the active insets while another system is driving them.
123
+ * - A reset called without an owner always wins (used by tests / teardown).
124
+ *
125
+ * @internal
126
+ */
127
+ export function setScreenInsetArea(next, owner) {
128
+ if (owner) {
129
+ screenInsetAreaOwner = owner;
130
+ }
131
+ screenInsetArea = { top: next.top, left: next.left, right: next.right, bottom: next.bottom };
132
+ }
133
+ /**
134
+ * @internal
135
+ */
136
+ export function resetScreenInsetArea(owner) {
137
+ // No-op for non-owners (see ownership rules on `setScreenInsetArea`).
138
+ // A reset with no owner always wins — used by tests and teardown.
139
+ if (owner && screenInsetAreaOwner !== owner)
140
+ return;
141
+ screenInsetAreaOwner = undefined;
142
+ screenInsetArea = { ...ZERO_INSETS };
143
+ }
144
+ /**
145
+ * @internal
146
+ */
147
+ export function getInteractableArea() {
148
+ return { ...interactableArea };
149
+ }
150
+ /**
151
+ * Sets the global interactable area.
152
+ *
153
+ * The `owner` symbol implements a cooperative reset-protection scheme shared
154
+ * with {@link resetInteractableArea}:
155
+ * - Writes always succeed — last writer claims ownership (the most recent
156
+ * `owner` passed to `set` is the one allowed to `reset`).
157
+ * - Resets from a non-matching owner are ignored, so a stale system can't
158
+ * stomp the active area while another system is driving it.
159
+ * - A reset called without an owner always wins (used by tests / teardown).
160
+ *
161
+ * @internal
162
+ */
163
+ export function setInteractableArea(next, owner) {
164
+ if (owner) {
165
+ interactableAreaOwner = owner;
166
+ }
167
+ interactableArea = { top: next.top, left: next.left, right: next.right, bottom: next.bottom };
168
+ }
169
+ /**
170
+ * @internal
171
+ */
172
+ export function resetInteractableArea(owner) {
173
+ // No-op for non-owners (see ownership rules on `setInteractableArea`).
174
+ // A reset with no owner always wins — used by tests and teardown.
175
+ if (owner && interactableAreaOwner !== owner)
176
+ return;
177
+ interactableAreaOwner = undefined;
178
+ interactableArea = { ...ZERO_INSETS };
179
+ }
180
+ /**
181
+ * @internal
182
+ */
183
+ export function calcOnViewport(value, ctx = getScaleCtx()) {
184
+ const [scale, unit] = getScaleAndUnit(value);
185
+ if (!ctx)
186
+ return scale;
187
+ // `ctx.ratio` is intentionally not read: '1vw' is 1% of the canvas width by
188
+ // definition, exactly as in CSS. It stays on ScaleContext as an informational
189
+ // density hint for callers that need to pick an asset resolution.
190
+ const { height, width } = ctx;
191
+ if (unit === 'vh')
192
+ return scaleOnDim(scale, height);
193
+ // by default, we scale by 'vw' (width)
194
+ return scaleOnDim(scale, width);
195
+ }
@@ -0,0 +1,14 @@
1
+ import { ReactEcs } from './react-ecs';
2
+ import { ReactBasedUiSystem } from './system';
3
+ export * from './components';
4
+ export * from './system';
5
+ export * from './react-ecs';
6
+ /**
7
+ * ReactEcs variable provides the function to render & destroy the specified UI
8
+ * @public
9
+ * @example
10
+ * import { ReactEcsRenderer } from '@dcl/sdk/react-ecs'
11
+ * ReactEcsRenderer.setUiRenderer(uiComponent)
12
+ */
13
+ export declare const ReactEcsRenderer: ReactBasedUiSystem;
14
+ export default ReactEcs;
package/dist/index.js ADDED
@@ -0,0 +1,15 @@
1
+ import { engine, pointerEventsSystem } from '@dcl/ecs';
2
+ import { ReactEcs } from './react-ecs';
3
+ import { createReactBasedUiSystem } from './system';
4
+ export * from './components';
5
+ export * from './system';
6
+ export * from './react-ecs';
7
+ /**
8
+ * ReactEcs variable provides the function to render & destroy the specified UI
9
+ * @public
10
+ * @example
11
+ * import { ReactEcsRenderer } from '@dcl/sdk/react-ecs'
12
+ * ReactEcsRenderer.setUiRenderer(uiComponent)
13
+ */
14
+ export const ReactEcsRenderer = /* @__PURE__ */ createReactBasedUiSystem(engine, pointerEventsSystem);
15
+ export default ReactEcs;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Injects the function used to detect whether the scene runs on a mobile
3
+ * platform. Called by `@dcl/sdk` with its `isMobile()` platform helper.
4
+ */
5
+ export declare function setIsMobileProvider(provider: () => boolean): void;
6
+ /**
7
+ * Whether the scene is running on a mobile platform, according to the
8
+ * injected provider. Platform detection is asynchronous on the SDK side, so
9
+ * this may return false during the first ticks and flip to true once the
10
+ * explorer information arrives.
11
+ */
12
+ export declare function isMobile(): boolean;