fix(ui): back recent header only when stuck
This commit is contained in:
@@ -7,6 +7,37 @@ type Args = {
|
||||
projectHeaderSentinelRefs: React.MutableRefObject<Map<string, HTMLDivElement | null>>;
|
||||
};
|
||||
|
||||
type StickyHeaderArgs = {
|
||||
enabled: boolean;
|
||||
isDesktopShellRuntime: boolean;
|
||||
};
|
||||
|
||||
export const useStickyHeader = (args: StickyHeaderArgs) => {
|
||||
const { enabled, isDesktopShellRuntime } = args;
|
||||
const [sentinel, setSentinel] = React.useState<HTMLDivElement | null>(null);
|
||||
const [isStuck, setIsStuck] = React.useState(false);
|
||||
|
||||
const sentinelRef = React.useCallback((node: HTMLDivElement | null) => {
|
||||
setSentinel(node);
|
||||
}, []);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (!enabled || !isDesktopShellRuntime || !sentinel) {
|
||||
setIsStuck(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const observer = new IntersectionObserver(([entry]) => {
|
||||
setIsStuck(entry.intersectionRatio < 1);
|
||||
}, { threshold: 1 });
|
||||
|
||||
observer.observe(sentinel);
|
||||
return () => observer.disconnect();
|
||||
}, [enabled, isDesktopShellRuntime, sentinel]);
|
||||
|
||||
return { isStuck, sentinelRef };
|
||||
};
|
||||
|
||||
export const useStickyProjectHeaders = (args: Args): Set<string> => {
|
||||
const { enabled = true, isDesktopShellRuntime, projectSections, projectHeaderSentinelRefs } = args;
|
||||
const [stuckProjectHeaders, setStuckProjectHeaders] = React.useState<Set<string>>(new Set());
|
||||
|
||||
Reference in New Issue
Block a user