@wordpress/components
Version:
UI components for WordPress.
73 lines (72 loc) • 2.27 kB
JavaScript
import clsx from "clsx";
import { forwardRef } from "@wordpress/element";
import { useInstanceId } from "@wordpress/compose";
import { FlexBlock } from "../flex/index.mjs";
import FormToggle from "../form-toggle/index.mjs";
import BaseControl from "../base-control/index.mjs";
import { HStack } from "../h-stack/index.mjs";
import { jsx, jsxs } from "react/jsx-runtime";
// packages/components/src/toggle-control/index.tsx
function UnforwardedToggleControl({ label, checked, help, className, onChange, disabled, __nextHasNoMarginBottom: _, ...additionalProps }, ref) {
function onChangeToggle(event) {
onChange(event.target.checked);
}
const id = `inspector-toggle-control-${useInstanceId(ToggleControl)}`;
let describedBy, helpLabel;
if (help) {
if (typeof help === "function") {
if (checked !== void 0) helpLabel = help(checked);
} else helpLabel = help;
if (helpLabel) describedBy = id + "__help";
}
return /* @__PURE__ */ jsx(BaseControl, {
id,
help: helpLabel && /* @__PURE__ */ jsx("span", {
className: "components-toggle-control__help",
children: helpLabel
}),
className: clsx("components-toggle-control", className),
children: /* @__PURE__ */ jsxs(HStack, {
justify: "flex-start",
spacing: 2,
children: [/* @__PURE__ */ jsx(FormToggle, {
id,
checked,
onChange: onChangeToggle,
"aria-describedby": describedBy,
disabled,
ref,
...additionalProps
}), /* @__PURE__ */ jsx(FlexBlock, {
as: "label",
htmlFor: id,
className: clsx("components-toggle-control__label", { "is-disabled": disabled }),
children: label
})]
})
});
}
/**
* ToggleControl is used to generate a toggle user interface.
*
* ```jsx
* import { ToggleControl } from '@wordpress/components';
* import { useState } from '@wordpress/element';
*
* const MyToggleControl = () => {
* const [ value, setValue ] = useState( false );
*
* return (
* <ToggleControl
* label="Fixed Background"
* checked={ value }
* onChange={ () => setValue( ( state ) => ! state ) }
* />
* );
* };
* ```
*/
const ToggleControl = forwardRef(UnforwardedToggleControl);
ToggleControl.displayName = "ToggleControl";
export { ToggleControl, ToggleControl as default };
//# sourceMappingURL=index.mjs.map