Skip to content

Commit

Permalink
feat: add useOverflow & useScroll hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-welker committed May 22, 2024
1 parent c0650bb commit 9b06383
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/lib/shared/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ export * from "./useGetDeviceStateFromRoomConfiguration";
export * from "./usePressHoldRelease";
export * from "./useRoomIBasicVolumeWithFeedback";
export * from "./useTimeAndDate";
export * from "./useOverflow";
export * from "./useScroll";
36 changes: 36 additions & 0 deletions src/lib/shared/hooks/useOverflow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { RefObject, useLayoutEffect, useState } from "react";

export function useOverflow<T extends HTMLElement | undefined>(ref:RefObject<T | undefined>, callback?:(hasOverflowVertical: boolean, hasOverflowHorizontal: boolean) => void):UseIsOverflowProps {
const [overflowHorizontal, setOverflowHorizontal] = useState(false);
const [overflowVertical, setOverflowVertical] = useState(false);


useLayoutEffect(() => {
const { current } = ref;

const trigger = () => {
const hasOverflowVertical = current && current.scrollHeight > current.clientHeight;
const hasOverflowHorizontal = current && current.scrollWidth > current.clientWidth;

setOverflowVertical(hasOverflowVertical ?? false);
setOverflowHorizontal(hasOverflowHorizontal ?? false);

if (!callback) return;

callback(hasOverflowVertical ?? false, hasOverflowHorizontal ?? false);
}

if (!current) return;

trigger();
}, [ref, callback])

return { overflowHorizontal, overflowVertical };
}

export interface UseIsOverflowProps {
overflowHorizontal: boolean;
overflowVertical: boolean;
}

export default useOverflow;
55 changes: 55 additions & 0 deletions src/lib/shared/hooks/useScroll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { RefObject, useLayoutEffect, useState } from "react";

export function useScroll<T extends HTMLElement | undefined>(ref: RefObject<T | undefined>): UseScrollProps {

const [horizontalScrollPosition, setHorizontalScrollPosition] = useState(ref?.current?.scrollLeft ?? 0);
const [verticalScrollPosition, setVerticalScrollPosition] = useState(ref?.current?.scrollTop ?? 0);

const scrollHorizontal = (increment: number) => {
const { current } = ref;

if (!current) return;

console.log(current.scrollLeft);

current.scrollLeft += increment;

console.log(current.scrollLeft);
}

const scrollVertical = (increment: number) => {
const { current } = ref;

if (!current) return;

console.log(current.scrollTop);

current.scrollTop += increment;

console.log(current.scrollTop);
}

useLayoutEffect(() => {
const { current } = ref;

const trigger = () => {
setHorizontalScrollPosition(current?.scrollLeft ?? 0);
setVerticalScrollPosition(current?.scrollTop ?? 0);
}

if (!current) return;

trigger();
}, [ref])

return {horizontalScrollPosition, verticalScrollPosition, scrollHorizontal, scrollVertical};
}

export default useScroll;

export interface UseScrollProps {
scrollHorizontal: (increment: number) => void;
scrollVertical: (increment: number) => void;
horizontalScrollPosition: number;
verticalScrollPosition: number;
}

0 comments on commit 9b06383

Please sign in to comment.