From cf136d80173d995dd13bf59c7c7416993b74d833 Mon Sep 17 00:00:00 2001 From: Pravus Date: Thu, 26 Oct 2023 19:58:12 +0200 Subject: [PATCH] feat: ui transform auto-size (#796) Exposed 'auto' option for width/height of ui transform --- .../etc/playground-assets.api.md | 4 ++-- .../src/components/uiTransform/types.ts | 8 +++---- .../src/components/uiTransform/utils.ts | 8 +++++-- test/react-ecs/transform.spec.tsx | 24 +++++++++++++++++++ 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/packages/@dcl/playground-assets/etc/playground-assets.api.md b/packages/@dcl/playground-assets/etc/playground-assets.api.md index 051b2c37d..9f6638b2d 100644 --- a/packages/@dcl/playground-assets/etc/playground-assets.api.md +++ b/packages/@dcl/playground-assets/etc/playground-assets.api.md @@ -3944,7 +3944,7 @@ export interface UiTransformProps { flexGrow?: number; flexShrink?: number; flexWrap?: FlexWrapType; - height?: PositionUnit; + height?: PositionUnit | 'auto'; justifyContent?: JustifyType; margin?: Partial | PositionShorthand; maxHeight?: PositionUnit; @@ -3956,7 +3956,7 @@ export interface UiTransformProps { pointerFilter?: PointerFilterType; position?: Partial | PositionShorthand; positionType?: PositionType; - width?: PositionUnit; + width?: PositionUnit | 'auto'; } // @public (undocumented) diff --git a/packages/@dcl/react-ecs/src/components/uiTransform/types.ts b/packages/@dcl/react-ecs/src/components/uiTransform/types.ts index cb0759c6e..8a3186db0 100644 --- a/packages/@dcl/react-ecs/src/components/uiTransform/types.ts +++ b/packages/@dcl/react-ecs/src/components/uiTransform/types.ts @@ -108,10 +108,10 @@ export interface UiTransformProps { padding?: Partial | PositionShorthand /** The margin shorthand property sets the margin area on all four sides of an element. */ margin?: Partial | PositionShorthand - /** The width property specifies the width of an element. */ - width?: PositionUnit - /** The height property specifies the height of an element. */ - height?: PositionUnit + /** The width property specifies the width of an element. Using 'auto' as value makes it adapt to its content. */ + width?: PositionUnit | 'auto' + /** The height property specifies the height of an element. Using 'auto' as value makes it adapt to its content. */ + height?: PositionUnit | 'auto' /** The min-width property sets the minimum width of an element. */ minWidth?: PositionUnit /** The max-width property sets the maximum width of an element. */ diff --git a/packages/@dcl/react-ecs/src/components/uiTransform/utils.ts b/packages/@dcl/react-ecs/src/components/uiTransform/utils.ts index 033f5ac19..8f2228d53 100644 --- a/packages/@dcl/react-ecs/src/components/uiTransform/utils.ts +++ b/packages/@dcl/react-ecs/src/components/uiTransform/utils.ts @@ -43,7 +43,7 @@ function isPoint(val: PositionUnit) { return typeof val === 'string' && val.endsWith('px') } -function parsePositionUnit(val?: PositionUnit): [number | undefined, YGUnit] { +function parsePositionUnit(val?: PositionUnit | 'auto'): [number | undefined, YGUnit] { function getValue(key: 'px' | '%', value: string) { return Number(value.slice(0, value.indexOf(key))) } @@ -52,6 +52,10 @@ function parsePositionUnit(val?: PositionUnit): [number | undefined, YGUnit] { return [undefined, YGUnit.YGU_UNDEFINED] } + if (val === 'auto') { + return [0, YGUnit.YGU_AUTO] + } + if (typeof val === 'number' || (typeof val === 'string' && !isNaN(Number(val)))) { return [Number(val), YGUnit.YGU_POINT] } @@ -115,7 +119,7 @@ type SizePropKeyUnit = `${SizePropName}Unit` type SizeReturnType = { [key in SizePropName]: number } & { [key in SizePropKeyUnit]: YGUnit } -export function parseSize(val: PositionUnit | undefined, key: SizePropName): Partial { +export function parseSize(val: PositionUnit | 'auto' | undefined, key: SizePropName): Partial { const unitKey: SizePropKeyUnit = `${key}Unit` const [value, unit] = parsePositionUnit(val) diff --git a/test/react-ecs/transform.spec.tsx b/test/react-ecs/transform.spec.tsx index b23a8b005..77a86504c 100644 --- a/test/react-ecs/transform.spec.tsx +++ b/test/react-ecs/transform.spec.tsx @@ -315,4 +315,28 @@ describe('UiTransform React Ecs', () => { pointerFilter: PointerFilterMode.PFM_BLOCK }) }) + + it('should parse auto size correctly', async () => { + const { engine, uiRenderer } = setupEngine() + const UiTransform = components.UiTransform(engine) + const entityIndex = engine.addEntity() as number + + // Helpers + const rootDivEntity = (entityIndex + 1) as Entity + const getUiTransform = (entity: Entity) => UiTransform.get(entity) + const ui = () => ( + + ) + uiRenderer.setUiRenderer(ui) + await engine.update(1) + expect(getUiTransform(rootDivEntity)).toMatchObject({ + widthUnit: YGUnit.YGU_AUTO, + heightUnit: YGUnit.YGU_AUTO + }) + }) })