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";
|
|
|
|
|
|
|
|
import classNames from "@calcom/lib/classNames";
|
|
|
|
|
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-02 21:16:36 +00:00
|
|
|
thumbProps?: {
|
|
|
|
className?: string;
|
|
|
|
};
|
2022-09-10 17:34:38 +00:00
|
|
|
fitToHeight?: boolean;
|
2023-03-02 18:15:28 +00:00
|
|
|
tooltip?: string;
|
2022-07-23 00:39:50 +00:00
|
|
|
}
|
|
|
|
) => {
|
2022-12-05 12:12:14 +00:00
|
|
|
const { label, fitToHeight, ...primitiveProps } = props;
|
2022-07-23 00:39:50 +00:00
|
|
|
const id = useId();
|
|
|
|
|
|
|
|
return (
|
2023-03-02 18:15:28 +00:00
|
|
|
<Wrapper tooltip={props.tooltip}>
|
|
|
|
<div className={classNames("flex h-auto w-auto flex-row items-center", fitToHeight && "h-fit")}>
|
|
|
|
<PrimitiveSwitch.Root
|
2022-08-31 11:13:27 +00:00
|
|
|
className={classNames(
|
2023-03-02 18:15:28 +00:00
|
|
|
props.checked ? "bg-gray-900" : "bg-gray-200",
|
|
|
|
primitiveProps.disabled ? "cursor-not-allowed" : "hover:bg-gray-300",
|
|
|
|
"focus:ring-brand-800 h-5 w-[34px] rounded-full shadow-none",
|
|
|
|
props.className
|
2022-08-31 11:13:27 +00:00
|
|
|
)}
|
2023-03-02 18:15:28 +00:00
|
|
|
{...primitiveProps}>
|
|
|
|
<PrimitiveSwitch.Thumb
|
|
|
|
id={id}
|
|
|
|
// Since we dont support global dark mode - we have to style dark mode components specifically on the instance for now
|
|
|
|
// TODO: Remove once we support global dark mode
|
|
|
|
className={classNames(
|
|
|
|
"block h-[14px] w-[14px] rounded-full bg-white 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]",
|
|
|
|
props.checked && "shadow-inner",
|
|
|
|
props.thumbProps?.className
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</PrimitiveSwitch.Root>
|
|
|
|
{label && (
|
|
|
|
<Label.Root
|
|
|
|
htmlFor={id}
|
|
|
|
className={classNames(
|
2023-03-08 23:16:26 +00:00
|
|
|
"align-text-top text-sm font-medium text-gray-900 ltr:ml-2 rtl:mr-2 dark:text-white",
|
2023-03-02 18:15:28 +00:00
|
|
|
primitiveProps.disabled ? "cursor-not-allowed opacity-25" : "cursor-pointer "
|
|
|
|
)}>
|
|
|
|
{label}
|
|
|
|
</Label.Root>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</Wrapper>
|
2022-07-23 00:39:50 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Switch;
|