Skip to content

Commit

Permalink
Fix doubleclick to play on playlists
Browse files Browse the repository at this point in the history
  • Loading branch information
martpie committed Jan 7, 2025
1 parent 4915a9f commit 460bd0b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 32 deletions.
25 changes: 25 additions & 0 deletions src/components/TrackRow.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,29 @@
border-top-color: rgba(255 255 255 / 0.2);
}
}

&.isDragging {
opacity: 0.5;
}

&.isOver:not(.isDragging) {
&::after {
pointer-events: none;
position: absolute;
z-index: 1;
display: block;
width: 100%;
content: "";
height: 2px;
background-color: var(--main-color);
}
}

&.isAbove::after {
top: -1px;
}

&.isBelow::after {
bottom: -1px;
}
}
21 changes: 8 additions & 13 deletions src/components/TrackRow.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useSortable } from '@dnd-kit/sortable';
import { CSS } from '@dnd-kit/utilities';
import cx from 'classnames';
import type React from 'react';

Expand Down Expand Up @@ -49,9 +48,10 @@ export default function TrackRow(props: Props) {
attributes,
listeners,
setNodeRef,
transform,
transition,
isDragging,
isOver,
activeIndex,
overIndex,
} = useSortable({
id: props.track.id,
disabled: !draggable,
Expand All @@ -61,18 +61,13 @@ export default function TrackRow(props: Props) {
},
});

const style: React.CSSProperties | undefined = transform
? {
transform: CSS.Translate.toString(transform),
transition,
zIndex: 2,
visibility: isDragging ? 'hidden' : 'visible',
}
: undefined;

const trackClasses = cx(styles.track, {
[styles.selected]: selected,
[styles.even]: index % 2 === 0,
[styles.isDragging]: isDragging,
[styles.isOver]: isOver,
[styles.isAbove]: isOver && overIndex < activeIndex,
[styles.isBelow]: isOver && overIndex > activeIndex,
});

return (
Expand All @@ -93,7 +88,7 @@ export default function TrackRow(props: Props) {
ref={setNodeRef}
{...listeners}
{...attributes}
style={{ ...style, ...props.style }} // memo that
style={props.style}
>
<div className={`${styles.cell} ${cellStyles.cellTrackPlaying}`}>
{props.isPlaying ? <PlayingIndicator /> : null}
Expand Down
28 changes: 9 additions & 19 deletions src/components/TracksList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import {
type DragEndEvent,
DragOverlay,
type DragStartEvent,
KeyboardSensor,
PointerSensor,
useSensor,
useSensors,
} from '@dnd-kit/core';
import { restrictToVerticalAxis } from '@dnd-kit/modifiers';
import {
Expand Down Expand Up @@ -220,16 +224,11 @@ export default function TracksList(props: Props) {
/**
* Playlist tracks re-order events handlers
*/
const [dragOverlay, setDragOverlay] = useState<Track | null>(null);

const onDragStart = useCallback(
(event: DragStartEvent) => {
const track = tracks[event.active.data.current?.index];
const pointerSensor = useSensor(PointerSensor, {
activationConstraint: { distance: 8 },
});

setDragOverlay(track);
},
[tracks],
);
const sensors = useSensors(pointerSensor, useSensor(KeyboardSensor));

const onDragEnd = useCallback(
(event: DragEndEvent) => {
Expand Down Expand Up @@ -472,10 +471,10 @@ export default function TracksList(props: Props) {

return (
<DndContext
onDragStart={onDragStart}
onDragEnd={onDragEnd}
id="dnd-playlist"
modifiers={DND_MODIFIERS}
sensors={sensors}
>
<div className={styles.tracksList}>
<Keybinding onKey={onKey} preventInputConflict />
Expand Down Expand Up @@ -526,15 +525,6 @@ export default function TracksList(props: Props) {
/>
);
})}
<DragOverlay dropAnimation={null}>
{dragOverlay ? (
<TrackRow
selected={selectedTracks.has(dragOverlay.id)}
track={dragOverlay}
index={-1}
/>
) : null}
</DragOverlay>
</SortableContext>
</div>
</div>
Expand Down

0 comments on commit 460bd0b

Please sign in to comment.