feat: FAB container and add back to top fab (#415)

This commit is contained in:
Innei
2023-04-24 11:28:03 +01:00
committed by GitHub
parent 8d6a3f1ce1
commit a5aa9d9182
6 changed files with 213 additions and 3 deletions
+45
View File
@@ -0,0 +1,45 @@
import { SVGProps, useEffect, useState } from "react"
import { throttle } from "~/lib/utils"
import { FABBase } from "../ui/FAB"
function BxBxsArrowToTop(props: SVGProps<SVGSVGElement>) {
return (
<svg width="1em" height="1em" viewBox="0 0 24 24" {...props}>
<path d="M6 4h12v2H6zm5 10v6h2v-6h5l-6-6l-6 6z" fill="currentColor" />
</svg>
)
}
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 (
<FABBase
id="to-top"
show={shouldShow}
onClick={() => {
window.scrollTo({
top: 0,
behavior: "smooth",
})
}}
>
<BxBxsArrowToTop />
</FABBase>
)
}
+6
View File
@@ -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<SiteLayoutProps> = ({
</div>
)}
<SiteFooter site={site.data} page={page.data} />
<FABContainer>
<BackToTopFAB />
</FABContainer>
</div>
)
}
+142
View File
@@ -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<React.SetStateAction<FABConfig[]>> | 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>,
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<HTMLButtonElement> = (
e,
) => {
onTransitionEnd?.(e)
!show && setMounted(false)
}
useEffect(() => {
if (show && !getMounted()) {
setAppearTransition(true)
setMounted(true)
requestAnimationFrame(() => {
setAppearTransition(false)
})
}
}, [show])
return (
<button
className={cn(
"bg-white inline-flex w-8 h-8 mt-2 items-center justify-center rounded-md border border-accent opacity-50 hover:opacity-100 focus:opacity-100 focus:outline-none transition-all duration-300",
(!show || appearTransition) && "translate-x-[60px]",
!mounted && "hidden",
className,
)}
onTransitionEnd={handleTransitionEnd}
{...rest}
>
{children}
</button>
)
}
export const FABContainer: FC<{
children: JSX.Element | JSX.Element[]
}> = (props) => {
const [fabConfig, setFabConfig] = useState<FABConfig[]>([])
useEffect(() => {
fab.register(setFabConfig)
return () => {
fab.destroy()
}
}, [])
const [serverSide, setServerSide] = useState(true)
useEffect(() => {
setServerSide(false)
}, [])
if (serverSide) return null
return (
<div
className={cn(
"z-[9] right-4 bottom-4 bottom-[calc(1rem+env(safe-area-inset-bottom))] fixed font-lg flex flex-col",
)}
>
{fabConfig.map((config) => {
const { id, onClick, icon } = config
return (
<FABBase id={id} onClick={onClick} key={id}>
{icon}
</FABBase>
)
})}
{props.children}
</div>
)
}
+1 -2
View File
@@ -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<IMediaStore>(() => {
}
})
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".
+3 -1
View File
@@ -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,
+16
View File
@@ -23,3 +23,19 @@ export function pick<T, K extends keyof T>(obj: T, keys: K[]): Pick<T, K> {
return acc
}, {} as Pick<T, K>)
}
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)
}
}
}