diff --git a/javascript/src/wrapAssembly.ts b/javascript/src/wrapAssembly.ts index cbb51a4564..f6689a2ca9 100644 --- a/javascript/src/wrapAssembly.ts +++ b/javascript/src/wrapAssembly.ts @@ -35,6 +35,7 @@ type Layout = { bottom: number; width: number; height: number; + hadOverflow: boolean; }; type Size = { @@ -85,6 +86,7 @@ export type Node = { getChildCount(): number; getComputedBorder(edge: Edge): number; getComputedBottom(): number; + getComputedHadOverflow(): boolean; getComputedHeight(): number; getComputedLayout(): Layout; getComputedLeft(): number; @@ -965,6 +967,10 @@ export default function wrapAssembly(lib: any): Yoga { return lib._YGNodeLayoutGetHeight(this._ptr); } + getComputedHadOverflow(): boolean { + return !!lib._YGNodeLayoutGetHadOverflow(this._ptr); + } + getComputedLayout(): Layout { return { left: lib._YGNodeLayoutGetLeft(this._ptr), @@ -973,6 +979,7 @@ export default function wrapAssembly(lib: any): Yoga { bottom: lib._YGNodeLayoutGetBottom(this._ptr), width: lib._YGNodeLayoutGetWidth(this._ptr), height: lib._YGNodeLayoutGetHeight(this._ptr), + hadOverflow: !!lib._YGNodeLayoutGetHadOverflow(this._ptr), }; } diff --git a/javascript/tests/YGHadOverflowTest.test.ts b/javascript/tests/YGHadOverflowTest.test.ts new file mode 100644 index 0000000000..c768042c1d --- /dev/null +++ b/javascript/tests/YGHadOverflowTest.test.ts @@ -0,0 +1,110 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import Yoga from 'yoga-layout'; +import {Direction, FlexDirection, Wrap} from 'yoga-layout'; + +test('children_overflow_no_wrap_and_no_flex_children', () => { + const root = Yoga.Node.create(); + root.setWidth(200); + root.setHeight(100); + root.setFlexDirection(FlexDirection.Column); + root.setFlexWrap(Wrap.NoWrap); + + const child0 = Yoga.Node.create(); + child0.setWidth(80); + child0.setHeight(40); + child0.setMargin(Yoga.EDGE_TOP, 10); + child0.setMargin(Yoga.EDGE_BOTTOM, 15); + root.insertChild(child0, 0); + + const child1 = Yoga.Node.create(); + child1.setWidth(80); + child1.setHeight(40); + child1.setMargin(Yoga.EDGE_BOTTOM, 5); + root.insertChild(child1, 1); + + root.calculateLayout(200, 100, Direction.LTR); + + expect(root.getComputedHadOverflow()).toBe(true); +}); + +test('no_overflow_no_wrap_and_flex_children', () => { + const root = Yoga.Node.create(); + root.setWidth(200); + root.setHeight(100); + root.setFlexDirection(FlexDirection.Column); + root.setFlexWrap(Wrap.NoWrap); + + const child0 = Yoga.Node.create(); + child0.setWidth(80); + child0.setHeight(40); + child0.setMargin(Yoga.EDGE_TOP, 10); + child0.setMargin(Yoga.EDGE_BOTTOM, 10); + root.insertChild(child0, 0); + + const child1 = Yoga.Node.create(); + child1.setWidth(80); + child1.setHeight(40); + child1.setMargin(Yoga.EDGE_BOTTOM, 5); + child1.setFlexShrink(1); + root.insertChild(child1, 1); + + root.calculateLayout(200, 100, Direction.LTR); + + expect(root.getComputedHadOverflow()).toBe(false); +}); + +test('hadOverflow_gets_reset_if_no_longer_valid', () => { + const root = Yoga.Node.create(); + root.setWidth(200); + root.setHeight(100); + root.setFlexDirection(FlexDirection.Column); + root.setFlexWrap(Wrap.NoWrap); + + const child0 = Yoga.Node.create(); + child0.setWidth(80); + child0.setHeight(40); + child0.setMargin(Yoga.EDGE_TOP, 10); + child0.setMargin(Yoga.EDGE_BOTTOM, 10); + root.insertChild(child0, 0); + + const child1 = Yoga.Node.create(); + child1.setWidth(80); + child1.setHeight(40); + child1.setMargin(Yoga.EDGE_BOTTOM, 5); + root.insertChild(child1, 1); + + root.calculateLayout(200, 100, Direction.LTR); + expect(root.getComputedHadOverflow()).toBe(true); + + child1.setFlexShrink(1); + root.calculateLayout(200, 100, Direction.LTR); + expect(root.getComputedHadOverflow()).toBe(false); +}); + +test('hadOverflow_included_in_getComputedLayout', () => { + const root = Yoga.Node.create(); + root.setWidth(200); + root.setHeight(100); + root.setFlexDirection(FlexDirection.Column); + + const child0 = Yoga.Node.create(); + child0.setWidth(80); + child0.setHeight(80); + root.insertChild(child0, 0); + + const child1 = Yoga.Node.create(); + child1.setWidth(80); + child1.setHeight(80); + root.insertChild(child1, 1); + + root.calculateLayout(200, 100, Direction.LTR); + + const layout = root.getComputedLayout(); + expect(layout.hadOverflow).toBe(true); +});