@wordpress/components
Version:
UI components for WordPress.
210 lines (186 loc) • 6.63 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import { createElement, Fragment } from "@wordpress/element";
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import deprecated from '@wordpress/deprecated';
import { forwardRef } from '@wordpress/element';
import { useInstanceId } from '@wordpress/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"',
version: '6.2'
});
(_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 UnforwardedButton(props, ref) {
var _children$, _children$$props;
const {
__next40pxDefaultSize,
isSmall,
isPressed,
isBusy,
isDestructive,
className,
disabled,
icon,
iconPosition = 'left',
iconSize,
showTooltip,
tooltipPosition,
shortcut,
label,
children,
text,
variant,
__experimentalIsFocusable: isFocusable,
describedBy,
...buttonOrAnchorProps
} = useDeprecatedProps(props);
const {
href,
target,
...additionalProps
} = 'href' in buttonOrAnchorProps ? buttonOrAnchorProps : {
href: undefined,
target: undefined,
...buttonOrAnchorProps
};
const instanceId = useInstanceId(Button, 'components-button__description');
const hasChildren = 'string' === typeof children && !!children || Array.isArray(children) && (children === null || children === void 0 ? void 0 : children[0]) && children[0] !== null && // Tooltip should not considered as a child
(children === null || children === void 0 ? void 0 : (_children$ = children[0]) === null || _children$ === void 0 ? void 0 : (_children$$props = _children$.props) === null || _children$$props === void 0 ? void 0 : _children$$props.className) !== 'components-tooltip';
const classes = classnames('components-button', className, {
'is-next-40px-default-size': __next40pxDefaultSize,
'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 && hasChildren,
'has-icon': !!icon
});
const trulyDisabled = disabled && !isFocusable;
const Tag = href !== undefined && !trulyDisabled ? 'a' : 'button';
const buttonProps = Tag === 'button' ? {
type: 'button',
disabled: trulyDisabled,
'aria-pressed': isPressed
} : {};
const anchorProps = Tag === 'a' ? {
href,
target
} : {};
if (disabled && isFocusable) {
// In this case, the button will be disabled, but still focusable and
// perceivable by screen reader users.
buttonProps['aria-disabled'] = true;
anchorProps['aria-disabled'] = true;
for (const disabledEvent of disabledEventsOnDisabledButton) {
additionalProps[disabledEvent] = event => {
if (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 !== null && children !== void 0 && children.length) && // The tooltip is not explicitly disabled.
false !== showTooltip);
const descriptionId = describedBy ? instanceId : undefined;
const describedById = additionalProps['aria-describedby'] || descriptionId;
const commonProps = {
className: classes,
'aria-label': additionalProps['aria-label'] || label,
'aria-describedby': describedById,
ref
};
const elementChildren = createElement(Fragment, null, icon && iconPosition === 'left' && createElement(Icon, {
icon: icon,
size: iconSize
}), text && createElement(Fragment, null, text), icon && iconPosition === 'right' && createElement(Icon, {
icon: icon,
size: iconSize
}), children);
const element = Tag === 'a' ? createElement("a", _extends({}, anchorProps, additionalProps, commonProps), elementChildren) : createElement("button", _extends({}, buttonProps, additionalProps, commonProps), elementChildren);
if (!shouldShowTooltip) {
return createElement(Fragment, null, element, describedBy && createElement(VisuallyHidden, null, createElement("span", {
id: descriptionId
}, describedBy)));
}
return createElement(Fragment, null, createElement(Tooltip, {
text: children !== null && children !== void 0 && children.length && describedBy ? describedBy : label,
shortcut: shortcut,
position: tooltipPosition
}, element), describedBy && createElement(VisuallyHidden, null, createElement("span", {
id: descriptionId
}, describedBy)));
}
/**
* Lets users take actions and make choices with a single click or tap.
*
* ```jsx
* import { Button } from '@wordpress/components';
* const Mybutton = () => (
* <Button
* variant="primary"
* onClick={ handleClick }
* >
* Click here
* </Button>
* );
* ```
*/
export const Button = forwardRef(UnforwardedButton);
export default Button;
//# sourceMappingURL=index.js.map