import React, {useCallback, useMemo} from 'react'; import {handleButtonPointerFocus} from '../helpers/button-pointer-focus'; import { CURRENT_COLOR, LIGHT_TEXT, TRANSPARENT, WHITE, getBackgroundFromHoverState, } from '../helpers/colors'; import {HOVERABLE_CLASS_NAME, hoverableStyle} from '../helpers/hoverable'; import {useZIndex} from '../state/z-index'; export type RenderInlineAction = (color: string) => React.ReactNode; export type InlineActionProps = Omit< React.ButtonHTMLAttributes, 'children' | 'onClick' | 'style' | 'title' > & { readonly onClick: React.MouseEventHandler; readonly renderAction: RenderInlineAction; readonly hoveredColor?: string; readonly unhoveredColor?: string; readonly variant: 'compact' | 'modal-header' | null; readonly style?: React.CSSProperties; }; export const InlineAction = ({ renderAction, onClick, disabled, hoveredColor = WHITE, unhoveredColor = LIGHT_TEXT, variant, style: customStyle, className, onPointerDown: onPointerDownProp, ...buttonProps }: InlineActionProps) => { const {tabIndex} = useZIndex(); const onPointerDown = useCallback( (event: React.PointerEvent) => { onPointerDownProp?.(event); handleButtonPointerFocus(event); }, [onPointerDownProp], ); const style: React.CSSProperties = useMemo(() => { return { border: 'none', height: variant === 'modal-header' ? 30 : 24, width: variant === 'compact' ? 14 : variant === 'modal-header' ? 30 : 24, padding: variant === 'modal-header' ? 3 : 0, display: 'inline-flex', justifyContent: 'center', alignItems: 'center', borderRadius: 3, opacity: disabled ? 0.5 : 1, pointerEvents: disabled ? 'none' : 'auto', ...hoverableStyle({ idleBackground: TRANSPARENT, hoverBackground: disabled ? TRANSPARENT : getBackgroundFromHoverState({hovered: true, selected: false}), idleColor: unhoveredColor, hoverColor: disabled ? unhoveredColor : hoveredColor, }), ...customStyle, }; }, [customStyle, disabled, hoveredColor, unhoveredColor, variant]); return ( ); };