Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support size-* convenience utility #315

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project ad

[comment]: # 'Section Titles: Added/Fixed/Changed/Removed'

## [4.5.0] - 2024-07-22

- added support for `size-*` shorthand utility
[(#314)](https://github.com/jaredh159/tailwind-react-native-classnames/issues/314) and
[(#315)](https://github.com/jaredh159/tailwind-react-native-classnames/pull/315)

## [4.4.0] - 2024-07-09

- added support for arbitrary named colors (eg. `text-[lemonchiffon]`)
Expand Down
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