Skip to content

Commit

Permalink
feat(AsideHeader): add drag placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
jenshenJ committed Dec 6, 2024
1 parent adb757a commit 7ed469b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ interface AllPagesListItemProps {
editMode?: boolean;
enableSorting?: boolean;
onToggle: () => void;
onDragStart?: () => void;
onDragEnd?: () => void;
}

export const AllPagesListItem: React.FC<AllPagesListItemProps> = (props) => {
const {item, editMode, onToggle, enableSorting} = props;
const {item, editMode, onToggle, enableSorting, onDragStart, onDragEnd} = props;
const onPinButtonClick = useCallback(
(e: MouseEvent<HTMLButtonElement | HTMLAnchorElement>) => {
e.stopPropagation();
Expand All @@ -35,12 +37,30 @@ export const AllPagesListItem: React.FC<AllPagesListItemProps> = (props) => {
}
};

const onItemDrugStart = (e: MouseEvent<HTMLDivElement>) => {
if (editMode && onDragStart) {
e.stopPropagation();
e.preventDefault();
onDragStart();
}
};

const onItemDrugEnd = (e: MouseEvent<HTMLDivElement>) => {
if (editMode && onDragEnd) {
e.stopPropagation();
e.preventDefault();
onDragEnd();
}
};

return (
<div
key={item.id}
className={b({'edit-mode': editMode && enableSorting})}
onClick={onItemClick}
draggable={editMode}
onMouseDown={onItemDrugStart}
onMouseUp={onItemDrugEnd}
>
{item.icon ? (
<Icon className={b('icon')} data={item.icon} size={item.iconSize} />
Expand Down
7 changes: 7 additions & 0 deletions src/components/AllPagesPanel/AllPagesPanel.scss
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@
&__item_editMode {
padding: 0 var(--g-spacing-6);
}

&__drag-placeholder {
padding-left: 88px;
padding-right: 68px;
text-wrap: nowrap;
visibility: hidden;
}
}
41 changes: 31 additions & 10 deletions src/components/AllPagesPanel/AllPagesPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import React, {ReactNode, useCallback, useEffect, useMemo, useRef, useState} from 'react';

import {Gear} from '@gravity-ui/icons';
import {Button, Flex, Icon, List, ListItemData, Text} from '@gravity-ui/uikit';
Expand Down Expand Up @@ -30,6 +30,9 @@ export const AllPagesPanel: React.FC<AllPagesPanelProps> = (props) => {
menuItemsRef.current = menuItems;

const [isEditMode, setIsEditMode] = useState(false);

const [dragingItemTitle, setDragingItemTitle] = useState<ReactNode | null>(null);

const toggleEditMode = useCallback(() => {
setIsEditMode((prev) => !prev);
}, []);
Expand Down Expand Up @@ -72,16 +75,28 @@ export const AllPagesPanel: React.FC<AllPagesPanelProps> = (props) => {
[onMenuItemsChanged, editMenuProps],
);

const onDragEnd = useCallback(() => {
setDragingItemTitle(null);
}, [onMenuItemsChanged]);

Check warning on line 80 in src/components/AllPagesPanel/AllPagesPanel.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

React Hook useCallback has an unnecessary dependency: 'onMenuItemsChanged'. Either exclude it or remove the dependency array

const itemRender = useCallback(
(item: ListItemData<MenuItem>, _isActive: boolean, _itemIndex: number) => (
<AllPagesListItem
item={item}
editMode={isEditMode}
onToggle={() => togglePageVisibility(item)}
enableSorting={editMenuProps?.enableSorting}
/>
),
[isEditMode, togglePageVisibility],
(item: ListItemData<MenuItem>, _isActive: boolean, _itemIndex: number) => {
const onDragStart = () => {
setDragingItemTitle(item.title);
};

return (
<AllPagesListItem
item={item}
onDragStart={onDragStart}
onDragEnd={onDragEnd}
editMode={isEditMode}
onToggle={() => togglePageVisibility(item)}
enableSorting={editMenuProps?.enableSorting}
/>
);
},
[isEditMode, togglePageVisibility, onMenuItemsChanged],

Check warning on line 99 in src/components/AllPagesPanel/AllPagesPanel.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

React Hook useCallback has missing dependencies: 'editMenuProps?.enableSorting' and 'onDragEnd'. Either include them or remove the dependency array
);

const onResetToDefaultClick = useCallback(() => {
Expand All @@ -106,6 +121,8 @@ export const AllPagesPanel: React.FC<AllPagesPanelProps> = (props) => {
newItems.splice(newIndex, 0, element);

onMenuItemsChanged?.(newItems);

setDragingItemTitle(null);
editMenuProps?.onChangeItemsOrder?.(element, oldIndex, newIndex);
},
[onMenuItemsChanged],

Check warning on line 128 in src/components/AllPagesPanel/AllPagesPanel.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

React Hook useCallback has a missing dependency: 'editMenuProps'. Either include it or remove the dependency array
Expand Down Expand Up @@ -141,6 +158,10 @@ export const AllPagesPanel: React.FC<AllPagesPanelProps> = (props) => {
onItemClick={onItemClick}
renderItem={itemRender}
/>

{dragingItemTitle && (
<div className={b('drag-placeholder')}>{dragingItemTitle}</div>
)}
</div>
) : (
Object.keys(groupedItems).map((category) => {
Expand Down

0 comments on commit 7ed469b

Please sign in to comment.