UNPKG

@yamada-ui/toggle

Version:

Yamada UI toggle component

123 lines (122 loc) 3.78 kB
"use client" "use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/toggle-group.tsx var toggle_group_exports = {}; __export(toggle_group_exports, { ToggleGroup: () => ToggleGroup, useToggleGroup: () => useToggleGroup }); module.exports = __toCommonJS(toggle_group_exports); var import_core = require("@yamada-ui/core"); var import_use_controllable_state = require("@yamada-ui/use-controllable-state"); var import_utils = require("@yamada-ui/utils"); var import_react = require("react"); var import_jsx_runtime = require("react/jsx-runtime"); var [ToggleGroupProvider, useToggleGroup] = (0, import_utils.createContext)( { name: "ToggleGroupContext", strict: false } ); var ToggleGroup = (0, import_core.forwardRef)( ({ className, size, variant, defaultValue, direction, isDisabled, disabled = isDisabled, flexDirection = direction, isReadOnly, readOnly = isReadOnly, value: valueProp, onChange: onChangeProp, ...rest }, ref) => { const [value, setValue] = (0, import_use_controllable_state.useControllableState)({ defaultValue, value: valueProp, onChange: onChangeProp }); const controlledRef = (0, import_react.useRef)(!(0, import_utils.isUndefined)(value)); const onChange = (0, import_react.useCallback)( (value2) => { if ((0, import_utils.isUndefined)(value2)) return; setValue((prev) => { if ((0, import_utils.isArray)(prev)) { const isIncluded = prev.includes(value2); if (isIncluded) { return prev.filter((prevValue) => prevValue !== value2); } else { return [...prev, value2]; } } else { if (value2 === prev) { return void 0; } else { return value2; } } }); }, [setValue] ); const css = { display: "inline-flex", flexDirection, gap: "0.5rem" }; const values = (0, import_react.useMemo)( () => ({ size, variant, controlled: controlledRef.current, disabled, readOnly, value, onChange }), [value, size, variant, disabled, readOnly, onChange] ); (0, import_utils.useUpdateEffect)(() => { if ((0, import_utils.isUndefined)(value)) return; controlledRef.current = true; }, [valueProp]); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ToggleGroupProvider, { value: values, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_core.ui.div, { ref, className: (0, import_utils.cx)("ui-toggle-group", className), role: "group", __css: css, ...rest } ) }); } ); ToggleGroup.displayName = "ToggleGroup"; ToggleGroup.__ui__ = "ToggleGroup"; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ToggleGroup, useToggleGroup }); //# sourceMappingURL=toggle-group.js.map