Skip to content
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

[responsive] manually trigger resize #192

Closed
techniq opened this issue Nov 7, 2017 · 10 comments · Fixed by #198
Closed

[responsive] manually trigger resize #192

techniq opened this issue Nov 7, 2017 · 10 comments · Fixed by #198

Comments

@techniq
Copy link
Collaborator

techniq commented Nov 7, 2017

Currently when using withParentSize, if the parent changes size NOT from resizing the window (ex. show/hide side drawer that resizes the width of the main content), the width is not recalculated.

Need a way to force the component call resize() (based on a prop change, or expose the actual resize function to the user). Maybe if we use a render function (which would be nice), something like...

<ParentSize>
{({ parentWidth, parentHeight, resize }) => (
  ...
  <button onClick={resize} />
)}
</ParentSize>

or maybe add a componentWillReceiveProps to ParentSize and any prop change will cause a resize to be called. In my case, I have a drawerWidth prop that could trigger this.

<ParentSize drawerWidth={drawerWidth}>...</ParentSize>
@techniq
Copy link
Collaborator Author

techniq commented Nov 7, 2017

react-resize-detector looks to support this, although it appears to have an issue

@williaster
Copy link
Collaborator

@conglei and I recently ran into this in another project, and have an implementation that passes the resize callback to the child function/component as you suggested that we can port over here.

react-resize-detector is interesting. perhaps we could have an implementation that adds the window scroll listeners on top of window.resize + passes resize for maximum flexibility/utility?

@techniq
Copy link
Collaborator Author

techniq commented Nov 7, 2017 via email

@hshoff
Copy link
Member

hshoff commented Nov 7, 2017

good ideas for making this more robust. I ran into this myself the other day. Another idea would be a Mutation Observer that watches the target node's dimensions and triggers the rerender on change. Just an idea though, I haven't investigated it yet https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver

@techniq
Copy link
Collaborator Author

techniq commented Nov 7, 2017

@hshoff I didn't know if a Mutation Observer would detect it's parents dimensions changing (and thus it's own) or just the contents/attributes/children of an element/component (I've not used Mutation Observers myself yet but have read about them.

@techniq
Copy link
Collaborator Author

techniq commented Nov 7, 2017

We could use a ResizerObserver, but it would require a polyfill as it's not supported on any browser yet

@techniq
Copy link
Collaborator Author

techniq commented Nov 7, 2017

See the note at the bottom of the Google Developers article about polyfilling

screen shot 2017-11-07 at 11 55 36 am

@hshoff hshoff changed the title Refreshing manually refreshing parent size Nov 8, 2017
hshoff added a commit that referenced this issue Nov 13, 2017
 [responsive] add `<ParentSize />` component. fixes #192
@techniq
Copy link
Collaborator Author

techniq commented Nov 14, 2017

fyi, I noticed the WithParentSize HOC doesn't use the new <ParentSize /> component so it doesn't resize on DOM changes

@williaster
Copy link
Collaborator

do you think there's still a need for the HOC? or should we deprecate it and just use <ParentSize /> going forward?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants