UNPKG

@trail-ui/react

Version:
82 lines (80 loc) 2.99 kB
// src/switch/switch.tsx import { CheckIcon, CloseIcon } from "@trail-ui/icons"; import { clsx } from "@trail-ui/shared-utils"; import { forwardRef } from "react"; import { jsx, jsxs } from "react/jsx-runtime"; var Switch = forwardRef( ({ label, labelId, isToggled, activeText, inactiveText, isDisabled = false, onChange, tabIndex = void 0, onFocus, classNames }, ref) => { const handleToggle = () => { if (isDisabled) { return; } onChange(!isToggled); }; return /* @__PURE__ */ jsxs( "div", { className: 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__ */ jsx("span", { id: labelId != null ? labelId : "label", className: clsx("mr-0.5", classNames == null ? void 0 : classNames.label), children: label }), /* @__PURE__ */ jsxs( "div", { role: "switch", ref, "aria-labelledby": labelId != null ? labelId : "label", "aria-checked": isToggled, tabIndex: tabIndex != null ? tabIndex : isDisabled ? -1 : 0, className: 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__ */ jsxs("div", { className: "absolute inset-0 flex items-center justify-between px-1", children: [ /* @__PURE__ */ jsx(CheckIcon, { className: "h-5 w-5 text-neutral-50" }), /* @__PURE__ */ jsx(CloseIcon, { className: "h-5 w-5 text-neutral-50" }) ] }), /* @__PURE__ */ 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__ */ jsx("span", { "aria-hidden": "true", className: classNames == null ? void 0 : classNames.text, children: isToggled ? activeText : inactiveText }) ] } ); } ); Switch.displayName = "Switch"; var switch_default = Switch; export { switch_default };