forked from vadimdemedes/ink
/
styles.ts
369 lines (305 loc) Β· 9.38 KB
/
styles.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* eslint-disable @typescript-eslint/prefer-nullish-coalescing */
import {type Boxes} from 'cli-boxes';
import {type LiteralUnion} from 'type-fest';
import {type ForegroundColorName} from 'chalk';
// eslint-disable-next-line n/file-extension-in-import
import Yoga, {type Node as YogaNode} from 'yoga-wasm-web/auto';
export type Styles = {
readonly textWrap?:
| 'wrap'
| 'end'
| 'middle'
| 'truncate-end'
| 'truncate'
| 'truncate-middle'
| 'truncate-start';
readonly position?: 'absolute' | 'relative';
/**
* Top margin.
*/
readonly marginTop?: number;
/**
* Bottom margin.
*/
readonly marginBottom?: number;
/**
* Left margin.
*/
readonly marginLeft?: number;
/**
* Right margin.
*/
readonly marginRight?: number;
/**
* Top padding.
*/
readonly paddingTop?: number;
/**
* Bottom padding.
*/
readonly paddingBottom?: number;
/**
* Left padding.
*/
readonly paddingLeft?: number;
/**
* Right padding.
*/
readonly paddingRight?: number;
/**
* This property defines the ability for a flex item to grow if necessary.
* See [flex-grow](https://css-tricks.com/almanac/properties/f/flex-grow/).
*/
readonly flexGrow?: number;
/**
* It specifies the βflex shrink factorβ, which determines how much the flex item will shrink relative to the rest of the flex items in the flex container when there isnβt enough space on the row.
* See [flex-shrink](https://css-tricks.com/almanac/properties/f/flex-shrink/).
*/
readonly flexShrink?: number;
/**
* It establishes the main-axis, thus defining the direction flex items are placed in the flex container.
* See [flex-direction](https://css-tricks.com/almanac/properties/f/flex-direction/).
*/
readonly flexDirection?: 'row' | 'column' | 'row-reverse' | 'column-reverse';
/**
* It specifies the initial size of the flex item, before any available space is distributed according to the flex factors.
* See [flex-basis](https://css-tricks.com/almanac/properties/f/flex-basis/).
*/
readonly flexBasis?: number | string;
/**
* The align-items property defines the default behavior for how items are laid out along the cross axis (perpendicular to the main axis).
* See [align-items](https://css-tricks.com/almanac/properties/a/align-items/).
*/
readonly alignItems?: 'flex-start' | 'center' | 'flex-end' | 'stretch';
/**
* It makes possible to override the align-items value for specific flex items.
* See [align-self](https://css-tricks.com/almanac/properties/a/align-self/).
*/
readonly alignSelf?: 'flex-start' | 'center' | 'flex-end' | 'auto';
/**
* It defines the alignment along the main axis.
* See [justify-content](https://css-tricks.com/almanac/properties/j/justify-content/).
*/
readonly justifyContent?:
| 'flex-start'
| 'flex-end'
| 'space-between'
| 'space-around'
| 'center';
/**
* Width of the element in spaces.
* You can also set it in percent, which will calculate the width based on the width of parent element.
*/
readonly width?: number | string;
/**
* Height of the element in lines (rows).
* You can also set it in percent, which will calculate the height based on the height of parent element.
*/
readonly height?: number | string;
/**
* Sets a minimum width of the element.
*/
readonly minWidth?: number | string;
/**
* Sets a minimum height of the element.
*/
readonly minHeight?: number | string;
/**
* Set this property to `none` to hide the element.
*/
readonly display?: 'flex' | 'none';
/**
* Add a border with a specified style.
* If `borderStyle` is `undefined` (which it is by default), no border will be added.
*/
readonly borderStyle?: keyof Boxes;
/**
* Change border color.
* Accepts the same values as `color` in <Text> component.
*/
readonly borderColor?: LiteralUnion<ForegroundColorName, string>;
/**
* Behavior for an element's overflow in horizontal direction.
*/
readonly overflowX?: 'visible' | 'hidden';
/**
* Behavior for an element's overflow in vertical direction.
*/
readonly overflowY?: 'visible' | 'hidden';
};
const applyPositionStyles = (node: YogaNode, style: Styles): void => {
if ('position' in style) {
node.setPositionType(
style.position === 'absolute'
? Yoga.POSITION_TYPE_ABSOLUTE
: Yoga.POSITION_TYPE_RELATIVE
);
}
};
const applyMarginStyles = (node: YogaNode, style: Styles): void => {
if ('marginLeft' in style) {
node.setMargin(Yoga.EDGE_START, style.marginLeft || 0);
}
if ('marginRight' in style) {
node.setMargin(Yoga.EDGE_END, style.marginRight || 0);
}
if ('marginTop' in style) {
node.setMargin(Yoga.EDGE_TOP, style.marginTop || 0);
}
if ('marginBottom' in style) {
node.setMargin(Yoga.EDGE_BOTTOM, style.marginBottom || 0);
}
};
const applyPaddingStyles = (node: YogaNode, style: Styles): void => {
if ('paddingLeft' in style) {
node.setPadding(Yoga.EDGE_LEFT, style.paddingLeft || 0);
}
if ('paddingRight' in style) {
node.setPadding(Yoga.EDGE_RIGHT, style.paddingRight || 0);
}
if ('paddingTop' in style) {
node.setPadding(Yoga.EDGE_TOP, style.paddingTop || 0);
}
if ('paddingBottom' in style) {
node.setPadding(Yoga.EDGE_BOTTOM, style.paddingBottom || 0);
}
};
const applyFlexStyles = (node: YogaNode, style: Styles): void => {
if ('flexGrow' in style) {
node.setFlexGrow(style.flexGrow ?? 0);
}
if ('flexShrink' in style) {
node.setFlexShrink(
typeof style.flexShrink === 'number' ? style.flexShrink : 1
);
}
if ('flexDirection' in style) {
if (style.flexDirection === 'row') {
node.setFlexDirection(Yoga.FLEX_DIRECTION_ROW);
}
if (style.flexDirection === 'row-reverse') {
node.setFlexDirection(Yoga.FLEX_DIRECTION_ROW_REVERSE);
}
if (style.flexDirection === 'column') {
node.setFlexDirection(Yoga.FLEX_DIRECTION_COLUMN);
}
if (style.flexDirection === 'column-reverse') {
node.setFlexDirection(Yoga.FLEX_DIRECTION_COLUMN_REVERSE);
}
}
if ('flexBasis' in style) {
if (typeof style.flexBasis === 'number') {
node.setFlexBasis(style.flexBasis);
} else if (typeof style.flexBasis === 'string') {
node.setFlexBasisPercent(Number.parseInt(style.flexBasis, 10));
} else {
// This should be replaced with node.setFlexBasisAuto() when new Yoga release is out
node.setFlexBasis(Number.NaN);
}
}
if ('alignItems' in style) {
if (style.alignItems === 'stretch' || !style.alignItems) {
node.setAlignItems(Yoga.ALIGN_STRETCH);
}
if (style.alignItems === 'flex-start') {
node.setAlignItems(Yoga.ALIGN_FLEX_START);
}
if (style.alignItems === 'center') {
node.setAlignItems(Yoga.ALIGN_CENTER);
}
if (style.alignItems === 'flex-end') {
node.setAlignItems(Yoga.ALIGN_FLEX_END);
}
}
if ('alignSelf' in style) {
if (style.alignSelf === 'auto' || !style.alignSelf) {
node.setAlignSelf(Yoga.ALIGN_AUTO);
}
if (style.alignSelf === 'flex-start') {
node.setAlignSelf(Yoga.ALIGN_FLEX_START);
}
if (style.alignSelf === 'center') {
node.setAlignSelf(Yoga.ALIGN_CENTER);
}
if (style.alignSelf === 'flex-end') {
node.setAlignSelf(Yoga.ALIGN_FLEX_END);
}
}
if ('justifyContent' in style) {
if (style.justifyContent === 'flex-start' || !style.justifyContent) {
node.setJustifyContent(Yoga.JUSTIFY_FLEX_START);
}
if (style.justifyContent === 'center') {
node.setJustifyContent(Yoga.JUSTIFY_CENTER);
}
if (style.justifyContent === 'flex-end') {
node.setJustifyContent(Yoga.JUSTIFY_FLEX_END);
}
if (style.justifyContent === 'space-between') {
node.setJustifyContent(Yoga.JUSTIFY_SPACE_BETWEEN);
}
if (style.justifyContent === 'space-around') {
node.setJustifyContent(Yoga.JUSTIFY_SPACE_AROUND);
}
}
};
const applyDimensionStyles = (node: YogaNode, style: Styles): void => {
if ('width' in style) {
if (typeof style.width === 'number') {
node.setWidth(style.width);
} else if (typeof style.width === 'string') {
node.setWidthPercent(Number.parseInt(style.width, 10));
} else {
node.setWidthAuto();
}
}
if ('height' in style) {
if (typeof style.height === 'number') {
node.setHeight(style.height);
} else if (typeof style.height === 'string') {
node.setHeightPercent(Number.parseInt(style.height, 10));
} else {
node.setHeightAuto();
}
}
if ('minWidth' in style) {
if (typeof style.minWidth === 'string') {
node.setMinWidthPercent(Number.parseInt(style.minWidth, 10));
} else {
node.setMinWidth(style.minWidth ?? 0);
}
}
if ('minHeight' in style) {
if (typeof style.minHeight === 'string') {
node.setMinHeightPercent(Number.parseInt(style.minHeight, 10));
} else {
node.setMinHeight(style.minHeight ?? 0);
}
}
};
const applyDisplayStyles = (node: YogaNode, style: Styles): void => {
if ('display' in style) {
node.setDisplay(
style.display === 'flex' ? Yoga.DISPLAY_FLEX : Yoga.DISPLAY_NONE
);
}
};
const applyBorderStyles = (node: YogaNode, style: Styles): void => {
if ('borderStyle' in style) {
const borderWidth = typeof style.borderStyle === 'string' ? 1 : 0;
node.setBorder(Yoga.EDGE_TOP, borderWidth);
node.setBorder(Yoga.EDGE_BOTTOM, borderWidth);
node.setBorder(Yoga.EDGE_LEFT, borderWidth);
node.setBorder(Yoga.EDGE_RIGHT, borderWidth);
}
};
const styles = (node: YogaNode, style: Styles = {}): void => {
applyPositionStyles(node, style);
applyMarginStyles(node, style);
applyPaddingStyles(node, style);
applyFlexStyles(node, style);
applyDimensionStyles(node, style);
applyDisplayStyles(node, style);
applyBorderStyles(node, style);
};
export default styles;