@trail-ui/react
Version:
102 lines (100 loc) • 4.22 kB
JavaScript
;
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/switch/switch.tsx
var switch_exports = {};
__export(switch_exports, {
default: () => switch_default
});
module.exports = __toCommonJS(switch_exports);
var import_icons = require("@trail-ui/icons");
var import_shared_utils = require("@trail-ui/shared-utils");
var import_react = require("react");
var import_jsx_runtime = require("react/jsx-runtime");
var Switch = (0, import_react.forwardRef)(
({
label,
labelId,
isToggled,
activeText,
inactiveText,
isDisabled = false,
onChange,
tabIndex = void 0,
onFocus,
classNames
}, ref) => {
const handleToggle = () => {
if (isDisabled) {
return;
}
onChange(!isToggled);
};
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
"div",
{
className: (0, import_shared_utils.clsx)(
isDisabled ? "pointer-events-none select-none opacity-40" : "",
"flex items-center gap-1.5 text-sm text-neutral-900",
classNames == null ? void 0 : classNames.base
),
children: [
label && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { id: labelId != null ? labelId : "label", className: (0, import_shared_utils.clsx)("mr-0.5", classNames == null ? void 0 : classNames.label), children: label }),
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
"div",
{
role: "switch",
ref,
"aria-labelledby": labelId != null ? labelId : "label",
"aria-checked": isToggled,
tabIndex: tabIndex != null ? tabIndex : isDisabled ? -1 : 0,
className: (0, import_shared_utils.clsx)(
`relative inline-flex h-5 w-10 cursor-pointer items-center rounded-full transition-all duration-300 ease-in-out focus-visible:outline-2 focus-visible:outline-offset-[3px] focus-visible:outline-purple-600`,
isToggled ? "bg-purple-600" : "bg-neutral-600",
classNames == null ? void 0 : classNames.switch
),
onClick: handleToggle,
onKeyDown: (e) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
handleToggle();
}
},
onFocus,
children: [
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "absolute inset-0 flex items-center justify-between px-1", children: [
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_icons.CheckIcon, { className: "h-5 w-5 text-neutral-50" }),
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_icons.CloseIcon, { className: "h-5 w-5 text-neutral-50" })
] }),
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
"div",
{
className: `absolute left-0.5 top-0.5 h-4 w-4 rounded-full bg-neutral-50 transition-transform duration-300 ${isToggled ? "translate-x-5" : "translate-x-0"}`
}
)
]
}
),
activeText && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { "aria-hidden": "true", className: classNames == null ? void 0 : classNames.text, children: isToggled ? activeText : inactiveText })
]
}
);
}
);
Switch.displayName = "Switch";
var switch_default = Switch;