Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow consumers to define drag source opacity in drop animation #160

Merged
merged 4 commits into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/drop-animation-opacity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@dnd-kit/core": patch
---

Allow consumers to define drag source opacity in drop animation by setting the `dragSourceOpacity` option to a number on the `dropAnimation` prop of `DragOverlay`.
4 changes: 3 additions & 1 deletion packages/core/src/components/DragOverlay/DragOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ const defaultTransition: TransitionGetter = (activatorEvent) => {
return isKeyboardActivator ? 'transform 250ms ease' : undefined;
};

const defaultDropAnimation: DropAnimation = {
export const defaultDropAnimation: DropAnimation = {
duration: 250,
easing: 'ease',
dragSourceOpacity: 0,
};

export const DragOverlay = React.memo(
Expand Down Expand Up @@ -126,6 +127,7 @@ export const DragOverlay = React.memo(
draggableNodes,
duration: dropAnimation?.duration,
easing: dropAnimation?.easing,
dragSourceOpacity: dropAnimation?.dragSourceOpacity,
node: overlayNode.nodeRef.current,
transform: attributesSnapshot.current?.transform,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {getViewRect} from '../../../utilities';
export interface DropAnimation {
duration: number;
easing: string;
dragSourceOpacity?: number;
}

interface Arguments {
Expand All @@ -17,6 +18,7 @@ interface Arguments {
draggableNodes: DraggableNodes;
duration: DropAnimation['duration'] | undefined;
easing: DropAnimation['easing'] | undefined;
dragSourceOpacity: DropAnimation['dragSourceOpacity'] | undefined;
node: HTMLElement | null;
transform: Transform | undefined;
}
Expand All @@ -28,6 +30,7 @@ export function useDropAnimation({
draggableNodes,
duration,
easing,
dragSourceOpacity,
node,
transform,
}: Arguments) {
Expand Down Expand Up @@ -72,7 +75,10 @@ export function useDropAnimation({
});
const originalOpacity = finalNode.style.opacity;

finalNode.style.opacity = '0';
if (dragSourceOpacity != null) {
finalNode.style.opacity = `${dragSourceOpacity}`;
}

const nodeAnimation = node.animate(
[
{
Expand All @@ -91,7 +97,7 @@ export function useDropAnimation({
nodeAnimation.onfinish = () => {
setDropAnimationComplete(true);

if (finalNode) {
if (finalNode && dragSourceOpacity != null) {
finalNode.style.opacity = originalOpacity;
}
};
Expand All @@ -109,6 +115,7 @@ export function useDropAnimation({
draggableNodes,
duration,
easing,
dragSourceOpacity,
node,
transform,
]);
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/components/DragOverlay/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export {DragOverlay, Props} from './DragOverlay';
export {DragOverlay, defaultDropAnimation} from './DragOverlay';
export type {Props} from './DragOverlay';
export type {DropAnimation} from './hooks';
14 changes: 4 additions & 10 deletions packages/core/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
export {
Announcements,
defaultAnnouncements,
ScreenReaderInstructions,
} from './Accessibility';
export {defaultAnnouncements} from './Accessibility';
export type {Announcements, ScreenReaderInstructions} from './Accessibility';
export {DndContext} from './DndContext';
export type {
CancelDrop,
Expand All @@ -11,8 +8,5 @@ export type {
DragMoveEvent,
DragStartEvent,
} from './DndContext';
export {
DragOverlay,
Props as DragOverlayProps,
DropAnimation,
} from './DragOverlay';
export {DragOverlay, defaultDropAnimation} from './DragOverlay';
export type {DropAnimation, Props as DragOverlayProps} from './DragOverlay';
7 changes: 6 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export {DndContext, DragOverlay, defaultAnnouncements} from './components';
export {
DndContext,
DragOverlay,
defaultAnnouncements,
defaultDropAnimation,
} from './components';
export type {
Announcements,
CancelDrop,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
DragOverEvent,
LayoutMeasuring,
LayoutMeasuringStrategy,
DropAnimation,
defaultDropAnimation,
} from '@dnd-kit/core';
import {
SortableContext,
Expand Down Expand Up @@ -66,6 +68,11 @@ const layoutMeasuring: Partial<LayoutMeasuring> = {
strategy: LayoutMeasuringStrategy.BeforeDragging,
};

const dropAnimation: DropAnimation = {
...defaultDropAnimation,
dragSourceOpacity: 0.5,
};

export function SortableTree() {
const [items, setItems] = useState(() => initialItems);
const [activeId, setActiveId] = useState<string | null>(null);
Expand Down Expand Up @@ -144,7 +151,7 @@ export function SortableTree() {
/>
))}
{createPortal(
<DragOverlay>
<DragOverlay dropAnimation={dropAnimation}>
{activeId && activeItem ? (
<TreeItem
depth={activeItem.depth}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
border: 1px solid #dedede;
color: #222;
box-sizing: border-box;
user-select: none;
}

.Text {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const TreeItem = forwardRef<HTMLDivElement, Props>(
<div className={styles.TreeItem} ref={ref}>
<Handle {...handleProps} />
<span className={styles.Text}>{value}</span>
{!clone && !ghost && <Remove onClick={onRemove} />}
{!clone && <Remove onClick={onRemove} />}
{clone && childCount && childCount > 1 ? (
<span className={styles.Count}>{childCount}</span>
) : null}
Expand Down
File renamed without changes.
11 changes: 7 additions & 4 deletions stories/components/Item/components/Action/Action.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ $focused-outline-color: #4c9ffe;
outline: none;
appearance: none;
background-color: transparent;
-webkit-tap-highlight-color: transparent;

&:hover {
background-color: var(--action-background, rgba(0, 0, 0, 0.05));
@media (hover: hover) {
&:hover {
background-color: var(--action-background, rgba(0, 0, 0, 0.05));

svg {
fill: #6f7b88;
svg {
fill: #6f7b88;
}
}
}

Expand Down