Skip to main content

dioxus_dnd/core/components/
provider.rs

1//! The [`DndProvider`] component: provides a `DndContext<T>` to a subtree.
2
3use dioxus::prelude::*;
4
5use crate::core::collision::ReleasePolicy;
6use crate::core::hooks::{use_dnd_provider, use_zone_registry};
7use crate::core::types::Direction;
8
9/// Provides a `DndContext<T>` to its children.
10#[component]
11pub fn DndProvider<T: Clone + PartialEq + 'static>(
12    /// Internal marker; never set this.
13    #[props(default)]
14    phantom: std::marker::PhantomData<T>,
15    /// Layout direction: `Direction::Rtl` mirrors keyboard navigation and
16    /// spatial zone ordering to follow the visual right-to-left flow.
17    #[props(default)]
18    dir: Direction,
19    /// Collision detector, recovery radius, and sticky-hover behavior for
20    /// this provider's zones.
21    #[props(default)]
22    release: ReleasePolicy<T>,
23    children: Element,
24) -> Element {
25    let _ = phantom;
26    use_dnd_provider::<T>();
27    let mut registry = use_zone_registry::<T>();
28    // Seed the provider policy before children register or a headless VDOM
29    // can drive a drag. The effect below keeps later prop changes reactive.
30    use_hook(move || {
31        registry.set_direction(dir);
32        registry.set_release_policy(release);
33    });
34    use_effect(use_reactive!(|(dir, release)| {
35        registry.set_direction(dir);
36        registry.set_release_policy(release);
37    }));
38    rsx! {
39        {children}
40    }
41}