UNPKG

@wordpress/components

Version:
93 lines (92 loc) • 3.52 kB
import clsx from "clsx"; import { forwardRef } from "@wordpress/element"; import { VisuallyHidden } from "../visually-hidden/index.mjs"; import { StyledField, StyledHelp, StyledLabel, StyledVisualLabel, Wrapper } from "./styles/base-control-styles.mjs"; import { contextConnectWithoutRef, useContextSystem } from "../context/index.mjs"; import { jsx, jsxs } from "react/jsx-runtime"; import { useBaseControlProps } from "./hooks.mjs"; // packages/components/src/base-control/index.tsx const UnconnectedBaseControl = (props) => { const { id, label, hideLabelFromVision = false, help, className, children } = useContextSystem(props, "BaseControl"); return /* @__PURE__ */ jsxs(Wrapper, { className, children: [/* @__PURE__ */ jsxs(StyledField, { className: "components-base-control__field", children: [ label && id && (hideLabelFromVision ? /* @__PURE__ */ jsx(VisuallyHidden, { as: "label", htmlFor: id, children: label }) : /* @__PURE__ */ jsx(StyledLabel, { className: "components-base-control__label", htmlFor: id, children: label })), label && !id && (hideLabelFromVision ? /* @__PURE__ */ jsx(VisuallyHidden, { as: "label", children: label }) : /* @__PURE__ */ jsx(VisualLabel, { children: label })), children ] }), !!help && /* @__PURE__ */ jsx(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__ */ jsx(StyledVisualLabel, { ref, ...restProps, className: clsx("components-base-control__label", className), children }); }; const VisualLabel = 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(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 }); export { BaseControl, BaseControl as default, VisualLabel, useBaseControlProps }; //# sourceMappingURL=index.mjs.map