mirror of
https://github.com/stackblitz/bolt.new
synced 2024-11-27 22:42:21 +00:00
63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
|
import { motion } from 'framer-motion';
|
||
|
import { memo } from 'react';
|
||
|
import { classNames } from '~/utils/classNames';
|
||
|
import { genericMemo } from '~/utils/react';
|
||
|
|
||
|
interface SliderOption<T> {
|
||
|
value: T;
|
||
|
text: string;
|
||
|
}
|
||
|
|
||
|
export interface SliderOptions<T> {
|
||
|
left: SliderOption<T>;
|
||
|
right: SliderOption<T>;
|
||
|
}
|
||
|
|
||
|
interface SliderProps<T> {
|
||
|
selected: T;
|
||
|
options: SliderOptions<T>;
|
||
|
setSelected?: (selected: T) => void;
|
||
|
}
|
||
|
|
||
|
export const Slider = genericMemo(<T,>({ selected, options, setSelected }: SliderProps<T>) => {
|
||
|
const isLeftSelected = selected === options.left.value;
|
||
|
|
||
|
return (
|
||
|
<div className="flex items-center flex-wrap gap-1 border rounded-lg p-1">
|
||
|
<SliderButton selected={isLeftSelected} setSelected={() => setSelected?.(options.left.value)}>
|
||
|
{options.left.text}
|
||
|
</SliderButton>
|
||
|
<SliderButton selected={!isLeftSelected} setSelected={() => setSelected?.(options.right.value)}>
|
||
|
{options.right.text}
|
||
|
</SliderButton>
|
||
|
</div>
|
||
|
);
|
||
|
});
|
||
|
|
||
|
interface SliderButtonProps {
|
||
|
selected: boolean;
|
||
|
children: string | JSX.Element | Array<JSX.Element | string>;
|
||
|
setSelected: () => void;
|
||
|
}
|
||
|
|
||
|
const SliderButton = memo(({ selected, children, setSelected }: SliderButtonProps) => {
|
||
|
return (
|
||
|
<button
|
||
|
onClick={setSelected}
|
||
|
className={classNames(
|
||
|
'bg-transparent text-sm transition-colors px-2.5 py-0.5 rounded-md relative',
|
||
|
selected ? 'text-white' : 'text-gray-600 hover:text-accent-600 hover:bg-accent-600/10',
|
||
|
)}
|
||
|
>
|
||
|
<span className="relative z-10">{children}</span>
|
||
|
{selected && (
|
||
|
<motion.span
|
||
|
layoutId="pill-tab"
|
||
|
transition={{ type: 'spring', duration: 0.5 }}
|
||
|
className="absolute inset-0 z-0 bg-accent-600 rounded-md"
|
||
|
></motion.span>
|
||
|
)}
|
||
|
</button>
|
||
|
);
|
||
|
});
|