Skip to content
Open
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 @@ -63,6 +63,7 @@ jsi::Object NativeIntersectionObserver::observeV2(
auto thresholds = options.thresholds;
auto rootThresholds = options.rootThresholds;
auto rootMargin = options.rootMargin;
auto scrollMargin = options.scrollMargin;
auto& uiManager = getUIManagerFromRuntime(runtime);

intersectionObserverManager_.observe(
Expand All @@ -72,6 +73,7 @@ jsi::Object NativeIntersectionObserver::observeV2(
thresholds,
rootThresholds,
rootMargin,
scrollMargin,
uiManager);

return tokenFromShadowNodeFamily(runtime, shadowNodeFamily);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ using NativeIntersectionObserverObserveOptions = NativeIntersectionObserverNativ
// rootThresholds
std::optional<std::vector<Float>>,
// rootMargin
std::optional<std::string>,
// scrollMargin
std::optional<std::string>>;

template <>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,23 @@ bool YogaLayoutableShadowNode::getIsLayoutClean() const {
return !YGNodeIsDirty(&yogaNode_);
}

bool YogaLayoutableShadowNode::hasContentClip() const {
return YGNodeStyleGetOverflow(&yogaNode_) != YGOverflowVisible;
}

bool YogaLayoutableShadowNode::isScrollContainer() const {
// No `default`, so new `overflow` values must be classified explicitly
// (-Wswitch).
switch (YGNodeStyleGetOverflow(&yogaNode_)) {
case YGOverflowHidden:
case YGOverflowScroll:
return true;
case YGOverflowVisible:
return false;
}
return false;
}

#pragma mark - Mutating Methods

void YogaLayoutableShadowNode::enableMeasurement() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ class YogaLayoutableShadowNode : public LayoutableShadowNode {
void dirtyLayout() override;
bool getIsLayoutClean() const override;

bool hasContentClip() const override;

bool isScrollContainer() const override;

/*
* Computes layout using Yoga layout engine.
* See `LayoutableShadowNode` for more details.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,21 @@ LayoutMetrics LayoutableShadowNode::computeRelativeLayoutMetrics(
resultFrame.origin += currentShadowNode->getContentOriginOffset(true);
}

// Only nodes with a content clip clip their content. Clipping
// `overflow: visible` nodes to their content bounds would cut off content
// that a dilated (clipMargin) scroll container below them has revealed.
if (policy.enableOverflowClipping &&
(i != size - 1 || policy.clipSpecifiedAncestor)) {
auto overflowInset = currentShadowNode->getLayoutMetrics().overflowInset;
auto overflowRect = insetBy(
currentFrame * currentShadowNode->getTransform(), overflowInset);
(i != size - 1 || policy.clipSpecifiedAncestor) &&
currentShadowNode->hasContentClip()) {
auto overflowRect = currentFrame * currentShadowNode->getTransform();
// Only scroll containers are dilated, and the measured node is not its
// own container, so its clip is never dilated.
if (policy.clipMargin.has_value() && i != 0 &&
currentShadowNode->isScrollContainer()) {
overflowRect = outsetBy(
overflowRect,
resolveEdgeMargins(*policy.clipMargin, overflowRect.size));
}
resultFrame = Rect::intersect(resultFrame, overflowRect);
if (resultFrame.size.width == 0 && resultFrame.size.height == 0) {
return EmptyLayoutMetrics;
Expand Down Expand Up @@ -224,6 +234,14 @@ Point LayoutableShadowNode::getContentOriginOffset(
return {.x = 0, .y = 0};
}

bool LayoutableShadowNode::hasContentClip() const {
return false;
}

bool LayoutableShadowNode::isScrollContainer() const {
return false;
}

bool LayoutableShadowNode::canBeTouchTarget() const {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <array>
#include <cmath>
#include <memory>
#include <optional>
#include <vector>

#include <React/Debug.h>
Expand Down Expand Up @@ -42,12 +43,19 @@ class LayoutableShadowNode : public ShadowNode {
struct LayoutInspectingPolicy {
bool includeTransform{true};
bool includeViewportOffset{false};
// Clip against every node with a content clip (see hasContentClip) on the
// way up from the measured node.
bool enableOverflowClipping{false};
// When enableOverflowClipping is true, also clip against the ancestor
// passed as the reference node. IntersectionObserver sets this false:
// the spec clips ancestors *until* the root, then intersects with the
// rootMargin-expanded root intersection rectangle.
bool clipSpecifiedAncestor{true};
// When enableOverflowClipping is true, expand the clip rect of every
// scroll container ancestor (see isScrollContainer) by these margins
// before clipping (not the measured node's own clip). Percentages resolve
// against each clip rect. This is IntersectionObserver's `scrollMargin`.
std::optional<EdgeMargins> clipMargin{};
};

using UnsharedList = std::vector<LayoutableShadowNode *>;
Expand Down Expand Up @@ -140,6 +148,21 @@ class LayoutableShadowNode : public ShadowNode {
*/
virtual Point getContentOriginOffset(bool includeTransform) const;

/*
* Whether the node clips its content to its bounds (CSS "content clip"),
* e.g. `overflow` other than `visible`.
* Default implementation returns `false`.
*/
virtual bool hasContentClip() const;

/*
* Whether the node is a scroll container in the CSS sense (`overflow:
* hidden` or `scroll`). Every scroll container has a content clip, but not
* every content clip is a scroll container (e.g. CSS `overflow: clip`).
* Default implementation returns `false`.
*/
virtual bool isScrollContainer() const;

/*
* Sets layout metrics for the shadow node.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,40 @@ inline Rect outsetBy(const Rect &rect, const EdgeInsets &outsets) noexcept
.height = rect.size.height + outsets.top + outsets.bottom}};
}

/*
* A margin value that is either an absolute length in points or a percentage
* of the size it is resolved against.
*/
struct MarginValue {
Float value{};
bool isPercentage{false};

bool operator==(const MarginValue &rhs) const noexcept = default;
};

/*
* EdgeMargins
*/
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.
*/
inline EdgeInsets resolveEdgeMargins(const EdgeMargins &margins, const Size &size) noexcept
{
const auto resolve = [](const MarginValue &margin, Float basis) {
return margin.isPercentage ? margin.value / 100.0f * basis : margin.value;
};

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

} // namespace facebook::react

namespace std {
Expand Down
Loading
Loading