Skip to content

Commit a7bc8e3

Browse files
fix text sizing (with dangerous static logic overrides)
1 parent f52dbfb commit a7bc8e3

3 files changed

Lines changed: 37 additions & 6 deletions

File tree

externals/design-sdk

packages/builder-web-core/widgets-native/html-text-element/index.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { Dynamic } from "@reflect-ui/core/lib/_utility-types";
1414
* You can select wich element to render with `elementPreference`. - choose between h1 ~ h6, p, span, etc.
1515
*/
1616
export class Text extends TextChildWidget {
17-
_type: "Text";
17+
_type: "Text" = "Text";
1818

1919
// text properties
2020
data: Dynamic<string>;
@@ -74,15 +74,27 @@ export class Text extends TextChildWidget {
7474
"text-decoration": css.textDecoration(this.textStyle.decoration),
7575
"text-shadow": css.textShadow(this.textStyle.textShadow),
7676
// ------------------------------------------
77-
"min-height": css.px(this.height),
78-
// TODO: do not specify width when parent is a flex container.
79-
// Also flex: 1 is required to make the text wrap.
80-
width: css.px(this.width),
77+
...textWH({ width: this.width, height: this.height }),
8178
};
8279

8380
return <CSSProperties>textStyle;
8481
}
8582

83+
get finalStyle() {
84+
const superFinalStyle = super.finalStyle;
85+
// TODO: this is a dirty fix ------------------------------------------------
86+
// the text's width should not be overriden by the constraint's preference.
87+
if (this.width === undefined) {
88+
delete superFinalStyle["width"];
89+
}
90+
if (this.height === undefined) {
91+
delete superFinalStyle["height"];
92+
}
93+
// --------------------------------------------------------------------------
94+
95+
return { ...superFinalStyle };
96+
}
97+
8698
jsxConfig(): StylableJSXElementConfig {
8799
return <StylableJSXElementConfig>{
88100
type: "tag-and-attr",
@@ -99,3 +111,12 @@ const __get_dedicated_element_tag = (t?: WebTextElement | undefined) => {
99111
return __default_element_tag;
100112
}
101113
};
114+
115+
function textWH({ width, height }: { width: number; height: number }) {
116+
return {
117+
// TODO: do not specify width when parent is a flex container.
118+
// Also flex: 1 is required to make the text wrap.
119+
width: css.px(width),
120+
"min-height": css.px(height),
121+
};
122+
}

packages/designto-token/main.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,16 @@ function handle_by_types(
270270
break;
271271

272272
case nodes.ReflectSceneNodeType.text:
273+
// FIXME: aberation handling (remove me if required) --------------------------------
274+
// FIXME: this is for giving extra space for text so it won't break line accidently.
275+
// FIXME: consider applying this only to a preview build
276+
if (node.data.length <= 6 && node.data.length > 2) {
277+
node.width = node.width + 1;
278+
} else if (node.data.length < 30) {
279+
node.width = node.width + 2;
280+
}
281+
// FIXME: ---------------------------------------------------------------------------------
282+
273283
tokenizedTarget = tokenizeText.fromText(node as nodes.ReflectTextNode);
274284
break;
275285

0 commit comments

Comments
 (0)