Skip to content

Commit 7ac0e19

Browse files
switch container for merging widgets to WrappingContainer
1 parent 980cf9f commit 7ac0e19

9 files changed

Lines changed: 69 additions & 39 deletions

File tree

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default function handleWithFlags(node: ReflectSceneNode) {
2323
}
2424

2525
function _handle_with_flags(node, flags: FlagsParseResult) {
26+
// #region widget altering flags
2627
// artwork
2728
const artwork_flag_alias =
2829
flags["artwork"] ||
@@ -44,6 +45,20 @@ function _handle_with_flags(node, flags: FlagsParseResult) {
4445
return tokenize_flagged_wrap(node, wrap_flag_alias);
4546
}
4647

48+
if (flags.__meta?.contains_button_flag) {
49+
return tokenize_flagged_button(node, flags[keys.flag_key__as_button]);
50+
}
51+
52+
if (flags.__meta?.contains_input_flag) {
53+
return tokenize_flagged_textfield(node, flags[keys.flag_key__as_input]);
54+
}
55+
56+
if (flags.__meta?.contains_slider_flag) {
57+
return tokenize_flagged_slider(node, flags[keys.flag_key__as_slider]);
58+
}
59+
// #endregion
60+
61+
// #region element altering flags
4762
// heading
4863
const heading_flag_alias =
4964
flags[keys.flag_key__as_h1] ||
@@ -61,7 +76,9 @@ function _handle_with_flags(node, flags: FlagsParseResult) {
6176
if (span_flag_alias) {
6277
return tokenize_flagged_span(node, span_flag_alias);
6378
}
79+
// #endregion
6480

81+
// #region style extension flags
6582
const paragraph_flag_alias = flags[keys.flag_key__as_p];
6683
if (paragraph_flag_alias) {
6784
return tokenize_flagged_paragraph(node, paragraph_flag_alias);
@@ -87,16 +104,5 @@ function _handle_with_flags(node, flags: FlagsParseResult) {
87104
if (fix_wh_flags.length) {
88105
return tokenize_flagged_fix_wh(node, fix_wh_flags);
89106
}
90-
91-
if (flags.__meta?.contains_button_flag) {
92-
return tokenize_flagged_button(node, flags[keys.flag_key__as_button]);
93-
}
94-
95-
if (flags.__meta?.contains_input_flag) {
96-
return tokenize_flagged_textfield(node, flags[keys.flag_key__as_input]);
97-
}
98-
99-
if (flags.__meta?.contains_slider_flag) {
100-
return tokenize_flagged_slider(node, flags[keys.flag_key__as_slider]);
101-
}
107+
// #endregion style extension flags
102108
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import type {
77
} from "@design-sdk/figma-node";
88
import assert from "assert";
99
import { tokenizeButton } from "../../token-widgets";
10+
import { WrappingContainer } from "../../tokens";
1011

1112
export function tokenize_flagged_button(
1213
node: ReflectSceneNode,
1314
flag: AsButtonFlag
14-
): ButtonStyleButton | Container<ButtonStyleButton> {
15+
): ButtonStyleButton | WrappingContainer<ButtonStyleButton> {
1516
if (flag.value === false) return;
1617

1718
const validated = validate_button(node);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import assert from "assert";
1111
import { tokenizeLayout } from "../../token-layout";
1212
import { unwrappedChild } from "../../wrappings";
1313
import { RoundSliderThumbShape } from "@reflect-ui/core/lib/slider.thumb";
14+
import { WrappingContainer } from "../../tokens";
1415

1516
/**
1617
*
@@ -32,7 +33,7 @@ import { RoundSliderThumbShape } from "@reflect-ui/core/lib/slider.thumb";
3233
export function tokenize_flagged_slider(
3334
node: ReflectSceneNode,
3435
flag: AsSliderFlag
35-
): Slider | Container {
36+
): Slider | WrappingContainer {
3637
if (flag.value === false) return;
3738

3839
const validated = validate_slider(node);
@@ -75,9 +76,9 @@ export function tokenize_flagged_slider(
7576
)
7677
) as Container;
7778

78-
// @ts-ignore FIXME: no tsignore
79-
return new Container({
79+
return new WrappingContainer({
8080
...container,
81+
key: keyFromNode(node),
8182
child: new Slider({
8283
key: _key,
8384
...container,

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { detectIf } from "@reflect-ui/detection";
1919
import { paintToColor } from "@design-sdk/core/utils/colors";
2020
import { tokenizeLayout } from "../../token-layout";
2121
import { unwrappedChild } from "../../wrappings";
22+
import { WrappingContainer } from "../../tokens";
2223

2324
/**
2425
*
@@ -40,7 +41,7 @@ import { unwrappedChild } from "../../wrappings";
4041
export function tokenize_flagged_textfield(
4142
node: ReflectSceneNode,
4243
flag: AsInputFlag
43-
): TextField | Container {
44+
): TextField | WrappingContainer {
4445
if (flag.value === false) return;
4546

4647
const validated = validate_input(node);
@@ -76,9 +77,9 @@ export function tokenize_flagged_textfield(
7677
)
7778
) as Container;
7879

79-
// @ts-ignore FIXME: no tsignore
80-
return new Container({
80+
return new WrappingContainer({
8181
...container,
82+
key: keyFromNode(node),
8283
child: new TextField({
8384
key: _key,
8485
...container,

packages/designto-token/token-widgets/button-widget.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Colors, Container, EdgeInsets, WidgetKey } from "@reflect-ui/core";
77
import assert from "assert";
88
import { unwrappedChild } from "../wrappings";
99
import { tokenizeLayout } from "../token-layout";
10+
import { WrappingContainer } from "../tokens";
1011

1112
/**
1213
* Note - this universal button is used temporarily. the button tokens will be splited into more specific usecase following material button classification.
@@ -18,7 +19,7 @@ import { tokenizeLayout } from "../token-layout";
1819
function button(
1920
node: ReflectSceneNode,
2021
manifest: manifests.DetectedButtonManifest
21-
): core.ButtonStyleButton | core.Container<core.ButtonStyleButton> {
22+
): core.ButtonStyleButton | WrappingContainer<core.ButtonStyleButton> {
2223
assert(manifest.text, "text is required for button composing at this point");
2324

2425
// TODO:
@@ -67,14 +68,14 @@ function button(
6768
)
6869
);
6970

70-
// @ts-ignore FIXME: no tsignore
71-
return new Container({
71+
return new WrappingContainer({
7272
...container,
73+
key: keyFromNode(node),
7374
child: button,
7475
});
7576
}
7677

77-
return new core.Container({
78+
return new WrappingContainer({
7879
key: WidgetKey.copyWith(_key, { id: _key.id + ".sizedbox" }),
7980
width: sizing.width,
8081
height: sizing.height,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from "./stretched";
2+
export * from "./wrapping-container";
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Container, Widget } from "@reflect-ui/core";
2+
3+
/**
4+
* Special token for wrapping a detected component with a container.
5+
*/
6+
export class WrappingContainer<
7+
T extends Widget = Widget
8+
> extends Container<T> {}

packages/designto-token/wrappings/wrapping.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
OverflowBox,
1010
SingleChildScrollView,
1111
} from "@reflect-ui/core";
12-
import { Stretched } from "../tokens";
12+
import { Stretched, WrappingContainer } from "../tokens";
1313

1414
export type WrappingToken =
1515
// layout / positioning / sizing wrappers
@@ -25,15 +25,17 @@ export type WrappingToken =
2525
// effect wrappers
2626
| Blurred
2727
// clip wrappers
28-
| ClipRRect;
28+
| ClipRRect
29+
// wrapping container
30+
| WrappingContainer;
2931

3032
/**
3133
* CAUTION - this is not related to `Wrap` Widget. this unwrapps a (nested) token that is wrapped with typeof `WrappingToken`
3234
* @param maybeWrapped
3335
* @returns
3436
*/
3537
export function unwrappedChild<T extends Widget>(maybeWrapped: Widget): T {
36-
if (
38+
const isWrappingWidget =
3739
maybeWrapped instanceof SizedBox ||
3840
maybeWrapped instanceof Stretched ||
3941
maybeWrapped instanceof Positioned ||
@@ -42,8 +44,10 @@ export function unwrappedChild<T extends Widget>(maybeWrapped: Widget): T {
4244
maybeWrapped instanceof Rotation ||
4345
maybeWrapped instanceof Opacity ||
4446
maybeWrapped instanceof Blurred ||
45-
maybeWrapped instanceof ClipRRect
46-
) {
47+
maybeWrapped instanceof ClipRRect ||
48+
maybeWrapped instanceof WrappingContainer;
49+
50+
if (isWrappingWidget) {
4751
return unwrappedChild(maybeWrapped.child);
4852
}
4953
return maybeWrapped as T;

packages/designto-web/tokens-to-web-widget/index.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { compose_instanciation } from "./compose-instanciation";
2121
import { IWHStyleWidget } from "@reflect-ui/core";
2222
import * as reusable from "@code-features/component/tokens";
2323
import assert from "assert";
24+
import { WrappingContainer } from "@designto/token/tokens";
2425

2526
interface WebWidgetComposerConfig {
2627
/**
@@ -233,6 +234,23 @@ function compose<T extends JsxWidget>(
233234
} else if (widget instanceof core.Slider) {
234235
thisWebWidget = compose_unwrapped_slider(_key, widget);
235236
}
237+
// wrapping container
238+
else if (widget instanceof WrappingContainer) {
239+
// #region
240+
// mergable widgets for web
241+
if (widget.child instanceof core.TextField) {
242+
thisWebWidget = compose_unwrapped_text_input(_key, widget.child, widget);
243+
} else if (widget.child instanceof core.ButtonStyleButton) {
244+
thisWebWidget = compose_unwrapped_button(_key, widget.child, widget);
245+
} else if (widget.child instanceof core.Slider) {
246+
thisWebWidget = compose_unwrapped_slider(_key, widget.child, widget);
247+
} else {
248+
throw new Error(
249+
`Unsupported widget type: ${widget.child.constructor.name}`
250+
);
251+
}
252+
// #endregion
253+
}
236254
// #endregion
237255

238256
// execution order matters - some above widgets inherits from Container, this shall be handled at the last.
@@ -249,17 +267,6 @@ function compose<T extends JsxWidget>(
249267
container.y = widget.y;
250268
container.background = widget.background;
251269
thisWebWidget = container;
252-
253-
// #region
254-
// mergable widgets for web
255-
if (widget.child instanceof core.TextField) {
256-
thisWebWidget = compose_unwrapped_text_input(_key, widget.child, widget);
257-
} else if (widget.child instanceof core.ButtonStyleButton) {
258-
thisWebWidget = compose_unwrapped_button(_key, widget.child, widget);
259-
} else if (widget.child instanceof core.Slider) {
260-
thisWebWidget = compose_unwrapped_slider(_key, widget.child, widget);
261-
}
262-
// #endregion
263270
}
264271

265272
// -------------------------------------

0 commit comments

Comments
 (0)