-
-
Notifications
You must be signed in to change notification settings - Fork 771
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Innei <tukon479@gmail.com>
- Loading branch information
Showing
6 changed files
with
195 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use client' | ||
|
||
import { useViewport } from '~/atoms' | ||
import { usePageScrollLocationSelector } from '~/providers/root/page-scroll-info-provider' | ||
import { springScrollToTop } from '~/utils/spring' | ||
|
||
import { FABBase } from './FABContainer' | ||
|
||
export const BackToTopFAB = () => { | ||
const windowHeight = useViewport((v) => v.h) | ||
const shouldShow = usePageScrollLocationSelector( | ||
(scrollTop) => { | ||
return scrollTop > windowHeight / 5 | ||
}, | ||
[windowHeight], | ||
) | ||
|
||
return ( | ||
<FABBase | ||
id="to-top" | ||
aria-label="Back to top" | ||
show={shouldShow} | ||
onClick={springScrollToTop} | ||
> | ||
<i className="icon-[mingcute--arow-to-up-line]" /> | ||
</FABBase> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
'use client' | ||
|
||
import React, { useEffect, useState } from 'react' | ||
import type { PropsWithChildren } from 'react' | ||
|
||
import { useStateToRef } from '~/hooks/common/use-state-ref' | ||
import { clsxm } from '~/utils/helper' | ||
|
||
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 = ( | ||
props: PropsWithChildren< | ||
{ | ||
id: string | ||
show?: boolean | ||
children: JSX.Element | ||
} & React.DetailedHTMLProps< | ||
React.ButtonHTMLAttributes<HTMLButtonElement>, | ||
HTMLButtonElement | ||
> | ||
>, | ||
) => { | ||
const { children, show = true, ...extra } = props | ||
const { className, onTransitionEnd, ...rest } = extra | ||
|
||
const [mounted, setMounted] = useState(true) | ||
const [appearTransition, setAppearTransition] = useState(false) | ||
const getMounted = useStateToRef(mounted) | ||
const handleTransitionEnd: React.TransitionEventHandler<HTMLButtonElement> = ( | ||
e, | ||
) => { | ||
onTransitionEnd?.(e) | ||
|
||
!show && setMounted(false) | ||
} | ||
|
||
useEffect(() => { | ||
if (show && !getMounted.current) { | ||
setAppearTransition(true) | ||
setMounted(true) | ||
|
||
requestAnimationFrame(() => { | ||
setAppearTransition(false) | ||
}) | ||
} | ||
}, [show]) | ||
|
||
return ( | ||
<button | ||
className={clsxm( | ||
'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', | ||
(!show || appearTransition) && 'translate-x-[60px]', | ||
!mounted && 'hidden', | ||
className, | ||
)} | ||
onTransitionEnd={handleTransitionEnd} | ||
{...rest} | ||
> | ||
{children} | ||
</button> | ||
) | ||
} | ||
|
||
export const FABContainer = (props: { | ||
children: JSX.Element | JSX.Element[] | ||
}) => { | ||
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 | ||
data-testid="fab-container" | ||
className={clsxm( | ||
'font-lg fixed bottom-4 bottom-[calc(1rem+env(safe-area-inset-bottom))] right-4 z-[9] flex flex-col', | ||
)} | ||
> | ||
{fabConfig.map((config) => { | ||
const { id, onClick, icon } = config | ||
return ( | ||
<FABBase id={id} onClick={onClick} key={id}> | ||
{icon} | ||
</FABBase> | ||
) | ||
})} | ||
{props.children} | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './BackToTopFAB' | ||
export * from './FABContainer' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3742f6c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
springtide – ./
springtide.vercel.app
springtide-git-main-innei.vercel.app
springtide-innei.vercel.app