UNPKG

@wordpress/components

Version:
170 lines (169 loc) • 6.11 kB
import clsx from "clsx"; import deprecated from "@wordpress/deprecated"; import { Children, Fragment, forwardRef, isValidElement } from "@wordpress/element"; import { useInstanceId } from "@wordpress/compose"; import Tooltip from "../tooltip/index.mjs"; import Icon from "../icon/index.mjs"; import { VisuallyHidden } from "../visually-hidden/index.mjs"; import { positionToPlacement } from "../popover/utils.mjs"; import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime"; // packages/components/src/button/index.tsx const disabledEventsOnDisabledButton = ["onMouseDown", "onClick"]; /** * Whether `children` will render any content, to differentiate an icon-only * button or an icon-with-text button. * * `Children.toArray` discards the children that render nothing (`null`, * `undefined`, and booleans), which is what conditionally rendered content * evaluates to when its condition is unmet. * * @param children The children passed to the button. */ const hasRenderableChildren = (children) => Children.toArray(children).some((child) => { if (!isValidElement(child)) return child !== ""; if (child.type === Fragment) return hasRenderableChildren(child.props.children); return child.props.className !== "components-tooltip"; }); function useDeprecatedProps({ __experimentalIsFocusable, isDefault, isPrimary, isSecondary, isTertiary, isLink, isPressed, isSmall, size, variant, describedBy, ...otherProps }) { let computedSize = size; let computedVariant = variant; const newProps = { accessibleWhenDisabled: __experimentalIsFocusable, "aria-pressed": isPressed, description: describedBy }; if (isSmall) computedSize ??= "small"; if (isPrimary) computedVariant ??= "primary"; if (isTertiary) computedVariant ??= "tertiary"; if (isSecondary) computedVariant ??= "secondary"; if (isDefault) { deprecated("wp.components.Button `isDefault` prop", { since: "5.4", alternative: "variant=\"secondary\"" }); computedVariant ??= "secondary"; } if (isLink) computedVariant ??= "link"; return { ...newProps, ...otherProps, size: computedSize, variant: computedVariant }; } /** * 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> * ); * ``` */ const Button = forwardRef(function UnforwardedButton(props, ref) { const { __next40pxDefaultSize, accessibleWhenDisabled, isBusy, isDestructive, className, disabled, icon, iconPosition = "left", iconSize, showTooltip, tooltipPosition, shortcut, label, children, size = "default", text, variant, description, ...buttonOrAnchorProps } = useDeprecatedProps(props); const { href, target, "aria-checked": ariaChecked, "aria-pressed": ariaPressed, "aria-selected": ariaSelected, ...additionalProps } = "href" in buttonOrAnchorProps ? buttonOrAnchorProps : { href: void 0, target: void 0, ...buttonOrAnchorProps }; const instanceId = useInstanceId(Button, "components-button__description"); const classes = clsx("components-button", className, { "is-next-40px-default-size": __next40pxDefaultSize, "is-secondary": variant === "secondary", "is-primary": variant === "primary", "is-small": size === "small", "is-compact": size === "compact", "is-tertiary": variant === "tertiary", "is-pressed": [ true, "true", "mixed" ].includes(ariaPressed), "is-pressed-mixed": ariaPressed === "mixed", "is-busy": isBusy, "is-link": variant === "link", "is-destructive": isDestructive, "has-text": !!icon && (text || hasRenderableChildren(children)), "has-icon": !!icon, "has-icon-right": iconPosition === "right" }); const trulyDisabled = disabled && !accessibleWhenDisabled; const Tag = href !== void 0 && !disabled ? "a" : "button"; const buttonProps = Tag === "button" ? { type: "button", disabled: trulyDisabled, "aria-checked": ariaChecked, "aria-pressed": ariaPressed, "aria-selected": ariaSelected } : {}; const anchorProps = Tag === "a" ? { href, target } : {}; const disableEventProps = {}; if (disabled && accessibleWhenDisabled) { buttonProps["aria-disabled"] = true; anchorProps["aria-disabled"] = true; for (const disabledEvent of disabledEventsOnDisabledButton) disableEventProps[disabledEvent] = (event) => { if (event) { event.stopPropagation(); event.preventDefault(); } }; } const shouldShowTooltip = !trulyDisabled && (showTooltip && !!label || !!shortcut || !!label && !children?.length && false !== showTooltip); const descriptionId = description ? instanceId : void 0; const describedById = additionalProps["aria-describedby"] || descriptionId; const commonProps = { className: classes, "aria-label": additionalProps["aria-label"] || label, "aria-describedby": describedById, ref }; const elementChildren = /* @__PURE__ */ jsxs(Fragment$1, { children: [ icon && iconPosition === "left" && /* @__PURE__ */ jsx(Icon, { icon, size: iconSize }), text && /* @__PURE__ */ jsx(Fragment$1, { children: text }), children, icon && iconPosition === "right" && /* @__PURE__ */ jsx(Icon, { icon, size: iconSize }) ] }); const element = Tag === "a" ? /* @__PURE__ */ jsx("a", { ...anchorProps, ...additionalProps, ...disableEventProps, ...commonProps, children: elementChildren }) : /* @__PURE__ */ jsx("button", { ...buttonProps, ...additionalProps, ...disableEventProps, ...commonProps, children: elementChildren }); const tooltipProps = shouldShowTooltip ? { text: children?.length && description ? description : label, shortcut, placement: tooltipPosition && positionToPlacement(tooltipPosition) } : {}; return /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx(Tooltip, { ...tooltipProps, children: element }), description && /* @__PURE__ */ jsx(VisuallyHidden, { children: /* @__PURE__ */ jsx("span", { id: descriptionId, children: description }) })] }); }); export { Button, Button as default }; //# sourceMappingURL=index.mjs.map