1880 Refactored Dropdown components (#1884)

* updated DropdownButton props

* refactoring in progress

* working but layout is wrong

* fixed bug

* wip on ColumnHeadWithDropdown

* fix bug

* fix bug

* remove unused styled component

* fix z-index bug

* add an optional argument to DropdownMenu to control the offset of the menu

* add an optional argument to DropdownMenu to control the offset of the menu

* modify files after PR comments

* clean the way the offset is handled

* fix lint
This commit is contained in:
bosiraphael
2023-10-05 18:11:54 +02:00
committed by GitHub
parent b8282e6789
commit 922f8eca0e
23 changed files with 213 additions and 182 deletions

View File

@ -2,17 +2,17 @@ import { CompanyProgressPicker } from '@/companies/components/CompanyProgressPic
import { useCreateCompanyProgress } from '@/companies/hooks/useCreateCompanyProgress'; import { useCreateCompanyProgress } from '@/companies/hooks/useCreateCompanyProgress';
import { PageHotkeyScope } from '@/types/PageHotkeyScope'; import { PageHotkeyScope } from '@/types/PageHotkeyScope';
import { IconButton } from '@/ui/button/components/IconButton'; import { IconButton } from '@/ui/button/components/IconButton';
import { DropdownButton } from '@/ui/dropdown/components/DropdownButton'; import { useDropdown } from '@/ui/dropdown/hooks/useDropdown';
import { useDropdownButton } from '@/ui/dropdown/hooks/useDropdownButton';
import { IconPlus } from '@/ui/icon/index'; import { IconPlus } from '@/ui/icon/index';
import { EntityForSelect } from '@/ui/input/relation-picker/types/EntityForSelect'; import { EntityForSelect } from '@/ui/input/relation-picker/types/EntityForSelect';
import { RelationPickerHotkeyScope } from '@/ui/input/relation-picker/types/RelationPickerHotkeyScope'; import { RelationPickerHotkeyScope } from '@/ui/input/relation-picker/types/RelationPickerHotkeyScope';
import { useSnackBar } from '@/ui/snack-bar/hooks/useSnackBar'; import { useSnackBar } from '@/ui/snack-bar/hooks/useSnackBar';
import { ViewBarDropdownButton } from '@/ui/view-bar/components/ViewBarDropdownButton';
export const PipelineAddButton = () => { export const PipelineAddButton = () => {
const { enqueueSnackBar } = useSnackBar(); const { enqueueSnackBar } = useSnackBar();
const { closeDropdownButton, toggleDropdownButton } = useDropdownButton({ const { closeDropdown, toggleDropdown } = useDropdown({
dropdownId: 'add-pipeline-progress', dropdownId: 'add-pipeline-progress',
}); });
@ -47,28 +47,28 @@ export const PipelineAddButton = () => {
console.error('There was a problem with the pipeline stage selection.'); console.error('There was a problem with the pipeline stage selection.');
return; return;
} }
closeDropdownButton(); closeDropdown();
createCompanyProgress(selectedCompany.id, selectedPipelineStageId); createCompanyProgress(selectedCompany.id, selectedPipelineStageId);
}; };
return ( return (
<DropdownButton <ViewBarDropdownButton
dropdownId="add-pipeline-progress" dropdownId="add-pipeline-progress"
buttonComponents={ buttonComponent={
<IconButton <IconButton
Icon={IconPlus} Icon={IconPlus}
size="medium" size="medium"
dataTestId="add-company-progress-button" dataTestId="add-company-progress-button"
accent="default" accent="default"
variant="secondary" variant="secondary"
onClick={toggleDropdownButton} onClick={toggleDropdown}
/> />
} }
dropdownComponents={ dropdownComponents={
<CompanyProgressPicker <CompanyProgressPicker
companyId={null} companyId={null}
onSubmit={handleCompanySelected} onSubmit={handleCompanySelected}
onCancel={closeDropdownButton} onCancel={closeDropdown}
/> />
} }
hotkey={{ hotkey={{

View File

@ -1,6 +1,6 @@
import { useResetRecoilState } from 'recoil'; import { useResetRecoilState } from 'recoil';
import { DropdownButton } from '@/ui/dropdown/components/DropdownButton'; import { ViewBarDropdownButton } from '@/ui/view-bar/components/ViewBarDropdownButton';
import { viewEditModeState } from '@/ui/view-bar/states/viewEditModeState'; import { viewEditModeState } from '@/ui/view-bar/states/viewEditModeState';
import { BoardOptionsDropdownKey } from '../types/BoardOptionsDropdownKey'; import { BoardOptionsDropdownKey } from '../types/BoardOptionsDropdownKey';
@ -23,8 +23,8 @@ export const BoardOptionsDropdown = ({
const resetViewEditMode = useResetRecoilState(viewEditModeState); const resetViewEditMode = useResetRecoilState(viewEditModeState);
return ( return (
<DropdownButton <ViewBarDropdownButton
buttonComponents={<BoardOptionsDropdownButton />} buttonComponent={<BoardOptionsDropdownButton />}
dropdownComponents={ dropdownComponents={
<BoardOptionsDropdownContent <BoardOptionsDropdownContent
customHotkeyScope={customHotkeyScope} customHotkeyScope={customHotkeyScope}

View File

@ -1,20 +1,20 @@
import { StyledHeaderDropdownButton } from '@/ui/dropdown/components/StyledHeaderDropdownButton'; import { StyledHeaderDropdownButton } from '@/ui/dropdown/components/StyledHeaderDropdownButton';
import { useDropdownButton } from '@/ui/dropdown/hooks/useDropdownButton'; import { useDropdown } from '@/ui/dropdown/hooks/useDropdown';
import { BoardOptionsDropdownKey } from '../types/BoardOptionsDropdownKey'; import { BoardOptionsDropdownKey } from '../types/BoardOptionsDropdownKey';
export const BoardOptionsDropdownButton = () => { export const BoardOptionsDropdownButton = () => {
const { isDropdownButtonOpen, toggleDropdownButton } = useDropdownButton({ const { isDropdownOpen, toggleDropdown } = useDropdown({
dropdownId: BoardOptionsDropdownKey, dropdownId: BoardOptionsDropdownKey,
}); });
const handleClick = () => { const handleClick = () => {
toggleDropdownButton(); toggleDropdown();
}; };
return ( return (
<StyledHeaderDropdownButton <StyledHeaderDropdownButton
isUnfolded={isDropdownButtonOpen} isUnfolded={isDropdownOpen}
onClick={handleClick} onClick={handleClick}
> >
Options Options

View File

@ -16,7 +16,7 @@ import { DropdownMenuSearchInput } from '@/ui/dropdown/components/DropdownMenuSe
import { StyledDropdownMenu } from '@/ui/dropdown/components/StyledDropdownMenu'; import { StyledDropdownMenu } from '@/ui/dropdown/components/StyledDropdownMenu';
import { StyledDropdownMenuItemsContainer } from '@/ui/dropdown/components/StyledDropdownMenuItemsContainer'; import { StyledDropdownMenuItemsContainer } from '@/ui/dropdown/components/StyledDropdownMenuItemsContainer';
import { StyledDropdownMenuSeparator } from '@/ui/dropdown/components/StyledDropdownMenuSeparator'; import { StyledDropdownMenuSeparator } from '@/ui/dropdown/components/StyledDropdownMenuSeparator';
import { useDropdownButton } from '@/ui/dropdown/hooks/useDropdownButton'; import { useDropdown } from '@/ui/dropdown/hooks/useDropdown';
import { import {
IconChevronLeft, IconChevronLeft,
IconLayoutKanban, IconLayoutKanban,
@ -144,7 +144,7 @@ export const BoardOptionsDropdownContent = ({
const { handleFieldVisibilityChange } = useBoardCardFields(); const { handleFieldVisibilityChange } = useBoardCardFields();
const { closeDropdownButton } = useDropdownButton({ const { closeDropdown } = useDropdown({
dropdownId: BoardOptionsDropdownKey, dropdownId: BoardOptionsDropdownKey,
}); });
@ -152,7 +152,7 @@ export const BoardOptionsDropdownContent = ({
Key.Escape, Key.Escape,
() => { () => {
resetViewEditMode(); resetViewEditMode();
closeDropdownButton(); closeDropdown();
}, },
customHotkeyScope.scope, customHotkeyScope.scope,
); );
@ -163,7 +163,7 @@ export const BoardOptionsDropdownContent = ({
handleStageSubmit(); handleStageSubmit();
handleViewNameSubmit(); handleViewNameSubmit();
resetViewEditMode(); resetViewEditMode();
closeDropdownButton(); closeDropdown();
}, },
customHotkeyScope.scope, customHotkeyScope.scope,
); );

View File

@ -1,18 +1,12 @@
import { useTheme } from '@emotion/react'; import { useTheme } from '@emotion/react';
import styled from '@emotion/styled'; import styled from '@emotion/styled';
import { useDropdownButton } from '@/ui/dropdown/hooks/useDropdownButton';
import { FieldMetadata } from '@/ui/field/types/FieldMetadata'; import { FieldMetadata } from '@/ui/field/types/FieldMetadata';
import { ColumnDefinition } from '../types/ColumnDefinition'; import { ColumnDefinition } from '../types/ColumnDefinition';
import { EntityTableHeaderOptions } from './EntityTableHeaderOptions'; type ColumnHeadProps = {
type OwnProps = {
column: ColumnDefinition<FieldMetadata>; column: ColumnDefinition<FieldMetadata>;
isFirstColumn: boolean;
isLastColumn: boolean;
primaryColumnKey: string;
}; };
const StyledTitle = styled.div` const StyledTitle = styled.div`
@ -41,32 +35,17 @@ const StyledText = styled.span`
white-space: nowrap; white-space: nowrap;
`; `;
export const ColumnHead = ({ export const ColumnHead = ({ column }: ColumnHeadProps) => {
column,
isFirstColumn,
isLastColumn,
primaryColumnKey,
}: OwnProps) => {
const theme = useTheme(); const theme = useTheme();
const { openDropdownButton } = useDropdownButton({
dropdownId: column.key + '-header',
});
return ( return (
<> <>
<StyledTitle onClick={openDropdownButton}> <StyledTitle>
<StyledIcon> <StyledIcon>
{column.Icon && <column.Icon size={theme.icon.size.md} />} {column.Icon && <column.Icon size={theme.icon.size.md} />}
</StyledIcon> </StyledIcon>
<StyledText>{column.name}</StyledText> <StyledText>{column.name}</StyledText>
</StyledTitle> </StyledTitle>
<EntityTableHeaderOptions
column={column}
isFirstColumn={isFirstColumn}
isLastColumn={isLastColumn}
primaryColumnKey={primaryColumnKey}
/>
</> </>
); );
}; };

View File

@ -0,0 +1,38 @@
import { DropdownMenu } from '@/ui/dropdown/components/DropdownMenu';
import { FieldMetadata } from '@/ui/field/types/FieldMetadata';
import { ColumnDefinition } from '../types/ColumnDefinition';
import { ColumnHead } from './ColumnHead';
import { TableColumnDropdownMenu } from './TableColumnDropdownMenu';
type ColumnHeadProps = {
column: ColumnDefinition<FieldMetadata>;
isFirstColumn: boolean;
isLastColumn: boolean;
primaryColumnKey: string;
};
export const ColumnHeadWithDropdown = ({
column,
isFirstColumn,
isLastColumn,
primaryColumnKey,
}: ColumnHeadProps) => {
return (
<DropdownMenu
clickableComponent={<ColumnHead column={column} />}
dropdownId={column.key + '-header'}
dropdownComponents={
<TableColumnDropdownMenu
column={column}
isFirstColumn={isFirstColumn}
isLastColumn={isLastColumn}
primaryColumnKey={primaryColumnKey}
/>
}
dropdownHotkeyScope={{ scope: column.key + '-header' }}
dropdownOffset={{ x: 0, y: -8 }}
/>
);
};

View File

@ -17,7 +17,7 @@ import { tableColumnsByKeyScopedSelector } from '../states/selectors/tableColumn
import { visibleTableColumnsScopedSelector } from '../states/selectors/visibleTableColumnsScopedSelector'; import { visibleTableColumnsScopedSelector } from '../states/selectors/visibleTableColumnsScopedSelector';
import { tableColumnsScopedState } from '../states/tableColumnsScopedState'; import { tableColumnsScopedState } from '../states/tableColumnsScopedState';
import { ColumnHead } from './ColumnHead'; import { ColumnHeadWithDropdown } from './ColumnHeadWithDropdown';
import { EntityTableColumnMenu } from './EntityTableColumnMenu'; import { EntityTableColumnMenu } from './EntityTableColumnMenu';
import { SelectAllCheckbox } from './SelectAllCheckbox'; import { SelectAllCheckbox } from './SelectAllCheckbox';
@ -76,6 +76,11 @@ const StyledTableHead = styled.thead`
cursor: pointer; cursor: pointer;
`; `;
const StyledColumnHeadContainer = styled.div`
position: relative;
z-index: 1;
`;
export const EntityTableHeader = () => { export const EntityTableHeader = () => {
const [resizeFieldOffset, setResizeFieldOffset] = useRecoilState( const [resizeFieldOffset, setResizeFieldOffset] = useRecoilState(
resizeFieldOffsetState, resizeFieldOffsetState,
@ -183,12 +188,14 @@ export const EntityTableHeader = () => {
COLUMN_MIN_WIDTH, COLUMN_MIN_WIDTH,
)} )}
> >
<ColumnHead <StyledColumnHeadContainer>
column={column} <ColumnHeadWithDropdown
isFirstColumn={index === 1} column={column}
isLastColumn={index === visibleTableColumns.length - 1} isFirstColumn={index === 1}
primaryColumnKey={primaryColumn.key} isLastColumn={index === visibleTableColumns.length - 1}
/> primaryColumnKey={primaryColumn.key}
/>
</StyledColumnHeadContainer>
<StyledResizeHandler <StyledResizeHandler
className="cursor-col-resize" className="cursor-col-resize"

View File

@ -1,39 +0,0 @@
import styled from '@emotion/styled';
import { DropdownButton } from '@/ui/dropdown/components/DropdownButton';
import {
EntityTableHeaderOptionsProps,
TableColumnDropdownMenu,
} from './TableColumnDropdownMenu';
const StyledDropdownContainer = styled.div`
left: 0px;
position: absolute;
top: 32px;
z-index: 1;
`;
export const EntityTableHeaderOptions = ({
isFirstColumn,
isLastColumn,
primaryColumnKey,
column,
}: EntityTableHeaderOptionsProps) => {
return (
<StyledDropdownContainer>
<DropdownButton
dropdownId={column.key + '-header'}
dropdownComponents={
<TableColumnDropdownMenu
column={column}
isFirstColumn={isFirstColumn}
isLastColumn={isLastColumn}
primaryColumnKey={primaryColumnKey}
/>
}
dropdownHotkeyScope={{ scope: column.key + '-header' }}
/>
</StyledDropdownContainer>
);
};

View File

@ -1,6 +1,6 @@
import { StyledDropdownMenu } from '@/ui/dropdown/components/StyledDropdownMenu'; import { StyledDropdownMenu } from '@/ui/dropdown/components/StyledDropdownMenu';
import { StyledDropdownMenuItemsContainer } from '@/ui/dropdown/components/StyledDropdownMenuItemsContainer'; import { StyledDropdownMenuItemsContainer } from '@/ui/dropdown/components/StyledDropdownMenuItemsContainer';
import { useDropdownButton } from '@/ui/dropdown/hooks/useDropdownButton'; import { useDropdown } from '@/ui/dropdown/hooks/useDropdown';
import { FieldMetadata } from '@/ui/field/types/FieldMetadata'; import { FieldMetadata } from '@/ui/field/types/FieldMetadata';
import { IconArrowLeft, IconArrowRight, IconEyeOff } from '@/ui/icon'; import { IconArrowLeft, IconArrowRight, IconEyeOff } from '@/ui/icon';
import { MenuItem } from '@/ui/menu-item/components/MenuItem'; import { MenuItem } from '@/ui/menu-item/components/MenuItem';
@ -25,12 +25,12 @@ export const TableColumnDropdownMenu = ({
const { handleColumnVisibilityChange, handleMoveTableColumn } = const { handleColumnVisibilityChange, handleMoveTableColumn } =
useTableColumns(); useTableColumns();
const { closeDropdownButton } = useDropdownButton({ const { closeDropdown } = useDropdown({
dropdownId: ColumnHeadDropdownId, dropdownId: ColumnHeadDropdownId,
}); });
const handleColumnMoveLeft = () => { const handleColumnMoveLeft = () => {
closeDropdownButton(); closeDropdown();
if (isFirstColumn) { if (isFirstColumn) {
return; return;
} }
@ -38,7 +38,7 @@ export const TableColumnDropdownMenu = ({
}; };
const handleColumnMoveRight = () => { const handleColumnMoveRight = () => {
closeDropdownButton(); closeDropdown();
if (isLastColumn) { if (isLastColumn) {
return; return;
} }

View File

@ -1,7 +1,7 @@
import { useResetRecoilState } from 'recoil'; import { useResetRecoilState } from 'recoil';
import { DropdownButton } from '@/ui/dropdown/components/DropdownButton';
import { HotkeyScope } from '@/ui/utilities/hotkey/types/HotkeyScope'; import { HotkeyScope } from '@/ui/utilities/hotkey/types/HotkeyScope';
import { ViewBarDropdownButton } from '@/ui/view-bar/components/ViewBarDropdownButton';
import { viewEditModeState } from '@/ui/view-bar/states/viewEditModeState'; import { viewEditModeState } from '@/ui/view-bar/states/viewEditModeState';
import { TableOptionsDropdownId } from '../../constants/TableOptionsDropdownId'; import { TableOptionsDropdownId } from '../../constants/TableOptionsDropdownId';
@ -19,8 +19,8 @@ export const TableOptionsDropdown = ({
const resetViewEditMode = useResetRecoilState(viewEditModeState); const resetViewEditMode = useResetRecoilState(viewEditModeState);
return ( return (
<DropdownButton <ViewBarDropdownButton
buttonComponents={<TableOptionsDropdownButton />} buttonComponent={<TableOptionsDropdownButton />}
dropdownHotkeyScope={customHotkeyScope} dropdownHotkeyScope={customHotkeyScope}
dropdownId={TableOptionsDropdownId} dropdownId={TableOptionsDropdownId}
dropdownComponents={<TableOptionsDropdownContent />} dropdownComponents={<TableOptionsDropdownContent />}

View File

@ -1,16 +1,16 @@
import { TableOptionsDropdownId } from '@/ui/data-table/constants/TableOptionsDropdownId'; import { TableOptionsDropdownId } from '@/ui/data-table/constants/TableOptionsDropdownId';
import { StyledHeaderDropdownButton } from '@/ui/dropdown/components/StyledHeaderDropdownButton'; import { StyledHeaderDropdownButton } from '@/ui/dropdown/components/StyledHeaderDropdownButton';
import { useDropdownButton } from '@/ui/dropdown/hooks/useDropdownButton'; import { useDropdown } from '@/ui/dropdown/hooks/useDropdown';
export const TableOptionsDropdownButton = () => { export const TableOptionsDropdownButton = () => {
const { isDropdownButtonOpen, toggleDropdownButton } = useDropdownButton({ const { isDropdownOpen, toggleDropdown } = useDropdown({
dropdownId: TableOptionsDropdownId, dropdownId: TableOptionsDropdownId,
}); });
return ( return (
<StyledHeaderDropdownButton <StyledHeaderDropdownButton
isUnfolded={isDropdownButtonOpen} isUnfolded={isDropdownOpen}
onClick={toggleDropdownButton} onClick={toggleDropdown}
> >
Options Options
</StyledHeaderDropdownButton> </StyledHeaderDropdownButton>

View File

@ -9,7 +9,7 @@ import { DropdownMenuInputContainer } from '@/ui/dropdown/components/DropdownMen
import { StyledDropdownMenu } from '@/ui/dropdown/components/StyledDropdownMenu'; import { StyledDropdownMenu } from '@/ui/dropdown/components/StyledDropdownMenu';
import { StyledDropdownMenuItemsContainer } from '@/ui/dropdown/components/StyledDropdownMenuItemsContainer'; import { StyledDropdownMenuItemsContainer } from '@/ui/dropdown/components/StyledDropdownMenuItemsContainer';
import { StyledDropdownMenuSeparator } from '@/ui/dropdown/components/StyledDropdownMenuSeparator'; import { StyledDropdownMenuSeparator } from '@/ui/dropdown/components/StyledDropdownMenuSeparator';
import { useDropdownButton } from '@/ui/dropdown/hooks/useDropdownButton'; import { useDropdown } from '@/ui/dropdown/hooks/useDropdown';
import { IconChevronLeft, IconFileImport, IconTag } from '@/ui/icon'; import { IconChevronLeft, IconFileImport, IconTag } from '@/ui/icon';
import { MenuItem } from '@/ui/menu-item/components/MenuItem'; import { MenuItem } from '@/ui/menu-item/components/MenuItem';
import { useScopedHotkeys } from '@/ui/utilities/hotkey/hooks/useScopedHotkeys'; import { useScopedHotkeys } from '@/ui/utilities/hotkey/hooks/useScopedHotkeys';
@ -37,7 +37,7 @@ export const TableOptionsDropdownContent = () => {
const scopeId = useRecoilScopeId(TableRecoilScopeContext); const scopeId = useRecoilScopeId(TableRecoilScopeContext);
const { onImport } = useContext(ViewBarContext); const { onImport } = useContext(ViewBarContext);
const { closeDropdownButton } = useDropdownButton({ const { closeDropdown } = useDropdown({
dropdownId: TableOptionsDropdownId, dropdownId: TableOptionsDropdownId,
}); });
@ -114,7 +114,7 @@ export const TableOptionsDropdownContent = () => {
Key.Escape, Key.Escape,
() => { () => {
resetViewEditMode(); resetViewEditMode();
closeDropdownButton(); closeDropdown();
}, },
TableOptionsHotkeyScope.Dropdown, TableOptionsHotkeyScope.Dropdown,
); );
@ -125,7 +125,7 @@ export const TableOptionsDropdownContent = () => {
handleViewNameSubmit(); handleViewNameSubmit();
resetMenu(); resetMenu();
resetViewEditMode(); resetViewEditMode();
closeDropdownButton(); closeDropdown();
}, },
TableOptionsHotkeyScope.Dropdown, TableOptionsHotkeyScope.Dropdown,
); );

View File

@ -8,13 +8,13 @@ import { useScopedHotkeys } from '@/ui/utilities/hotkey/hooks/useScopedHotkeys';
import { HotkeyScope } from '@/ui/utilities/hotkey/types/HotkeyScope'; import { HotkeyScope } from '@/ui/utilities/hotkey/types/HotkeyScope';
import { useListenClickOutside } from '@/ui/utilities/pointer-event/hooks/useListenClickOutside'; import { useListenClickOutside } from '@/ui/utilities/pointer-event/hooks/useListenClickOutside';
import { useDropdownButton } from '../hooks/useDropdownButton'; import { useDropdown } from '../hooks/useDropdown';
import { useInternalHotkeyScopeManagement } from '../hooks/useInternalHotkeyScopeManagement'; import { useInternalHotkeyScopeManagement } from '../hooks/useInternalHotkeyScopeManagement';
import { DropdownToggleEffect } from './DropdownToggleEffect'; import { DropdownToggleEffect } from './DropdownToggleEffect';
type OwnProps = { type DropdownMenuProps = {
buttonComponents?: JSX.Element | JSX.Element[]; clickableComponent?: JSX.Element | JSX.Element[];
dropdownComponents: JSX.Element | JSX.Element[]; dropdownComponents: JSX.Element | JSX.Element[];
dropdownId: string; dropdownId: string;
hotkey?: { hotkey?: {
@ -23,36 +23,40 @@ type OwnProps = {
}; };
dropdownHotkeyScope: HotkeyScope; dropdownHotkeyScope: HotkeyScope;
dropdownPlacement?: Placement; dropdownPlacement?: Placement;
dropdownOffset?: { x: number; y: number };
onClickOutside?: () => void; onClickOutside?: () => void;
onClose?: () => void; onClose?: () => void;
onOpen?: () => void; onOpen?: () => void;
}; };
export const DropdownButton = ({ export const DropdownMenu = ({
buttonComponents, clickableComponent,
dropdownComponents, dropdownComponents,
dropdownId, dropdownId,
hotkey, hotkey,
dropdownHotkeyScope, dropdownHotkeyScope,
dropdownPlacement = 'bottom-end', dropdownPlacement = 'bottom-end',
dropdownOffset = { x: 0, y: 0 },
onClickOutside, onClickOutside,
onClose, onClose,
onOpen, onOpen,
}: OwnProps) => { }: DropdownMenuProps) => {
const containerRef = useRef<HTMLDivElement>(null); const containerRef = useRef<HTMLDivElement>(null);
const { isDropdownButtonOpen, toggleDropdownButton, closeDropdownButton } = const { isDropdownOpen, toggleDropdown, closeDropdown } = useDropdown({
useDropdownButton({ dropdownId,
dropdownId, });
});
const { refs, floatingStyles } = useFloating({ const { refs, floatingStyles } = useFloating({
placement: dropdownPlacement, placement: dropdownPlacement,
middleware: [flip(), offset({ mainAxis: 8, crossAxis: 0 })], middleware: [
flip(),
offset({ mainAxis: dropdownOffset.y, crossAxis: dropdownOffset.x }),
],
}); });
const handleHotkeyTriggered = () => { const handleHotkeyTriggered = () => {
toggleDropdownButton(); toggleDropdown();
}; };
useListenClickOutside({ useListenClickOutside({
@ -60,8 +64,8 @@ export const DropdownButton = ({
callback: () => { callback: () => {
onClickOutside?.(); onClickOutside?.();
if (isDropdownButtonOpen) { if (isDropdownOpen) {
closeDropdownButton(); closeDropdown();
} }
}, },
}); });
@ -74,24 +78,25 @@ export const DropdownButton = ({
useScopedHotkeys( useScopedHotkeys(
Key.Escape, Key.Escape,
() => { () => {
closeDropdownButton(); closeDropdown();
}, },
dropdownHotkeyScope.scope, dropdownHotkeyScope.scope,
[closeDropdownButton], [closeDropdown],
); );
return ( return (
<div ref={containerRef}> <div ref={containerRef}>
{clickableComponent && (
<div ref={refs.setReference} onClick={toggleDropdown}>
{clickableComponent}
</div>
)}
{hotkey && ( {hotkey && (
<HotkeyEffect <HotkeyEffect
hotkey={hotkey} hotkey={hotkey}
onHotkeyTriggered={handleHotkeyTriggered} onHotkeyTriggered={handleHotkeyTriggered}
/> />
)} )}
{buttonComponents && ( {isDropdownOpen && (
<div ref={refs.setReference}>{buttonComponents}</div>
)}
{isDropdownButtonOpen && (
<div data-select-disable ref={refs.setFloating} style={floatingStyles}> <div data-select-disable ref={refs.setFloating} style={floatingStyles}>
{dropdownComponents} {dropdownComponents}
</div> </div>

View File

@ -1,6 +1,6 @@
import { useEffect } from 'react'; import { useEffect } from 'react';
import { useDropdownButton } from '../hooks/useDropdownButton'; import { useDropdown } from '@/ui/dropdown/hooks/useDropdown';
export const DropdownToggleEffect = ({ export const DropdownToggleEffect = ({
dropdownId, dropdownId,
@ -11,15 +11,15 @@ export const DropdownToggleEffect = ({
onDropdownClose?: () => void; onDropdownClose?: () => void;
onDropdownOpen?: () => void; onDropdownOpen?: () => void;
}) => { }) => {
const { isDropdownButtonOpen } = useDropdownButton({ dropdownId }); const { isDropdownOpen } = useDropdown({ dropdownId });
useEffect(() => { useEffect(() => {
if (isDropdownButtonOpen) { if (isDropdownOpen) {
onDropdownOpen?.(); onDropdownOpen?.();
} else { } else {
onDropdownClose?.(); onDropdownClose?.();
} }
}, [isDropdownButtonOpen, onDropdownClose, onDropdownOpen]); }, [isDropdownOpen, onDropdownClose, onDropdownOpen]);
return null; return null;
}; };

View File

@ -5,7 +5,7 @@ import { dropdownButtonHotkeyScopeScopedFamilyState } from '../states/dropdownBu
import { isDropdownButtonOpenScopedFamilyState } from '../states/isDropdownButtonOpenScopedFamilyState'; import { isDropdownButtonOpenScopedFamilyState } from '../states/isDropdownButtonOpenScopedFamilyState';
import { DropdownRecoilScopeContext } from '../states/recoil-scope-contexts/DropdownRecoilScopeContext'; import { DropdownRecoilScopeContext } from '../states/recoil-scope-contexts/DropdownRecoilScopeContext';
export const useDropdownButton = ({ dropdownId }: { dropdownId: string }) => { export const useDropdown = ({ dropdownId }: { dropdownId: string }) => {
const { const {
setHotkeyScopeAndMemorizePreviousScope, setHotkeyScopeAndMemorizePreviousScope,
goBackToPreviousHotkeyScope, goBackToPreviousHotkeyScope,
@ -49,9 +49,9 @@ export const useDropdownButton = ({ dropdownId }: { dropdownId: string }) => {
}; };
return { return {
isDropdownButtonOpen, isDropdownOpen: isDropdownButtonOpen,
closeDropdownButton, closeDropdown: closeDropdownButton,
toggleDropdownButton, toggleDropdown: toggleDropdownButton,
openDropdownButton, openDropdown: openDropdownButton,
}; };
}; };

View File

@ -4,12 +4,12 @@ import { useOpenCreateActivityDrawer } from '@/activities/hooks/useOpenCreateAct
import { ActivityTargetableEntity } from '@/activities/types/ActivityTargetableEntity'; import { ActivityTargetableEntity } from '@/activities/types/ActivityTargetableEntity';
import { PageHotkeyScope } from '@/types/PageHotkeyScope'; import { PageHotkeyScope } from '@/types/PageHotkeyScope';
import { IconButton } from '@/ui/button/components/IconButton'; import { IconButton } from '@/ui/button/components/IconButton';
import { DropdownButton } from '@/ui/dropdown/components/DropdownButton';
import { StyledDropdownMenu } from '@/ui/dropdown/components/StyledDropdownMenu'; import { StyledDropdownMenu } from '@/ui/dropdown/components/StyledDropdownMenu';
import { StyledDropdownMenuItemsContainer } from '@/ui/dropdown/components/StyledDropdownMenuItemsContainer'; import { StyledDropdownMenuItemsContainer } from '@/ui/dropdown/components/StyledDropdownMenuItemsContainer';
import { useDropdownButton } from '@/ui/dropdown/hooks/useDropdownButton'; import { useDropdown } from '@/ui/dropdown/hooks/useDropdown';
import { IconCheckbox, IconNotes, IconPlus } from '@/ui/icon/index'; import { IconCheckbox, IconNotes, IconPlus } from '@/ui/icon/index';
import { MenuItem } from '@/ui/menu-item/components/MenuItem'; import { MenuItem } from '@/ui/menu-item/components/MenuItem';
import { ViewBarDropdownButton } from '@/ui/view-bar/components/ViewBarDropdownButton';
import { ActivityType } from '~/generated/graphql'; import { ActivityType } from '~/generated/graphql';
const StyledContainer = styled.div` const StyledContainer = styled.div`
@ -21,28 +21,28 @@ export const ShowPageAddButton = ({
}: { }: {
entity: ActivityTargetableEntity; entity: ActivityTargetableEntity;
}) => { }) => {
const { closeDropdownButton, toggleDropdownButton } = useDropdownButton({ const { closeDropdown, toggleDropdown } = useDropdown({
dropdownId: 'add-show-page', dropdownId: 'add-show-page',
}); });
const openCreateActivity = useOpenCreateActivityDrawer(); const openCreateActivity = useOpenCreateActivityDrawer();
const handleSelect = (type: ActivityType) => { const handleSelect = (type: ActivityType) => {
openCreateActivity({ type, targetableEntities: [entity] }); openCreateActivity({ type, targetableEntities: [entity] });
closeDropdownButton(); closeDropdown();
}; };
return ( return (
<StyledContainer> <StyledContainer>
<DropdownButton <ViewBarDropdownButton
dropdownId="add-show-page" dropdownId="add-show-page"
buttonComponents={ buttonComponent={
<IconButton <IconButton
Icon={IconPlus} Icon={IconPlus}
size="medium" size="medium"
dataTestId="add-showpage-button" dataTestId="add-showpage-button"
accent="default" accent="default"
variant="secondary" variant="secondary"
onClick={toggleDropdownButton} onClick={toggleDropdown}
/> />
} }
dropdownComponents={ dropdownComponents={

View File

@ -1,16 +1,16 @@
import { LightButton } from '@/ui/button/components/LightButton'; import { LightButton } from '@/ui/button/components/LightButton';
import { useDropdownButton } from '@/ui/dropdown/hooks/useDropdownButton'; import { useDropdown } from '@/ui/dropdown/hooks/useDropdown';
import { IconPlus } from '@/ui/icon'; import { IconPlus } from '@/ui/icon';
import { FilterDropdownId } from '../constants/FilterDropdownId'; import { FilterDropdownId } from '../constants/FilterDropdownId';
export const AddFilterFromDropdownButton = () => { export const AddFilterFromDropdownButton = () => {
const { toggleDropdownButton } = useDropdownButton({ const { toggleDropdown } = useDropdown({
dropdownId: FilterDropdownId, dropdownId: FilterDropdownId,
}); });
const handleClick = () => { const handleClick = () => {
toggleDropdownButton(); toggleDropdown();
}; };
return ( return (

View File

@ -1,5 +1,5 @@
import { StyledHeaderDropdownButton } from '@/ui/dropdown/components/StyledHeaderDropdownButton'; import { StyledHeaderDropdownButton } from '@/ui/dropdown/components/StyledHeaderDropdownButton';
import { useDropdownButton } from '@/ui/dropdown/hooks/useDropdownButton'; import { useDropdown } from '@/ui/dropdown/hooks/useDropdown';
import { useRecoilScopedState } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopedState'; import { useRecoilScopedState } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopedState';
import { FilterDropdownId } from '../constants/FilterDropdownId'; import { FilterDropdownId } from '../constants/FilterDropdownId';
@ -12,7 +12,7 @@ import { selectedOperandInDropdownScopedState } from '../states/selectedOperandI
export const MultipleFiltersButton = () => { export const MultipleFiltersButton = () => {
const { ViewBarRecoilScopeContext } = useViewBarContext(); const { ViewBarRecoilScopeContext } = useViewBarContext();
const { isDropdownButtonOpen, toggleDropdownButton } = useDropdownButton({ const { isDropdownOpen, toggleDropdown } = useDropdown({
dropdownId: FilterDropdownId, dropdownId: FilterDropdownId,
}); });
@ -44,13 +44,13 @@ export const MultipleFiltersButton = () => {
}; };
const handleClick = () => { const handleClick = () => {
toggleDropdownButton(); toggleDropdown();
resetState(); resetState();
}; };
return ( return (
<StyledHeaderDropdownButton <StyledHeaderDropdownButton
isUnfolded={isDropdownButtonOpen} isUnfolded={isDropdownOpen}
onClick={handleClick} onClick={handleClick}
> >
Filter Filter

View File

@ -1,10 +1,10 @@
import { DropdownButton } from '@/ui/dropdown/components/DropdownButton';
import { HotkeyScope } from '@/ui/utilities/hotkey/types/HotkeyScope'; import { HotkeyScope } from '@/ui/utilities/hotkey/types/HotkeyScope';
import { FilterDropdownId } from '../constants/FilterDropdownId'; import { FilterDropdownId } from '../constants/FilterDropdownId';
import { MultipleFiltersButton } from './MultipleFiltersButton'; import { MultipleFiltersButton } from './MultipleFiltersButton';
import { MultipleFiltersDropdownContent } from './MultipleFiltersDropdownContent'; import { MultipleFiltersDropdownContent } from './MultipleFiltersDropdownContent';
import { ViewBarDropdownButton } from './ViewBarDropdownButton';
type MultipleFiltersDropdownButtonProps = { type MultipleFiltersDropdownButtonProps = {
hotkeyScope: HotkeyScope; hotkeyScope: HotkeyScope;
@ -14,9 +14,9 @@ export const MultipleFiltersDropdownButton = ({
hotkeyScope, hotkeyScope,
}: MultipleFiltersDropdownButtonProps) => { }: MultipleFiltersDropdownButtonProps) => {
return ( return (
<DropdownButton <ViewBarDropdownButton
dropdownId={FilterDropdownId} dropdownId={FilterDropdownId}
buttonComponents={<MultipleFiltersButton />} buttonComponent={<MultipleFiltersButton />}
dropdownComponents={<MultipleFiltersDropdownContent />} dropdownComponents={<MultipleFiltersDropdownContent />}
dropdownHotkeyScope={hotkeyScope} dropdownHotkeyScope={hotkeyScope}
/> />

View File

@ -2,12 +2,11 @@ import { useCallback, useState } from 'react';
import { produce } from 'immer'; import { produce } from 'immer';
import { LightButton } from '@/ui/button/components/LightButton'; import { LightButton } from '@/ui/button/components/LightButton';
import { DropdownButton } from '@/ui/dropdown/components/DropdownButton';
import { DropdownMenuHeader } from '@/ui/dropdown/components/DropdownMenuHeader'; import { DropdownMenuHeader } from '@/ui/dropdown/components/DropdownMenuHeader';
import { StyledDropdownMenu } from '@/ui/dropdown/components/StyledDropdownMenu'; import { StyledDropdownMenu } from '@/ui/dropdown/components/StyledDropdownMenu';
import { StyledDropdownMenuItemsContainer } from '@/ui/dropdown/components/StyledDropdownMenuItemsContainer'; import { StyledDropdownMenuItemsContainer } from '@/ui/dropdown/components/StyledDropdownMenuItemsContainer';
import { StyledDropdownMenuSeparator } from '@/ui/dropdown/components/StyledDropdownMenuSeparator'; import { StyledDropdownMenuSeparator } from '@/ui/dropdown/components/StyledDropdownMenuSeparator';
import { useDropdownButton } from '@/ui/dropdown/hooks/useDropdownButton'; import { useDropdown } from '@/ui/dropdown/hooks/useDropdown';
import { IconChevronDown } from '@/ui/icon'; import { IconChevronDown } from '@/ui/icon';
import { MenuItem } from '@/ui/menu-item/components/MenuItem'; import { MenuItem } from '@/ui/menu-item/components/MenuItem';
import { HotkeyScope } from '@/ui/utilities/hotkey/types/HotkeyScope'; import { HotkeyScope } from '@/ui/utilities/hotkey/types/HotkeyScope';
@ -20,6 +19,8 @@ import { sortsScopedState } from '../states/sortsScopedState';
import { SortDefinition } from '../types/SortDefinition'; import { SortDefinition } from '../types/SortDefinition';
import { SORT_DIRECTIONS, SortDirection } from '../types/SortDirection'; import { SORT_DIRECTIONS, SortDirection } from '../types/SortDirection';
import { ViewBarDropdownButton } from './ViewBarDropdownButton';
export type SortDropdownButtonProps = { export type SortDropdownButtonProps = {
hotkeyScope: HotkeyScope; hotkeyScope: HotkeyScope;
isPrimaryButton?: boolean; isPrimaryButton?: boolean;
@ -53,17 +54,17 @@ export const SortDropdownButton = ({
const isSortSelected = sorts.length > 0; const isSortSelected = sorts.length > 0;
const { toggleDropdownButton } = useDropdownButton({ const { toggleDropdown } = useDropdown({
dropdownId: SortDropdownId, dropdownId: SortDropdownId,
}); });
const handleButtonClick = () => { const handleButtonClick = () => {
toggleDropdownButton(); toggleDropdown();
resetState(); resetState();
}; };
const handleAddSort = (selectedSortDefinition: SortDefinition) => { const handleAddSort = (selectedSortDefinition: SortDefinition) => {
toggleDropdownButton(); toggleDropdown();
setSorts( setSorts(
produce(sorts, (existingSortsDraft) => { produce(sorts, (existingSortsDraft) => {
@ -90,10 +91,10 @@ export const SortDropdownButton = ({
}; };
return ( return (
<DropdownButton <ViewBarDropdownButton
dropdownId={SortDropdownId} dropdownId={SortDropdownId}
dropdownHotkeyScope={hotkeyScope} dropdownHotkeyScope={hotkeyScope}
buttonComponents={ buttonComponent={
<LightButton <LightButton
title="Sort" title="Sort"
active={isSortSelected} active={isSortSelected}
@ -140,6 +141,6 @@ export const SortDropdownButton = ({
</StyledDropdownMenu> </StyledDropdownMenu>
} }
onClose={handleDropdownButtonClose} onClose={handleDropdownButtonClose}
></DropdownButton> ></ViewBarDropdownButton>
); );
}; };

View File

@ -1,6 +1,6 @@
import { ReactNode } from 'react'; import { ReactNode } from 'react';
import { useDropdownButton } from '@/ui/dropdown/hooks/useDropdownButton'; import { useDropdown } from '@/ui/dropdown/hooks/useDropdown';
import { TopBar } from '@/ui/top-bar/TopBar'; import { TopBar } from '@/ui/top-bar/TopBar';
import { FiltersHotkeyScope } from '../types/FiltersHotkeyScope'; import { FiltersHotkeyScope } from '../types/FiltersHotkeyScope';
@ -23,7 +23,7 @@ export const ViewBar = ({
optionsDropdownButton, optionsDropdownButton,
optionsDropdownKey, optionsDropdownKey,
}: ViewBarProps) => { }: ViewBarProps) => {
const { openDropdownButton: openOptionsDropdownButton } = useDropdownButton({ const { openDropdown: openOptionsDropdownButton } = useDropdown({
dropdownId: optionsDropdownKey, dropdownId: optionsDropdownKey,
}); });

View File

@ -0,0 +1,47 @@
import { Keys } from 'react-hotkeys-hook';
import { Placement } from '@floating-ui/react';
import { DropdownMenu } from '@/ui/dropdown/components/DropdownMenu';
import { HotkeyScope } from '@/ui/utilities/hotkey/types/HotkeyScope';
type DropdownButtonProps = {
buttonComponent: JSX.Element | JSX.Element[];
dropdownComponents: JSX.Element | JSX.Element[];
dropdownId: string;
hotkey?: {
key: Keys;
scope: string;
};
dropdownHotkeyScope: HotkeyScope;
dropdownPlacement?: Placement;
onClickOutside?: () => void;
onClose?: () => void;
onOpen?: () => void;
};
export const ViewBarDropdownButton = ({
buttonComponent,
dropdownComponents,
dropdownId,
hotkey,
dropdownHotkeyScope,
dropdownPlacement = 'bottom-end',
onClickOutside,
onClose,
onOpen,
}: DropdownButtonProps) => {
return (
<DropdownMenu
clickableComponent={buttonComponent}
dropdownComponents={dropdownComponents}
dropdownId={dropdownId}
hotkey={hotkey}
dropdownHotkeyScope={dropdownHotkeyScope}
dropdownOffset={{ x: 0, y: 8 }}
dropdownPlacement={dropdownPlacement}
onClickOutside={onClickOutside}
onClose={onClose}
onOpen={onOpen}
/>
);
};

View File

@ -8,12 +8,11 @@ import {
useSetRecoilState, useSetRecoilState,
} from 'recoil'; } from 'recoil';
import { DropdownButton } from '@/ui/dropdown/components/DropdownButton';
import { StyledDropdownButtonContainer } from '@/ui/dropdown/components/StyledDropdownButtonContainer'; import { StyledDropdownButtonContainer } from '@/ui/dropdown/components/StyledDropdownButtonContainer';
import { StyledDropdownMenu } from '@/ui/dropdown/components/StyledDropdownMenu'; import { StyledDropdownMenu } from '@/ui/dropdown/components/StyledDropdownMenu';
import { StyledDropdownMenuItemsContainer } from '@/ui/dropdown/components/StyledDropdownMenuItemsContainer'; import { StyledDropdownMenuItemsContainer } from '@/ui/dropdown/components/StyledDropdownMenuItemsContainer';
import { StyledDropdownMenuSeparator } from '@/ui/dropdown/components/StyledDropdownMenuSeparator'; import { StyledDropdownMenuSeparator } from '@/ui/dropdown/components/StyledDropdownMenuSeparator';
import { useDropdownButton } from '@/ui/dropdown/hooks/useDropdownButton'; import { useDropdown } from '@/ui/dropdown/hooks/useDropdown';
import { import {
IconChevronDown, IconChevronDown,
IconList, IconList,
@ -42,6 +41,8 @@ import { ViewsDropdownId } from '../constants/ViewsDropdownId';
import { ViewBarContext } from '../contexts/ViewBarContext'; import { ViewBarContext } from '../contexts/ViewBarContext';
import { useRemoveView } from '../hooks/useRemoveView'; import { useRemoveView } from '../hooks/useRemoveView';
import { ViewBarDropdownButton } from './ViewBarDropdownButton';
const StyledBoldDropdownMenuItemsContainer = styled( const StyledBoldDropdownMenuItemsContainer = styled(
StyledDropdownMenuItemsContainer, StyledDropdownMenuItemsContainer,
)` )`
@ -105,10 +106,9 @@ export const ViewsDropdownButton = ({
entityCountInCurrentViewState as RecoilValueReadOnly<number>, entityCountInCurrentViewState as RecoilValueReadOnly<number>,
); );
const { isDropdownButtonOpen, closeDropdownButton, toggleDropdownButton } = const { isDropdownOpen, closeDropdown } = useDropdown({
useDropdownButton({ dropdownId: ViewsDropdownId,
dropdownId: ViewsDropdownId, });
});
const setViewEditMode = useSetRecoilState(viewEditModeState); const setViewEditMode = useSetRecoilState(viewEditModeState);
@ -127,15 +127,15 @@ export const ViewsDropdownButton = ({
set(filtersScopedState(recoilScopeId), savedFilters); set(filtersScopedState(recoilScopeId), savedFilters);
set(sortsScopedState(recoilScopeId), savedSorts); set(sortsScopedState(recoilScopeId), savedSorts);
set(currentViewIdScopedState(recoilScopeId), viewId); set(currentViewIdScopedState(recoilScopeId), viewId);
closeDropdownButton(); closeDropdown();
}, },
[onViewSelect, recoilScopeId, closeDropdownButton], [onViewSelect, recoilScopeId, closeDropdown],
); );
const handleAddViewButtonClick = () => { const handleAddViewButtonClick = () => {
setViewEditMode({ mode: 'create', viewId: undefined }); setViewEditMode({ mode: 'create', viewId: undefined });
onViewEditModeChange?.(); onViewEditModeChange?.();
closeDropdownButton(); closeDropdown();
}; };
const handleEditViewButtonClick = ( const handleEditViewButtonClick = (
@ -145,7 +145,7 @@ export const ViewsDropdownButton = ({
event.stopPropagation(); event.stopPropagation();
setViewEditMode({ mode: 'edit', viewId }); setViewEditMode({ mode: 'edit', viewId });
onViewEditModeChange?.(); onViewEditModeChange?.();
closeDropdownButton(); closeDropdown();
}; };
const { removeView } = useRemoveView(); const { removeView } = useRemoveView();
@ -157,22 +157,15 @@ export const ViewsDropdownButton = ({
event.stopPropagation(); event.stopPropagation();
await removeView(viewId); await removeView(viewId);
closeDropdownButton(); closeDropdown();
};
const handleViewButtonClick = () => {
toggleDropdownButton();
}; };
return ( return (
<DropdownButton <ViewBarDropdownButton
dropdownId={ViewsDropdownId} dropdownId={ViewsDropdownId}
dropdownHotkeyScope={hotkeyScope} dropdownHotkeyScope={hotkeyScope}
buttonComponents={ buttonComponent={
<StyledDropdownButtonContainer <StyledDropdownButtonContainer isUnfolded={isDropdownOpen}>
isUnfolded={isDropdownButtonOpen}
onClick={handleViewButtonClick}
>
<StyledViewIcon size={theme.icon.size.md} /> <StyledViewIcon size={theme.icon.size.md} />
<StyledViewName> <StyledViewName>
{currentView?.name || defaultViewName} {currentView?.name || defaultViewName}