-
Notifications
You must be signed in to change notification settings - Fork 1.2k
✨ [Popover] New mutationObserveConfig prop #4303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
size-limit report
|
bf2e8a7 to
35b2f20
Compare
kyledurand
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code looks good just waiting on the tests for PositionedOverlay. Was there some difficulty testing that component or just haven't gotten to it yet?
UNRELEASED.md
Outdated
|
|
||
| ### Enhancements | ||
|
|
||
| - `Popover` and `PositionedOverlay` can now accept a `mutationObserveConfig` prop ([#4303](https://github.com/Shopify/polaris-react/pull/4303)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| - `Popover` and `PositionedOverlay` can now accept a `mutationObserveConfig` prop ([#4303](https://github.com/Shopify/polaris-react/pull/4303)) | |
| - Added `mutationObserveConfig` prop to `Popover` and `PositionedOverlay` ([#4303](https://github.com/Shopify/polaris-react/pull/4303)) |
Per our guidelines
Past tense verb + brief issue/enhancement description in <ComponentName>
| }); | ||
| }); | ||
|
|
||
| describe('mutationObserveConfig', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anyone know of a good example I can reference for testing a mutation observer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Off the top of my head you might be able to use window.dispatchEvent to trigger this but @AndrewMusgrave might have a more thorough idea on how to test this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we have any example of testing mutation observer. But we might be able to override the prototype & spy on it?
// ⬇️ untested "pseudo" code
let mutationObserverObserveSpy: jest.SpyInstance;
beforeEach(() => {
mutationObserverObserveSpy = jest.spyOn(MutationObserver.prototype, 'observe');
});
afterEach(() => {
mutationObserverObserveSpy.mockRestore();
});
it(...) {
...
expect(mutationObserverObserveSpy).toHaveBeenCalledWith(expect.any(Function), CONFIG)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice - thanks @AndrewMusgrave .
I needed to use the DOM node for the first argument, but other than that, seems like this worked as intended 👍
@kyledurand / @AndrewMusgrave mind giving another review?
Another thing to consider... and @clauderic can chime in on this as well, but perhaps we want listening for attributes changes to be the default behaviour? Maybe we don't even need this new mutationObserveConfig prop? What do you both think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd lean towards listening to attribute mutations by default and not opening this up to consumers.
Polaris components should ideally work well together out of the box without having to dive into complex configuration steps. You should be able to use a Collapsible component within a Popover without any additional config IMO.
If attributes change, it means that the children of the popover have changed for some reason, and should typically be an indication that the layout has changed (for example, if the class attribute changes or the style attribute). Of course there may also be attributes that change that have no impact on the layout.
Listening for attributes by default definitely means that there could potentially be more mutations reported and thus that the logic for measuring / positioning the popover may be called more often. In my opinion that's not a major performance concern so long as the resize / positioning logic is throttled or debounced.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kyledurand / @AndrewMusgrave what are your thoughts on @clauderic comments? Should I go ahead with the PR as is, or should we always observe attribute changes?
Depending on the answer, would we still want to support the new mutationObserveConfig prop? It does allow for more flexibility, but just how valuable of a feature that is for our consumers I don't know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @clauderic. Polaris components should work out of the box. If adding attributes to the config fixes a bug, you shouldn't have to opt into the fix 😄 I don't suspect we'll face a performance issue, but if we do. We can always evaluate making an API change then!
I could not figure out how to properly test this part of the code, and couldn't track down any existing tests for this within Polaris. Any chance you have a good reference to point to? |
9c99ac5 to
7c1feea
Compare
7c1feea to
7b5e3d4
Compare
kyledurand
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I sort of agree with what @clauderic is saying about Polaris components working together by default but it's hard to anticipate everything our consumers will do by combining our components. Polaris doesn't really have anyone working on it at the moment except for the teams that are using it. Adding this logic internally also adds complexity to the already overly complex Popover component. I'm down for you to 🚢 @beefchimi
7b5e3d4 to
2933994
Compare
|
Closing in favour of: #4372 |
This PR adds a new
mutationObserveConfig?: MutationObserverInit;prop to thePositionedOverlaycomponent.Within
Polaris, thePositionedOverlaycomponent is used by bothPopoverOverlayandTooltipOverlay. At the moment, I only have an interest in leveragingmutationObserveConfigforPopover. So, bothPopoverandPopoverOverlayhave been updated to include the newmutationObserveConfigprop. I have leftTooltipOverlayalone for now.The motivation for this PR stems from a need to have dynamically updating content within a
Popoverfor use within Online Store. We are implementing some new UI that will utilize aCollapsiblewithin aPopover. As it stands currently, toggling a nestedCollapsiblewill not always update the dimensions of thePopover. This is because theMutationObserverused withinPositionedOverlayignores everything exceptchildListandsubtree. By opening up theoptionsargument of.observe()for customization, I am now able to pass inattributes: trueas part of theoptionsobject, enabling a re-render of thePopoverwhenCollapsiblechanges.Props go to @AndrewMusgrave for helping me get to this solution.
Playground code
thing.mp4