|
| 1 | +'use client' |
| 2 | + |
| 3 | +import React, { useEffect, useState } from 'react' |
| 4 | +import type { PropsWithChildren } from 'react' |
| 5 | + |
| 6 | +import { useStateToRef } from '~/hooks/common/use-state-ref' |
| 7 | +import { clsxm } from '~/utils/helper' |
| 8 | + |
| 9 | +export interface FABConfig { |
| 10 | + id: string |
| 11 | + icon: JSX.Element |
| 12 | + onClick: () => void |
| 13 | +} |
| 14 | + |
| 15 | +class FABStatic { |
| 16 | + private setState: React.Dispatch<React.SetStateAction<FABConfig[]>> | null = |
| 17 | + null |
| 18 | + register(setter: any) { |
| 19 | + this.setState = setter |
| 20 | + } |
| 21 | + destroy() { |
| 22 | + this.setState = null |
| 23 | + } |
| 24 | + |
| 25 | + add(fabConfig: FABConfig) { |
| 26 | + if (!this.setState) return |
| 27 | + |
| 28 | + const id = fabConfig.id |
| 29 | + |
| 30 | + this.setState((state) => { |
| 31 | + if (state.find((config) => config.id === id)) return state |
| 32 | + return [...state, fabConfig] |
| 33 | + }) |
| 34 | + |
| 35 | + return () => { |
| 36 | + this.remove(fabConfig.id) |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + remove(id: string) { |
| 41 | + if (!this.setState) return |
| 42 | + this.setState((state) => { |
| 43 | + return state.filter((config) => config.id !== id) |
| 44 | + }) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +const fab = new FABStatic() |
| 49 | + |
| 50 | +export const useFAB = (fabConfig: FABConfig) => { |
| 51 | + useEffect(() => { |
| 52 | + return fab.add(fabConfig) |
| 53 | + }, []) |
| 54 | +} |
| 55 | + |
| 56 | +export const FABBase = ( |
| 57 | + props: PropsWithChildren< |
| 58 | + { |
| 59 | + id: string |
| 60 | + show?: boolean |
| 61 | + children: JSX.Element |
| 62 | + } & React.DetailedHTMLProps< |
| 63 | + React.ButtonHTMLAttributes<HTMLButtonElement>, |
| 64 | + HTMLButtonElement |
| 65 | + > |
| 66 | + >, |
| 67 | +) => { |
| 68 | + const { children, show = true, ...extra } = props |
| 69 | + const { className, onTransitionEnd, ...rest } = extra |
| 70 | + |
| 71 | + const [mounted, setMounted] = useState(true) |
| 72 | + const [appearTransition, setAppearTransition] = useState(false) |
| 73 | + const getMounted = useStateToRef(mounted) |
| 74 | + const handleTransitionEnd: React.TransitionEventHandler<HTMLButtonElement> = ( |
| 75 | + e, |
| 76 | + ) => { |
| 77 | + onTransitionEnd?.(e) |
| 78 | + |
| 79 | + !show && setMounted(false) |
| 80 | + } |
| 81 | + |
| 82 | + useEffect(() => { |
| 83 | + if (show && !getMounted.current) { |
| 84 | + setAppearTransition(true) |
| 85 | + setMounted(true) |
| 86 | + |
| 87 | + requestAnimationFrame(() => { |
| 88 | + setAppearTransition(false) |
| 89 | + }) |
| 90 | + } |
| 91 | + }, [show]) |
| 92 | + |
| 93 | + return ( |
| 94 | + <button |
| 95 | + className={clsxm( |
| 96 | + 'mt-2 inline-flex h-8 w-8 items-center justify-center rounded-md border border-accent bg-white text-accent opacity-50 transition-all duration-300 hover:opacity-100 focus:opacity-100 focus:outline-none', |
| 97 | + (!show || appearTransition) && 'translate-x-[60px]', |
| 98 | + !mounted && 'hidden', |
| 99 | + className, |
| 100 | + )} |
| 101 | + onTransitionEnd={handleTransitionEnd} |
| 102 | + {...rest} |
| 103 | + > |
| 104 | + {children} |
| 105 | + </button> |
| 106 | + ) |
| 107 | +} |
| 108 | + |
| 109 | +export const FABContainer = (props: { |
| 110 | + children: JSX.Element | JSX.Element[] |
| 111 | +}) => { |
| 112 | + const [fabConfig, setFabConfig] = useState<FABConfig[]>([]) |
| 113 | + useEffect(() => { |
| 114 | + fab.register(setFabConfig) |
| 115 | + return () => { |
| 116 | + fab.destroy() |
| 117 | + } |
| 118 | + }, []) |
| 119 | + |
| 120 | + const [serverSide, setServerSide] = useState(true) |
| 121 | + |
| 122 | + useEffect(() => { |
| 123 | + setServerSide(false) |
| 124 | + }, []) |
| 125 | + |
| 126 | + if (serverSide) return null |
| 127 | + |
| 128 | + return ( |
| 129 | + <div |
| 130 | + data-testid="fab-container" |
| 131 | + className={clsxm( |
| 132 | + 'font-lg fixed bottom-4 bottom-[calc(1rem+env(safe-area-inset-bottom))] right-4 z-[9] flex flex-col', |
| 133 | + )} |
| 134 | + > |
| 135 | + {fabConfig.map((config) => { |
| 136 | + const { id, onClick, icon } = config |
| 137 | + return ( |
| 138 | + <FABBase id={id} onClick={onClick} key={id}> |
| 139 | + {icon} |
| 140 | + </FABBase> |
| 141 | + ) |
| 142 | + })} |
| 143 | + {props.children} |
| 144 | + </div> |
| 145 | + ) |
| 146 | +} |
0 commit comments