Skip to content

Commit 5f41834

Browse files
Merge pull request #121 from gridaco/support-flex
Fix flex layout building from figma autolayout with proper layoutGrow value handling
2 parents 39dac80 + 038cac9 commit 5f41834

File tree

10 files changed

+57
-24
lines changed

10 files changed

+57
-24
lines changed

editor/scaffolds/code/index.tsx

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,35 @@ export function CodeSegment() {
5656
const targetted =
5757
designq.find_node_by_id_under_entry(targetId, root?.entry) ?? root?.entry;
5858

59-
const targetStateRef = useRef<ReflectSceneNode>();
60-
targetStateRef.current = targetted;
59+
const targetStateRef =
60+
useRef<{
61+
node: ReflectSceneNode;
62+
config: config.FrameworkConfig;
63+
}>();
64+
targetStateRef.current = { node: targetted, config: framework_config };
6165

6266
const on_result = (result: Result) => {
67+
if (
68+
result.framework.framework !==
69+
targetStateRef?.current?.config.framework ||
70+
result.id !== targetStateRef?.current?.node.id
71+
) {
72+
return;
73+
}
74+
6375
if (framework_config.language == "dart") {
6476
// special formatter support for dartlang
6577
result.code.raw = utils_dart.format(result.code.raw);
6678
result.scaffold.raw = utils_dart.format(result.scaffold.raw);
6779
}
6880

69-
if (result.id == targetStateRef?.current?.id) {
70-
setResult(result);
71-
}
81+
setResult(result);
7282
};
7383

7484
useEffect(() => {
7585
const __target = targetted;
76-
if (__target && framework_config) {
86+
const __framework_config = framework_config;
87+
if (__target && __framework_config) {
7788
const _input = {
7889
id: __target.id,
7990
name: __target.name,
@@ -88,7 +99,7 @@ export function CodeSegment() {
8899
// build code without assets fetch
89100
designToCode({
90101
input: _input,
91-
framework: framework_config,
102+
framework: __framework_config,
92103
asset_config: { skip_asset_replacement: true },
93104
build_config: build_config,
94105
})
@@ -99,7 +110,7 @@ export function CodeSegment() {
99110
if (!MainImageRepository.instance.empty) {
100111
designToCode({
101112
input: root,
102-
framework: framework_config,
113+
framework: __framework_config,
103114
asset_config: { asset_repository: MainImageRepository.instance },
104115
build_config: build_config,
105116
})

externals/design-sdk

packages/builder-css-styles/border-radius/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,16 @@ export function borderRadius(r: BorderRadiusManifest): CSSProperties {
5454
if (!r) {
5555
return;
5656
}
57-
5857
if (r.all) {
5958
if (isCircularRadius(r.all)) {
6059
return {
6160
"border-radius": px(r.all),
6261
};
6362
} else {
6463
console.warn("elliptical border radius not supported");
64+
return {
65+
"border-radius": px(r.all.x),
66+
};
6567
}
6668
} else {
6769
return {

packages/builder-css-styles/tricks/trick-flex-sizing/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export function flexsizing({
3030
case MainAxisSize.max: {
3131
return {
3232
"align-self": "stretch",
33-
flex: 1, // This is a temporary solution, since stretch can be used on non-space-between parent, but still the item should stretch, we use flex 1 to do this.
3433
};
3534
}
3635
case MainAxisSize.min: {

packages/builder-react-native/rn-styles/border.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ export function border(p: Border, rad: BorderRadius): ViewStyle {
3030
};
3131

3232
// if colors are all same
33-
if (equals(p.top?.color, p.right?.color, p.bottom?.color, p.left?.color)) {
33+
if (
34+
p.top?.color && // not undefined
35+
equals(p.top?.color, p.right?.color, p.bottom?.color, p.left?.color)
36+
) {
3437
o = {
3538
...o,
3639
borderColor: css.color(p.top.color),

packages/builder-web-core/widgets-native/flex/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export class Flex extends MultiChildWidget implements CssMinHeightMixin {
9595
this.mainAxisSize = p.mainAxisSize;
9696
this.crossAxisAlignment = p.crossAxisAlignment;
9797
this.verticalDirection = p.verticalDirection;
98+
this.flexWrap = p.flexWrap; // cssonly
9899
//
99100

100101
//
@@ -107,7 +108,6 @@ export class Flex extends MultiChildWidget implements CssMinHeightMixin {
107108

108109
// css only
109110
this.overflow = p.overflow;
110-
this.flexWrap = p.flexWrap;
111111
}
112112

113113
jsxConfig(): StylableJSXElementConfig {
@@ -120,10 +120,10 @@ export class Flex extends MultiChildWidget implements CssMinHeightMixin {
120120
styleData(): CSSProperties {
121121
return {
122122
display: "flex",
123+
overflow: this.overflow,
123124
...css.justifyContent(this.mainAxisAlignment),
124125
"flex-direction": direction(this.direction),
125126
"align-items": this.crossAxisAlignment,
126-
overflow: this.overflow,
127127
flex: this.flex,
128128
"flex-wrap": this.flexWrap,
129129
gap: this.itemSpacing && css.px(this.itemSpacing),

packages/designto-code/universal/design-to-code.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
default_tokenizer_config,
1616
TokenizerConfig,
1717
} from "@designto/token/config";
18-
import { default_build_configuration } from "@designto/config";
18+
import { default_build_configuration, FrameworkConfig } from "@designto/config";
1919
import { reusable } from "@code-features/component";
2020
import assert from "assert";
2121

@@ -28,7 +28,9 @@ interface AssetsConfig {
2828
custom_asset_replacement?: { type: "static"; resource: string };
2929
}
3030

31-
export type Result = output.ICodeOutput & { widget: Widget };
31+
export type Result = output.ICodeOutput & { widget: Widget } & {
32+
framework: FrameworkConfig;
33+
};
3234

3335
export async function designToCode({
3436
input,
@@ -101,6 +103,11 @@ export async function designToCode({
101103
reusable_widget_tree: reusable_widget_tree,
102104
};
103105

106+
const _extend_result = {
107+
..._tokenized_widget_input,
108+
framework: framework_config,
109+
};
110+
104111
switch (framework_config.framework) {
105112
case "vanilla":
106113
return {
@@ -110,7 +117,7 @@ export async function designToCode({
110117
vanilla_config: framework_config,
111118
asset_config: asset_config,
112119
})),
113-
..._tokenized_widget_input,
120+
..._extend_result,
114121
};
115122
case "react":
116123
return {
@@ -120,7 +127,7 @@ export async function designToCode({
120127
react_config: framework_config,
121128
asset_config: asset_config,
122129
})),
123-
..._tokenized_widget_input,
130+
..._extend_result,
124131
};
125132
case "react-native":
126133
return {
@@ -130,7 +137,7 @@ export async function designToCode({
130137
reactnative_config: framework_config,
131138
asset_config: asset_config,
132139
})),
133-
..._tokenized_widget_input,
140+
..._extend_result,
134141
};
135142
case "flutter":
136143
return {
@@ -140,7 +147,7 @@ export async function designToCode({
140147
flutter_config: framework_config,
141148
asset_config: asset_config,
142149
})),
143-
..._tokenized_widget_input,
150+
..._extend_result,
144151
};
145152
}
146153

packages/designto-token/support-flags/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ import { tokenize_flagged_wrap } from "./token-wrap";
1414
import { tokenize_flagged_wh_declaration } from "./token-wh";
1515
import { tokenize_flagged_fix_wh } from "./token-wh-fix";
1616

17-
export default function (node: ReflectSceneNode) {
17+
export default function handleWithFlags(node: ReflectSceneNode) {
1818
const flags = parse(node.name);
19-
return handle_with_flags(node, flags);
19+
return _handle_with_flags(node, flags);
2020
}
2121

22-
function handle_with_flags(node, flags: FlagsParseResult) {
22+
function _handle_with_flags(node, flags: FlagsParseResult) {
2323
// artwork
2424
const artwork_flag_alias =
2525
flags["artwork"] ||

packages/designto-token/token-graphics/vector.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function fromVector(vector: ReflectVectorNode) {
2525
// we are not sure when specifically this happens, but as reported, curvy lines does not contain a vector paths.
2626
// so we just return a image bake of it.
2727
process.env.NODE_ENV === "development" &&
28-
console.warn(
28+
console.info(
2929
`tried to get path data from vector, but none was provided. baking as a bitmap instead.`,
3030
vector
3131
);

packages/designto-token/token-layout/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,17 @@ function flex_or_stack_from_frame(
127127
};
128128

129129
if (frame.isAutoLayout) {
130+
// const __is_this_autolayout_frame_under_autolayout_parent =
131+
// frame.parent instanceof nodes.ReflectFrameNode &&
132+
// frame.parent.isAutoLayout;
133+
134+
/// > From the docs: https://www.figma.com/plugin-docs/api/properties/nodes-layoutalign
135+
/// Changing this property will cause the x, y, size, and relativeTransform properties on this node to change, if applicable (inside an auto-layout frame).
136+
/// - Setting "STRETCH" will make the node "stretch" to fill the width of the parent vertical auto - layout frame, or the height of the parent horizontal auto - layout frame excluding the frame's padding.
137+
/// - If the current node is an auto layout frame(e.g.an auto layout frame inside a parent auto layout frame) if you set layoutAlign to “STRETCH” you should set the corresponding axis – either primaryAxisSizingMode or counterAxisSizingMode – to be“FIXED”. This is because an auto - layout frame cannot simultaneously stretch to fill its parent and shrink to hug its children.
138+
/// - Setting "INHERIT" does not "stretch" the node.
139+
///
140+
130141
// TODO: inspect me. We're not 100% sure this is the correct behaviour.
131142
switch (frame.layoutMode) {
132143
case Axis.horizontal:

0 commit comments

Comments
 (0)