Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

TWButton.js

97 lines3.4 KB

1import * as React from 'react';
2import { forwardRefWithAs } from './forwardRefWithAs';
3import cx from 'classnames';
4const mapIntentToColor = {
5 primary: 'blue',
6 success: 'green',
7 danger: 'red',
8 warning: 'yellow'
9};
10export const TWButton = forwardRefWithAs(({
11 as: is = 'button',
12 children,
13 color,
14 intent = 'none',
15 size = 'md',
16 className,
17 icon,
18 minimal,
19 onClick,
20 iconRight,
21 ...props
22}, ref) => {
23 const tailwindColor = mapIntentToColor[intent] ?? color;
24 const iconIsOnlyChild = !!(icon && !iconRight && !children || iconRight && !icon && !children);
25 let sizeClasses;
26 let iconClasses;
27​
28 if (size === 'xs') {
29 sizeClasses = 'text-xs rounded-md leading-4';
30 sizeClasses += iconIsOnlyChild ? ' p-1.5' : ' py-1.5 px-2.5';
31 iconClasses = 'h-3 w-3';
32 }
33​
34 if (size === 'sm') {
35 sizeClasses = 'text-sm rounded-md leading-4';
36 sizeClasses += iconIsOnlyChild ? ' p-2' : ' py-2 px-3';
37 iconClasses = 'h-3 w-3';
38 }
39​
40 if (size === 'md') {
41 sizeClasses = 'text-sm rounded-md leading-5';
42 sizeClasses += iconIsOnlyChild ? ' p-2' : ' py-2 px-4';
43 iconClasses = 'h-5 w-5';
44 }
45​
46 if (size === 'lg') {
47 sizeClasses = 'text-base rounded-md leading-6';
48 sizeClasses += iconIsOnlyChild ? ' p-2' : ' py-2 px-4';
49 iconClasses = 'h-6 w-6';
50 }
51​
52 if (size === 'xl') {
53 sizeClasses = 'text-base rounded-md leading-6';
54 sizeClasses += iconIsOnlyChild ? ' p-3' : ' py-3 px-6';
55 iconClasses = 'h-6 w-6';
56 }
57​
58 const leftIcon = icon ? React.cloneElement(icon, { ...icon.props,
59 height: '1em',
60 width: '1em',
61 className: cx('block', {
62 'text-white': !!tailwindColor && !minimal,
63 [`text-${tailwindColor}-700`]: !!tailwindColor && !!minimal,
64 'text-gray-600': !tailwindColor,
65 '-ml-1 mr-2': !iconIsOnlyChild
66 }, iconClasses, icon?.props.className)
67 }) : null;
68 const rightIcon = iconRight ? React.cloneElement(iconRight, { ...iconRight.props,
69 height: '1em',
70 width: '1em',
71 className: cx('block ', {
72 'text-white': !!tailwindColor && !minimal,
73 [`text-${tailwindColor}-700`]: !!tailwindColor && !!minimal,
74 'text-gray-600': !tailwindColor,
75 '-mr-1 ml-2': !iconIsOnlyChild
76 }, iconClasses, iconRight?.props.className)
77 }) : null;
78 const propsToPass = { ...props,
79 className: cx('font-medium inline-flex items-center justify-center focus:outline-none transition duration-150 ease-in-out', {
80 [`shadow-xs border border-transparent text-white bg-${tailwindColor}-600 hover:bg-${tailwindColor}-500 focus:border-${tailwindColor}-700 focus:shadow-outline-${tailwindColor} active:border-${tailwindColor}-700`]: !!tailwindColor && !minimal,
81 [`shadow-xs border border-gray-300 text-gray-700 bg-white hover:text-gray-500 focus:shadow-outline-blue focus:border-blue-300 active:text-gray-800 active:bg-gray-50`]: !tailwindColor && !minimal,
82 [`hover:bg-${tailwindColor}-100 active:bg-${tailwindColor}-200 text-${tailwindColor}-700 focus:shadow-outline-${tailwindColor} border-transparent`]: minimal && tailwindColor,
83 [`hover:bg-gray-200 active:bg-gray-300 text-gray-900 focus:shadow-outline-blue border-transparent`]: minimal && !tailwindColor
84 }, sizeClasses, className),
85 children: <>
86 {leftIcon}
87 {children}
88 {rightIcon}
89 </>
90 };
91 return React.createElement(is, { ...propsToPass,
92 ref,
93 onClick,
94 className: propsToPass.className
95 });
96});
97TWButton.displayName = 'TWButton';