Skip to content
This repository has been archived by the owner on Nov 30, 2023. It is now read-only.

Commit

Permalink
fix the drag and drop overflow scroll (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
benhoneywill authored Mar 21, 2022
1 parent bf76505 commit e0f1004
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 60 deletions.
101 changes: 55 additions & 46 deletions app/components/drag-and-drop/components/category-drop-zone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { FC } from "react";

import { useContext } from "react";

import { Box } from "@chakra-ui/layout";
import { Box, HStack } from "@chakra-ui/layout";
import { Droppable } from "react-beautiful-dnd";
import { Button } from "@chakra-ui/button";
import { useColorModeValue } from "@chakra-ui/react";
Expand All @@ -22,54 +22,63 @@ const CategoryDropZone: FC = () => {
const dragColor = useColorModeValue("blue.200", "blue.700");

return (
<Droppable
droppableId="category"
type="category"
isDropDisabled={readonly}
direction="horizontal"
>
{(provided, snapshot) => (
<Box
{...provided.droppableProps}
ref={provided.innerRef}
bg={snapshot.isDraggingOver ? dragColor : ""}
width="100%"
height="100%"
userSelect="none"
>
<HorizontalScroller>
{categories.map((category, index) => (
<DraggableCategory
key={category.id}
category={category}
index={index}
/>
))}
<HorizontalScroller>
<Droppable
droppableId="category"
type="category"
isDropDisabled={readonly}
direction="horizontal"
>
{(provided, snapshot) => (
<Box
{...provided.droppableProps}
ref={provided.innerRef}
bg={snapshot.isDraggingOver ? dragColor : ""}
height="100%"
userSelect="none"
>
<HStack
spacing={0}
alignItems="flex-start"
width="100%"
height="100%"
px={3}
py={6}
>
{categories.map((category, index) => (
<DraggableCategory
key={category.id}
category={category}
index={index}
/>
))}

{provided.placeholder}
{provided.placeholder}

{addCategory && (
<Box
width="290px"
flex="0 0 290px"
padding={2}
borderRadius="md"
mx={1}
>
<Button
isFullWidth
size="sm"
colorScheme="blue"
onClick={addCategory}
{addCategory && (
<Box
width="290px"
flex="0 0 290px"
padding={2}
borderRadius="md"
mx={1}
onMouseDown={(e) => e.stopPropagation()}
>
New category
</Button>
</Box>
)}
</HorizontalScroller>
</Box>
)}
</Droppable>
<Button
isFullWidth
size="sm"
colorScheme="blue"
onClick={addCategory}
>
New category
</Button>
</Box>
)}
</HStack>
</Box>
)}
</Droppable>
</HorizontalScroller>
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ const DraggableCategory: FC<DraggableCategoryProps> = memo(
const { readonly } = useContext(dragAndDropContext);

return (
<Box key={category.id} height="100%" pointerEvents="none">
<Box
key={category.id}
height="100%"
pointerEvents="none"
onMouseDown={(e) => e.stopPropagation()}
>
<Draggable
draggableId={category.id}
index={index}
Expand Down
19 changes: 6 additions & 13 deletions app/components/drag-and-drop/components/horizontal-scroller.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { FC } from "react";

import { useEffect, useRef, useState } from "react";

import { HStack } from "@chakra-ui/layout";
import { Box } from "@chakra-ui/layout";
import {
useBreakpointValue,
useColorModeValue,
Expand Down Expand Up @@ -70,31 +70,24 @@ const HorizontalScroller: FC = ({ children }) => {
}, [isDown, bgDragEnabled, scrollLeft, startX]);

return (
<HStack
spacing={0}
<Box
overflowX="auto"
alignItems="flex-start"
width="100%"
height="100%"
css={bgDragEnabled && scrollBarStyles}
px={3}
pt={6}
pb={bgDragEnabled ? 2 : 6}
ref={scroller}
display="flex"
justifyContent="stretch"
onMouseDown={(e) => {
if (
scroller.current &&
e.target === scroller.current &&
bgDragEnabled
) {
if (scroller.current && bgDragEnabled) {
setIsDown(true);
setStartX(e.pageX - scroller.current?.offsetLeft);
setScrollLeft(scroller.current?.scrollLeft);
}
}}
>
{children}
</HStack>
</Box>
);
};

Expand Down

0 comments on commit e0f1004

Please sign in to comment.