- refactor context menu and action bar into seperate components

- fix styling context menu
This commit is contained in:
brendanlaschke
2023-08-10 21:30:25 +02:00
parent 807506549a
commit b76f01d930
16 changed files with 337 additions and 82 deletions

View File

@ -4,42 +4,11 @@ import { useRecoilValue, useSetRecoilState } from 'recoil';
import { contextMenuPositionState } from '@/ui/table/states/contextMenuPositionState';
import { PositionType } from '../types/PositionType';
type OwnProps = {
children: React.ReactNode | React.ReactNode[];
selectedIds: string[];
};
type StyledContainerProps = {
position: PositionType;
};
const StyledContainerContextMenu = styled.div<StyledContainerProps>`
align-items: center;
background: ${({ theme }) => theme.background.secondary};
border: 1px solid ${({ theme }) => theme.border.color.medium};
border-radius: ${({ theme }) => theme.border.radius.md};
bottom: ${(props) => (props.position.x ? 'auto' : '38px')};
box-shadow: ${({ theme }) => theme.boxShadow.strong};
left: ${(props) => (props.position.x ? `${props.position.x}px` : '50%')};
padding-bottom: ${({ theme }) => theme.spacing(1)};
padding-left: ${({ theme }) => theme.spacing(1)};
padding-right: ${({ theme }) => theme.spacing(1)};
padding-top: ${({ theme }) => theme.spacing(1)};
position: ${(props) => (props.position.x ? 'fixed' : 'absolute')};
top: ${(props) => (props.position.y ? `${props.position.y}px` : 'auto')};
transform: translateX(-50%);
width: 160px;
z-index: 1;
div {
justify-content: left;
}
`;
const StyledContainerActionBar = styled.div`
align-items: center;
background: ${({ theme }) => theme.background.secondary};
@ -79,16 +48,9 @@ export function ActionBar({ children, selectedIds }: OwnProps) {
};
}, [setContextMenuPosition]);
if (selectedIds.length === 0) {
if (selectedIds.length === 0 || position.x || position.y) {
return null;
}
if (position.x && position.y) {
return (
<StyledContainerContextMenu className="action-bar" position={position}>
{children}
</StyledContainerContextMenu>
);
}
return (
<StyledContainerActionBar className="action-bar">
{children}

View File

@ -0,0 +1,64 @@
import React, { useEffect } from 'react';
import styled from '@emotion/styled';
import { useRecoilValue, useSetRecoilState } from 'recoil';
import { contextMenuPositionState } from '@/ui/table/states/contextMenuPositionState';
import { PositionType } from '../types/PositionType';
type OwnProps = {
children: React.ReactNode | React.ReactNode[];
selectedIds: string[];
};
type StyledContainerProps = {
position: PositionType;
};
const StyledContainerContextMenu = styled.div<StyledContainerProps>`
align-items: flex-start;
background: ${({ theme }) => theme.background.secondary};
border: 1px solid ${({ theme }) => theme.border.color.light};
border-radius: ${({ theme }) => theme.border.radius.md};
box-shadow: ${({ theme }) => theme.boxShadow.strong};
display: flex;
flex-direction: column;
gap: 1px;
left: ${(props) => `${props.position.x}px`};
position: fixed;
top: ${(props) => `${props.position.y}px`};
transform: translateX(-50%);
width: 160px;
z-index: 1;
`;
export function ContextMenu({ children, selectedIds }: OwnProps) {
const position = useRecoilValue(contextMenuPositionState);
const setContextMenuPosition = useSetRecoilState(contextMenuPositionState);
useEffect(() => {
function handleClickOutside(event: MouseEvent) {
if (!(event.target as HTMLElement).closest('.action-bar')) {
setContextMenuPosition({ x: null, y: null });
}
}
document.addEventListener('mousedown', handleClickOutside);
// Cleanup the event listener when the component unmounts
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, [setContextMenuPosition]);
if (selectedIds.length === 0 || (!position.x && !position.y)) {
return null;
}
return (
<StyledContainerContextMenu className="action-bar" position={position}>
{children}
</StyledContainerContextMenu>
);
}

View File

@ -1,17 +0,0 @@
import { IconNotes } from '@/ui/icon/index';
import { EntityTableActionBarButton } from './EntityTableActionBarButton';
type OwnProps = {
onClick: () => void;
};
export function TableActionBarButtonToggleComments({ onClick }: OwnProps) {
return (
<EntityTableActionBarButton
label="Note"
icon={<IconNotes size={16} />}
onClick={onClick}
/>
);
}

View File

@ -1,17 +0,0 @@
import { IconCheckbox } from '@/ui/icon/index';
import { EntityTableActionBarButton } from './EntityTableActionBarButton';
type OwnProps = {
onClick: () => void;
};
export function TableActionBarButtonToggleTasks({ onClick }: OwnProps) {
return (
<EntityTableActionBarButton
label="Task"
icon={<IconCheckbox size={16} />}
onClick={onClick}
/>
);
}

View File

@ -0,0 +1,16 @@
import React from 'react';
import { useRecoilValue } from 'recoil';
import { ContextMenu } from '@/ui/context-menu/components/ContextMenu';
import { selectedRowIdsSelector } from '../../states/selectedRowIdsSelector';
type OwnProps = {
children: React.ReactNode | React.ReactNode[];
};
export function EntityTableContextMenu({ children }: OwnProps) {
const selectedRowIds = useRecoilValue(selectedRowIdsSelector);
return <ContextMenu selectedIds={selectedRowIds}>{children}</ContextMenu>;
}

View File

@ -0,0 +1,58 @@
import { ReactNode } from 'react';
import styled from '@emotion/styled';
type OwnProps = {
icon: ReactNode;
label: string;
type?: 'standard' | 'warning';
onClick: () => void;
};
type StyledButtonProps = {
type: 'standard' | 'warning';
};
const StyledButton = styled.div<StyledButtonProps>`
align-items: center;
align-self: stretch;
border-radius: ${({ theme }) => theme.border.radius.sm};
color: ${(props) =>
props.type === 'warning'
? props.theme.color.red
: props.theme.font.color.secondary};
cursor: pointer;
display: flex;
gap: ${({ theme }) => theme.spacing(2)};
height: 32px;
padding-left: ${({ theme }) => theme.spacing(1)};
padding-right: ${({ theme }) => theme.spacing(1)};
transition: background 0.1s ease;
user-select: none;
&:hover {
background: ${({ theme, type }) =>
type === 'warning'
? theme.tag.background.red
: theme.background.tertiary};
}
`;
const StyledButtonLabel = styled.div`
font-weight: ${({ theme }) => theme.font.weight.medium};
margin-left: ${({ theme }) => theme.spacing(2)};
`;
export function EntityTableContextMenuEntry({
label,
icon,
type = 'standard',
onClick,
}: OwnProps) {
return (
<StyledButton type={type} onClick={onClick}>
{icon}
<StyledButtonLabel>{label}</StyledButtonLabel>
</StyledButton>
);
}

View File

@ -1,6 +1,6 @@
import { atom } from 'recoil';
import { PositionType } from '@/ui/action-bar/types/PositionType';
import { PositionType } from '@/ui/context-menu/types/PositionType';
export const contextMenuPositionState = atom<PositionType>({
key: 'contextMenuPositionState',