/
button.tsx
145 lines (132 loc) · 3.78 KB
/
button.tsx
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { useMergeRefs } from "@chakra-ui/react-use-merge-refs"
import {
chakra,
forwardRef,
HTMLChakraProps,
omitThemingProps,
SystemStyleObject,
ThemingProps,
useStyleConfig,
} from "@chakra-ui/system"
import { cx, dataAttr } from "@chakra-ui/shared-utils"
import { useMemo } from "react"
import { useButtonGroup } from "./button-context"
import { ButtonIcon } from "./button-icon"
import { ButtonSpinner } from "./button-spinner"
import { ButtonOptions } from "./button-types"
import { useButtonType } from "./use-button-type"
export interface ButtonProps
extends HTMLChakraProps<"button">,
ButtonOptions,
ThemingProps<"Button"> {}
/**
* Button component is used to trigger an action or event, such as submitting a form, opening a Dialog, canceling an action, or performing a delete operation.
*
* @see Docs https://chakra-ui.com/docs/components/button
* @see WAI-ARIA https://www.w3.org/WAI/ARIA/apg/patterns/button/
*/
export const Button = forwardRef<ButtonProps, "button">((props, ref) => {
const group = useButtonGroup()
const styles = useStyleConfig("Button", { ...group, ...props })
const {
isDisabled = group?.isDisabled,
isLoading,
isActive,
children,
leftIcon,
rightIcon,
loadingText,
iconSpacing = "0.5rem",
type,
spinner,
spinnerPlacement = "start",
className,
as,
...rest
} = omitThemingProps(props)
/**
* When button is used within ButtonGroup (i.e. flushed with sibling buttons),
* it is important to add a `zIndex` on focus.
*
* So let's read the component styles and then add `zIndex` to it.
*/
const buttonStyles: SystemStyleObject = useMemo(() => {
// @ts-ignore
const _focus = { ...styles?.["_focus"], zIndex: 1 }
return {
display: "inline-flex",
appearance: "none",
alignItems: "center",
justifyContent: "center",
userSelect: "none",
position: "relative",
whiteSpace: "nowrap",
verticalAlign: "middle",
outline: "none",
...styles,
...(!!group && { _focus }),
}
}, [styles, group])
const { ref: _ref, type: defaultType } = useButtonType(as)
const contentProps = { rightIcon, leftIcon, iconSpacing, children }
return (
<chakra.button
disabled={isDisabled || isLoading}
ref={useMergeRefs(ref, _ref)}
as={as}
type={type ?? defaultType}
data-active={dataAttr(isActive)}
data-loading={dataAttr(isLoading)}
__css={buttonStyles}
className={cx("chakra-button", className)}
{...rest}
>
{isLoading && spinnerPlacement === "start" && (
<ButtonSpinner
className="chakra-button__spinner--start"
label={loadingText}
placement="start"
spacing={iconSpacing}
>
{spinner}
</ButtonSpinner>
)}
{isLoading ? (
loadingText || (
<chakra.span opacity={0}>
<ButtonContent {...contentProps} />
</chakra.span>
)
) : (
<ButtonContent {...contentProps} />
)}
{isLoading && spinnerPlacement === "end" && (
<ButtonSpinner
className="chakra-button__spinner--end"
label={loadingText}
placement="end"
spacing={iconSpacing}
>
{spinner}
</ButtonSpinner>
)}
</chakra.button>
)
})
Button.displayName = "Button"
type ButtonContentProps = Pick<
ButtonProps,
"leftIcon" | "rightIcon" | "children" | "iconSpacing"
>
function ButtonContent(props: ButtonContentProps) {
const { leftIcon, rightIcon, children, iconSpacing } = props
return (
<>
{leftIcon && <ButtonIcon marginEnd={iconSpacing}>{leftIcon}</ButtonIcon>}
{children}
{rightIcon && (
<ButtonIcon marginStart={iconSpacing}>{rightIcon}</ButtonIcon>
)}
</>
)
}