Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
188 additions
and
139 deletions.
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
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
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { useState } from 'react' | ||
|
||
type State = Record<number, number> | ||
|
||
const useCumulativeHeightState = () => { | ||
const [state, set] = useState<State>({}) | ||
|
||
const updateRowHeight = ({ | ||
height, | ||
index, | ||
}: { | ||
height: number | ||
index: number | ||
}) => { | ||
if (state[index] === height) { | ||
return | ||
} | ||
|
||
set(prev => ({ ...prev, [index]: height })) | ||
} | ||
|
||
const getAccumulatedHeight = (index: number) => { | ||
const sortedIndices = Object.keys(state) | ||
.map(key => parseInt(key, 10)) | ||
.sort((a, b) => a - b) | ||
|
||
const indices = sortedIndices.slice(0, sortedIndices.indexOf(index)) | ||
return indices.reduce((acc, cur) => (state[cur] || 0) + acc, 0) | ||
} | ||
|
||
return { | ||
getAccumulatedHeight, | ||
updateRowHeight, | ||
} | ||
} | ||
|
||
export default useCumulativeHeightState |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { useState, useLayoutEffect, useCallback } from 'react' | ||
import throttle from 'throttleit' | ||
|
||
const listeners = new Set<Function>() | ||
|
||
const scrollHandler = throttle(() => { | ||
const scrollOffset = window.pageYOffset | ||
listeners.forEach(fn => fn(scrollOffset)) | ||
}, 100) | ||
|
||
function detach(fn: Function) { | ||
listeners.delete(fn) | ||
if (listeners.size === 0) { | ||
window.removeEventListener('scroll', scrollHandler) | ||
} | ||
} | ||
|
||
function attach(fn: Function) { | ||
if (listeners.size === 0) { | ||
window.addEventListener('scroll', scrollHandler) | ||
} | ||
listeners.add(fn) | ||
return () => detach(fn) | ||
} | ||
|
||
/** | ||
* Hook that handles the scroll position. | ||
* It uses one single window scroll listener. | ||
* @returns {Number} - scroll position value | ||
*/ | ||
export function useScrollThreshold<T extends HTMLElement>({ | ||
offset, | ||
}: { | ||
offset: number | ||
}) { | ||
// Initial element distance to the top of the container | ||
const [initialOffsetTop, setInitialOffsetTop] = useState<number>() | ||
const [hasReachedThreshold, setReachedThreshold] = useState(false) | ||
|
||
// We use this callback as a ref to get the instance of the element once its mounted | ||
// Reference: https://reactjs.org/docs/hooks-faq.html#how-can-i-measure-a-dom-node | ||
const mountedDivRef = useCallback( | ||
(node: T) => { | ||
if (node !== null) { | ||
// `offsetTop` can be used for position:sticky, but not for position:fixed | ||
// If we decide to change the css implementation for a js one, this should be revisited | ||
setInitialOffsetTop(node.offsetTop) | ||
} | ||
}, | ||
// The rule below is disabled because we want to get a new callback every time | ||
// the `offset` changes, which is a way to listen for possible sticky row size changes | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[offset] | ||
) | ||
|
||
useLayoutEffect( | ||
() => | ||
attach((scroll: number) => { | ||
const reached = | ||
initialOffsetTop != null && initialOffsetTop - scroll <= offset | ||
|
||
// checks if reached state has changed | ||
// if not, do nothing | ||
if (hasReachedThreshold === reached) return | ||
|
||
// otherwise, update the state | ||
setReachedThreshold(reached) | ||
}), | ||
[hasReachedThreshold, initialOffsetTop, offset] | ||
) | ||
|
||
return { | ||
hasReachedThreshold, | ||
ref: mountedDivRef, | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
declare module 'throttleit' |