@wordpress/components
Version:
UI components for WordPress.
128 lines (127 loc) • 5.25 kB
JavaScript
;
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_element = require("@wordpress/element");
let ___visually_hidden_index_ts = require("../visually-hidden/index.cjs");
let __styles_base_control_styles_tsx = require("./styles/base-control-styles.cjs");
let ___context_index_ts = require("../context/index.cjs");
let react_jsx_runtime = require("react/jsx-runtime");
let __hooks_ts = require("./hooks.cjs");
// packages/components/src/base-control/index.tsx
const UnconnectedBaseControl = (props) => {
const { id, label, hideLabelFromVision = false, help, className, children } = (0, ___context_index_ts.useContextSystem)(props, "BaseControl");
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(__styles_base_control_styles_tsx.Wrapper, {
className,
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)(__styles_base_control_styles_tsx.StyledField, {
className: "components-base-control__field",
children: [
label && id && (hideLabelFromVision ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___visually_hidden_index_ts.VisuallyHidden, {
as: "label",
htmlFor: id,
children: label
}) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__styles_base_control_styles_tsx.StyledLabel, {
className: "components-base-control__label",
htmlFor: id,
children: label
})),
label && !id && (hideLabelFromVision ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___visually_hidden_index_ts.VisuallyHidden, {
as: "label",
children: label
}) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(VisualLabel, { children: label })),
children
]
}), !!help && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__styles_base_control_styles_tsx.StyledHelp, {
id: id ? id + "__help" : void 0,
className: "components-base-control__help",
children: help
})]
});
};
const UnforwardedVisualLabel = (props, ref) => {
const { className, children, ...restProps } = props;
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__styles_base_control_styles_tsx.StyledVisualLabel, {
ref,
...restProps,
className: (0, clsx.default)("components-base-control__label", className),
children
});
};
const VisualLabel = (0, _wordpress_element.forwardRef)(UnforwardedVisualLabel);
/**
* `BaseControl` is a low-level component used to generate labels and help text for components handling user inputs.
*
* ```jsx
* import { BaseControl, useBaseControlProps } from '@wordpress/components';
*
* // Render a `BaseControl` for a textarea input
* const MyCustomTextareaControl = ({ children, ...baseProps }) => (
* // `useBaseControlProps` is a convenience hook to get the props for the `BaseControl`
* // and the inner control itself. Namely, it takes care of generating a unique `id`,
* // properly associating it with the `label` and `help` elements.
* const { baseControlProps, controlProps } = useBaseControlProps( baseProps );
*
* return (
* <BaseControl { ...baseControlProps }>
* <textarea { ...controlProps }>
* { children }
* </textarea>
* </BaseControl>
* );
* );
* ```
*/
const BaseControl = Object.assign((0, ___context_index_ts.contextConnectWithoutRef)(UnconnectedBaseControl, "BaseControl"), {
/**
* `BaseControl.VisualLabel` is used to render a purely visual label inside a `BaseControl` component.
*
* It should only be used in cases where the children being rendered inside `BaseControl` are already accessibly labeled,
* e.g., a button, but we want an additional visual label for that section equivalent to the labels `BaseControl` would
* otherwise use if the `label` prop was passed.
*
* ```jsx
* import { BaseControl } from '@wordpress/components';
*
* const MyBaseControl = () => (
* <BaseControl help="This button is already accessibly labeled.">
* <BaseControl.VisualLabel>Author</BaseControl.VisualLabel>
* <Button>Select an author</Button>
* </BaseControl>
* );
* ```
*/
VisualLabel });
exports.BaseControl = BaseControl;
exports.default = BaseControl;
exports.VisualLabel = VisualLabel;
Object.defineProperty(exports, "useBaseControlProps", {
enumerable: true,
get: function() {
return __hooks_ts.useBaseControlProps;
}
});
//# sourceMappingURL=index.cjs.map