diff --git a/src/components/site/BackToTopFAB.tsx b/src/components/site/BackToTopFAB.tsx new file mode 100644 index 00000000..ebe8945f --- /dev/null +++ b/src/components/site/BackToTopFAB.tsx @@ -0,0 +1,45 @@ +import { SVGProps, useEffect, useState } from "react" + +import { throttle } from "~/lib/utils" + +import { FABBase } from "../ui/FAB" + +function BxBxsArrowToTop(props: SVGProps) { + return ( + + + + ) +} + +const isShouldShow = () => + document.documentElement.scrollTop > document.documentElement.clientHeight + +export const BackToTopFAB: React.FC<{}> = () => { + const [shouldShow, setShouldShow] = useState(isShouldShow()) + useEffect(() => { + const handler = throttle(() => { + setShouldShow(isShouldShow()) + }, 16) + + document.addEventListener("scroll", handler) + return () => { + document.removeEventListener("scroll", handler) + } + }, []) + + return ( + { + window.scrollTo({ + top: 0, + behavior: "smooth", + }) + }} + > + + + ) +} diff --git a/src/components/site/SiteLayout.tsx b/src/components/site/SiteLayout.tsx index 12e95d6d..744758b1 100644 --- a/src/components/site/SiteLayout.tsx +++ b/src/components/site/SiteLayout.tsx @@ -14,6 +14,8 @@ import { useCheckLike, useCheckMint, useGetPage } from "~/queries/page" import { useGetSite, useGetSubscription } from "~/queries/site" import { SEOHead } from "../common/SEOHead" +import { FABContainer } from "../ui/FAB" +import { BackToTopFAB } from "./BackToTopFAB" import { SiteFooter } from "./SiteFooter" import { SiteHeader } from "./SiteHeader" @@ -107,6 +109,10 @@ export const SiteLayout: React.FC = ({ )} + + + + ) } diff --git a/src/components/ui/FAB.tsx b/src/components/ui/FAB.tsx new file mode 100644 index 00000000..908626d4 --- /dev/null +++ b/src/components/ui/FAB.tsx @@ -0,0 +1,142 @@ +import React, { FC, PropsWithChildren, useEffect, useState } from "react" + +import { useGetState } from "~/hooks/useGetState" +import { cn } from "~/lib/utils" + +export interface FABConfig { + id: string + icon: JSX.Element + onClick: () => void +} + +class FABStatic { + private setState: React.Dispatch> | null = + null + register(setter: any) { + this.setState = setter + } + destroy() { + this.setState = null + } + + add(fabConfig: FABConfig) { + if (!this.setState) return + + const id = fabConfig.id + + this.setState((state) => { + if (state.find((config) => config.id === id)) return state + return [...state, fabConfig] + }) + + return () => { + this.remove(fabConfig.id) + } + } + + remove(id: string) { + if (!this.setState) return + this.setState((state) => { + return state.filter((config) => config.id !== id) + }) + } +} + +const fab = new FABStatic() + +export const useFAB = (fabConfig: FABConfig) => { + useEffect(() => { + return fab.add(fabConfig) + }, []) +} + +export const FABBase: FC< + PropsWithChildren< + { + id: string + show?: boolean + children: JSX.Element + } & React.DetailedHTMLProps< + React.ButtonHTMLAttributes, + HTMLButtonElement + > + > +> = (props) => { + const { children, id, show = true, ...extra } = props + const { className, onTransitionEnd, ...rest } = extra + + const [mounted, setMounted] = useState(true) + const [appearTransition, setAppearTransition] = useState(false) + const getMounted = useGetState(mounted) + const handleTransitionEnd: React.TransitionEventHandler = ( + e, + ) => { + onTransitionEnd?.(e) + + !show && setMounted(false) + } + + useEffect(() => { + if (show && !getMounted()) { + setAppearTransition(true) + setMounted(true) + + requestAnimationFrame(() => { + setAppearTransition(false) + }) + } + }, [show]) + + return ( + + ) +} + +export const FABContainer: FC<{ + children: JSX.Element | JSX.Element[] +}> = (props) => { + const [fabConfig, setFabConfig] = useState([]) + useEffect(() => { + fab.register(setFabConfig) + return () => { + fab.destroy() + } + }, []) + + const [serverSide, setServerSide] = useState(true) + + useEffect(() => { + setServerSide(false) + }, []) + + if (serverSide) return null + + return ( +
+ {fabConfig.map((config) => { + const { id, onClick, icon } = config + return ( + + {icon} + + ) + })} + {props.children} +
+ ) +} diff --git a/src/hooks/useDarkMode.ts b/src/hooks/useDarkMode.ts index c3f4b25e..789f663e 100644 --- a/src/hooks/useDarkMode.ts +++ b/src/hooks/useDarkMode.ts @@ -2,6 +2,7 @@ import { useEffect, useRef, useState } from "react" import { create } from "zustand" import { delStorage, getStorage, setStorage } from "~/lib/storage" +import { isServerSide } from "~/lib/utils" import { useGetState } from "./useGetState" @@ -17,8 +18,6 @@ const useMediaStore = create(() => { } }) -const isServerSide = () => typeof window === "undefined" - interface DarkModeConfig { classNameDark?: string // A className to set "dark mode". Default = "dark". classNameLight?: string // A className to set "light mode". Default = "light". diff --git a/src/lib/confetti.ts b/src/lib/confetti.ts index edbba7a9..97cd9694 100644 --- a/src/lib/confetti.ts +++ b/src/lib/confetti.ts @@ -1,7 +1,9 @@ import confetti from "canvas-confetti" +import { isServerSide } from "./utils" + export function showConfetti() { - if (typeof window !== "undefined") { + if (!isServerSide()) { const end = Date.now() + 100 const config: confetti.Options = { particleCount: 25, diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 7576d03d..3a4eac70 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -23,3 +23,19 @@ export function pick(obj: T, keys: K[]): Pick { return acc }, {} as Pick) } + +export const isServerSide = () => typeof window === "undefined" + +export const throttle = (func: Function, limit: number) => { + let inThrottle: boolean + return function () { + const args = arguments + // @ts-ignore + const context = this + if (!inThrottle) { + func.apply(context, args) + inThrottle = true + setTimeout(() => (inThrottle = false), limit) + } + } +}