UNPKG

@alice-ui/react

Version:
31 lines (29 loc) 1.78 kB
// src/switch/switch.tsx import { clsx } from "@alice-ui/shared-utils"; import { toggle } from "@alice-ui/theme"; import { cloneElement, forwardRef, useMemo } from "react"; import { Switch as AriaSwitch } from "react-aria-components"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; function Switch(props, ref) { const { children, classNames, className, color, size, thumbIcon, ...otherProps } = props; const slots = useMemo(() => toggle({ color, size }), [color, size]); const baseStyles = clsx(classNames == null ? void 0 : classNames.base, className); return /* @__PURE__ */ jsx(AriaSwitch, { ref, className: slots.base({ class: baseStyles }), ...otherProps, children: (renderProps) => { const thumbIconProps = { "data-checked": String(renderProps.isSelected), isSelected: renderProps.isSelected, className: slots.thumbIcon({ class: classNames == null ? void 0 : classNames.thumbIcon }), width: "1em", height: "1em" }; const clonedThumbIcon = typeof thumbIcon === "function" ? thumbIcon(thumbIconProps) : thumbIcon && cloneElement(thumbIcon, thumbIconProps); return /* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsx("span", { "aria-hidden": "true", className: slots.wrapper({ class: classNames == null ? void 0 : classNames.wrapper }), children: /* @__PURE__ */ jsx("span", { className: slots.thumb({ class: classNames == null ? void 0 : classNames.thumb }), children: thumbIcon && clonedThumbIcon }) }), children && /* @__PURE__ */ jsx("span", { className: slots.label({ class: classNames == null ? void 0 : classNames.label }), children: typeof children === "function" ? children(renderProps) : children }) ] }); } }); } var _Switch = forwardRef(Switch); export { _Switch };