File size: 1,657 Bytes
0bfe2e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { cva, VariantProps } from 'class-variance-authority';
import * as React from 'react';
import { Button, ButtonProps } from '.';
import { cn, defineStyleAnatomy } from '../core/styling';

/* -------------------------------------------------------------------------------------------------
 * Anatomy
 * -----------------------------------------------------------------------------------------------*/

export const IconButtonAnatomy = defineStyleAnatomy({
  root: cva('UI-IconButton_root p-0 flex-none', {
    variants: {
      size: {
        xs: 'text-xl h-6 w-6',
        sm: 'text-xl h-8 w-8',
        md: 'text-2xl h-10 w-10',
        lg: 'text-3xl h-12 w-12',
        xl: 'text-4xl h-14 w-14',
      },
    },
    defaultVariants: {
      size: 'md',
    },
  }),
});

/* -------------------------------------------------------------------------------------------------
 * IconButton
 * -----------------------------------------------------------------------------------------------*/

export type IconButtonProps = Omit<
  ButtonProps,
  'leftIcon' | 'rightIcon' | 'iconSpacing' | 'iconClass' | 'children'
> &
  VariantProps<typeof IconButtonAnatomy.root> & {
    icon?: React.ReactNode;
  };

export const IconButton = React.forwardRef<HTMLButtonElement, IconButtonProps>(
  (props, ref) => {
    const { className, icon, size, loading, ...rest } = props;

    return (
      <Button
        className={cn(IconButtonAnatomy.root({ size }), className)}
        loading={loading}
        iconSpacing="0"
        {...rest}
        ref={ref}
      >
        {!loading && icon}
      </Button>
    );
  }
);

IconButton.displayName = 'IconButton';