Skip to content

Commit 9b61716

Browse files
committed
Add support for negative values for spacings
If the space alias starts with a `-` sign the value is considered negative so it's multiply by -1 after being translated using the correct scale. Also adjust the types to take into account the negative versions of the aliases defined in the theme config.
1 parent 697af2a commit 9b61716

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

src/core/layout/Layout.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,29 @@ import type {
55
Size, SizeAlias, Spacing, SpacingAlias
66
} from './types';
77

8+
const isNegativeAlias = ( alias: string ) => (
9+
alias.charAt( 0 ) === '-'
10+
);
11+
12+
const translateSpacing = (
13+
alias: SpacingAlias,
14+
scale: { [ x: SpacingAlias ]: Spacing }
15+
): Spacing | undefined => {
16+
if ( !alias ) { return undefined; }
17+
18+
let scaleAlias = String( alias );
19+
let isNegative = false;
20+
21+
if ( isNegativeAlias( scaleAlias ) ) {
22+
isNegative = true;
23+
scaleAlias = scaleAlias.slice( 1 );
24+
}
25+
26+
if ( !scale[ scaleAlias ] ) { return undefined; }
27+
28+
return Number( scale[ scaleAlias ] ) * ( isNegative ? -1 : 1 );
29+
};
30+
831
export default class Layout {
932
private _config: LayoutConfig;
1033

@@ -19,7 +42,7 @@ export default class Layout {
1942
}
2043

2144
spacing( alias: SpacingAlias ): Spacing | undefined {
22-
return this._config.spacings[ alias ];
45+
return translateSpacing( alias, this._config.spacings );
2346
}
2447

2548
size( alias: SizeAlias ): Size | undefined {

src/core/theme/defaultTheme/components/Chip.ts

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export default {
2323
size: 'xs',
2424
marginLeft: '2',
2525
color: 'primary.900',
26+
padding: 0.5,
27+
marginY: -0.5,
2628
borderRadius: 'full',
2729
__pressed: {
2830
backgroundColor: 'secondary.300'

src/core/theme/types.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,29 @@ export type ColorType = Leaves<IThemeConfig[ 'palette' ][ 'base' ]>
1919

2020
type RNStyles = ViewStyle & ImageStyle & TextStyle;
2121

22+
type GetLayoutScaleValuesWithNegativeValues<
23+
T extends keyof IThemeConfig[ 'layout' ]
24+
> = {
25+
[K in keyof IThemeConfig[ 'layout' ][T]]-?: K extends string | number
26+
? K | `-${K}`
27+
: never
28+
}[keyof IThemeConfig[ 'layout' ][T]]
29+
30+
type SpaceType = GetLayoutScaleValuesWithNegativeValues<'spacings'>
31+
| ( string & {} )
32+
| ( number & {} );
33+
2234
type GetThemeScaleValues<T extends keyof IThemeConfig> = 'palette' extends T
2335
? ColorType
2436
: keyof IThemeConfig[T] | ( string & {} ) | ( number & {} );
2537

2638
type GetThemeCategorizedScaleValues<
2739
T extends keyof IThemeConfig, Y extends keyof IThemeConfig[T]
28-
> = (
29-
keyof IThemeConfig[T][Y] | ( string & {} ) | ( number & {} )
30-
);
40+
> = 'layout' extends T
41+
? 'spacings' extends Y
42+
? SpaceType
43+
: keyof IThemeConfig[T][Y] | ( string & {} ) | ( number & {} )
44+
: keyof IThemeConfig[T][Y] | ( string & {} ) | ( number & {} );
3145

3246
type GetCategorizedRNStyles<key, category extends keyof IThemeConfig, scale = null> = (
3347
scale extends keyof IThemeConfig[ category ]

0 commit comments

Comments
 (0)