UNPKG

@wordpress/components

Version:
1 lines • 5.65 kB
{"version":3,"file":"index.cjs","names":["useContextSystem","Wrapper","StyledField","VisuallyHidden","StyledLabel","StyledHelp","StyledVisualLabel","forwardRef","contextConnectWithoutRef"],"sources":["../../src/base-control/index.tsx"],"sourcesContent":["import clsx from 'clsx';\nimport type { ForwardedRef } from 'react';\nimport { forwardRef } from '@wordpress/element';\nimport { VisuallyHidden } from '../visually-hidden';\nimport type { BaseControlProps, BaseControlVisualLabelProps } from './types';\nimport {\n\tWrapper,\n\tStyledField,\n\tStyledLabel,\n\tStyledHelp,\n\tStyledVisualLabel,\n} from './styles/base-control-styles';\nimport type { WordPressComponentProps } from '../context';\nimport { contextConnectWithoutRef, useContextSystem } from '../context';\n\nexport { useBaseControlProps } from './hooks';\n\nconst UnconnectedBaseControl = (\n\tprops: WordPressComponentProps< BaseControlProps, null, false >\n) => {\n\tconst {\n\t\tid,\n\t\tlabel,\n\t\thideLabelFromVision = false,\n\t\thelp,\n\t\tclassName,\n\t\tchildren,\n\t} = useContextSystem( props, 'BaseControl' );\n\n\treturn (\n\t\t<Wrapper className={ className }>\n\t\t\t<StyledField className=\"components-base-control__field\">\n\t\t\t\t{ label &&\n\t\t\t\t\tid &&\n\t\t\t\t\t( hideLabelFromVision ? (\n\t\t\t\t\t\t<VisuallyHidden as=\"label\" htmlFor={ id }>\n\t\t\t\t\t\t\t{ label }\n\t\t\t\t\t\t</VisuallyHidden>\n\t\t\t\t\t) : (\n\t\t\t\t\t\t<StyledLabel\n\t\t\t\t\t\t\tclassName=\"components-base-control__label\"\n\t\t\t\t\t\t\thtmlFor={ id }\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t{ label }\n\t\t\t\t\t\t</StyledLabel>\n\t\t\t\t\t) ) }\n\t\t\t\t{ label &&\n\t\t\t\t\t! id &&\n\t\t\t\t\t( hideLabelFromVision ? (\n\t\t\t\t\t\t<VisuallyHidden as=\"label\">{ label }</VisuallyHidden>\n\t\t\t\t\t) : (\n\t\t\t\t\t\t<VisualLabel>{ label }</VisualLabel>\n\t\t\t\t\t) ) }\n\t\t\t\t{ children }\n\t\t\t</StyledField>\n\t\t\t{ !! help && (\n\t\t\t\t<StyledHelp\n\t\t\t\t\tid={ id ? id + '__help' : undefined }\n\t\t\t\t\tclassName=\"components-base-control__help\"\n\t\t\t\t>\n\t\t\t\t\t{ help }\n\t\t\t\t</StyledHelp>\n\t\t\t) }\n\t\t</Wrapper>\n\t);\n};\n\nconst UnforwardedVisualLabel = (\n\tprops: WordPressComponentProps< BaseControlVisualLabelProps, 'span' >,\n\tref: ForwardedRef< any >\n) => {\n\tconst { className, children, ...restProps } = props;\n\n\treturn (\n\t\t<StyledVisualLabel\n\t\t\tref={ ref }\n\t\t\t{ ...restProps }\n\t\t\tclassName={ clsx( 'components-base-control__label', className ) }\n\t\t>\n\t\t\t{ children }\n\t\t</StyledVisualLabel>\n\t);\n};\n\nexport const VisualLabel = forwardRef( UnforwardedVisualLabel );\n\n/**\n * `BaseControl` is a low-level component used to generate labels and help text for components handling user inputs.\n *\n * ```jsx\n * import { BaseControl, useBaseControlProps } from '@wordpress/components';\n *\n * // Render a `BaseControl` for a textarea input\n * const MyCustomTextareaControl = ({ children, ...baseProps }) => (\n * \t// `useBaseControlProps` is a convenience hook to get the props for the `BaseControl`\n * \t// and the inner control itself. Namely, it takes care of generating a unique `id`,\n * \t// properly associating it with the `label` and `help` elements.\n * \tconst { baseControlProps, controlProps } = useBaseControlProps( baseProps );\n *\n * \treturn (\n * \t\t<BaseControl { ...baseControlProps }>\n * \t\t\t<textarea { ...controlProps }>\n * \t\t\t { children }\n * \t\t\t</textarea>\n * \t\t</BaseControl>\n * \t);\n * );\n * ```\n */\nexport const BaseControl = Object.assign(\n\tcontextConnectWithoutRef( UnconnectedBaseControl, 'BaseControl' ),\n\n\t{\n\t\t/**\n\t\t * `BaseControl.VisualLabel` is used to render a purely visual label inside a `BaseControl` component.\n\t\t *\n\t\t * It should only be used in cases where the children being rendered inside `BaseControl` are already accessibly labeled,\n\t\t * e.g., a button, but we want an additional visual label for that section equivalent to the labels `BaseControl` would\n\t\t * otherwise use if the `label` prop was passed.\n\t\t *\n\t\t * ```jsx\n\t\t * import { BaseControl } from '@wordpress/components';\n\t\t *\n\t\t * const MyBaseControl = () => (\n\t\t * \t<BaseControl help=\"This button is already accessibly labeled.\">\n\t\t * \t\t<BaseControl.VisualLabel>Author</BaseControl.VisualLabel>\n\t\t * \t\t<Button>Select an author</Button>\n\t\t * \t</BaseControl>\n\t\t * );\n\t\t * ```\n\t\t */\n\t\tVisualLabel,\n\t}\n);\n\nexport default BaseControl;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiBA,MAAM,0BACL,UACI;CACJ,MAAM,EACL,IACA,OACA,sBAAsB,OACtB,MACA,WACA,cAAA,GACGA,oBAAAA,iBAAAA,CAAkB,OAAO,aAAc;CAE3C,OACC,iBAAA,GAAA,kBAAA,KAAA,CAACC,iCAAAA,SAAD;EAAqB;EAArB,UAAA,CACC,iBAAA,GAAA,kBAAA,KAAA,CAACC,iCAAAA,aAAD;GAAa,WAAU;GAAvB,UAAA;IACG,SACD,OACE,sBACD,iBAAA,GAAA,kBAAA,IAAA,CAACC,4BAAAA,gBAAD;KAAgB,IAAG;KAAQ,SAAU;KAClC,UAAA;IACa,CAAA,IAEhB,iBAAA,GAAA,kBAAA,IAAA,CAACC,iCAAAA,aAAD;KACC,WAAU;KACV,SAAU;KAER,UAAA;IACU,CAAA;IAEb,SACD,CAAE,OACA,sBACD,iBAAA,GAAA,kBAAA,IAAA,CAACD,4BAAAA,gBAAD;KAAgB,IAAG;KAAU,UAAA;IAAuB,CAAA,IAEpD,iBAAA,GAAA,kBAAA,IAAA,CAAC,aAAD,EAAA,UAAe,MAAoB,CAAA;IAEnC;GACU;EACX,CAAA,GAAA,CAAC,CAAE,QACJ,iBAAA,GAAA,kBAAA,IAAA,CAACE,iCAAAA,YAAD;GACC,IAAK,KAAK,KAA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