Skip to content

Commit

Permalink
Allow for seeding useVisible with preferred default
Browse files Browse the repository at this point in the history
  • Loading branch information
jb3 committed Sep 11, 2024
1 parent 8a87927 commit 00e71b8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion thallium-frontend/src/components/CartStatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const CartStatus = () => {
const price = cart.cart.reduce((acc, item) => acc + (parseFloat(item.estPrice) * item.quantity), 0);

const staticButtonRef = useRef<HTMLButtonElement>(null);
const buttonVisible = useVisible(staticButtonRef);
const buttonVisible = useVisible(staticButtonRef, true);

const checkoutMsg = total > 0 ? "Confirm & Checkout >" : "Add some items to proceed to checkout";

Expand Down
22 changes: 14 additions & 8 deletions thallium-frontend/src/utils/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import { useState, useEffect, useMemo } from "react";
import { type RefObject } from "react";

export function useVisible(ref: RefObject<HTMLElement>) {
const [isVisible, setVisible] = useState(false);
export function useVisible(ref: RefObject<HTMLElement>, initialState: boolean = false) {

Check failure on line 4 in thallium-frontend/src/utils/hooks.ts

View workflow job for this annotation

GitHub Actions / lint-frontend / lint

Type boolean trivially inferred from a boolean literal, remove type annotation
const [isVisible, setVisible] = useState(initialState);

const intersectionObserver = useMemo(() => {
return new IntersectionObserver(
([entry]) => { setVisible(entry.isIntersecting) },

Check failure on line 9 in thallium-frontend/src/utils/hooks.ts

View workflow job for this annotation

GitHub Actions / lint-frontend / lint

Missing semicolon
{
threshold: 0.5
}
)

Check failure on line 13 in thallium-frontend/src/utils/hooks.ts

View workflow job for this annotation

GitHub Actions / lint-frontend / lint

Missing semicolon
}, [ref]);

Check warning on line 14 in thallium-frontend/src/utils/hooks.ts

View workflow job for this annotation

GitHub Actions / lint-frontend / lint

React Hook useMemo has an unnecessary dependency: 'ref'. Either exclude it or remove the dependency array

useEffect(() => {
const observer = new IntersectionObserver(
([entry]) => { setVisible(entry.isIntersecting); }
);

useEffect(() => {
if (ref.current)
observer.observe(ref.current);
intersectionObserver.observe(ref.current);

return () => { observer.disconnect(); };
return () => { intersectionObserver.disconnect(); };
}, [ref]);

Check warning on line 22 in thallium-frontend/src/utils/hooks.ts

View workflow job for this annotation

GitHub Actions / lint-frontend / lint

React Hook useEffect has a missing dependency: 'intersectionObserver'. Either include it or remove the dependency array

return isVisible;
Expand Down

0 comments on commit 00e71b8

Please sign in to comment.