@gechiui/components
Version:
UI components for GeChiUI.
175 lines (153 loc) • 5.07 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import { createElement, Fragment } from "@gechiui/element";
// @ts-nocheck
/**
* External dependencies
*/
import classnames from 'classnames';
import { isArray } from 'lodash';
/**
* GeChiUI dependencies
*/
import deprecated from '@gechiui/deprecated';
import { forwardRef } from '@gechiui/element';
import { useInstanceId } from '@gechiui/compose';
/**
* Internal dependencies
*/
import Tooltip from '../tooltip';
import Icon from '../icon';
import { VisuallyHidden } from '../visually-hidden';
const disabledEventsOnDisabledButton = ['onMouseDown', 'onClick'];
function useDeprecatedProps(_ref) {
let {
isDefault,
isPrimary,
isSecondary,
isTertiary,
isLink,
variant,
...otherProps
} = _ref;
let computedVariant = variant;
if (isPrimary) {
var _computedVariant;
(_computedVariant = computedVariant) !== null && _computedVariant !== void 0 ? _computedVariant : computedVariant = 'primary';
}
if (isTertiary) {
var _computedVariant2;
(_computedVariant2 = computedVariant) !== null && _computedVariant2 !== void 0 ? _computedVariant2 : computedVariant = 'tertiary';
}
if (isSecondary) {
var _computedVariant3;
(_computedVariant3 = computedVariant) !== null && _computedVariant3 !== void 0 ? _computedVariant3 : computedVariant = 'secondary';
}
if (isDefault) {
var _computedVariant4;
deprecated('Button isDefault prop', {
since: '5.4',
alternative: 'variant="secondary"'
});
(_computedVariant4 = computedVariant) !== null && _computedVariant4 !== void 0 ? _computedVariant4 : computedVariant = 'secondary';
}
if (isLink) {
var _computedVariant5;
(_computedVariant5 = computedVariant) !== null && _computedVariant5 !== void 0 ? _computedVariant5 : computedVariant = 'link';
}
return { ...otherProps,
variant: computedVariant
};
}
export function Button(props, ref) {
const {
href,
target,
isSmall,
isPressed,
isBusy,
isDestructive,
className,
disabled,
icon,
iconPosition = 'left',
iconSize,
showTooltip,
tooltipPosition,
shortcut,
label,
children,
text,
variant,
__experimentalIsFocusable: isFocusable,
describedBy,
...additionalProps
} = useDeprecatedProps(props);
const instanceId = useInstanceId(Button, 'components-button__description');
const classes = classnames('components-button', className, {
'is-secondary': variant === 'secondary',
'is-primary': variant === 'primary',
'is-small': isSmall,
'is-tertiary': variant === 'tertiary',
'is-pressed': isPressed,
'is-busy': isBusy,
'is-link': variant === 'link',
'is-destructive': isDestructive,
'has-text': !!icon && !!children,
'has-icon': !!icon
});
const trulyDisabled = disabled && !isFocusable;
const Tag = href !== undefined && !trulyDisabled ? 'a' : 'button';
const tagProps = Tag === 'a' ? {
href,
target
} : {
type: 'button',
disabled: trulyDisabled,
'aria-pressed': isPressed
};
if (disabled && isFocusable) {
// In this case, the button will be disabled, but still focusable and
// perceivable by screen reader users.
tagProps['aria-disabled'] = true;
for (const disabledEvent of disabledEventsOnDisabledButton) {
additionalProps[disabledEvent] = event => {
event.stopPropagation();
event.preventDefault();
};
}
} // Should show the tooltip if...
const shouldShowTooltip = !trulyDisabled && ( // an explicit tooltip is passed or...
showTooltip && label || // there's a shortcut or...
shortcut || // there's a label and...
!!label && ( // the children are empty and...
!children || isArray(children) && !children.length) && // the tooltip is not explicitly disabled.
false !== showTooltip);
const descriptionId = describedBy ? instanceId : null;
const describedById = additionalProps['aria-describedby'] || descriptionId;
const element = createElement(Tag, _extends({}, tagProps, additionalProps, {
className: classes,
"aria-label": additionalProps['aria-label'] || label,
"aria-describedby": describedById,
ref: ref
}), icon && iconPosition === 'left' && createElement(Icon, {
icon: icon,
size: iconSize
}), text && createElement(Fragment, null, text), icon && iconPosition === 'right' && createElement(Icon, {
icon: icon,
size: iconSize
}), children);
if (!shouldShowTooltip) {
return createElement(Fragment, null, element, describedBy && createElement(VisuallyHidden, null, createElement("span", {
id: descriptionId
}, describedBy)));
}
return createElement(Fragment, null, createElement(Tooltip, {
text: describedBy ? describedBy : label,
shortcut: shortcut,
position: tooltipPosition
}, element), describedBy && createElement(VisuallyHidden, null, createElement("span", {
id: descriptionId
}, describedBy)));
}
export default forwardRef(Button);
//# sourceMappingURL=index.js.map