pub struct FocusAwareModalStack { /* private fields */ }Expand description
Modal stack with integrated focus management.
This wrapper provides automatic focus trapping when modals open and focus restoration when they close. It manages both the modal stack and focus manager in a coordinated way.
§Invariants
- Focus trap stack depth equals the number of modals with focus groups.
- Each modal’s focus group ID is unique and not reused.
- Pop operations always call
pop_trapfor modals with focus groups.
Implementations§
Source§impl FocusAwareModalStack
impl FocusAwareModalStack
Sourcepub fn with_focus_manager(focus_manager: FocusManager) -> Self
pub fn with_focus_manager(focus_manager: FocusManager) -> Self
Create from existing stack and focus manager.
Use this when you already have a FocusManager in your application
and want to integrate modal focus trapping.
Sourcepub fn push(&mut self, modal: Box<dyn StackModal>) -> ModalId
pub fn push(&mut self, modal: Box<dyn StackModal>) -> ModalId
Push a modal without focus trapping.
The modal will be rendered and receive events, but focus is not managed.
Sourcepub fn push_with_trap(
&mut self,
modal: Box<dyn StackModal>,
focusable_ids: Vec<FocusId>,
) -> ModalId
pub fn push_with_trap( &mut self, modal: Box<dyn StackModal>, focusable_ids: Vec<FocusId>, ) -> ModalId
Sourcepub fn pop(&mut self) -> Option<ModalResult>
pub fn pop(&mut self) -> Option<ModalResult>
Pop the top modal.
If the modal had a focus group, the focus trap is popped and focus is restored to where it was before the modal opened.
Sourcepub fn pop_id(&mut self, id: ModalId) -> Option<ModalResult>
pub fn pop_id(&mut self, id: ModalId) -> Option<ModalResult>
Pop a specific modal by ID.
Warning: Popping a non-top modal with a focus group will NOT restore
focus correctly. The focus trap stack is LIFO, so only the top modal’s
trap can be safely popped. Prefer using pop() for correct focus handling.
§Behavior
- If the modal is the top modal and has a focus group,
pop_trap()is called - If the modal is NOT the top modal, the focus trap is NOT popped (would corrupt state)
Sourcepub fn pop_all(&mut self) -> Vec<ModalResult>
pub fn pop_all(&mut self) -> Vec<ModalResult>
Pop all modals, restoring focus to the original state.
Sourcepub fn handle_event(&mut self, event: &Event) -> Option<ModalResult>
pub fn handle_event(&mut self, event: &Event) -> Option<ModalResult>
Handle an event, routing to the top modal.
If the modal closes (via Escape, backdrop click, etc.), the focus trap is automatically popped and focus is restored.
Sourcepub fn is_focus_trapped(&self) -> bool
pub fn is_focus_trapped(&self) -> bool
Check if focus is currently trapped in a modal.
Sourcepub fn stack(&self) -> &ModalStack
pub fn stack(&self) -> &ModalStack
Get a reference to the underlying modal stack.
Sourcepub fn stack_mut(&mut self) -> &mut ModalStack
pub fn stack_mut(&mut self) -> &mut ModalStack
Get a mutable reference to the underlying modal stack.
Warning: Direct manipulation may desync focus state.
Sourcepub fn focus_manager(&self) -> &FocusManager
pub fn focus_manager(&self) -> &FocusManager
Get a reference to the focus manager.
Sourcepub fn focus_manager_mut(&mut self) -> &mut FocusManager
pub fn focus_manager_mut(&mut self) -> &mut FocusManager
Get a mutable reference to the focus manager.