2022-07-23 00:39:50 +00:00
|
|
|
import { useId } from "@radix-ui/react-id";
|
|
|
|
import * as Label from "@radix-ui/react-label";
|
|
|
|
import * as PrimitiveSwitch from "@radix-ui/react-switch";
|
|
|
|
import React from "react";
|
|
|
|
|
2023-04-06 08:17:53 +00:00
|
|
|
import cx from "@calcom/lib/classNames";
|
2022-07-23 00:39:50 +00:00
|
|
|
|
2023-03-02 18:15:28 +00:00
|
|
|
import { Tooltip } from "../../tooltip";
|
|
|
|
|
|
|
|
const Wrapper = ({ children, tooltip }: { tooltip?: string; children: React.ReactNode }) => {
|
|
|
|
if (!tooltip) {
|
|
|
|
return <>{children}</>;
|
|
|
|
}
|
|
|
|
return <Tooltip content={tooltip}>{children}</Tooltip>;
|
|
|
|
};
|
2022-07-23 00:39:50 +00:00
|
|
|
const Switch = (
|
|
|
|
props: React.ComponentProps<typeof PrimitiveSwitch.Root> & {
|
|
|
|
label?: string;
|
2022-09-10 17:34:38 +00:00
|
|
|
fitToHeight?: boolean;
|
2023-04-13 02:10:23 +00:00
|
|
|
disabled?: boolean;
|
2023-03-02 18:15:28 +00:00
|
|
|
tooltip?: string;
|
2023-04-19 20:17:54 +00:00
|
|
|
labelOnLeading?: boolean;
|
2023-04-06 08:17:53 +00:00
|
|
|
classNames?: {
|
|
|
|
container?: string;
|
|
|
|
thumb?: string;
|
|
|
|
};
|
2022-07-23 00:39:50 +00:00
|
|
|
}
|
|
|
|
) => {
|
2023-04-19 20:17:54 +00:00
|
|
|
const { label, fitToHeight, classNames, labelOnLeading, ...primitiveProps } = props;
|
2022-07-23 00:39:50 +00:00
|
|
|
const id = useId();
|
2023-04-19 14:39:23 +00:00
|
|
|
const isChecked = props.checked || props.defaultChecked;
|
2022-07-23 00:39:50 +00:00
|
|
|
return (
|
2023-03-02 18:15:28 +00:00
|
|
|
<Wrapper tooltip={props.tooltip}>
|
2023-04-06 08:17:53 +00:00
|
|
|
<div
|
|
|
|
className={cx(
|
|
|
|
"flex h-auto w-auto flex-row items-center",
|
|
|
|
fitToHeight && "h-fit",
|
2023-04-19 20:17:54 +00:00
|
|
|
labelOnLeading && "flex-row-reverse",
|
2023-04-06 08:17:53 +00:00
|
|
|
classNames?.container
|
|
|
|
)}>
|
2023-03-02 18:15:28 +00:00
|
|
|
<PrimitiveSwitch.Root
|
2023-04-06 08:17:53 +00:00
|
|
|
className={cx(
|
2023-04-19 14:39:23 +00:00
|
|
|
props.checked || props.defaultChecked ? "bg-brand-default" : "bg-emphasis",
|
2023-04-05 18:14:46 +00:00
|
|
|
primitiveProps.disabled && "cursor-not-allowed",
|
|
|
|
"focus:ring-brand-default h-5 w-[34px] rounded-full shadow-none",
|
2023-03-02 18:15:28 +00:00
|
|
|
props.className
|
2022-08-31 11:13:27 +00:00
|
|
|
)}
|
2023-03-02 18:15:28 +00:00
|
|
|
{...primitiveProps}>
|
|
|
|
<PrimitiveSwitch.Thumb
|
|
|
|
id={id}
|
2023-04-06 08:17:53 +00:00
|
|
|
className={cx(
|
2023-04-05 18:14:46 +00:00
|
|
|
"block h-[14px] w-[14px] rounded-full transition will-change-transform ltr:translate-x-[4px] rtl:-translate-x-[4px] ltr:[&[data-state='checked']]:translate-x-[17px] rtl:[&[data-state='checked']]:-translate-x-[17px]",
|
2023-04-19 14:39:23 +00:00
|
|
|
isChecked ? "bg-brand-accent shadow-inner" : "bg-default",
|
2023-04-06 08:17:53 +00:00
|
|
|
classNames?.thumb
|
2023-03-02 18:15:28 +00:00
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</PrimitiveSwitch.Root>
|
|
|
|
{label && (
|
|
|
|
<Label.Root
|
|
|
|
htmlFor={id}
|
2023-04-06 08:17:53 +00:00
|
|
|
className={cx(
|
2023-04-09 09:13:57 +00:00
|
|
|
"text-emphasis ms-2 align-text-top text-sm font-medium",
|
2023-04-19 20:17:54 +00:00
|
|
|
primitiveProps.disabled ? "cursor-not-allowed opacity-25" : "cursor-pointer",
|
|
|
|
labelOnLeading && "flex-1"
|
2023-03-02 18:15:28 +00:00
|
|
|
)}>
|
|
|
|
{label}
|
|
|
|
</Label.Root>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</Wrapper>
|
2022-07-23 00:39:50 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Switch;
|