import * as RadixToggleGroup from "@radix-ui/react-toggle-group"; import type { ReactNode } from "react"; import { useEffect, useRef, useState } from "react"; import { classNames } from "@calcom/lib"; import { Tooltip } from "@calcom/ui"; interface ToggleGroupProps extends Omit { options: { value: string; label: string | ReactNode; disabled?: boolean; tooltip?: string; iconLeft?: ReactNode; }[]; isFullWidth?: boolean; } const OptionalTooltipWrapper = ({ children, tooltipText, }: { children: ReactNode; tooltipText?: ReactNode; }) => { if (tooltipText) { return ( {children} ); } return <>{children}; }; export const ToggleGroup = ({ options, onValueChange, isFullWidth, ...props }: ToggleGroupProps) => { const [value, setValue] = useState(props.defaultValue); const activeRef = useRef(null); useEffect(() => { if (value && onValueChange) onValueChange(value); }, [value, onValueChange]); return ( <> {/* Active toggle. It's a separate element so we can animate it nicely. */} {options.map((option) => ( { if (node && value === option.value) { // Sets position of active toggle element with inline styles. // This way we trigger as little rerenders as possible. if (!activeRef.current || activeRef?.current.style.left === `${node.offsetLeft}px`) return; activeRef.current.style.left = `${node.offsetLeft}px`; activeRef.current.style.width = `${node.offsetWidth}px`; } return node; }}>
{option.iconLeft && {option.iconLeft}} {option.label}
))}
); };