We're dsplce.co, check out our work on github.com/dsplce-co π€
dioxus-modal
Modal composable for Dioxus β A minimal and type-safe framework for modals in Dioxus applications.
βΈ»
π€ Features
β Type-safe modal system with generics β Pass additional context to your modals β Close your modals from anywhere with a global fn β ARIA-compliant β Esc key handling β Portal-style rendering with proper z-index β Proc macro for modal component creation β Zero external CSS dependencies
βΈ»
π¦ Installation
Add to your Cargo.toml:
[]
= "0.3"
This crate requires Rust 2024 edition and Dioxus version 0.7. Check the compatibility table for other supported Dioxus versions.
Fullstack applications
You need to enable the crate's SSR feature on the server for fullstack apps. In your Cargo.toml:
[]
= ["dioxus/server", "dioxus-modal/ssr"]
This will tell dioxus-modal and its dependencies not to perform DOM-related operations at the stage of server side rendering.
βΈ»
π§ͺ Usage
1. Set up modal collector
Add the ModalCollector component to your app to enable rendering modals.
use *;
use *;
2. Create modal component
Imagine in your application there is a user list view, and you want to add the functionality to delete a user to it. You decide a confirmation dialog would come in handy.
In dioxus-modal, your modal component needs to adhere to the following signature:
;
Where:
Inputis dynamic data typically not known until the modal's opening is triggered (in our example it would be the user to delete). Should satisfy'staticContextis something constant, passed to the modal on registration and thus not changeable (eg. a function responsible for user deletion). Should satisfy'static + Clone
Let's implement the confirmation dialog:
use *;
3. Use the modal
Now that you've defined the confirmation modal, let's call it using the use_modal! macro:
use ;
use *;
βΈ»
π API Reference
ModalCollector
Singleton component that manages modal state and rendering.
#[modal]
Proc macro that helps the ModalCollector render your modals.
use_modal!
Creates a typed modal controller:
// Without context
let modal = use_modal!;
// With context
let modal = use_modal!;
Returns a modal struct with the methods:
open(args)- Opens the modal with provided argumentsclose()- Closes the modal
close
Close the modal from anywhere in your application:
close;
Defining a modal
With both context and input:
let modal = use_modal!;
modal.open
Skipping context:
let modal = use_modal!;
modal.open
Skipping input:
let modal = use_modal!;
modal.open
Modal Features
- Accessibility: Proper ARIA attributes (
role="dialog",aria-modal) - Keyboard Navigation: Esc key closes modal out of the box
- Global State: Modals have a single place to render, and there is always one modal visible at a time (why would you want to show more than one modal at a time? π€¨)
- Overlay: Semi-transparent backdrop with proper positioning
- Responsive: Full viewport coverage with centered content
- Transitions: Built-in smooth enter/leave modal transitions
βΈ»
Compatibility with Dioxus versions
| Dioxus version | dioxus-modal version |
|---|---|
0.7 |
0.3 |
0.6 |
0.2 |
βΈ»
π Repo & Contributions
π¦ Crate: crates.io/crates/dioxus-modal π οΈ Repo: github.com/dsplce-co/dioxus-modal
Contributions, issues, ideas? Hit us up π€
βΈ»
π License
MIT or Apache-2.0, at your option.
βΈ»