Skip to content

Commit cdb96be

Browse files
committed
Make useScrollDepth generic over element type
Tests attaching the scroll ref to a <div> failed to type-check because RefObject<HTMLElement> isn't assignable to Ref<HTMLDivElement>.
1 parent dfb46eb commit cdb96be

2 files changed

Lines changed: 5 additions & 5 deletions

File tree

app/web/features/analytics/journeyHooks.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ describe("useScrollDepth", () => {
120120
it("logs max scroll depth on unmount when attached to a container", () => {
121121
// Use a real component so the ref is attached before useEffect runs
122122
function ScrollComponent() {
123-
const scrollRef = useScrollDepth("page.scroll_depth", {
123+
const scrollRef = useScrollDepth<HTMLDivElement>("page.scroll_depth", {
124124
path: "/about",
125125
});
126126
return <div ref={scrollRef} data-testid="scroller" />;
@@ -155,7 +155,7 @@ describe("useScrollDepth", () => {
155155

156156
it("tracks maximum scroll depth, not final position", () => {
157157
function ScrollComponent() {
158-
const scrollRef = useScrollDepth("scroll.depth");
158+
const scrollRef = useScrollDepth<HTMLDivElement>("scroll.depth");
159159
return <div ref={scrollRef} data-testid="scroller" />;
160160
}
161161

@@ -198,7 +198,7 @@ describe("useScrollDepth", () => {
198198

199199
it("does not log if no scrolling occurred", () => {
200200
function ScrollComponent() {
201-
const scrollRef = useScrollDepth("scroll.depth");
201+
const scrollRef = useScrollDepth<HTMLDivElement>("scroll.depth");
202202
return <div ref={scrollRef} data-testid="scroller" />;
203203
}
204204

app/web/features/analytics/journeyHooks.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export function useFunnelStep(
9494
* const scrollRef = useScrollDepth("article.read", { article_id: post.id });
9595
* return <article ref={scrollRef}>...</article>;
9696
*/
97-
export function useScrollDepth(
97+
export function useScrollDepth<T extends HTMLElement = HTMLElement>(
9898
eventType: string,
9999
properties: Record<string, unknown> = {},
100100
) {
@@ -103,7 +103,7 @@ export function useScrollDepth(
103103

104104
const maxDepthRef = useRef(0);
105105
const hasLoggedRef = useRef(false);
106-
const containerRef = useRef<HTMLElement | null>(null);
106+
const containerRef = useRef<T | null>(null);
107107

108108
const logDepth = useCallback(() => {
109109
if (hasLoggedRef.current) return;

0 commit comments

Comments
 (0)