Skip to content

Commit

Permalink
v2.0.1 - disable pointer events during editing, support function as c…
Browse files Browse the repository at this point in the history
…hildren
  • Loading branch information
rot1024 committed Sep 30, 2021
1 parent 423db5f commit 929356a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-align",
"version": "2.0.0",
"version": "2.0.1",
"author": "KaWaite",
"module": "dist/react-align.esm.js",
"license": "MIT",
Expand Down
50 changes: 37 additions & 13 deletions src/GridItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import React, {
useMemo,
CSSProperties,
useState,
PropsWithChildren,
useCallback,
ReactNode,
} from 'react';
import { Draggable } from 'react-beautiful-dnd';

Expand All @@ -24,33 +24,41 @@ export type ItemProps = {
iconSize?: number;
iconColor?: string;
onExtend?: (extended: boolean) => void;
children?:
| ReactNode
| ((context: {
id: string;
editing: boolean;
isDragging: boolean;
isHovered: boolean;
extended: boolean;
extendable: boolean;
disabled: boolean;
index: number;
}) => ReactNode);
};

export default function GridItem({
className,
children,
id,
index,
extendable,
extended,
disabled,
extendable = false,
extended = false,
disabled = false,
onExtend,
// Picky stuff.
style,
editorStyle,
iconSize,
iconColor = 'rgb(255, 255, 255)',
...props
}: PropsWithChildren<ItemProps>) {
}: ItemProps) {
const { end, vertical } = props as {
end?: boolean;
vertical?: boolean;
};
const {
editing: enabled,
isDragging,
onExtend: onExtend2,
} = useAlignContext();
const { editing, isDragging, onExtend: onExtend2 } = useAlignContext();
const [isHovered, setHovered] = useState(false);
const handleExtend = useCallback(() => {
if (!extendable) return;
Expand All @@ -67,6 +75,20 @@ export default function GridItem({
[end]
);

const ctx = useMemo(
() => ({
id,
editing,
isDragging,
isHovered,
extended,
extendable,
disabled,
index,
}),
[disabled, editing, extendable, extended, id, index, isDragging, isHovered]
);

return (
<Draggable draggableId={id} index={index} isDragDisabled={disabled}>
{(provided, snapshot) => (
Expand All @@ -77,18 +99,21 @@ export default function GridItem({
style={{
flex: extended && !snapshot.isDragging ? 'auto' : undefined,
opacity: snapshot.isDragging ? 0.5 : 1,
...(enabled ? editorStyle : style),
...(editing ? editorStyle : style),
...provided.draggableProps.style,
}}
onMouseEnter={() => setHovered(true)}
onMouseLeave={() => setHovered(false)}
>
<div style={{ pointerEvents: editing ? 'none' : undefined }}>
{typeof children === 'function' ? children(ctx) : children}
</div>
<div
className="overlay"
style={{
display:
!disabled &&
enabled &&
editing &&
isHovered &&
(snapshot.isDragging || !isDragging)
? 'block'
Expand Down Expand Up @@ -117,7 +142,6 @@ export default function GridItem({
)}
</div>
</div>
{children}
</div>
)}
</Draggable>
Expand Down

0 comments on commit 929356a

Please sign in to comment.