#import #import #import #import #import #import using namespace facebook::react; extern const char EffectUIStickyHeaderName[] = "EffectUIStickyHeader"; namespace { class StickyHeaderProps final : public ViewProps { public: using ViewProps::ViewProps; }; } // namespace using HeaderNode = ConcreteViewShadowNode; using HeaderDescriptor = ConcreteComponentDescriptor; // Pins itself to the top of its scroll view on the UI thread, as the scroll // moves and after every mount, until the next header pushes it away. Fabric // keeps the unpinned layout, which every pin starts from, so the layout and // the pin land in the same frame. @interface EffectUIStickyHeader : RCTViewComponentView @end @implementation EffectUIStickyHeader { __weak RCTScrollViewComponentView *_scroll; } - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { _props = HeaderNode::defaultSharedProps(); } return self; } + (void)load { [RCTComponentViewFactory.currentComponentViewFactory registerComponentViewClass:self]; } + (ComponentDescriptorProvider)componentDescriptorProvider { return concreteComponentDescriptorProvider(); } - (void)didMoveToWindow { [super didMoveToWindow]; RCTScrollViewComponentView *scroll = self.window ? [RCTScrollViewComponentView findScrollViewComponentViewForView:self] : nil; if (_scroll == scroll) return; [_scroll.scrollViewDelegateSplitter removeDelegate:self]; _scroll = scroll; [scroll.scrollViewDelegateSplitter addDelegate:self]; [self pin]; } // The splitter calls this synchronously, also for the offset changes // maintainVisibleContentPosition makes during a mount. - (void)scrollViewDidScroll:(UIScrollView *)scrollView { [self pin]; } // Fabric moves the view back to its layout when that changes. - (void)updateLayoutMetrics:(const LayoutMetrics &)layoutMetrics oldLayoutMetrics:(const LayoutMetrics &)oldLayoutMetrics { [super updateLayoutMetrics:layoutMetrics oldLayoutMetrics:oldLayoutMetrics]; [self pin]; } // A neighboring header can move or mount while this one is stationary. - (void)mountingTransactionDidMount:(const MountingTransaction &)transaction withSurfaceTelemetry:(const SurfaceTelemetry &)telemetry { [self pin]; } - (void)pin { UIScrollView *scrollView = _scroll.scrollView; UIView *parent = self.superview; if (!scrollView || !parent) return; CGFloat top = _layoutMetrics.frame.origin.y; CGFloat height = _layoutMetrics.frame.size.height; CGFloat y = MAX(top, [parent convertPoint:scrollView.bounds.origin fromView:scrollView].y); for (UIView *view in parent.subviews) { if (view == self || ![view isKindOfClass:EffectUIStickyHeader.class]) continue; CGFloat next = ((EffectUIStickyHeader *)view)->_layoutMetrics.frame.origin.y; if (next > top) y = MIN(y, MAX(top, next - height)); } // Fabric places views by center and bounds, which hold under a transform. CGPoint center = self.center; center.y = y + height * self.layer.anchorPoint.y; if (self.center.y != center.y) self.center = center; } @end