/
index.js
216 lines (192 loc) · 5.16 KB
/
index.js
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
* Copyright IBM Corp. 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import React, { useRef, useState } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { ChevronDown } from '@carbon/icons-react';
import { IconButton } from '../IconButton';
import Button from '../Button';
import { Menu } from '../Menu';
import { useAttachedMenu } from '../../internal/useAttachedMenu';
import { useId } from '../../internal/useId';
import { useMergedRefs } from '../../internal/useMergedRefs';
import { usePrefix } from '../../internal/usePrefix';
const spacing = 0; // top and bottom spacing between the button and the menu. in px
const defaultTranslations = {
'carbon.combo-button.additional-actions': 'Additional actions',
};
function defaultTranslateWithId(messageId) {
return defaultTranslations[messageId];
}
const ComboButton = React.forwardRef(function ComboButton(
{
children,
className,
disabled,
label,
onClick,
size = 'lg',
menuAlignment = 'bottom',
tooltipAlignment,
translateWithId: t = defaultTranslateWithId,
...rest
},
forwardRef
) {
const id = useId('combobutton');
const prefix = usePrefix();
const containerRef = useRef(null);
const menuRef = useRef(null);
const ref = useMergedRefs([forwardRef, containerRef]);
const [width, setWidth] = useState(0);
const {
open,
x,
y,
handleClick: hookOnClick,
handleMousedown: handleTriggerMousedown,
handleClose,
} = useAttachedMenu(containerRef);
function handleTriggerClick() {
if (containerRef.current) {
const { width: w } = containerRef.current.getBoundingClientRect();
setWidth(w);
hookOnClick();
}
}
function handlePrimaryActionClick(e) {
if (onClick) {
onClick(e);
}
}
function handleOpen() {
menuRef.current.style.inlineSize = `${width}px`;
menuRef.current.style.minInlineSize = `${width}px`;
if (menuAlignment !== 'bottom' && menuAlignment !== 'top') {
menuRef.current.style.inlineSize = `fit-content`;
}
}
const containerClasses = classNames(
`${prefix}--combo-button__container`,
`${prefix}--combo-button__container--${size}`,
{
[`${prefix}--combo-button__container--open`]: open,
},
className
);
const menuClasses = classNames(`${prefix}--combo-button__${menuAlignment}`);
const primaryActionClasses = classNames(
`${prefix}--combo-button__primary-action`
);
const triggerClasses = classNames(`${prefix}--combo-button__trigger`);
return (
<div
{...rest}
className={containerClasses}
ref={ref}
aria-owns={open ? id : null}>
<div className={primaryActionClasses}>
<Button
title={label}
size={size}
disabled={disabled}
onClick={handlePrimaryActionClick}>
{label}
</Button>
</div>
<IconButton
className={triggerClasses}
label={t('carbon.combo-button.additional-actions')}
size={size}
disabled={disabled}
align={tooltipAlignment}
aria-haspopup
aria-expanded={open}
onClick={handleTriggerClick}
onMouseDown={handleTriggerMousedown}
aria-controls={open ? id : null}>
<ChevronDown />
</IconButton>
<Menu
containerRef={containerRef}
menuAlignment={menuAlignment}
className={menuClasses}
ref={menuRef}
id={id}
label={t('carbon.combo-button.additional-actions')}
mode="basic"
size={size}
open={open}
onClose={handleClose}
onOpen={handleOpen}
x={x}
y={[y[0] - spacing, y[1] + spacing]}>
{children}
</Menu>
</div>
);
});
ComboButton.propTypes = {
/**
* A collection of MenuItems to be rendered as additional actions for this ComboButton.
*/
children: PropTypes.node.isRequired,
/**
* Additional CSS class names.
*/
className: PropTypes.string,
/**
* Specify whether the ComboButton should be disabled, or not.
*/
disabled: PropTypes.bool,
/**
* Provide the label to be renderd on the primary action button.
*/
label: PropTypes.string.isRequired,
/**
* Experimental property. Specify how the menu should align with the button element
*/
menuAlignment: PropTypes.oneOf([
'top',
'top-start',
'top-end',
'bottom',
'bottom-start',
'bottom-end',
]),
/**
* Provide an optional function to be called when the primary action element is clicked.
*/
onClick: PropTypes.func,
/**
* Specify the size of the buttons and menu.
*/
size: PropTypes.oneOf(['sm', 'md', 'lg']),
/**
* Specify how the trigger tooltip should be aligned.
*/
tooltipAlignment: PropTypes.oneOf([
'top',
'top-left',
'top-start',
'top-right',
'top-end',
'bottom',
'bottom-left',
'bottom-start',
'bottom-right',
'bottom-end',
'left',
'right',
]),
/**
* Optional method that takes in a message id and returns an
* internationalized string.
*/
translateWithId: PropTypes.func,
};
export { ComboButton };