@threlte/flex 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,8 +12,8 @@ export const contentGroup = new Group();
12
12
  group.userData.isNode = true;
13
13
  export const { node } = createNodeContext(order);
14
14
  addNode(node, group, $$restProps);
15
- updateNodeProps(node, { ...classParser?.(_class), ...$$restProps }, true);
16
- $: updateNodeProps(node, { ...classParser?.(_class), ...$$restProps });
15
+ updateNodeProps(node, { ...classParser?.(_class, {}), ...$$restProps }, true);
16
+ $: updateNodeProps(node, { ...classParser?.(_class, {}), ...$$restProps });
17
17
  onDestroy(() => {
18
18
  removeNode(node);
19
19
  });
@@ -120,7 +120,8 @@ const flexContext = createFlexContext({
120
120
  const { mainAxis, crossAxis, depthAxis } = flexContext;
121
121
  const { node: rootNode } = createNodeContext();
122
122
  $: rootNode.setWidth(width * scaleFactor), rootNode.setHeight(height * scaleFactor);
123
- $: applyNodeProps(rootNode, { ...classParser?.(_class), ...$$restProps }, scaleFactor), reflow();
123
+ $: applyNodeProps(rootNode, { ...classParser?.(_class, {}), ...$$restProps }, scaleFactor),
124
+ reflow();
124
125
  $: flexContext.rootWidth.set(width), flexContext.reflow('Updated root width');
125
126
  $: flexContext.rootHeight.set(height), flexContext.reflow('Updated root height');
126
127
  $: flexContext.mainAxis.set(plane[0]), flexContext.reflow('Updated main axis');
@@ -1,6 +1,6 @@
1
1
  import type { Align, FlexDirection, Justify, Node, Wrap } from 'yoga-layout';
2
2
  export type FlexPlane = 'xy' | 'yz' | 'xz';
3
- export type ClassParser = (className: string) => NodeProps;
3
+ export type ClassParser = (className: string, props: NodeProps) => NodeProps;
4
4
  export type Axis = 'x' | 'y' | 'z';
5
5
  /**
6
6
  * This map provides the prop setters as well as the types for the props. The
@@ -1,8 +1,8 @@
1
- import type { NodeProps } from '../lib/props';
1
+ import type { ClassParser } from '../lib/props';
2
2
  /**
3
3
  * This function is a type helper for creating a class parser. A class parser is
4
4
  * a function that takes a class name and returns a NodeProps object. This can
5
5
  * be used to create Tailwind-like class names that resolve to yoga-layout
6
6
  * props.
7
7
  */
8
- export declare const createClassParser: (callback: (className: string) => NodeProps) => (className: string) => NodeProps;
8
+ export declare const createClassParser: (callback: ClassParser) => ClassParser;
@@ -1,2 +1 @@
1
- import type { NodeProps } from '../lib/props';
2
- export declare const tailwindParser: (className: string) => NodeProps;
1
+ export declare const tailwindParser: import("../lib/props").ClassParser;
@@ -1,6 +1,5 @@
1
1
  import { createClassParser } from './createClassParser';
2
- export const tailwindParser = createClassParser((string) => {
3
- const styles = {};
2
+ export const tailwindParser = createClassParser((string, props) => {
4
3
  const classes = string.split(' ').map((className) => className.trim());
5
4
  const parseNumericOrAutoOrPercentageValue = (value) => {
6
5
  if (value === 'auto') {
@@ -37,141 +36,141 @@ export const tailwindParser = createClassParser((string) => {
37
36
  // padding
38
37
  if (className.startsWith('p-')) {
39
38
  const [, value] = className.split('-');
40
- styles.padding = parseNumericOrPercentageValue(value);
39
+ props.padding = parseNumericOrPercentageValue(value);
41
40
  }
42
41
  if (className.startsWith('px-')) {
43
42
  const [, value] = className.split('-');
44
- styles.paddingLeft = parseNumericOrPercentageValue(value);
45
- styles.paddingRight = parseNumericOrPercentageValue(value);
43
+ props.paddingLeft = parseNumericOrPercentageValue(value);
44
+ props.paddingRight = parseNumericOrPercentageValue(value);
46
45
  }
47
46
  if (className.startsWith('py-')) {
48
47
  const [, value] = className.split('-');
49
- styles.paddingTop = parseNumericOrPercentageValue(value);
50
- styles.paddingBottom = parseNumericOrPercentageValue(value);
48
+ props.paddingTop = parseNumericOrPercentageValue(value);
49
+ props.paddingBottom = parseNumericOrPercentageValue(value);
51
50
  }
52
51
  if (className.startsWith('pt-')) {
53
52
  const [, value] = className.split('-');
54
- styles.paddingTop = parseNumericOrPercentageValue(value);
53
+ props.paddingTop = parseNumericOrPercentageValue(value);
55
54
  }
56
55
  if (className.startsWith('pr-')) {
57
56
  const [, value] = className.split('-');
58
- styles.paddingRight = parseNumericOrPercentageValue(value);
57
+ props.paddingRight = parseNumericOrPercentageValue(value);
59
58
  }
60
59
  if (className.startsWith('pb-')) {
61
60
  const [, value] = className.split('-');
62
- styles.paddingBottom = parseNumericOrPercentageValue(value);
61
+ props.paddingBottom = parseNumericOrPercentageValue(value);
63
62
  }
64
63
  if (className.startsWith('pl-')) {
65
64
  const [, value] = className.split('-');
66
- styles.paddingLeft = parseNumericOrPercentageValue(value);
65
+ props.paddingLeft = parseNumericOrPercentageValue(value);
67
66
  }
68
67
  // margin
69
68
  if (className.startsWith('m-')) {
70
69
  const [, value] = className.split('-');
71
- styles.margin = parseNumericOrAutoOrPercentageValue(value);
70
+ props.margin = parseNumericOrAutoOrPercentageValue(value);
72
71
  }
73
72
  if (className.startsWith('mx-')) {
74
73
  const [, value] = className.split('-');
75
- styles.marginLeft = parseNumericOrAutoOrPercentageValue(value);
76
- styles.marginRight = parseNumericOrAutoOrPercentageValue(value);
74
+ props.marginLeft = parseNumericOrAutoOrPercentageValue(value);
75
+ props.marginRight = parseNumericOrAutoOrPercentageValue(value);
77
76
  }
78
77
  if (className.startsWith('my-')) {
79
78
  const [, value] = className.split('-');
80
- styles.marginTop = parseNumericOrAutoOrPercentageValue(value);
81
- styles.marginBottom = parseNumericOrAutoOrPercentageValue(value);
79
+ props.marginTop = parseNumericOrAutoOrPercentageValue(value);
80
+ props.marginBottom = parseNumericOrAutoOrPercentageValue(value);
82
81
  }
83
82
  if (className.startsWith('mt-')) {
84
83
  const [, value] = className.split('-');
85
- styles.marginTop = parseNumericOrAutoOrPercentageValue(value);
84
+ props.marginTop = parseNumericOrAutoOrPercentageValue(value);
86
85
  }
87
86
  if (className.startsWith('mr-')) {
88
87
  const [, value] = className.split('-');
89
- styles.marginRight = parseNumericOrAutoOrPercentageValue(value);
88
+ props.marginRight = parseNumericOrAutoOrPercentageValue(value);
90
89
  }
91
90
  if (className.startsWith('mb-')) {
92
91
  const [, value] = className.split('-');
93
- styles.marginBottom = parseNumericOrAutoOrPercentageValue(value);
92
+ props.marginBottom = parseNumericOrAutoOrPercentageValue(value);
94
93
  }
95
94
  if (className.startsWith('ml-')) {
96
95
  const [, value] = className.split('-');
97
- styles.marginLeft = parseNumericOrAutoOrPercentageValue(value);
96
+ props.marginLeft = parseNumericOrAutoOrPercentageValue(value);
98
97
  }
99
98
  // width
100
99
  if (className.startsWith('w-')) {
101
100
  const [, value] = className.split('-');
102
- styles.width = parseNumericOrAutoOrPercentageValue(value);
101
+ props.width = parseNumericOrAutoOrPercentageValue(value);
103
102
  }
104
103
  // height
105
104
  if (className.startsWith('h-')) {
106
105
  const [, value] = className.split('-');
107
- styles.height = parseNumericOrAutoOrPercentageValue(value);
106
+ props.height = parseNumericOrAutoOrPercentageValue(value);
108
107
  }
109
108
  // flex-basis
110
109
  if (className.startsWith('basis-')) {
111
110
  const [, value] = className.split('-');
112
- styles.flexBasis = parseNumericOrAutoOrPercentageValue(value);
111
+ props.flexBasis = parseNumericOrAutoOrPercentageValue(value);
113
112
  }
114
113
  // flex-grow
115
114
  if (className.startsWith('grow-')) {
116
115
  const [, value] = className.split('-');
117
- styles.flexGrow = Number(value);
116
+ props.flexGrow = Number(value);
118
117
  }
119
118
  // flex-shrink
120
119
  if (className.startsWith('shrink-')) {
121
120
  const [, value] = className.split('-');
122
- styles.flexShrink = Number(value);
121
+ props.flexShrink = Number(value);
123
122
  }
124
123
  // flex-direction
125
124
  if (className.startsWith('flex-')) {
126
125
  // first the flex direction
127
126
  switch (className) {
128
127
  case 'flex-row':
129
- styles.flexDirection = 'Row';
128
+ props.flexDirection = 'Row';
130
129
  break;
131
130
  case 'flex-row-reverse':
132
- styles.flexDirection = 'RowReverse';
131
+ props.flexDirection = 'RowReverse';
133
132
  break;
134
133
  case 'flex-col':
135
- styles.flexDirection = 'Column';
134
+ props.flexDirection = 'Column';
136
135
  break;
137
136
  case 'flex-col-reverse':
138
- styles.flexDirection = 'ColumnReverse';
137
+ props.flexDirection = 'ColumnReverse';
139
138
  break;
140
139
  case 'flex-wrap':
141
- styles.flexWrap = 'Wrap';
140
+ props.flexWrap = 'Wrap';
142
141
  break;
143
142
  case 'flex-wrap-reverse':
144
- styles.flexWrap = 'WrapReverse';
143
+ props.flexWrap = 'WrapReverse';
145
144
  break;
146
145
  case 'flex-nowrap':
147
- styles.flexWrap = 'NoWrap';
146
+ props.flexWrap = 'NoWrap';
148
147
  break;
149
148
  default:
150
149
  // flex shorthand
151
150
  const [, value] = className.split('-');
152
- styles.flex = Number(value);
151
+ props.flex = Number(value);
153
152
  }
154
153
  }
155
154
  // justify-content
156
155
  if (className.startsWith('justify-')) {
157
156
  switch (className) {
158
157
  case 'justify-start':
159
- styles.justifyContent = 'FlexStart';
158
+ props.justifyContent = 'FlexStart';
160
159
  break;
161
160
  case 'justify-end':
162
- styles.justifyContent = 'FlexEnd';
161
+ props.justifyContent = 'FlexEnd';
163
162
  break;
164
163
  case 'justify-center':
165
- styles.justifyContent = 'Center';
164
+ props.justifyContent = 'Center';
166
165
  break;
167
166
  case 'justify-between':
168
- styles.justifyContent = 'SpaceBetween';
167
+ props.justifyContent = 'SpaceBetween';
169
168
  break;
170
169
  case 'justify-around':
171
- styles.justifyContent = 'SpaceAround';
170
+ props.justifyContent = 'SpaceAround';
172
171
  break;
173
172
  case 'justify-evenly':
174
- styles.justifyContent = 'SpaceEvenly';
173
+ props.justifyContent = 'SpaceEvenly';
175
174
  break;
176
175
  }
177
176
  }
@@ -179,19 +178,19 @@ export const tailwindParser = createClassParser((string) => {
179
178
  if (className.startsWith('items-')) {
180
179
  switch (className) {
181
180
  case 'items-start':
182
- styles.alignItems = 'FlexStart';
181
+ props.alignItems = 'FlexStart';
183
182
  break;
184
183
  case 'items-end':
185
- styles.alignItems = 'FlexEnd';
184
+ props.alignItems = 'FlexEnd';
186
185
  break;
187
186
  case 'items-center':
188
- styles.alignItems = 'Center';
187
+ props.alignItems = 'Center';
189
188
  break;
190
189
  case 'items-baseline':
191
- styles.alignItems = 'Baseline';
190
+ props.alignItems = 'Baseline';
192
191
  break;
193
192
  case 'items-stretch':
194
- styles.alignItems = 'Stretch';
193
+ props.alignItems = 'Stretch';
195
194
  break;
196
195
  }
197
196
  }
@@ -199,28 +198,28 @@ export const tailwindParser = createClassParser((string) => {
199
198
  if (className.startsWith('content-')) {
200
199
  switch (className) {
201
200
  case 'content-normal':
202
- styles.alignContent = 'Auto';
201
+ props.alignContent = 'Auto';
203
202
  break;
204
203
  case 'content-start':
205
- styles.alignContent = 'FlexStart';
204
+ props.alignContent = 'FlexStart';
206
205
  break;
207
206
  case 'content-end':
208
- styles.alignContent = 'FlexEnd';
207
+ props.alignContent = 'FlexEnd';
209
208
  break;
210
209
  case 'content-center':
211
- styles.alignContent = 'Center';
210
+ props.alignContent = 'Center';
212
211
  break;
213
212
  case 'content-between':
214
- styles.alignContent = 'SpaceBetween';
213
+ props.alignContent = 'SpaceBetween';
215
214
  break;
216
215
  case 'content-around':
217
- styles.alignContent = 'SpaceAround';
216
+ props.alignContent = 'SpaceAround';
218
217
  break;
219
218
  case 'content-stretch':
220
- styles.alignContent = 'Stretch';
219
+ props.alignContent = 'Stretch';
221
220
  break;
222
221
  case 'content-baseline':
223
- styles.alignContent = 'Baseline';
222
+ props.alignContent = 'Baseline';
224
223
  break;
225
224
  }
226
225
  }
@@ -228,73 +227,73 @@ export const tailwindParser = createClassParser((string) => {
228
227
  if (className.startsWith('self-')) {
229
228
  switch (className) {
230
229
  case 'self-auto':
231
- styles.alignSelf = 'Auto';
230
+ props.alignSelf = 'Auto';
232
231
  break;
233
232
  case 'self-start':
234
- styles.alignSelf = 'FlexStart';
233
+ props.alignSelf = 'FlexStart';
235
234
  break;
236
235
  case 'self-end':
237
- styles.alignSelf = 'FlexEnd';
236
+ props.alignSelf = 'FlexEnd';
238
237
  break;
239
238
  case 'self-center':
240
- styles.alignSelf = 'Center';
239
+ props.alignSelf = 'Center';
241
240
  break;
242
241
  case 'self-stretch':
243
- styles.alignSelf = 'Stretch';
242
+ props.alignSelf = 'Stretch';
244
243
  break;
245
244
  case 'self-baseline':
246
- styles.alignSelf = 'Baseline';
245
+ props.alignSelf = 'Baseline';
247
246
  break;
248
247
  }
249
248
  }
250
249
  // Gaps
251
250
  if (className.startsWith('gap-x-')) {
252
251
  const [, value] = className.split('-');
253
- styles.gapColumn = Number(value);
252
+ props.gapColumn = Number(value);
254
253
  }
255
254
  else if (className.startsWith('gap-y-')) {
256
255
  const [, value] = className.split('-');
257
- styles.gapRow = Number(value);
256
+ props.gapRow = Number(value);
258
257
  }
259
258
  else if (className.startsWith('gap-')) {
260
259
  const [, value] = className.split('-');
261
- styles.gap = Number(value);
260
+ props.gap = Number(value);
262
261
  }
263
262
  // Position
264
263
  if (className.startsWith('top-')) {
265
264
  const [, value] = className.split('-');
266
- styles.top = parseNumericOrPercentageValue(value);
265
+ props.top = parseNumericOrPercentageValue(value);
267
266
  }
268
267
  if (className.startsWith('right-')) {
269
268
  const [, value] = className.split('-');
270
- styles.right = parseNumericOrPercentageValue(value);
269
+ props.right = parseNumericOrPercentageValue(value);
271
270
  }
272
271
  if (className.startsWith('bottom-')) {
273
272
  const [, value] = className.split('-');
274
- styles.bottom = parseNumericOrPercentageValue(value);
273
+ props.bottom = parseNumericOrPercentageValue(value);
275
274
  }
276
275
  if (className.startsWith('left-')) {
277
276
  const [, value] = className.split('-');
278
- styles.left = parseNumericOrPercentageValue(value);
277
+ props.left = parseNumericOrPercentageValue(value);
279
278
  }
280
279
  // aspect ratio
281
280
  if (className.startsWith('aspect-')) {
282
281
  switch (className) {
283
282
  case 'aspect-square':
284
- styles.aspectRatio = 1;
283
+ props.aspectRatio = 1;
285
284
  break;
286
285
  case 'aspect-landscape':
287
- styles.aspectRatio = 16 / 9;
286
+ props.aspectRatio = 16 / 9;
288
287
  break;
289
288
  case 'aspect-portrait':
290
- styles.aspectRatio = 9 / 16;
289
+ props.aspectRatio = 9 / 16;
291
290
  break;
292
291
  default:
293
292
  const [, value] = className.split('-');
294
293
  const [width, height] = value.split('/');
295
- styles.aspectRatio = Number(width) / Number(height);
294
+ props.aspectRatio = Number(width) / Number(height);
296
295
  }
297
296
  }
298
297
  });
299
- return styles;
298
+ return props;
300
299
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@threlte/flex",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "author": "Grischa Erbe <hello@legrisch.com> (https://legrisch.com)",
5
5
  "license": "MIT",
6
6
  "devDependencies": {