dioxus-dnd-kit 0.1.3

A flexible, performant, and layout-stable Drag-and-Drop library for Dioxus.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
use dioxus::prelude::*;
use dioxus_dnd_kit::prelude::*;
use gloo_timers::future::TimeoutFuture;

const APP_CSS: &str = r#"
html,
body {
    margin: 0;
    min-height: 100%;
    background: #f5f7fb;
    color: #1f2937;
    font-family:
        Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input {
    font: inherit;
}

.relative {
    position: relative;
}

.resize-page {
    box-sizing: border-box;
    min-height: 100vh;
    padding: 32px;
    background: #f5f7fb;
}

.resize-shell {
    width: min(1120px, 100%);
    margin: 0 auto;
    display: grid;
    grid-template-columns: minmax(360px, 1fr) 320px;
    gap: 24px;
    align-items: start;
}

.resize-header {
    grid-column: 1 / -1;
    display: flex;
    justify-content: space-between;
    gap: 24px;
    align-items: flex-end;
    padding-bottom: 8px;
}

.resize-title {
    margin: 0;
    font-size: 28px;
    line-height: 1.1;
    font-weight: 750;
}

.resize-subtitle {
    margin: 8px 0 0;
    max-width: 680px;
    color: #5b6472;
    line-height: 1.5;
}

.resize-counter {
    min-width: 136px;
    border: 1px solid #cfd7e6;
    border-radius: 8px;
    padding: 12px 14px;
    background: #ffffff;
    text-align: right;
}

.resize-counter span {
    display: block;
    color: #6b7280;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.06em;
}

.resize-counter strong {
    display: block;
    margin-top: 4px;
    font-size: 24px;
}

.resize-workspace,
.resize-inspector {
    border: 1px solid #d7dde8;
    border-radius: 8px;
    background: #ffffff;
}

.resize-workspace {
    padding: 18px;
}

.resize-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 16px;
}

.resize-toolbar-title {
    margin: 0;
    font-size: 15px;
    font-weight: 700;
}

.resize-toggle {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    color: #374151;
    font-size: 14px;
    white-space: nowrap;
}

.resize-toggle input {
    width: 18px;
    height: 18px;
    accent-color: #0f766e;
}

.resize-action {
    border: 1px solid #b8c2d4;
    border-radius: 8px;
    padding: 8px 11px;
    background: #f8fafc;
    color: #1f2937;
    cursor: pointer;
}

.resize-action:hover {
    background: #eef4ff;
}

.resize-droppable {
    display: grid;
    gap: 14px;
    min-height: 450px;
    padding: 14px;
    border: 1px solid #ccd5e3;
    border-radius: 8px;
    background: #f8fafc;
}

.resize-droppable > .relative {
    min-height: 74px;
}

.task-card {
    box-sizing: border-box;
    height: 74px;
    overflow: hidden;
    border: 1px solid #d5dce8;
    border-left: 5px solid #2563eb;
    border-radius: 8px;
    background: #ffffff;
    box-shadow: 0 1px 2px rgba(15, 23, 42, 0.06);
    transition:
        height 180ms ease,
        border-color 180ms ease,
        box-shadow 180ms ease;
}

.task-card-expanded {
    box-shadow: 0 10px 24px rgba(15, 23, 42, 0.10);
}

.task-content {
    box-sizing: border-box;
    height: 100%;
    padding: 12px;
    display: grid;
    grid-template-rows: auto 1fr;
    gap: 10px;
}

.task-main {
    display: grid;
    grid-template-columns: 38px minmax(0, 1fr) auto;
    gap: 12px;
    align-items: center;
}

.drag-handle {
    width: 38px;
    height: 38px;
    display: grid;
    place-items: center;
    border: 1px solid #cbd5e1;
    border-radius: 8px;
    background: #f1f5f9;
    color: #475569;
    cursor: grab;
    user-select: none;
}

.drag-handle:active {
    cursor: grabbing;
}

.task-name {
    min-width: 0;
}

.task-name strong {
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 15px;
}

.task-name span {
    display: block;
    margin-top: 3px;
    color: #64748b;
    font-size: 12px;
}

.task-details {
    margin: 0;
    color: #475569;
    line-height: 1.45;
    font-size: 14px;
}

.resize-inspector {
    padding: 18px;
    display: grid;
    gap: 16px;
}

.inspector-title {
    margin: 0;
    font-size: 15px;
    font-weight: 700;
}

.metric {
    display: grid;
    gap: 4px;
}

.metric span {
    color: #64748b;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.06em;
}

.metric strong,
.metric code {
    color: #111827;
    font-size: 15px;
}

.height-list {
    display: grid;
    gap: 8px;
    margin: 0;
    padding: 0;
    list-style: none;
}

.height-list li {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    border-bottom: 1px solid #e5e7eb;
    padding-bottom: 8px;
    color: #374151;
}

.height-list li:last-child {
    border-bottom: 0;
    padding-bottom: 0;
}

.placeholder {
    box-sizing: border-box;
    height: 100%;
    border: 1px dashed #94a3b8;
    border-radius: 8px;
    background: repeating-linear-gradient(
        135deg,
        #eef2ff,
        #eef2ff 10px,
        #e0f2fe 10px,
        #e0f2fe 20px
    );
}

@media (max-width: 860px) {
    .resize-page {
        padding: 18px;
    }

    .resize-shell {
        grid-template-columns: 1fr;
    }

    .resize-header {
        display: grid;
        gap: 14px;
    }

    .resize-counter {
        text-align: left;
    }

    .resize-toolbar {
        align-items: flex-start;
        flex-direction: column;
    }
}
"#;

fn main() {
    launch(App);
}

#[derive(Clone, Debug, PartialEq)]
struct ResizeTask {
    id: usize,
    title: &'static str,
    detail: &'static str,
    expanded: bool,
    collapsed_height: u32,
    expanded_height: u32,
    tone: &'static str,
}

impl ResizeTask {
    fn height(&self) -> u32 {
        if self.expanded {
            self.expanded_height
        } else {
            self.collapsed_height
        }
    }
}

#[component]
fn App() -> Element {
    let tasks = use_signal(|| {
        vec![
            ResizeTask {
                id: 1,
                title: "Inbox triage",
                detail: "Expanded content simulates a panel with controls, metadata, and wrapped text.",
                expanded: false,
                collapsed_height: 74,
                expanded_height: 156,
                tone: "#2563eb",
            },
            ResizeTask {
                id: 2,
                title: "Design review",
                detail: "This row starts expanded so the list has mixed heights before any drag begins.",
                expanded: true,
                collapsed_height: 74,
                expanded_height: 174,
                tone: "#0f766e",
            },
            ResizeTask {
                id: 3,
                title: "Release checklist",
                detail: "Collapse and expand this item, then drag around its neighbors to validate rects.",
                expanded: false,
                collapsed_height: 74,
                expanded_height: 146,
                tone: "#b45309",
            },
            ResizeTask {
                id: 4,
                title: "QA notes",
                detail: "The collision target should follow the current visual height, not a stale cached one.",
                expanded: false,
                collapsed_height: 74,
                expanded_height: 168,
                tone: "#be123c",
            },
        ]
    });
    let layout_version = use_signal(|| 0_u64);
    let recalculate_count = use_signal(|| 0_u64);
    let mut auto_resize_active = use_signal(|| false);

    let render_tasks = tasks;
    let render_layout_version = layout_version;
    let render = move |task: ResizeTask| {
        let tasks = render_tasks;
        let layout_version = render_layout_version;
        let id = task.id;
        let title = task.title;
        let detail = task.detail;
        let expanded = task.expanded;
        let height = task.height();
        let tone = task.tone;
        let state_label = if expanded { "Expanded" } else { "Collapsed" };
        let card_class = if expanded {
            "task-card task-card-expanded"
        } else {
            "task-card"
        };

        rsx! {
            div {
                class: "{card_class}",
                style: "height: {height}px; border-left-color: {tone};",
                div { class: "task-content",
                    div { class: "task-main",
                        DraggableHandler { class: "drag-handle", item: task.clone(), "::: " }
                        div { class: "task-name",
                            strong { "{title}" }
                            span { "#{id} / {state_label} / {height}px" }
                        }
                        label {
                            class: "resize-toggle",
                            onmousedown: move |evt| evt.stop_propagation(),
                            input {
                                r#type: "checkbox",
                                checked: expanded,
                                onchange: move |evt| {
                                    set_task_expanded(tasks, id, evt.checked());
                                    request_layout_recalculation(layout_version);
                                }
                            }
                            "Expanded"
                        }
                    }
                    p { class: "task-details", "{detail}" }
                }
            }
        }
    };

    let placeholder_render = move |_| {
        rsx! {
            div { class: "placeholder" }
        }
    };

    let order_text = tasks()
        .iter()
        .map(|task| task.id.to_string())
        .collect::<Vec<_>>()
        .join(" -> ");

    rsx! {
        style { "{APP_CSS}" }

        DraggableView {
            class: "resize-page",
            key_gen: |task: ResizeTask| task.id.to_string(),
            render,
            placeholder_render,

            ResizeRecalculationBridge {
                layout_version,
                recalculate_count,
            }
            ActiveResizeProbe {
                auto_resize_active,
                tasks,
                layout_version,
            }

            div { class: "resize-shell",
                header { class: "resize-header",
                    div {
                        h1 { class: "resize-title", "Resizable DnD list" }
                        p { class: "resize-subtitle",
                            "A focused regression surface for collapse/expand layout changes and cached collision rectangles."
                        }
                    }
                    div { class: "resize-counter",
                        span { "Recalculations" }
                        strong { "{recalculate_count()}" }
                    }
                }

                main { class: "resize-workspace",
                    div { class: "resize-toolbar",
                        h2 { class: "resize-toolbar-title", "Drag surface" }
                        label { class: "resize-toggle",
                            input {
                                r#type: "checkbox",
                                checked: auto_resize_active(),
                                onchange: move |evt| auto_resize_active.set(evt.checked()),
                            }
                            "Auto resize active"
                        }
                    }

                    Droppable {
                        class: "resize-droppable",
                        items: tasks,
                    }
                }

                aside { class: "resize-inspector",
                    h2 { class: "inspector-title", "Diagnostics" }
                    div { class: "metric",
                        span { "Order" }
                        code { "{order_text}" }
                    }
                    div { class: "metric",
                        span { "Layout version" }
                        strong { "{layout_version()}" }
                    }
                    button {
                        class: "resize-action",
                        onclick: move |_| request_layout_recalculation(layout_version),
                        "Force recalculation"
                    }
                    ul { class: "height-list",
                        for task in tasks().iter() {
                            li {
                                span { "#{task.id} {task.title}" }
                                strong { "{task.height()}px" }
                            }
                        }
                    }
                }
            }
        }
    }
}

#[component]
fn ResizeRecalculationBridge(
    layout_version: Signal<u64>,
    mut recalculate_count: Signal<u64>,
) -> Element {
    let mut context = use_context::<DndContext<ResizeTask>>();

    use_effect(move || {
        let version = layout_version();
        if version == 0 {
            return;
        }

        spawn(async move {
            TimeoutFuture::new(35).await;
            context.recalculate_rects.with_mut(|value| *value += 1);
            recalculate_count.with_mut(|value| *value += 1);
        });
    });

    rsx! {}
}

#[component]
fn ActiveResizeProbe(
    auto_resize_active: Signal<bool>,
    mut tasks: Signal<Vec<ResizeTask>>,
    mut layout_version: Signal<u64>,
) -> Element {
    let context = use_context::<DndContext<ResizeTask>>();
    let mut last_active_id = use_signal(|| None::<usize>);

    use_effect(move || {
        let active_id = context.active.read().as_ref().map(|task| task.id);

        if !auto_resize_active() {
            last_active_id.set(None);
            return;
        }

        let Some(active_id) = active_id else {
            last_active_id.set(None);
            return;
        };

        if *last_active_id.peek() == Some(active_id) {
            return;
        }
        last_active_id.set(Some(active_id));

        spawn(async move {
            TimeoutFuture::new(450).await;
            let still_active = context
                .active
                .peek()
                .as_ref()
                .map(|task| task.id == active_id)
                .unwrap_or(false);

            if still_active {
                toggle_task_height(tasks, active_id);
                request_layout_recalculation(layout_version);
            }
        });
    });

    rsx! {}
}

fn set_task_expanded(mut tasks: Signal<Vec<ResizeTask>>, id: usize, expanded: bool) {
    tasks.with_mut(|tasks| {
        if let Some(task) = tasks.iter_mut().find(|task| task.id == id) {
            task.expanded = expanded;
        }
    });
}

fn toggle_task_height(mut tasks: Signal<Vec<ResizeTask>>, id: usize) {
    tasks.with_mut(|tasks| {
        if let Some(task) = tasks.iter_mut().find(|task| task.id == id) {
            task.expanded = !task.expanded;
        }
    });
}

fn request_layout_recalculation(mut layout_version: Signal<u64>) {
    layout_version.with_mut(|version| *version += 1);
}