import React, { MouseEvent, PropsWithoutRef, RefAttributes, useImperativeHandle, useMemo, useRef } from 'react' import css from 'styled-jsx/css' import useTheme from '../use-theme' import { useModalContext } from './modal-context' import Button, { ButtonProps } from '../button/button' type ModalActionEvent = MouseEvent & { close: () => void } interface Props { className?: string passive?: boolean disabled?: boolean onClick?: (event: ModalActionEvent) => void } const defaultProps = { className: '', passive: false, disabled: false } export type ModalActionProps = Props & typeof defaultProps & Omit const ModalAction = React.forwardRef>( ( { className, children, onClick, passive, disabled, ...props }, ref: React.Ref ) => { const theme = useTheme() const btnRef = useRef(null) const { close } = useModalContext() useImperativeHandle(ref, () => btnRef.current) const clickHandler = (event: MouseEvent) => { if (disabled) return const actionEvent = Object.assign({}, event, { close: () => close && close() }) onClick && onClick(actionEvent) } const color = useMemo(() => { return passive ? theme.palette.accents_10 : theme.palette.foreground }, [theme.palette, passive]) const bgColor = useMemo(() => { return disabled ? theme.palette.accents_1 : theme.palette.background }, [theme.palette, disabled]) const { className: resolveClassName, styles } = css.resolve` button.btn { font-size: 0.75rem; border: none; color: ${color}; background-color: ${theme.palette.background}; display: flex; -webkit-box-align: center; align-items: center; -webkit-box-pack: center; justify-content: center; flex: 1; height: 100%; border-radius: 0; min-width: 0; } button.btn:hover, button.btn:focus { color: ${disabled ? color : theme.palette.foreground}; background-color: ${disabled ? bgColor : theme.palette.accents_1}; } ` const overrideProps = { ...props, effect: false, ref: btnRef } return ( ) } ) type ModalActionComponent = React.ForwardRefExoticComponent< PropsWithoutRef

& RefAttributes > type ComponentProps = Partial & Omit & Partial> ModalAction.defaultProps = defaultProps export default ModalAction as ModalActionComponent