UNPKG

@wordpress/components

Version:
85 lines (84 loc) • 2.38 kB
import clsx from "clsx"; import { forwardRef } from "@wordpress/element"; import { Circle, Path, SVG } from "@wordpress/primitives"; import { jsx, jsxs } from "react/jsx-runtime"; // packages/components/src/form-toggle/index.tsx const noop = () => {}; const onLabel = /* @__PURE__ */ jsx(SVG, { className: "components-form-toggle__label components-form-toggle__on-label", xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 8 8", children: /* @__PURE__ */ jsx(Path, { d: "M4 1.5v5", fill: "none", stroke: "currentColor", strokeLinecap: "round" }) }); const offLabel = /* @__PURE__ */ jsx(SVG, { className: "components-form-toggle__label components-form-toggle__off-label", xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 8 8", children: /* @__PURE__ */ jsx(Circle, { cx: 4, cy: 4, r: 2.5, fill: "none", stroke: "currentColor" }) }); function UnforwardedFormToggle(props, ref) { const { className, checked, id, disabled, onChange = noop, onClick, ...additionalProps } = props; const wrapperClasses = clsx("components-form-toggle", className, { "is-checked": checked, "is-disabled": disabled }); return /* @__PURE__ */ jsxs("span", { className: wrapperClasses, children: [ /* @__PURE__ */ jsx("input", { className: "components-form-toggle__input", id, type: "checkbox", checked, onChange, disabled, onClick: (event) => { event.currentTarget.focus(); onClick?.(event); }, ...additionalProps, ref }), /* @__PURE__ */ jsx("span", { className: "components-form-toggle__track" }), /* @__PURE__ */ jsxs("span", { className: "components-form-toggle__labels", children: [onLabel, offLabel] }), /* @__PURE__ */ jsx("span", { className: "components-form-toggle__thumb" }) ] }); } /** * FormToggle switches a single setting on or off. * * ```jsx * import { FormToggle } from '@wordpress/components'; * import { useState } from '@wordpress/element'; * * const MyFormToggle = () => { * const [ isChecked, setChecked ] = useState( true ); * * return ( * <FormToggle * checked={ isChecked } * onChange={ () => setChecked( ( state ) => ! state ) } * /> * ); * }; * ``` */ const FormToggle = forwardRef(UnforwardedFormToggle); FormToggle.displayName = "FormToggle"; export { FormToggle, FormToggle as default, noop }; //# sourceMappingURL=index.mjs.map