clauderic / clauderic/dnd-kit

[question, svelte]: Is there a way to initialize draggables lazy?

Open
#2,000 0 comments 0 reactions 0 assignees View on GitHub
question svelte triage:done
Dominant language
TypeScript
Stars
17.6k
Forks
924
Avg merge
2d 10h
Merged PRs (30d)
2

Description

I had a lot of components and want to have ability to attach draggable lazily.
Maybe outside of component on hover.

Example:
Image

My code:

```svelte

import type { Interval } from '../state/Interval.svelte';

import Grip from '@lucide/svelte/icons/grip';
import { createDraggable } from '$lib/core/dndkit';
import { getTimelineContext } from '../context/timeline-context';

interface Props {
trackId: string;
interval: Interval;
}

const timelineCtx = getTimelineContext();

let { trackId, interval }: Props = $props();

const left = $derived(
interval.offset * timelineCtx.viewport.pixelsPerMs - timelineCtx.viewport.scrollLeft
);

const width = $derived(interval.duration * timelineCtx.viewport.pixelsPerMs);
const innerPlacementOffset = $derived(left < 0 ? left * -1 : 0);

const draggable = createDraggable({
get id() {
return interval.id;
},
get data() {
return {
tag: 'interval' as const,
trackId
};
}
});

{#if left + width > 0 && left < timelineCtx.viewport.width}


{#if width > 20}




Dragging Area


{interval.id}


{/if}

{/if}
```

PS: $lib/core/dndkit is my typesafe wrapper
PS2: This question exactly about laziness, because all "optimizations" I can make is not only about laziness of attachments

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.