-
Notifications
You must be signed in to change notification settings - Fork 842
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
[EuiDataGrid] Account for horizontal scrollbar when determining height #5478
Merged
chandlerprall
merged 6 commits into
elastic:main
from
chandlerprall:bug/4848-horizontal-scroll-bar
Dec 16, 2021
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3230f05
Account for horizontal scrollbar when determining height of datagrid …
chandlerprall 1986d2b
Merge branch 'main' into bug/4848-horizontal-scroll-bar
chandlerprall 9d2a0ec
Test for presence of horizontal scroll bar when verifying there is no…
chandlerprall 227e2be
changelog
chandlerprall 798daea
Update src/components/datagrid/data_grid.spec.tsx
chandlerprall 7ab607f
Update src/components/datagrid/data_grid.spec.tsx
chandlerprall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,7 @@ import { | |
} from '../data_grid_types'; | ||
import { makeRowManager } from './data_grid_row_manager'; | ||
import { useForceRender } from '../../../services/hooks/useForceRender'; | ||
import { useUpdateEffect } from '../../../services'; | ||
|
||
export const VIRTUALIZED_CONTAINER_CLASS = 'euiDataGrid__virtualized'; | ||
|
||
|
@@ -265,6 +266,8 @@ const useUnconstrainedHeight = ({ | |
defaultHeight, | ||
headerRowHeight, | ||
footerRowHeight, | ||
outerGridRef, | ||
innerGridRef, | ||
}: { | ||
rowHeightUtils: RowHeightUtils; | ||
startRow: number; | ||
|
@@ -274,6 +277,8 @@ const useUnconstrainedHeight = ({ | |
defaultHeight: number; | ||
headerRowHeight: number; | ||
footerRowHeight: number; | ||
outerGridRef: React.MutableRefObject<HTMLDivElement | null>; | ||
innerGridRef: React.MutableRefObject<HTMLDivElement | null>; | ||
}) => { | ||
// when a row height is updated, force a re-render of the grid body to update the unconstrained height | ||
const forceRender = useForceRender(); | ||
|
@@ -307,11 +312,29 @@ const useUnconstrainedHeight = ({ | |
// how many rows to provide space for on the screen | ||
const rowCountToAffordFor = endRow - startRow; | ||
|
||
// watch the inner element for a change to its width | ||
// which may cause the horizontal scrollbar to be added or removed | ||
const { width: innerWidth } = useResizeObserver( | ||
innerGridRef.current, | ||
'width' | ||
); | ||
useUpdateEffect(forceRender, [innerWidth]); | ||
|
||
// https://stackoverflow.com/a/5038256 | ||
const hasHorizontalScroll = | ||
(outerGridRef.current?.scrollWidth ?? 0) > | ||
(outerGridRef.current?.clientWidth ?? 0); | ||
// https://stackoverflow.com/a/24797425 | ||
Comment on lines
+323
to
+327
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Haha, I appreciate the extra context / honesty of adding stack overflow links 😆 |
||
const scrollbarHeight = hasHorizontalScroll | ||
? outerGridRef.current!.offsetHeight - outerGridRef.current!.clientHeight | ||
: 0; | ||
|
||
const unconstrainedHeight = | ||
defaultHeight * (rowCountToAffordFor - knownRowCount) + // guess how much space is required for unknown rows | ||
knownHeight + // computed pixel height of the known rows | ||
headerRowHeight + // account for header | ||
footerRowHeight; // account for footer | ||
footerRowHeight + // account for footer | ||
scrollbarHeight; // account for horizontal scrollbar | ||
|
||
return unconstrainedHeight; | ||
}; | ||
|
@@ -636,6 +659,12 @@ export const EuiDataGridBody: FunctionComponent<EuiDataGridBodyProps> = ( | |
} | ||
}, [getRowHeight]); | ||
|
||
const wrapperRef = useRef<HTMLDivElement | null>(null); | ||
const wrapperDimensions = useResizeObserver(wrapperRef.current); | ||
|
||
const outerGridRef = useRef<HTMLDivElement | null>(null); // container that becomes scrollable | ||
const innerGridRef = useRef<HTMLDivElement | null>(null); // container sized to fit all content | ||
Comment on lines
+665
to
+666
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also appreciate this extra comment context + reorganization |
||
|
||
const unconstrainedHeight = useUnconstrainedHeight({ | ||
rowHeightUtils, | ||
startRow, | ||
|
@@ -645,6 +674,8 @@ export const EuiDataGridBody: FunctionComponent<EuiDataGridBodyProps> = ( | |
defaultHeight, | ||
headerRowHeight, | ||
footerRowHeight, | ||
outerGridRef, | ||
innerGridRef, | ||
}); | ||
|
||
// unable to determine this until the container's size is known anyway | ||
|
@@ -653,11 +684,6 @@ export const EuiDataGridBody: FunctionComponent<EuiDataGridBodyProps> = ( | |
const [height, setHeight] = useState<number | undefined>(undefined); | ||
const [width, setWidth] = useState<number | undefined>(undefined); | ||
|
||
const wrapperRef = useRef<HTMLDivElement | null>(null); | ||
const wrapperDimensions = useResizeObserver(wrapperRef.current); | ||
|
||
const innerGridRef = useRef<HTMLDivElement | null>(null); | ||
|
||
// useState instead of useMemo as React reserves the right to drop memoized | ||
// values in the future, and that would be very bad here | ||
const [rowManager] = useState<EuiDataGridRowManager>(() => | ||
|
@@ -738,6 +764,7 @@ export const EuiDataGridBody: FunctionComponent<EuiDataGridBodyProps> = ( | |
{...(virtualizationOptions ? virtualizationOptions : {})} | ||
ref={setGridRef} | ||
innerElementType={InnerElement} | ||
outerRef={outerGridRef} | ||
innerRef={innerGridRef} | ||
className={VIRTUALIZED_CONTAINER_CLASS} | ||
columnCount={ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Yay, glad this is already coming in handy! 🎉