Skip to content

Commit

Permalink
feat: ui transform auto-size (#796)
Browse files Browse the repository at this point in the history
Exposed 'auto' option for width/height of ui transform
  • Loading branch information
pravusjif authored Oct 26, 2023
1 parent df58413 commit cf136d8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/@dcl/playground-assets/etc/playground-assets.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3944,7 +3944,7 @@ export interface UiTransformProps {
flexGrow?: number;
flexShrink?: number;
flexWrap?: FlexWrapType;
height?: PositionUnit;
height?: PositionUnit | 'auto';
justifyContent?: JustifyType;
margin?: Partial<Position> | PositionShorthand;
maxHeight?: PositionUnit;
Expand All @@ -3956,7 +3956,7 @@ export interface UiTransformProps {
pointerFilter?: PointerFilterType;
position?: Partial<Position> | PositionShorthand;
positionType?: PositionType;
width?: PositionUnit;
width?: PositionUnit | 'auto';
}

// @public (undocumented)
Expand Down
8 changes: 4 additions & 4 deletions packages/@dcl/react-ecs/src/components/uiTransform/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ export interface UiTransformProps {
padding?: Partial<Position> | PositionShorthand
/** The margin shorthand property sets the margin area on all four sides of an element. */
margin?: Partial<Position> | 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. */
Expand Down
8 changes: 6 additions & 2 deletions packages/@dcl/react-ecs/src/components/uiTransform/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
Expand All @@ -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]
}
Expand Down Expand Up @@ -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<SizeReturnType> {
export function parseSize(val: PositionUnit | 'auto' | undefined, key: SizePropName): Partial<SizeReturnType> {
const unitKey: SizePropKeyUnit = `${key}Unit`
const [value, unit] = parsePositionUnit(val)

Expand Down
24 changes: 24 additions & 0 deletions test/react-ecs/transform.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => (
<UiEntity
uiTransform={{
width: 'auto',
height: 'auto'
}}
/>
)
uiRenderer.setUiRenderer(ui)
await engine.update(1)
expect(getUiTransform(rootDivEntity)).toMatchObject({
widthUnit: YGUnit.YGU_AUTO,
heightUnit: YGUnit.YGU_AUTO
})
})
})

0 comments on commit cf136d8

Please sign in to comment.