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

rm width/height props, use ResizeObserver to get the grid's content dimensions #2130

Merged
merged 3 commits into from
Aug 25, 2020

Conversation

nstepien
Copy link
Contributor

@nstepien nstepien commented Aug 25, 2020

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.

@nstepien nstepien self-assigned this Aug 25, 2020
Comment on lines +18 to +19
const [gridWidth, setGridWidth] = useState(1);
const [gridHeight, setGridHeight] = useState(1);
Copy link
Contributor Author

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 };
Copy link
Contributor Author

@nstepien nstepien Aug 25, 2020

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.

Copy link
Contributor

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

Copy link
Contributor Author

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;
Copy link
Contributor Author

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);
Copy link
Contributor Author

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 👌 👌 👌

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏

Copy link
Contributor

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?

Copy link
Contributor Author

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;
Copy link
Contributor Author

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.

@nstepien nstepien marked this pull request as ready for review August 25, 2020 12:20
@nstepien nstepien requested a review from amanmahajan7 August 25, 2020 12:21
@nstepien nstepien changed the title rm width/height props, use ResizeObserver to get both the content dimensions of the grid rm width/height props, use ResizeObserver to get the grid's content dimensions Aug 25, 2020
Copy link
Contributor

@amanmahajan7 amanmahajan7 left a 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`
Copy link
Contributor

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?

Copy link
Contributor Author

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;
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost.

image

We could log something with console.error, but it'd spam jest tests as well.

const [gridHeight, setGridHeight] = useState(1);

useLayoutEffect(() => {
const { ResizeObserver } = window as typeof window & { ResizeObserver: ResizeObserver };
Copy link
Contributor

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏

@nstepien nstepien merged commit e5d6657 into canary Aug 25, 2020
@nstepien nstepien deleted the ns-dimensions branch August 25, 2020 14:05
@ekeuus
Copy link
Contributor

ekeuus commented Oct 9, 2020

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.

@nstepien
Copy link
Contributor Author

nstepien commented Oct 9, 2020

Use the className or style prop.

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

Successfully merging this pull request may close these issues.

4 participants