Skip to content

Commit

Permalink
[Sticky] Fix Sticky to update items when props change
Browse files Browse the repository at this point in the history
  • Loading branch information
sophschneider committed Apr 24, 2024
1 parent 1fef062 commit d26a954
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/thin-paws-wait.md
@@ -0,0 +1,5 @@
---
'@shopify/polaris': patch
---

Fixed `Sticky` to update sticky items when props change
32 changes: 31 additions & 1 deletion polaris-react/src/components/Sticky/Sticky.tsx
@@ -1,7 +1,7 @@
import React, {Component} from 'react';

import {getRectForNode} from '../../utilities/geometry';
import {useStickyManager} from '../../utilities/sticky-manager';
import {StickyManager, useStickyManager} from '../../utilities/sticky-manager';

interface State {
isSticky: boolean;
Expand Down Expand Up @@ -55,6 +55,36 @@ class StickyInner extends Component<CombinedProps, State> {
});
}

componentDidUpdate() {
const {
boundingElement,
offset = false,
disableWhenStacked = false,
stickyManager,
} = this.props;

if (!this.stickyNode || !this.placeHolderNode) return;

const stickyManagerItem = stickyManager.getStickyItem(this.stickyNode);
const didPropsChange =
!stickyManagerItem ||
boundingElement !== stickyManagerItem.boundingElement ||
offset !== stickyManagerItem.offset ||
disableWhenStacked !== stickyManagerItem.disableWhenStacked;

if (!didPropsChange) return;

stickyManager.unregisterStickyItem(this.stickyNode);
stickyManager.registerStickyItem({
stickyNode: this.stickyNode,
placeHolderNode: this.placeHolderNode,
handlePositioning: this.handlePositioning,
offset,
boundingElement,
disableWhenStacked,
});
}

componentWillUnmount() {
const {stickyManager} = this.props;
if (!this.stickyNode) return;
Expand Down
4 changes: 4 additions & 0 deletions polaris-react/src/utilities/sticky-manager/sticky-manager.ts
Expand Up @@ -67,6 +67,10 @@ export class StickyManager {
this.stickyItems.splice(nodeIndex, 1);
}

getStickyItem(node: HTMLElement) {
return this.stickyItems.find(({stickyNode}) => node === stickyNode);
}

setContainer(el: Document | HTMLElement) {
this.container = el;
if (isDocument(el)) {
Expand Down

0 comments on commit d26a954

Please sign in to comment.