-
Notifications
You must be signed in to change notification settings - Fork 1
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
Trying to create "breakpoint" classes on the parentNode #23
Comments
I'll have to double check, but I'm quite sure:
I think your use case could maybe be boiled down to something like this? Which doesn't need refs at all 😄 class ContentWithBreakpoints extends React.Component {
state = {width: 0, height: 0}
_handleReflow = (rect) => {
this.setState({width: rect.width, height: rect.height});
}
render() {
const {children} = this.props;
const {width, height} = this.state;
return (
<div
className={classNames({
SM: true,
MD: width > 300,
LG: width > 500
})}
>
<ResizeObserver onReflow={this._handleReflow}/>
{children}
</div>
);
}
} Then instead of <ContentWithBreakpoints>{children}</ContentWithBreakpoints> Which is probably better anyway because then you have all the control in the world – you can do some prop juggling about what the values for If you don't like the fact there's a wrapper div and you'd rather have the observer injected into whatever component you're rendering, you could probably also swing that with render props. class ContentWithBreakpoints extends React.Component {
state = {width: 0, height: 0}
_handleReflow = (rect) => {
this.setState({width: rect.width, height: rect.height});
}
render() {
const {children} = this.props;
const {width, height} = this.state;
const breakpoints = {
SM: true,
MD: width > 300,
LG: width > 500
};
return (
children({observer: <ResizeObserver onReflow={this._handleReflow}/>, breakpoints});
);
}
} and then: <ContentWithBreakpoints>
{({observer, breakpoints}) => (
<div className={...}>{observer}...</div>
)}
</ContentWithBreakpoints> /cc @10xjs |
I'm trying to implement breakpoints like this article:
https://philipwalton.com/articles/responsive-components-a-solution-to-the-container-queries-problem/#observing-container-resizes
Essentially I want the parentNode object to have classes like .SM .MD .LG depending on how big they are. The problem is accessing either the parentNode or embedding content in the children of the . Neither are easy.
The resize and reflow, only return the rect, which is great, but they could easily return the parentNode as well. This would make it much more useful in this usecase. If you embed content as children like
<ResizeObserver>{children}</ResizeObserver>
then the content isn't passed through.In the version I'm running locally, I just extend the reflow, resize, position to also return the parentNode. would you guys be open to me creating a PR to extend it to return parentNode as well?
The text was updated successfully, but these errors were encountered: