Skip to content

Commit

Permalink
support size-* convenience utility
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredh159 committed Jul 22, 2024
1 parent 86e80cd commit d45f255
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/UtilityParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { border, borderRadius } from './resolve/borders';
import * as h from './helpers';
import { inset } from './resolve/inset';
import { flexGrowShrink, flexBasis, flex, gap } from './resolve/flex';
import { widthHeight, minMaxWidthHeight } from './resolve/width-height';
import { widthHeight, size, minMaxWidthHeight } from './resolve/width-height';
import { letterSpacing } from './resolve/letter-spacing';
import { opacity } from './resolve/opacity';
import { shadowOpacity, shadowOffset } from './resolve/shadow';
Expand Down Expand Up @@ -295,6 +295,11 @@ export default class UtilityParser {
}
}

if (this.consumePeeked(`size-`)) {
style = size(this.rest, this.context, theme);
if (style) return style;
}

h.warn(`\`${this.rest}\` unknown or invalid utility`);
return null;
}
Expand Down
5 changes: 5 additions & 0 deletions src/__tests__/width-height.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ describe(`width/height utilities`, () => {
// arbitrary
[`h-[333px]`, { height: 333 }],

// size-*
[`size-1`, { width: 4, height: 4 }],
[`size-3/4`, { width: `75%`, height: `75%` }],
[`size-[333px]`, { width: 333, height: 333 }],

// not configged, use 0.25rem = 1 as formula
[`h-81`, { height: (81 / 4) * 16 }],

Expand Down
13 changes: 13 additions & 0 deletions src/resolve/width-height.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ export function widthHeight(
return unconfiggedStyle(type, value, context);
}

export function size(
value: string,
context: ParseContext = {},
theme?: TwTheme,
): StyleIR | null {
const width = widthHeight(`width`, value, context, theme?.width);
const height = widthHeight(`height`, value, context, theme?.height);
if (width?.kind !== `complete` || height?.kind !== `complete`) {
return null;
}
return complete({ ...width.style, ...height.style });
}

export function minMaxWidthHeight(
type: 'minWidth' | 'minHeight' | 'maxWidth' | 'maxHeight',
value: string,
Expand Down

0 comments on commit d45f255

Please sign in to comment.