import { forwardRefWithAs } from '@reach/utils'; import VisuallyHidden from '@reach/visually-hidden'; import { variant as styledSystemVariant } from 'styled-system'; import { Ref } from 'react'; import { useTheme } from 'styled-components'; import { Tooltip, TooltipProps } from '../src/Tooltip'; import { Box } from './Box'; import { Button, ButtonProps, NonSemanticButtonProps } from './Button'; import { SVGComponent } from './shared'; export interface NonSemanticIconButtonProps extends Omit { /** * Unlike usual, this prop only accepts a string. It is also never visible. It represents an accessible label when the * button is not wrapped by a Tooltip. You may ask: "Why not name this prop better, and not use 'children'"?. Many * a11y-first libraries we consume like @reach-ui and @radix-ui have adopted this pattern of expecting required * accessibility text as a 'children' prop. When we use the polymorphic 'as' prop for things like MenuButton, 'children' * would be required. */ children: string; icon: SVGComponent; /** * If you would like for the button to have its own tooltip, you can pass in the necessary props and the tooltip will be rendered immediately outside the IconButton, with the contents of this prop spread onto the Tooltip component. */ tooltip?: Pick & Omit; /** * When this prop is set to `false`, the assistive text is hidden. By default this is `true`. This should only be set to `false` when an iconButton has a parent element that already has a tooltip. In this case, assistive text is not required because a tooltip is present. */ showAssistiveText?: boolean; } export interface IconButtonProps extends Omit, NonSemanticIconButtonProps {} export const IconButton = forwardRefWithAs( ( { as = 'button', children, className, disabled = false, tooltip, icon, isLoading = false, showAssistiveText = true, size = 'medium', tx, type = 'button', variant = 'button-filled-blue', ...restOfProps }: IconButtonProps, ref: Ref, ) => { const theme = useTheme(); const shouldShowAssistiveText = !tooltip && showAssistiveText; const iconButton = ( ); return tooltip ? {iconButton} : iconButton; }, );