Skip to content

Commit

Permalink
Revert useWindow fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mdodgelooker committed Jan 8, 2021
1 parent f773299 commit 76eb23d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/components/src/Menu/MenuList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export const MenuListInternal = forwardRef(
return (child: ReactChild) => getMenuGroupHeight(child, compact)
}, [windowing, childArray, compact])

const { content, ref } = useWindow<HTMLUListElement>({
const { content, ref } = useWindow({
childHeight: childHeight,
children: children as JSX.Element | JSX.Element[],
enabled: windowing !== 'none',
Expand Down
26 changes: 17 additions & 9 deletions packages/components/src/utils/useWindow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ interface WindowHeightPayloadObject {
}

interface WindowHeightAction {
type: 'CHANGE'
payload: WindowHeightPayloadObject
type: 'CHANGE' | 'RESET'
payload: number | WindowHeightPayloadObject
}

const initialState = {
const initialState = (length: number) => ({
afterHeight: 0,
beforeHeight: 0,
end: 0,
end: length - 1,
scrollBottom: 0,
scrollTop: 0,
start: 0,
}
})
const bufferHeight = 1000

// For windowing lists with variable item height, a reducer that derives
Expand All @@ -81,6 +81,11 @@ const reducer: Reducer<WindowHeightState, WindowHeightAction> = (
state,
action
) => {
// RESET (if disabled after being enabled)
if (typeof action.payload === 'number') {
return initialState(action.payload)
}

let { beforeHeight, afterHeight, start, end } = state
const {
scrollPosition,
Expand Down Expand Up @@ -178,7 +183,10 @@ export const useWindow = <E extends HTMLElement = HTMLElement>({
const scrollPosition = useScrollPosition(containerElement)

// For variable childHeight
const [variable, dispatch] = useReducer(reducer, initialState)
const [variable, dispatch] = useReducer(
reducer,
initialState(childArray.length)
)
useEffect(() => {
// If using fixed childHeight, totalHeight will be 0
if (totalHeight > 0) {
Expand All @@ -192,6 +200,8 @@ export const useWindow = <E extends HTMLElement = HTMLElement>({
},
type: 'CHANGE',
})
} else {
dispatch({ payload: childHeightLadder.length, type: 'RESET' })
}
}
}, [enabled, childHeightLadder, height, scrollPosition, totalHeight])
Expand Down Expand Up @@ -226,14 +236,12 @@ export const useWindow = <E extends HTMLElement = HTMLElement>({

return {
containerElement,
content: enabled ? (
content: (
<>
{before}
{childArray.slice(start, end + 1)}
{after}
</>
) : (
childArray
),
ref: callbackRef,
}
Expand Down

0 comments on commit 76eb23d

Please sign in to comment.