UNPKG

@wordpress/components

Version:
201 lines (200 loc) • 7.95 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); // \0rolldown/runtime.js var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) { key = keys[i]; if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: ((k) => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); let clsx = require("clsx"); clsx = __toESM(clsx); let _wordpress_deprecated = require("@wordpress/deprecated"); _wordpress_deprecated = __toESM(_wordpress_deprecated); let _wordpress_element = require("@wordpress/element"); let _wordpress_compose = require("@wordpress/compose"); let ___tooltip_index_tsx = require("../tooltip/index.cjs"); ___tooltip_index_tsx = __toESM(___tooltip_index_tsx); let ___icon_index_tsx = require("../icon/index.cjs"); ___icon_index_tsx = __toESM(___icon_index_tsx); let ___visually_hidden_index_ts = require("../visually-hidden/index.cjs"); let ___popover_utils_ts = require("../popover/utils.cjs"); let react_jsx_runtime = require("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) => _wordpress_element.Children.toArray(children).some((child) => { if (!(0, _wordpress_element.isValidElement)(child)) return child !== ""; if (child.type === _wordpress_element.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) { (0, _wordpress_deprecated.default)("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 = (0, _wordpress_element.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 = (0, _wordpress_compose.useInstanceId)(Button, "components-button__description"); const classes = (0, clsx.default)("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__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [ icon && iconPosition === "left" && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___icon_index_tsx.default, { icon, size: iconSize }), text && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_jsx_runtime.Fragment, { children: text }), children, icon && iconPosition === "right" && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___icon_index_tsx.default, { icon, size: iconSize }) ] }); const element = Tag === "a" ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("a", { ...anchorProps, ...additionalProps, ...disableEventProps, ...commonProps, children: elementChildren }) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", { ...buttonProps, ...additionalProps, ...disableEventProps, ...commonProps, children: elementChildren }); const tooltipProps = shouldShowTooltip ? { text: children?.length && description ? description : label, shortcut, placement: tooltipPosition && (0, ___popover_utils_ts.positionToPlacement)(tooltipPosition) } : {}; return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(___tooltip_index_tsx.default, { ...tooltipProps, children: element }), description && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___visually_hidden_index_ts.VisuallyHidden, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { id: descriptionId, children: description }) })] }); }); exports.Button = Button; exports.default = Button; //# sourceMappingURL=index.cjs.map