@wordpress/components
Version:
UI components for WordPress.
1 lines • 3.71 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","names":[],"sources":["../../src/form-toggle/index.tsx"],"sourcesContent":["import clsx from 'clsx';\nimport type { ForwardedRef } from 'react';\nimport { forwardRef } from '@wordpress/element';\nimport { Circle, Path, SVG } from '@wordpress/primitives';\nimport type { FormToggleProps } from './types';\nimport type { WordPressComponentProps } from '../context';\n\nexport const noop = () => {};\n\nconst onLabel = (\n\t<SVG\n\t\tclassName=\"components-form-toggle__label components-form-toggle__on-label\"\n\t\txmlns=\"http://www.w3.org/2000/svg\"\n\t\tviewBox=\"0 0 8 8\"\n\t>\n\t\t<Path\n\t\t\td=\"M4 1.5v5\"\n\t\t\tfill=\"none\"\n\t\t\tstroke=\"currentColor\"\n\t\t\tstrokeLinecap=\"round\"\n\t\t/>\n\t</SVG>\n);\n\nconst offLabel = (\n\t<SVG\n\t\tclassName=\"components-form-toggle__label components-form-toggle__off-label\"\n\t\txmlns=\"http://www.w3.org/2000/svg\"\n\t\tviewBox=\"0 0 8 8\"\n\t>\n\t\t<Circle cx={ 4 } cy={ 4 } r={ 2.5 } fill=\"none\" stroke=\"currentColor\" />\n\t</SVG>\n);\n\nfunction UnforwardedFormToggle(\n\tprops: WordPressComponentProps< FormToggleProps, 'input', false >,\n\tref: ForwardedRef< HTMLInputElement >\n) {\n\tconst {\n\t\tclassName,\n\t\tchecked,\n\t\tid,\n\t\tdisabled,\n\t\tonChange = noop,\n\t\tonClick,\n\t\t...additionalProps\n\t} = props;\n\tconst wrapperClasses = clsx( 'components-form-toggle', className, {\n\t\t'is-checked': checked,\n\t\t'is-disabled': disabled,\n\t} );\n\n\treturn (\n\t\t<span className={ wrapperClasses }>\n\t\t\t<input\n\t\t\t\tclassName=\"components-form-toggle__input\"\n\t\t\t\tid={ id }\n\t\t\t\ttype=\"checkbox\"\n\t\t\t\tchecked={ checked }\n\t\t\t\tonChange={ onChange }\n\t\t\t\tdisabled={ disabled }\n\t\t\t\tonClick={ ( event ) => {\n\t\t\t\t\t// Compat code for Safari to ensure that the toggle is focused when clicked.\n\t\t\t\t\tevent.currentTarget.focus();\n\n\t\t\t\t\tonClick?.( event );\n\t\t\t\t} }\n\t\t\t\t{ ...additionalProps }\n\t\t\t\tref={ ref }\n\t\t\t/>\n\t\t\t<span className=\"components-form-toggle__track\"></span>\n\t\t\t<span className=\"components-form-toggle__labels\">\n\t\t\t\t{ onLabel }\n\t\t\t\t{ offLabel }\n\t\t\t</span>\n\t\t\t<span className=\"components-form-toggle__thumb\"></span>\n\t\t</span>\n\t);\n}\n\n/**\n * FormToggle switches a single setting on or off.\n *\n * ```jsx\n * import { FormToggle } from '@wordpress/components';\n * import { useState } from '@wordpress/element';\n *\n * const MyFormToggle = () => {\n * const [ isChecked, setChecked ] = useState( true );\n *\n * return (\n * <FormToggle\n * checked={ isChecked }\n * onChange={ () => setChecked( ( state ) => ! state ) }\n * />\n * );\n * };\n * ```\n */\nexport const FormToggle = forwardRef( UnforwardedFormToggle );\nFormToggle.displayName = 'FormToggle';\n\nexport default FormToggle;\n"],"mappings":";;;;;AAOA,MAAa,aAAa,CAAC;AAE3B,MAAM,UACL,oBAAC,KAAD;CACC,WAAU;CACV,OAAM;CACN,SAAQ;CAER,UAAA,oBAAC,MAAD;EACC,GAAE;EACF,MAAK;EACL,QAAO;EACP,eAAc;CACd,CAAA;AACG,CAAA;AAGN,MAAM,WACL,oBAAC,KAAD;CACC,WAAU;CACV,OAAM;CACN,SAAQ;CAER,UAAA,oBAAC,QAAD;EAAQ,IAAK;EAAI,IAAK;EAAI,GAAI;EAAM,MAAK;EAAO,QAAO;CAAgB,CAAA;AACnE,CAAA;AAGN,SAAS,sBACR,OACA,KACC;CACD,MAAM,EACL,WACA,SACA,IACA,UACA,WAAW,MACX,SACA,GAAG,oBACA;CACJ,MAAM,iBAAiB,KAAM,0BAA0B,WAAW;EACjE,cAAc;EACd,eAAe;CAChB,CAAE;CAEF,OACC,qBAAC,QAAD;EAAM,WAAY;EAAlB,UAAA;GACC,oBAAC,SAAD;IACC,WAAU;IACL;IACL,MAAK;IACK;IACC;IACA;IACX,UAAY,UAAW;KAEtB,MAAM,cAAc,MAAM;KAE1B,UAAW,KAAM;IAClB;IACA,GAAK;IACC;GACN,CAAA;GACD,oBAAC,QAAD,EAAM,WAAU,gCAAsC,CAAA;GACtD,qBAAC,QAAD;IAAM,WAAU;IAAhB,UAAA,CACG,SACA,QACG;;GACN,oBAAC,QAAD,EAAM,WAAU,gCAAsC,CAAA;EACjD;;AAER;;;;;;;;;;;;;;;;;;;;AAqBA,MAAa,aAAa,WAAY,qBAAsB;AAC5D,WAAW,cAAc"}