MCPcopy Create free account
hub / github.com/dailydotdev/apps / usePassedBlock

Function usePassedBlock

packages/shared/src/components/shell/usePassedBlock.ts:12–49  ·  view source on GitHub ↗
(
  ref: RefObject<HTMLElement>,
  enabled = true,
)

Source from the content-addressed store, hash-verified

10// block solid over its cover until a scroll; so the element is also
11// measured once whenever the page changes size, never on scroll.
12export const usePassedBlock = (
13 ref: RefObject<HTMLElement>,
14 enabled = true,
15): boolean => {
16 const [passed, setPassed] = useState(false);
17 const blockBottom = useShellEdge();
18
19 useEffect(() => {
20 const element = ref.current;
21 if (!enabled || !element || typeof IntersectionObserver === 'undefined') {
22 setPassed(false);
23 return undefined;
24 }
25
26 const observer = new IntersectionObserver(
27 ([entry]) =>
28 setPassed(
29 !entry.isIntersecting && entry.boundingClientRect.top < blockBottom,
30 ),
31 { rootMargin: `-${blockBottom}px 0px 0px 0px` },
32 );
33 observer.observe(element);
34 const settle =
35 typeof ResizeObserver === 'undefined'
36 ? undefined
37 : new ResizeObserver(() =>
38 setPassed(element.getBoundingClientRect().bottom <= blockBottom),
39 );
40 settle?.observe(document.body);
41
42 return () => {
43 observer.disconnect();
44 settle?.disconnect();
45 };
46 }, [ref, enabled, blockBottom]);
47
48 return passed;
49};
50
51// On a thing's page the block stays through the hero and a little past it,
52// so the name, Follow and the segments are seen arriving in it on the way

Callers 6

TagTopicPageFunction · 0.90
PostBodyFunction · 0.90
SquadPageLayoutFunction · 0.90
SquadProfileHeaderFunction · 0.90
ProfilePageFunction · 0.90
SourceShellPageFunction · 0.90

Calls 1

useShellEdgeFunction · 0.90

Tested by

no test coverage detected