-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
rm width/height props, use ResizeObserver to get the grid's content dimensions #2130
Conversation
…ensions of the grid
const [gridWidth, setGridWidth] = useState(1); | ||
const [gridHeight, setGridHeight] = useState(1); |
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've set to 1
by default just to avoid divisions by zero possibly breaking things.
const [gridHeight, setGridHeight] = useState(1); | ||
|
||
useLayoutEffect(() => { | ||
const { ResizeObserver } = window as typeof window & { ResizeObserver: ResizeObserver }; |
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 think it's fair to ask devs to use a polyfill to get the grid working, though that'd only be on IE11/old Edge/Safari 13.
Chrome and Firefox have had it for a long time now.
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.
May be we can add a note on the readme file that a polyfill is required for old browsers. Something like https://reactjs.org/docs/javascript-environment-requirements.html
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've added a line in the readme.
const { ResizeObserver } = window as typeof window & { ResizeObserver: ResizeObserver }; | ||
|
||
const resizeObserver = new ResizeObserver(entries => { | ||
const { width, height } = entries[0].contentRect; |
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.
We can now properly support css-only dimensions, custom border sizes for the grid, and custom scrollbar sizes as well.
|
||
document.body.appendChild(scrollDiv); | ||
size = scrollDiv.offsetWidth - scrollDiv.clientWidth; | ||
document.body.removeChild(scrollDiv); |
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.
We won't manually trigger layout/reflow in the middle of rendering now 👌 👌 👌
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.
👏
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.
@nstepien
Could you explain a little bit why we needed this originally? Is it for the scenario when the scroll bar appears and we need to get the new demension?
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.
We needed it to know what the scrollbar size was so we could correctly calculate the actual vertical/horizontal viewport.
@@ -4,6 +4,7 @@ | |||
// https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context | |||
// We set a stacking context so internal elements don't render on top of external components. | |||
contain: strict; | |||
height: 350px; |
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.
Same default as before, users can override however they want with height
/flex/grid/etc.
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.
This is awesome. A few tiny comments. @qili26 do you want to take a look as well?
@@ -3,6 +3,7 @@ | |||
## `alpha` to `canary` | |||
- **Added:** | |||
- **Props:** | |||
- `className` |
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.
Do we want to support the style prop?
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.
Let's add it if there's demand for it, see my opening comment.
const { ResizeObserver } = window as typeof window & { ResizeObserver: ResizeObserver }; | ||
|
||
// don't break in jest/jsdom and browsers that don't support ResizeObserver | ||
if (ResizeObserver == null) return; |
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.
So in this case we render an empty grid? I wonder if we should throw an exception
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.
const [gridHeight, setGridHeight] = useState(1); | ||
|
||
useLayoutEffect(() => { | ||
const { ResizeObserver } = window as typeof window & { ResizeObserver: ResizeObserver }; |
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.
May be we can add a note on the readme file that a polyfill is required for old browsers. Something like https://reactjs.org/docs/javascript-environment-requirements.html
|
||
document.body.appendChild(scrollDiv); | ||
size = scrollDiv.offsetWidth - scrollDiv.clientWidth; | ||
document.body.removeChild(scrollDiv); |
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.
👏
So there is no way of passing in the height variable as a prop? I am using css modules and seems like there is no way to really have a full height container anymore. |
Use the |
I've only added
classNames
to steer devs toward using proper css sizing rather than more js-heavy alternatives.We can always add support for
style={{ width, height }}
if there is demand for it.