import { createElement } from "react"; /** * External dependencies */ import classnames from 'classnames'; /** * Internal dependencies */ import { VisuallyHidden } from '../visually-hidden'; import { Wrapper, StyledField, StyledLabel, StyledHelp, StyledVisualLabel } from './styles/base-control-styles'; import { contextConnectWithoutRef, useContextSystem } from '../context'; export { useBaseControlProps } from './hooks'; /** * `BaseControl` is a 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 ( * * * * ); * ); * ``` */ const UnconnectedBaseControl = props => { const { __nextHasNoMarginBottom = false, id, label, hideLabelFromVision = false, help, className, children } = useContextSystem(props, 'BaseControl'); return createElement(Wrapper, { className: className }, createElement(StyledField, { className: "components-base-control__field" // TODO: Official deprecation for this should start after all internal usages have been migrated , __nextHasNoMarginBottom: __nextHasNoMarginBottom }, label && id && (hideLabelFromVision ? createElement(VisuallyHidden, { as: "label", htmlFor: id }, label) : createElement(StyledLabel, { className: "components-base-control__label", htmlFor: id }, label)), label && !id && (hideLabelFromVision ? createElement(VisuallyHidden, { as: "label" }, label) : createElement(VisualLabel, null, label)), children), !!help && createElement(StyledHelp, { id: id ? id + '__help' : undefined, className: "components-base-control__help", __nextHasNoMarginBottom: __nextHasNoMarginBottom }, help)); }; /** * `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. * * @example * import { BaseControl } from '@wordpress/components'; * * const MyBaseControl = () => ( * * Author * * * ); */ export const VisualLabel = ({ className, children, ...props }) => { return createElement(StyledVisualLabel, { ...props, className: classnames('components-base-control__label', className) }, children); }; export const BaseControl = Object.assign(contextConnectWithoutRef(UnconnectedBaseControl, 'BaseControl'), { VisualLabel }); export default BaseControl; //# sourceMappingURL=index.js.map