-
-
Notifications
You must be signed in to change notification settings - Fork 737
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new sticky component to handle stacked stickies
- Loading branch information
Showing
9 changed files
with
221 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { | ||
HTMLAttributes, | ||
ReactNode, | ||
useContext, | ||
useEffect, | ||
useRef, | ||
useState, | ||
} from 'react'; | ||
import { StickyContext } from './StickyContext'; | ||
import { styled } from '@mui/material'; | ||
|
||
const StyledSticky = styled('div', { | ||
shouldForwardProp: (prop) => prop !== 'top', | ||
})<{ top?: number }>(({ theme, top }) => ({ | ||
position: 'sticky', | ||
zIndex: theme.zIndex.sticky - 100, | ||
...(top !== undefined | ||
? { | ||
'&': { | ||
top, | ||
}, | ||
} | ||
: {}), | ||
})); | ||
|
||
interface IStickyProps extends HTMLAttributes<HTMLDivElement> { | ||
children: ReactNode; | ||
} | ||
|
||
export const Sticky = ({ children, ...props }: IStickyProps) => { | ||
const context = useContext(StickyContext); | ||
const ref = useRef<HTMLDivElement>(null); | ||
const [initialTopOffset, setInitialTopOffset] = useState<number | null>( | ||
null, | ||
); | ||
const [top, setTop] = useState<number>(); | ||
|
||
if (!context) { | ||
throw new Error( | ||
'Sticky component must be used within a StickyProvider', | ||
); | ||
} | ||
|
||
const { | ||
registerStickyItem, | ||
unregisterStickyItem, | ||
getTopOffset, | ||
stickyItems, | ||
} = context; | ||
|
||
useEffect(() => { | ||
if (ref.current && initialTopOffset === null) { | ||
setInitialTopOffset( | ||
ref.current | ||
? parseInt( | ||
getComputedStyle(ref.current).getPropertyValue('top'), | ||
) | ||
: 0, | ||
); | ||
} | ||
}, []); | ||
|
||
useEffect(() => { | ||
const resizeObserver = new ResizeObserver(() => { | ||
if (ref.current) { | ||
setTop(getTopOffset(ref) + (initialTopOffset || 0)); | ||
} | ||
}); | ||
|
||
if (ref.current) { | ||
resizeObserver.observe(ref.current); | ||
} | ||
|
||
setTop(getTopOffset(ref) + (initialTopOffset || 0)); | ||
|
||
return () => { | ||
if (ref.current) { | ||
resizeObserver.unobserve(ref.current); | ||
} | ||
}; | ||
}, [stickyItems, initialTopOffset, getTopOffset]); | ||
|
||
useEffect(() => { | ||
registerStickyItem(ref); | ||
|
||
return () => { | ||
unregisterStickyItem(ref); | ||
}; | ||
}, [ref, registerStickyItem, unregisterStickyItem]); | ||
|
||
return ( | ||
<StyledSticky ref={ref} top={top} {...props}> | ||
{children} | ||
</StyledSticky> | ||
); | ||
}; |
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,12 @@ | ||
import { RefObject, createContext } from 'react'; | ||
|
||
interface IStickyContext { | ||
stickyItems: RefObject<HTMLDivElement>[]; | ||
registerStickyItem: (ref: RefObject<HTMLDivElement>) => void; | ||
unregisterStickyItem: (ref: RefObject<HTMLDivElement>) => void; | ||
getTopOffset: (ref: RefObject<HTMLDivElement>) => number; | ||
} | ||
|
||
export const StickyContext = createContext<IStickyContext | undefined>( | ||
undefined, | ||
); |
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, useCallback, ReactNode, RefObject } from 'react'; | ||
import { StickyContext } from './StickyContext'; | ||
|
||
interface IStickyProviderProps { | ||
children: ReactNode; | ||
} | ||
|
||
export const StickyProvider = ({ children }: IStickyProviderProps) => { | ||
const [stickyItems, setStickyItems] = useState<RefObject<HTMLDivElement>[]>( | ||
[], | ||
); | ||
|
||
const registerStickyItem = useCallback( | ||
(item: RefObject<HTMLDivElement>) => { | ||
setStickyItems((prevItems) => { | ||
if (item && !prevItems.includes(item)) { | ||
const newItems = [...prevItems, item]; | ||
return newItems.sort((a, b) => { | ||
const elementA = a.current; | ||
const elementB = b.current; | ||
if (elementA && elementB) { | ||
return ( | ||
elementA.getBoundingClientRect().top - | ||
elementB.getBoundingClientRect().top | ||
); | ||
} | ||
return 0; | ||
}); | ||
} | ||
|
||
return prevItems; | ||
}); | ||
}, | ||
[], | ||
); | ||
|
||
const unregisterStickyItem = useCallback( | ||
(ref: RefObject<HTMLDivElement>) => { | ||
setStickyItems((prev) => prev.filter((item) => item !== ref)); | ||
}, | ||
[], | ||
); | ||
|
||
const getTopOffset = useCallback( | ||
(ref: RefObject<HTMLDivElement>) => { | ||
return stickyItems.some((item) => item === ref) | ||
? stickyItems | ||
.slice( | ||
0, | ||
stickyItems.findIndex((item) => item === ref), | ||
) | ||
.reduce((acc, item) => { | ||
return item === ref | ||
? acc | ||
: acc + | ||
(item.current?.getBoundingClientRect() | ||
.height || 0); | ||
}, 0) | ||
: 0; | ||
}, | ||
[stickyItems], | ||
); | ||
|
||
return ( | ||
<StickyContext.Provider | ||
value={{ | ||
stickyItems, | ||
registerStickyItem, | ||
unregisterStickyItem, | ||
getTopOffset, | ||
}} | ||
> | ||
{children} | ||
</StickyContext.Provider> | ||
); | ||
}; |
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