import iconURL from 'assets/icons/Gitako.png' import { useConfigs } from 'containers/ConfigsContext' import * as React from 'react' import { useDebounce, useWindowSize } from 'react-use' import { cx } from 'utils/cx' import { Icon } from './Icon' import { useResizeHandler } from './ResizeHandler' type Props = { error?: string | null className?: React.HTMLAttributes['className'] onHover?: React.HTMLAttributes['onMouseEnter'] onClick?: (e: PointerEvent) => void } const buttonHeight = 42 function getSafeDistance(y: number, height: number) { return Math.max(0, Math.min(y, height - buttonHeight)) } export function ToggleShowButton({ error, className, onClick, onHover }: Props) { const ref = React.useRef(null) const config = useConfigs() const [distance, setDistance] = React.useState(config.value.toggleButtonVerticalDistance) const { height } = useWindowSize() React.useEffect(() => { // make sure it is inside viewport const safeDistance = getSafeDistance(distance, height) if (safeDistance !== distance) setDistance(safeDistance) }, [height, distance]) // updating context useDebounce( () => config.onChange({ toggleButtonVerticalDistance: distance }), // too slow 100, [distance], ) React.useEffect(() => { if (ref.current) { ref.current.style.top = distance + 'px' } }, []) const { onPointerDown } = useResizeHandler( [distance, distance], ([, y]) => { const distance = getSafeDistance(y, height) setDistance(distance) if (ref.current) { ref.current.style.top = distance + 'px' } }, { onClick }, ) return (
{error && {error}}
) }