Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ LayoutMetrics LayoutableShadowNode::computeRelativeLayoutMetrics(
ShadowNodeTraits::Trait::FormsScrollContainer)) {
overflowRect = outsetBy(
overflowRect,
resolveEdgeMargins(*policy.clipMargin, currentFrame.size));
resolveEdgeMargins(*policy.clipMargin, currentFrame.size.width));
}
overflowRect = currentShadowNode->getTransform().applyWithCenter(
overflowRect, currentFrame.getCenter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ inline Rect outsetBy(const Rect &rect, const EdgeInsets &outsets) noexcept

/*
* A margin value that is either an absolute length in points or a percentage
* of the size it is resolved against.
* of the reference length it is resolved against.
*/
struct MarginValue {
Float value{};
Expand All @@ -99,21 +99,15 @@ struct MarginValue {
using EdgeMargins = RectangleEdges<MarginValue>;

/*
* Resolves margins into absolute insets against the given size.
* Percentages on the vertical edges resolve against the height, and those on
* the horizontal edges resolve against the width.
* Resolves percentage margins on all edges against the given reference length.
*/
inline EdgeInsets resolveEdgeMargins(const EdgeMargins &margins, const Size &size) noexcept
inline EdgeInsets resolveEdgeMargins(const EdgeMargins &margins, Float referenceLength) noexcept
{
const auto resolve = [](const MarginValue &margin, Float basis) {
return margin.isPercentage ? margin.value / 100.0f * basis : margin.value;
const auto resolve = [referenceLength](const MarginValue &margin) {
return margin.isPercentage ? margin.value / 100.0f * referenceLength : margin.value;
};

return EdgeInsets{
resolve(margins.left, size.width),
resolve(margins.top, size.height),
resolve(margins.right, size.width),
resolve(margins.bottom, size.height)};
return EdgeInsets{resolve(margins.left), resolve(margins.top), resolve(margins.right), resolve(margins.bottom)};
}

} // namespace facebook::react
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ IntersectionObserver::updateIntersectionObservation(
if (rootMargins_.has_value()) {
// Calculate the actual insets based on current root dimensions
// (converts percentages to pixels)
auto insets = resolveEdgeMargins(*rootMargins_, rootBoundingRect.size);
auto insets =
resolveEdgeMargins(*rootMargins_, rootBoundingRect.size.width);
// Use outsetBy to expand the root rect (positive values expand, negative
// contract)
rootMarginBoundingRect = outsetBy(rootBoundingRect, insets);
Expand All @@ -266,7 +267,7 @@ IntersectionObserver::updateIntersectionObservation(
ShadowNodeTraits::Trait::FormsScrollContainer)) {
rootClipRect = outsetBy(
rootClipRect,
resolveEdgeMargins(*scrollMargins_, rootBoundingRect.size));
resolveEdgeMargins(*scrollMargins_, rootBoundingRect.size.width));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export default class IntersectionObserver {
* Each side of the rectangle represented by `rootMargin` is added to the
* corresponding side in the root element's bounding box before the
* intersection test is performed.
* Percentages on all sides are relative to the width of the root's undilated box.
*/
// $FlowFixMe[unsafe-getters-setters]
get rootMargin(): string {
Expand Down Expand Up @@ -189,10 +190,11 @@ export default class IntersectionObserver {
* String with syntax similar to that of the CSS `margin` property.
* Each side of the rectangle represented by `scrollMargin` is added to the
* corresponding side of the clipping box of every scroll container between
* the root and the target, before the intersection test is performed.
* the root and the target, including the root if it is scrollable, before the
* intersection test is performed. Percentages on all sides are relative to
* the width of each scrollport's undilated clipping box.
*
* Unlike `rootMargin`, this does not affect the root itself, and so it does
* not affect the reported `rootBounds`.
* Unlike `rootMargin`, this does not affect the reported `rootBounds`.
*/
// $FlowFixMe[unsafe-getters-setters]
get scrollMargin(): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2803,7 +2803,7 @@ describe('IntersectionObserver', () => {

const root = Fantom.createRoot({
viewportWidth: 1000,
viewportHeight: 1000,
viewportHeight: 500,
});

Fantom.runTask(() => {
Expand Down Expand Up @@ -2840,7 +2840,7 @@ describe('IntersectionObserver', () => {
x: -50,
y: -50,
width: 1100,
height: 1100,
height: 600,
});
expectRectEquals(entries[0].intersectionRect, {
x: -50,
Expand All @@ -2849,7 +2849,7 @@ describe('IntersectionObserver', () => {
height: 100,
});
});
it('should calculate percentage rootMargin relative to root dimensions with custom root', () => {
it('should calculate percentage rootMargin relative to the undilated custom root width', () => {
const nodeRef = createRef<HostInstance>();
const rootNodeRef = createRef<HostInstance>();

Expand All @@ -2860,7 +2860,7 @@ describe('IntersectionObserver', () => {

Fantom.runTask(() => {
root.render(
<View style={{width: 200, height: 200}} ref={rootNodeRef}>
<View style={{width: 200, height: 400}} ref={rootNodeRef}>
<View
style={{
width: 50,
Expand Down Expand Up @@ -2893,7 +2893,7 @@ describe('IntersectionObserver', () => {
x: 50,
y: 50,
width: 100,
height: 100,
height: 300,
});
expectRectEquals(entries[0].intersectionRect, {
x: 50,
Expand All @@ -2913,7 +2913,7 @@ describe('IntersectionObserver', () => {

const root = Fantom.createRoot({
viewportWidth: 1000,
viewportHeight: 1000,
viewportHeight: 500,
});

Fantom.runTask(() => {
Expand Down Expand Up @@ -2949,7 +2949,7 @@ describe('IntersectionObserver', () => {
x: 0,
y: -100,
width: 1000,
height: 1100,
height: 600,
});
expectRectEquals(entries[0].intersectionRect, {
x: 0,
Expand Down Expand Up @@ -4139,6 +4139,7 @@ describe('IntersectionObserver', () => {
function renderTargetBelowTheFoldOfAScrollView(
marginTop: number = 150,
scale: number = 1,
width: number = 100,
) {
const nodeRef = React.createRef<HostInstance>();

Expand All @@ -4150,7 +4151,7 @@ describe('IntersectionObserver', () => {
root.render(
<ScrollView
style={{
width: 100,
width,
height: 100,
flexGrow: 0,
margin: scale === 1 ? 0 : 100,
Expand Down Expand Up @@ -4230,18 +4231,19 @@ describe('IntersectionObserver', () => {
marginTop: 150,
expectedRatio: 0.5,
},
{scrollMargin: '0% 0% 75% 0%', marginTop: 150, expectedRatio: 0.5},
{scrollMargin: '0% 0% 37.5% 0%', marginTop: 150, expectedRatio: 0.5},
{
scrollMargin: '0px 0px -20px 0px',
marginTop: 60,
expectedRatio: 0.4,
},
{scrollMargin: '0% 0% -20% 0%', marginTop: 60, expectedRatio: 0.4},
{scrollMargin: '0% 0% -10% 0%', marginTop: 60, expectedRatio: 0.4},
]) {
it(`should apply ${scrollMargin} in scrollport coordinates at scale ${scale}`, () => {
const node = renderTargetBelowTheFoldOfAScrollView(
marginTop,
scale,
200,
);
const entry = observeAndGetEntry(node, {scrollMargin});

Expand Down Expand Up @@ -4270,17 +4272,17 @@ describe('IntersectionObserver', () => {
expect(entries[0].intersectionRatio).toBe(0);
});

it('should resolve percentages against the scrollport, not the root', () => {
const node = renderTargetBelowTheFoldOfAScrollView();
it('should resolve percentages against the undilated scrollport width, not the root', () => {
const node = renderTargetBelowTheFoldOfAScrollView(150, 1, 200);

const intersectionObserverCallback = jest.fn();

Fantom.runTask(() => {
observer = new IntersectionObserver(intersectionObserverCallback, {
// 60% of the 100px scrollport is 60px, expanding its clip to
// 30% of the 200px scrollport width is 60px, expanding its clip to
// 0..160 and revealing 10px of the target. Were this resolved
// against the 1000px viewport it would reveal the target entirely.
scrollMargin: '0% 0% 60% 0%',
scrollMargin: '0% 0% 30% 0%',
threshold: [0.01],
});
observer.observe(node);
Expand All @@ -4297,6 +4299,77 @@ describe('IntersectionObserver', () => {
});
});

for (const {scrollMargin, scrollY} of [
{scrollMargin: '10% 0px 0px 0px', scrollY: 30},
{scrollMargin: '-10% 0px 0px 0px', scrollY: -10},
]) {
it(`should resolve top ${scrollMargin} against scrollport width`, () => {
const nodeRef = createRef<HostInstance>();
const scrollNodeRef = createRef<HostInstance>();
const root = Fantom.createRoot({
viewportWidth: 1000,
viewportHeight: 1000,
});
Fantom.runTask(() => {
root.render(
<ScrollView
ref={scrollNodeRef}
style={{width: 200, height: 100, flexGrow: 0, marginTop: 100}}>
<View ref={nodeRef} style={{width: 50, height: 50}} />
</ScrollView>,
);
});
Fantom.scrollTo(nullthrows(scrollNodeRef.current), {
x: 0,
y: scrollY,
});

const entry = observeAndGetEntry(nullthrows(nodeRef.current), {
scrollMargin,
});
expect(entry.isIntersecting).toBe(true);
expect(entry.intersectionRatio).toBe(0.8);
expectRectEquals(entry.intersectionRect, {
x: 0,
y: scrollY > 0 ? 80 : 120,
width: 50,
height: 40,
});
});
}

it('should resolve percentages independently against each nested scrollport width', () => {
const nodeRef = createRef<HostInstance>();
const root = Fantom.createRoot({
viewportWidth: 1000,
viewportHeight: 1000,
});
Fantom.runTask(() => {
root.render(
<ScrollView style={{width: 200, height: 100, flexGrow: 0}}>
<ScrollView style={{width: 300, height: 100, flexGrow: 0}}>
<View
ref={nodeRef}
style={{width: 50, height: 50, marginTop: 115}}
/>
</ScrollView>
</ScrollView>,
);
});

const entry = observeAndGetEntry(nullthrows(nodeRef.current), {
scrollMargin: '0px 0px 10% 0px',
});
expect(entry.isIntersecting).toBe(true);
expect(entry.intersectionRatio).toBe(0.1);
expectRectEquals(entry.intersectionRect, {
x: 0,
y: 115,
width: 50,
height: 5,
});
});

it('should contract the scrollport clip with negative values', () => {
// Target spans 60..110, so the unmodified scrollport clips it to
// 60..100 (80% visible).
Expand Down Expand Up @@ -4566,7 +4639,9 @@ describe('IntersectionObserver', () => {
});

describe('explicit root', () => {
function renderTargetBelowTheFoldOfAScrollViewRoot(): {
function renderTargetBelowTheFoldOfAScrollViewRoot(
width: number = 100,
): {
scrollNode: HostInstance,
node: HostInstance,
} {
Expand All @@ -4580,7 +4655,7 @@ describe('IntersectionObserver', () => {
Fantom.runTask(() => {
root.render(
<ScrollView
style={{width: 100, height: 100, flexGrow: 0}}
style={{width, height: 100, flexGrow: 0}}
ref={scrollNodeRef}>
<View
style={{width: 50, height: 50, marginTop: 150}}
Expand Down Expand Up @@ -4625,32 +4700,29 @@ describe('IntersectionObserver', () => {
});
});

it('should combine with rootMargin on a scrollable root, resolving both against the undilated root', () => {
it('should combine with rootMargin on a scrollable root, resolving both against the undilated root width', () => {
const {scrollNode, node} =
renderTargetBelowTheFoldOfAScrollViewRoot();
renderTargetBelowTheFoldOfAScrollViewRoot(200);

// rootMargin adds 40px and scrollMargin 25% of the undilated 100px
// root (25px), so the clip is 0..165. Resolving the percentage
// against the rootMargin-dilated root (140px) would give 0..175.
const entry = observeAndGetEntry(node, {
root: scrollNode,
rootMargin: '0px 0px 40px 0px',
rootMargin: '10% 50px 40px 50px',
scrollMargin: '0% 0% 25% 0%',
});

expect(entry.isIntersecting).toBe(true);
expect(entry.intersectionRatio).toBe(0.3);
expect(entry.intersectionRatio).toBe(0.8);
expectRectEquals(entry.intersectionRect, {
x: 0,
y: 150,
width: 50,
height: 15,
height: 40,
});
expectRectEquals(nullthrows(entry.rootBounds), {
x: 0,
y: 0,
width: 100,
height: 140,
x: -50,
y: -20,
width: 300,
height: 160,
});
});

Expand Down
2 changes: 1 addition & 1 deletion scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ facebook::react::ColorComponents facebook::react::colorComponentsFromColor(faceb
facebook::react::ColorComponents facebook::react::colorComponentsFromHostPlatformColor(facebook::react::Color color);
facebook::react::ColorSpace facebook::react::getDefaultColorSpace();
facebook::react::DisplayType facebook::react::displayTypeFromYGDisplay(YGDisplay display);
facebook::react::EdgeInsets facebook::react::resolveEdgeMargins(const facebook::react::EdgeMargins& margins, const facebook::react::Size& size) noexcept;
facebook::react::EdgeInsets facebook::react::resolveEdgeMargins(const facebook::react::EdgeMargins& margins, facebook::react::Float referenceLength) noexcept;
facebook::react::Float facebook::react::floatFromYogaFloat(float value);
facebook::react::Float facebook::react::floatFromYogaOptionalFloat(facebook::yoga::FloatOptional value);
facebook::react::LayoutMetrics facebook::react::layoutMetricsFromYogaNode(facebook::yoga::Node& yogaNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ facebook::react::ColorComponents facebook::react::colorComponentsFromHostPlatfor
facebook::react::ColorSpace facebook::react::getDefaultColorSpace();
facebook::react::ComponentRegistryFactory facebook::react::getDefaultComponentRegistryFactory();
facebook::react::DisplayType facebook::react::displayTypeFromYGDisplay(YGDisplay display);
facebook::react::EdgeInsets facebook::react::resolveEdgeMargins(const facebook::react::EdgeMargins& margins, const facebook::react::Size& size) noexcept;
facebook::react::EdgeInsets facebook::react::resolveEdgeMargins(const facebook::react::EdgeMargins& margins, facebook::react::Float referenceLength) noexcept;
facebook::react::Float facebook::react::floatFromYogaFloat(float value);
facebook::react::Float facebook::react::floatFromYogaOptionalFloat(facebook::yoga::FloatOptional value);
facebook::react::LayoutMetrics facebook::react::layoutMetricsFromYogaNode(facebook::yoga::Node& yogaNode);
Expand Down
2 changes: 1 addition & 1 deletion scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ facebook::react::ColorComponents facebook::react::colorComponentsFromColor(faceb
facebook::react::ColorComponents facebook::react::colorComponentsFromHostPlatformColor(facebook::react::Color color);
facebook::react::ColorSpace facebook::react::getDefaultColorSpace();
facebook::react::DisplayType facebook::react::displayTypeFromYGDisplay(YGDisplay display);
facebook::react::EdgeInsets facebook::react::resolveEdgeMargins(const facebook::react::EdgeMargins& margins, const facebook::react::Size& size) noexcept;
facebook::react::EdgeInsets facebook::react::resolveEdgeMargins(const facebook::react::EdgeMargins& margins, facebook::react::Float referenceLength) noexcept;
facebook::react::Float facebook::react::floatFromYogaFloat(float value);
facebook::react::Float facebook::react::floatFromYogaOptionalFloat(facebook::yoga::FloatOptional value);
facebook::react::LayoutMetrics facebook::react::layoutMetricsFromYogaNode(facebook::yoga::Node& yogaNode);
Expand Down
2 changes: 1 addition & 1 deletion scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ facebook::react::ColorComponents facebook::react::colorComponentsFromColor(faceb
facebook::react::ColorComponents facebook::react::colorComponentsFromHostPlatformColor(facebook::react::Color color);
facebook::react::ColorSpace facebook::react::getDefaultColorSpace();
facebook::react::DisplayType facebook::react::displayTypeFromYGDisplay(YGDisplay display);
facebook::react::EdgeInsets facebook::react::resolveEdgeMargins(const facebook::react::EdgeMargins& margins, const facebook::react::Size& size) noexcept;
facebook::react::EdgeInsets facebook::react::resolveEdgeMargins(const facebook::react::EdgeMargins& margins, facebook::react::Float referenceLength) noexcept;
facebook::react::Float facebook::react::floatFromYogaFloat(float value);
facebook::react::Float facebook::react::floatFromYogaOptionalFloat(facebook::yoga::FloatOptional value);
facebook::react::LayoutMetrics facebook::react::layoutMetricsFromYogaNode(facebook::yoga::Node& yogaNode);
Expand Down
2 changes: 1 addition & 1 deletion scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -3236,7 +3236,7 @@ facebook::react::ColorComponents facebook::react::colorComponentsFromColor(faceb
facebook::react::ColorComponents facebook::react::colorComponentsFromHostPlatformColor(facebook::react::Color color);
facebook::react::ColorSpace facebook::react::getDefaultColorSpace();
facebook::react::DisplayType facebook::react::displayTypeFromYGDisplay(YGDisplay display);
facebook::react::EdgeInsets facebook::react::resolveEdgeMargins(const facebook::react::EdgeMargins& margins, const facebook::react::Size& size) noexcept;
facebook::react::EdgeInsets facebook::react::resolveEdgeMargins(const facebook::react::EdgeMargins& margins, facebook::react::Float referenceLength) noexcept;
facebook::react::Float facebook::react::floatFromYogaFloat(float value);
facebook::react::Float facebook::react::floatFromYogaOptionalFloat(facebook::yoga::FloatOptional value);
facebook::react::LayoutMetrics facebook::react::layoutMetricsFromYogaNode(facebook::yoga::Node& yogaNode);
Expand Down
Loading
Loading