Skip to content

Commit fe2c3c3

Browse files
committed
chore: revamp layout props (minify syntax)
1 parent 05b28fe commit fe2c3c3

File tree

6 files changed

+104
-110
lines changed

6 files changed

+104
-110
lines changed

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import p from './spacing/padding';
33
import m from './spacing/margin';
44

55
// Layout
6-
import aspect_ratio from './layout/aspect-ratio';
6+
import aspect from './layout/aspect';
77
import object_fit from './layout/object-fit';
88
import display from './layout/display';
99
import direction from './layout/direction';
@@ -50,7 +50,7 @@ export {
5050
object_fit,
5151
display,
5252
direction,
53-
aspect_ratio,
53+
aspect,
5454
z,
5555
pos,
5656
overflow,

src/layout/aspect-ratio.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/layout/aspect.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import type { AspectRatio } from '../types/layout';
2+
3+
const aspectRatioValues: { [key: string]: number | string } = {
4+
auto: 'auto',
5+
square: 1,
6+
video: 16 / 9,
7+
};
8+
9+
const getAspectRatioValue = (key: string | number): number | string => {
10+
if (typeof key === 'number') return key;
11+
if (key === 'auto') return 'auto';
12+
if (!isNaN(Number(key))) return Number(key);
13+
14+
// Handle fractional strings like '1/3'
15+
const fractionMatch = key.match(/^(\d+)\/(\d+)$/);
16+
if (fractionMatch) {
17+
const numerator = Number(fractionMatch[1]);
18+
const denominator = Number(fractionMatch[2]);
19+
return numerator / denominator;
20+
}
21+
22+
const value = aspectRatioValues[key];
23+
if (value === undefined) throw new Error(`Invalid aspect ratio key: ${key}`);
24+
return value;
25+
};
26+
27+
const aspect = {
28+
custom_: (key: string | number): AspectRatio => {
29+
return { aspectRatio: getAspectRatioValue(key) };
30+
},
31+
} as { [key: string]: AspectRatio } & {
32+
custom_: (key: string | number) => AspectRatio;
33+
};
34+
35+
Object.keys(aspectRatioValues).forEach((key) => {
36+
aspect[key] = { aspectRatio: getAspectRatioValue(key) };
37+
});
38+
39+
// Example usage
40+
// const autoAspectRatio = aspect.auto; // { aspectRatio: 'auto' }
41+
// const squareAspectRatio = aspect.square; // { aspectRatio: 1 }
42+
// const videoAspectRatio = aspect.video; // { aspectRatio: 16 / 9 }
43+
// const customAspectRatio = aspect.custom_('4/3'); // { aspectRatio: 4 / 3 }
44+
// const customAspectRatioNumber = aspect.custom_(3); // { aspectRatio: 3 }
45+
// const customAspectRatioString = aspect.custom_('3'); // { aspectRatio: 3 }
46+
47+
export default aspect;

src/layout/display.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
11
import type { Display } from '../types/layout';
22

3-
const displayValues: { [key: string]: Display['display'] } = {
4-
hidden: 'none',
5-
flex: 'flex',
3+
const display: { [key: string]: Display } = {
4+
hidden: { display: 'none' },
5+
flex: { display: 'flex' },
66
};
77

8-
const getDisplayValue = (key: string): Display => {
9-
const value = displayValues[key];
10-
if (value === undefined) throw new Error(`Invalid display key: ${key}`);
11-
return { display: value };
12-
};
13-
14-
const display: { [key: string]: Display } = {};
15-
16-
Object.keys(displayValues).forEach((key) => {
17-
display[key] = getDisplayValue(key);
18-
});
19-
208
// Example usage
219
// const hiddenDisplay = display.hidden; // { display: 'none' }
2210
// const flexDisplay = display.flex; // { display: 'flex' }

src/layout/position.ts

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import type { PositionValue } from '../types/layout';
22

3-
const positions = {
3+
const positions: { [key: string]: number } = {
44
'0': 0,
55
'px': 1,
6-
'0.5': 2,
76
'1': 4,
8-
'1.5': 6,
97
'2': 8,
10-
'2.5': 10,
118
'3': 12,
12-
'3.5': 14,
139
'4': 16,
1410
'5': 20,
1511
'6': 24,
@@ -36,25 +32,6 @@ const positions = {
3632
'72': 288,
3733
'80': 320,
3834
'96': 384,
39-
'auto': 'auto',
40-
'1/2': '50%',
41-
'1/3': '33.333333%',
42-
'2/3': '66.666667%',
43-
'1/4': '25%',
44-
'2/4': '50%',
45-
'3/4': '75%',
46-
'full': '100%',
47-
};
48-
49-
// Magik
50-
const generatePositionProperties = (
51-
key: string
52-
): { [key: string]: PositionValue } => {
53-
const values: { [key: string]: PositionValue } = {};
54-
for (const [posKey, posValue] of Object.entries(positions)) {
55-
values[`${key}_${posKey.replace('/', '_')}`] = { [key]: posValue };
56-
}
57-
return values;
5835
};
5936

6037
// Now explicitly include the custom methods and the index signature for dynamically generated properties
@@ -63,10 +40,6 @@ const pos: {
6340
absolute: { position: string };
6441
fixed: { position: string };
6542
sticky: { position: string };
66-
r_custom: (value: number) => PositionValue;
67-
t_custom: (value: number) => PositionValue;
68-
l_custom: (value: number) => PositionValue;
69-
b_custom: (value: number) => PositionValue;
7043
[key: string]:
7144
| PositionValue
7245
| { position: string }
@@ -78,30 +51,43 @@ const pos: {
7851
fixed: { position: 'fixed' },
7952
sticky: { position: 'sticky' },
8053

81-
...generatePositionProperties('right'),
82-
...generatePositionProperties('top'),
83-
...generatePositionProperties('left'),
84-
...generatePositionProperties('bottom'),
85-
86-
r_custom(value: number): PositionValue {
87-
return { right: value };
54+
r_(value: number | string): PositionValue {
55+
return { right: Number(value) };
8856
},
89-
t_custom(value: number): PositionValue {
90-
return { top: value };
57+
t_(value: number): PositionValue {
58+
return { top: Number(value) };
9159
},
92-
l_custom(value: number): PositionValue {
93-
return { left: value };
60+
l_(value: number): PositionValue {
61+
return { left: Number(value) };
9462
},
95-
b_custom(value: number): PositionValue {
96-
return { bottom: value };
63+
b_(value: number): PositionValue {
64+
return { bottom: Number(value) };
9765
},
9866
};
9967

68+
Object.keys(positions).forEach((posKey) => {
69+
pos[`t_${posKey}`] = {
70+
top: positions[posKey],
71+
} as PositionValue;
72+
73+
pos[`r_${posKey}`] = {
74+
right: positions[posKey],
75+
} as PositionValue;
76+
77+
pos[`b_${posKey}`] = {
78+
bottom: positions[posKey],
79+
} as PositionValue;
80+
81+
pos[`l_${posKey}`] = {
82+
left: positions[posKey],
83+
} as PositionValue;
84+
});
85+
10086
// Example usage
10187
// console.log(pos.relative); // { position: 'relative' }
10288
// console.log(pos.right_1); // { right: 4 }
10389
// console.log(pos.top_2); // { top: 8 }
104-
// console.log(pos.left_1_2); // { left: '50%' }
105-
// console.log(pos.b_custom(20)); // { bottom: 20 }
90+
// console.log(pos.left_1); // { left: '50%' }
91+
// console.log(pos.b_(20)); // { bottom: 20 }
10692

10793
export default pos;

src/layout/z-index.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1-
const zIndex = {
2-
z_0: { zIndex: 0 },
3-
z_10: { zIndex: 10 },
4-
z_20: { zIndex: 20 },
5-
z_30: { zIndex: 30 },
6-
z_40: { zIndex: 40 },
7-
z_50: { zIndex: 50 },
8-
z_auto: { zIndex: 'auto' },
1+
const z = {
2+
index_0: { zIndex: 0 },
3+
index_10: { zIndex: 10 },
4+
index_20: { zIndex: 20 },
5+
index_30: { zIndex: 30 },
6+
index_40: { zIndex: 40 },
7+
index_50: { zIndex: 50 },
8+
index_auto: { zIndex: 'auto' },
9+
10+
index_(value: number | string): { zIndex: number | 'auto' } {
11+
if (value === 'auto') {
12+
return { zIndex: 'auto' };
13+
} else if (!isNaN(Number(value))) {
14+
return { zIndex: Number(value) };
15+
} else {
16+
throw new Error('Invalid zIndex value');
17+
}
18+
},
919
};
1020

1121
// Example usage
12-
// console.log(zIndex.z_0); // { zIndex: 0 }
13-
// console.log(zIndex.z_10); // { zIndex: 10 }
14-
// console.log(zIndex.z_20); // { zIndex: 20 }
15-
// console.log(zIndex.z_30); // { zIndex: 30 }
16-
// console.log(zIndex.z_40); // { zIndex: 40 }
17-
// console.log(zIndex.z_50); // { zIndex: 50 }
18-
// console.log(zIndex.z_auto); // { zIndex: 'auto' }
22+
// console.log(z.index_(10)); // { zIndex: 10 }
23+
// console.log(z.index_('auto')); // { zIndex: 'auto' }
24+
// console.log(z.index_('20')); // { zIndex: 20 }
25+
// console.log(z.index_('invalid')); // Error: Invalid zIndex value
1926

20-
export default zIndex;
27+
export default z;

0 commit comments

Comments
 (0)