// adapted from https://github.com/dericgw/react-tiny-fab/blob/master/src/index.tsx import './rtf.css'; import * as React from 'react'; import s from './Fab.module.scss'; const { useState } = React; export function IsFetching({ children }: { children: React.ReactNode }) { return {children}; } export const position = { right: 10, bottom: 10, }; interface ABProps extends React.HTMLAttributes { text?: string; onClick?: (e: React.MouseEvent) => unknown; 'data-testid'?: string; } const AB: React.FC = ({ children, ...p }) => ( ); interface MBProps extends Omit, 'tabIndex'> { tabIndex?: number; } export const MB: React.FC = ({ children, ...p }) => ( ); const defaultStyles: React.CSSProperties = { bottom: 24, right: 24 }; interface FabProps { event?: 'hover' | 'click'; style?: React.CSSProperties; alwaysShowTitle?: boolean; icon?: React.ReactNode; mainButtonStyles?: React.CSSProperties; onClick?: (e: React.MouseEvent) => unknown; text?: string; children?: React.ReactNode; } const Fab: React.FC = ({ event = 'hover', style = defaultStyles, alwaysShowTitle = false, children, icon, mainButtonStyles, onClick, text, ...p }) => { const [isOpen, setIsOpen] = useState(false); const ariaHidden = alwaysShowTitle || !isOpen; const open = () => setIsOpen(true); const close = () => setIsOpen(false); const enter = () => event === 'hover' && open(); const leave = () => event === 'hover' && close(); const toggle = (e: React.MouseEvent) => { if (onClick) { return onClick(e); } e.persist(); return event === 'click' ? (isOpen ? close() : open()) : null; }; const actionOnClick = ( e: React.MouseEvent, userFunc: (e: React.MouseEvent) => unknown ) => { e.persist(); setIsOpen(false); setTimeout(() => { userFunc(e); }, 1); }; const rc = () => React.Children.map(children, (ch, i) => { if (React.isValidElement(ch)) { return (
  • {React.cloneElement(ch, { 'data-testid': `action-button-${i}`, 'aria-label': ch.props.text || `Menu button ${i + 1}`, 'aria-hidden': ariaHidden, tabIndex: isOpen ? 0 : -1, ...ch.props, onClick: (e: React.MouseEvent) => { if (ch.props.onClick) actionOnClick(e, ch.props.onClick); }, })} {ch.props.text && ( {ch.props.text} )}
  • ); } return null; }); return (
    • {icon} {text && ( {text} )}
        {rc()}
    ); }; export { Fab, AB as Action };