-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdropdownContainer.tsx
More file actions
142 lines (135 loc) · 4.71 KB
/
Copy pathdropdownContainer.tsx
File metadata and controls
142 lines (135 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import * as RadixDropdown from '@radix-ui/react-dropdown-menu';
import classNames from 'classnames';
import { useEffect, useState, type ComponentProps, type ReactNode } from 'react';
import { Button, type ButtonVariant, type IButtonProps } from '../../button';
import { IconType } from '../../icon';
export interface IDropdownContainerProps extends Omit<ComponentProps<'div'>, 'dir'> {
/**
* Size of the dropdown trigger.
* @default lg
*/
size?: IButtonProps['size'];
/**
* Custom dropdown trigger displayed instead of the default button.
*/
customTrigger?: ReactNode;
/**
* Size of the dropdown trigger depending on the current breakpoint.
*/
responsiveSize?: IButtonProps['responsiveSize'];
/**
* Label of the dropdown trigger.
*/
label?: string;
/**
* Alignment of the dropdown content.
* @default start
*/
align?: RadixDropdown.DropdownMenuContentProps['align'];
/**
* Hides the dropdown trigger icon when set to true. This property has no effect when the label property
* is not set or is empty.
*/
hideIcon?: boolean;
/**
* Disables the dropdown when set to true.
*/
disabled?: boolean;
/**
* Whether the dropdown is open by default.
*/
defaultOpen?: boolean;
/**
* Whether the dropdown is open.
*/
open?: boolean;
/**
* Callback when the open state changes.
*/
onOpenChange?: (open: boolean) => void;
/**
* Sets a max width to the dropdown content as the remaining width between the trigger and the boundary edge.
* @default true
*/
constrainContentWidth?: boolean;
/**
* Sets a max height to the dropdown content as the remaining height between the trigger and the boundary edge.
* @default true
*/
constrainContentHeight?: boolean;
/**
* Additional classnames for the dropdown container (e.g. for setting a max width for the dropdown items).
*/
contentClassNames?: string;
/**
* Variant of the dropdown.
* @default tertiary
*/
variant?: ButtonVariant;
}
export const DropdownContainer: React.FC<IDropdownContainerProps> = (props) => {
const {
children,
size = 'lg',
responsiveSize,
label,
defaultOpen,
hideIcon,
open,
onOpenChange,
disabled,
align = 'start',
customTrigger,
constrainContentWidth = true,
constrainContentHeight = true,
contentClassNames,
variant = 'tertiary',
...otherProps
} = props;
const [isOpen, setIsOpen] = useState(open ?? defaultOpen);
const handleOpenChange = (open: boolean) => {
setIsOpen(open);
onOpenChange?.(open);
};
const triggerIcon = isOpen ? IconType.CHEVRON_UP : IconType.CHEVRON_DOWN;
const hasLabel = label != null && label.length > 0;
// Update internal isOpen state on open property change
useEffect(() => {
setIsOpen(open);
}, [open]);
return (
<RadixDropdown.Root open={open} defaultOpen={defaultOpen} onOpenChange={handleOpenChange} {...otherProps}>
<RadixDropdown.Trigger className="group" asChild={true} disabled={disabled}>
{customTrigger ?? (
<Button
variant={variant}
size={size}
responsiveSize={responsiveSize}
iconLeft={!hasLabel ? triggerIcon : undefined}
iconRight={hideIcon ? undefined : triggerIcon}
disabled={disabled}
className={isOpen ? 'border-neutral-300' : undefined}
>
{label}
</Button>
)}
</RadixDropdown.Trigger>
<RadixDropdown.Portal>
<RadixDropdown.Content
className={classNames(
'bg-neutral-0 shadow-neutral-sm flex min-w-48 flex-col gap-1.5 overflow-auto rounded-xl border border-neutral-100 p-2',
'z-[var(--guk-dropdown-container-content-z-index)]',
{ 'max-h-[var(--radix-dropdown-menu-content-available-height)]': constrainContentHeight },
{ 'max-w-[var(--radix-dropdown-menu-content-available-width)]': constrainContentWidth },
contentClassNames,
)}
onCloseAutoFocus={(event) => event.preventDefault()}
align={align}
sideOffset={4}
>
{children}
</RadixDropdown.Content>
</RadixDropdown.Portal>
</RadixDropdown.Root>
);
};