Skip to content

Commit

Permalink
Add IconButton component
Browse files Browse the repository at this point in the history
  • Loading branch information
Youakeem committed Sep 16, 2024
1 parent a8ee15e commit 77ce6fe
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/ui/src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type BaseProps = {
loading?: boolean
Icon?: ReactNode
iconPosition?: 'left' | 'right'
isIconButton?: boolean
}

export type ButtonProps<C extends React.ElementType> = PolymorphicComponentPropsWithRef<
Expand Down Expand Up @@ -51,6 +52,7 @@ export const Button: PolymorphicComponent = forwardRef(function Button<
children,
Icon,
iconPosition = 'left',
isIconButton,
...props
}: ButtonProps<C>,
ref?: PolymorphicRef<C>,
Expand Down Expand Up @@ -83,7 +85,7 @@ export const Button: PolymorphicComponent = forwardRef(function Button<
>
<span className={childrenWrapper} style={{ opacity: loading ? 0 : 1 }}>
{iconPosition === 'left' && Icon}
{children && <span className={textWrapper}>{children}</span>}
{children && <span className={clsx(!isIconButton && textWrapper)}>{children}</span>}
{iconPosition === 'right' && Icon}
</span>
{hiddenText && hiddenText}
Expand Down
17 changes: 17 additions & 0 deletions packages/ui/src/components/Button/IconButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { type ComponentProps, forwardRef } from 'react'
import { Button } from './Button'

type ButtonProps = Omit<ComponentProps<typeof Button<'button'>>, 'size'>

export const IconButton = forwardRef<HTMLButtonElement, ButtonProps>((props, forwardedRef) => (
<Button
variant="ghost"
size="small"
style={{ padding: '0', aspectRatio: '1', height: 'auto' }}
{...props}
isIconButton
ref={forwardedRef}
/>
))

IconButton.displayName = 'IconButton'
2 changes: 2 additions & 0 deletions packages/ui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export { HedvigLogo } from './components/HedvigLogo/HedvigLogo'
export { HedvigSymbol } from './components/HedvigSymbol/HedvigSymbol'
export { Space } from './components/Space'
export { Button, type ButtonProps } from './components/Button/Button'
export { IconButton } from './components/Button/IconButton'

export { InputBase } from './components/InputBase'
export type { InputBaseProps } from './components/InputBase'
export { Heading } from './components/Heading/Heading'
Expand Down

0 comments on commit 77ce6fe

Please sign in to comment.