11import { space , type SpaceValue } from "ui/settings" ;
22import fluidify from "./ofMixins/fluidify" ;
33
4- export const setSpace = ( args : string , force ?: "force" ) => {
5- const prop : keyof typeof properties = args . substring ( 0 , 1 ) ;
6- const pos : SpaceValue = args . substring ( 1 , 2 ) ;
7- const size : SpaceValue = args . substring ( 2 , 3 ) ;
8- const properties = {
9- b : "border-width" ,
10- m : "margin" ,
11- p : "padding" ,
12- } ;
13- const positions : Record < SpaceValue , string > = {
14- t : "top" ,
15- b : "bottom" ,
16- l : "left" ,
17- r : "right" ,
18- } ;
4+ type PropertyValue = keyof typeof properties ;
5+ type PositionValue = "a" | "h" | "v" | keyof typeof positions ;
6+ type SpaceShorthand = `${PropertyValue } ${PositionValue } ${SpaceValue } `;
7+
8+ const properties = {
9+ b : "border-width" ,
10+ m : "margin" ,
11+ p : "padding" ,
12+ } ;
13+ const positions = {
14+ t : "top" ,
15+ b : "bottom" ,
16+ l : "left" ,
17+ r : "right" ,
18+ } ;
19+
20+ export const setSpace = ( args : SpaceShorthand , force ?: "force" ) => {
21+ const prop : PropertyValue = args . substring ( 0 , 1 ) as PropertyValue ;
22+ const pos : PositionValue = args . substring ( 1 , 2 ) as PositionValue ;
23+ const size : SpaceValue = args . substring ( 2 , 3 ) as SpaceValue ;
1924 const isImportant = force === "force" ;
2025
2126 switch ( pos ) {
27+ // a = all sides
2228 case "a" :
2329 return fluidify (
2430 `${ properties [ prop ] } ` ,
2531 space [ size ] [ 0 ] ,
2632 space [ size ] [ 1 ] ,
2733 isImportant
2834 ) ;
29- case "k" :
30- return fluidify (
31- [ `${ properties [ prop ] } -left` , `${ properties [ prop ] } -right` ] ,
32- space [ size ] [ 0 ] ,
33- space [ size ] [ 1 ] ,
34- isImportant
35- ) ;
35+ // h = horizontal (left & right)
3636 case "h" :
3737 return fluidify (
3838 [ `${ properties [ prop ] } -left` , `${ properties [ prop ] } -right` ] ,
3939 space [ size ] [ 0 ] ,
4040 space [ size ] [ 1 ] ,
4141 isImportant
4242 ) ;
43+ // v = vertical (top & bottom)
4344 case "v" :
4445 return fluidify (
4546 [ `${ properties [ prop ] } -top` , `${ properties [ prop ] } -bottom` ] ,
4647 space [ size ] [ 0 ] ,
4748 space [ size ] [ 1 ] ,
4849 isImportant
4950 ) ;
51+ // default: single side (top / bottom / left / right)
5052 default :
5153 return fluidify (
5254 `${ properties [ prop ] } -${ positions [ pos ] } ` ,
@@ -57,4 +59,5 @@ export const setSpace = (args: string, force?: "force") => {
5759 }
5860} ;
5961
62+ // Re-export for use with typing setSpace()
6063export { SpaceValue } ;
0 commit comments