2025-03-09 21:59:21 +03:00
|
|
|
import React from 'react';
|
2023-12-15 17:34:50 +03:00
|
|
|
import clsx from 'clsx';
|
2023-12-25 16:53:27 +03:00
|
|
|
|
2025-02-21 21:15:05 +03:00
|
|
|
import { type Styling } from '../props';
|
2025-02-10 01:32:55 +03:00
|
|
|
|
2025-02-21 21:15:05 +03:00
|
|
|
interface DropdownProps extends Styling {
|
2025-03-09 21:59:21 +03:00
|
|
|
/** Reference to the dropdown element. */
|
|
|
|
ref?: React.Ref<HTMLDivElement>;
|
|
|
|
|
2025-03-07 02:46:19 +03:00
|
|
|
/** Unique ID for the dropdown. */
|
|
|
|
id?: string;
|
|
|
|
|
|
|
|
/** Margin for the dropdown. */
|
|
|
|
margin?: string;
|
|
|
|
|
2024-09-12 16:42:02 +03:00
|
|
|
/** Indicates whether the dropdown should stretch to the left. */
|
2023-12-28 14:04:44 +03:00
|
|
|
stretchLeft?: boolean;
|
2024-09-12 16:42:02 +03:00
|
|
|
|
|
|
|
/** Indicates whether the dropdown should stretch to the top. */
|
2024-06-26 19:00:29 +03:00
|
|
|
stretchTop?: boolean;
|
2024-09-12 16:42:02 +03:00
|
|
|
|
|
|
|
/** Indicates whether the dropdown is open. */
|
2023-12-28 14:04:44 +03:00
|
|
|
isOpen: boolean;
|
2023-07-20 17:11:03 +03:00
|
|
|
}
|
|
|
|
|
2024-09-12 16:42:02 +03:00
|
|
|
/**
|
2024-10-30 21:35:55 +03:00
|
|
|
* Animated list of children with optional positioning and visibility control.
|
2025-03-07 20:38:40 +03:00
|
|
|
* Note: Dropdown should be inside a relative container.
|
2024-09-12 16:42:02 +03:00
|
|
|
*/
|
2025-02-07 10:54:47 +03:00
|
|
|
export function Dropdown({
|
2024-09-19 17:49:25 +03:00
|
|
|
isOpen,
|
|
|
|
stretchLeft,
|
|
|
|
stretchTop,
|
2025-03-07 02:46:19 +03:00
|
|
|
margin,
|
2024-09-19 17:49:25 +03:00
|
|
|
className,
|
|
|
|
children,
|
|
|
|
...restProps
|
|
|
|
}: React.PropsWithChildren<DropdownProps>) {
|
2023-07-20 17:11:03 +03:00
|
|
|
return (
|
2025-03-07 02:46:19 +03:00
|
|
|
<div
|
|
|
|
tabIndex={-1}
|
|
|
|
className={clsx(
|
2025-03-13 14:42:48 +03:00
|
|
|
'cc-dropdown isolate z-topmost absolute grid bg-prim-0 border rounded-md shadow-lg text-sm',
|
|
|
|
stretchLeft ? 'right-0' : 'left-0',
|
|
|
|
stretchTop ? 'bottom-0' : 'top-full',
|
|
|
|
isOpen && 'open',
|
2025-03-07 02:46:19 +03:00
|
|
|
margin,
|
|
|
|
className
|
|
|
|
)}
|
|
|
|
aria-hidden={!isOpen}
|
|
|
|
{...restProps}
|
|
|
|
>
|
|
|
|
{children}
|
2023-12-28 14:04:44 +03:00
|
|
|
</div>
|
|
|
|
);
|
2023-07-20 17:11:03 +03:00
|
|
|
}
|