Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions src/lib/components/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import type { ComponentProps, FC, PropsWithChildren, ReactElement, ReactNode } from 'react';
import React, { Children, useCallback, useMemo, useState } from 'react';
import type { ComponentProps, Dispatch, FC, PropsWithChildren, ReactElement, ReactNode, SetStateAction } from 'react';
import React, { Children, useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { HiOutlineChevronDown, HiOutlineChevronLeft, HiOutlineChevronRight, HiOutlineChevronUp } from 'react-icons/hi';
import type { DeepPartial } from '..';
import { mergeDeep } from '../../helpers/mergeDeep';
Expand Down Expand Up @@ -43,6 +43,10 @@ export interface DropdownProps
theme?: DeepPartial<FlowbiteDropdownTheme>;
}

export interface TriggerWrapperProps extends ButtonProps {
setButtonWidth?: Dispatch<SetStateAction<number | undefined>>;
}

const icons: Record<string, FC<ComponentProps<'svg'>>> = {
top: HiOutlineChevronUp,
right: HiOutlineChevronRight,
Expand Down Expand Up @@ -75,6 +79,7 @@ const DropdownComponent: FC<DropdownProps> = ({
}, [placement]);

const [closeRequestKey, setCloseRequestKey] = useState<string | undefined>(undefined);
const [buttonWidth, setButtonWidth] = useState<number | undefined>(undefined);

// Extends DropdownItem's onClick to trigger a close request to the Floating component
const attachCloseListener = useCallback(
Expand Down Expand Up @@ -105,8 +110,23 @@ const DropdownComponent: FC<DropdownProps> = ({
[attachCloseListener, children, theme.content],
);

const TriggerWrapper: FC<ButtonProps> = ({ children }) =>
inline ? <button className={theme.inlineWrapper}>{children}</button> : <Button {...buttonProps}>{children}</Button>;
const TriggerWrapper: FC<TriggerWrapperProps> = ({ children, setButtonWidth }): JSX.Element => {
const ref = useRef<HTMLButtonElement | null>(null);

useEffect(() => {
if (ref.current) setButtonWidth?.(ref.current.clientWidth);
}, [ref]);

return inline ? (
<button ref={ref} className={theme.inlineWrapper}>
{children}
</button>
) : (
<Button ref={ref} {...buttonProps}>
{children}
</Button>
);
};

return (
<Floating
Expand All @@ -119,8 +139,9 @@ const DropdownComponent: FC<DropdownProps> = ({
theme={theme.floating}
closeRequestKey={closeRequestKey}
className={className}
minWidth={buttonWidth}
>
<TriggerWrapper>
<TriggerWrapper setButtonWidth={setButtonWidth}>
{label}
{arrowIcon && <Icon className={theme.arrowIcon} />}
</TriggerWrapper>
Expand Down
5 changes: 5 additions & 0 deletions src/lib/components/Floating/Floating.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface FloatingProps extends PropsWithChildren<Omit<ComponentProps<'di
animation?: false | `duration-${number}`;
arrow?: boolean;
closeRequestKey?: string;
minWidth?: number;
content: ReactNode;
placement?: 'auto' | Placement;
style?: 'dark' | 'light' | 'auto';
Expand All @@ -63,6 +64,9 @@ export const Floating: FC<FloatingProps> = ({
style = 'dark',
theme,
trigger = 'hover',
closeRequestKey,
className,
minWidth,
...props
}) => {
const arrowRef = useRef<HTMLDivElement>(null);
Expand Down Expand Up @@ -127,6 +131,7 @@ export const Floating: FC<FloatingProps> = ({
position: strategy,
top: y ?? ' ',
left: x ?? ' ',
minWidth,
},
...props,
})}
Expand Down