modal: Add keyboard option to Modal to enable/disable default keyboard event. (#400)

This commit is contained in:
ihavecoke 2024-11-08 13:50:05 +08:00 committed by GitHub
parent d4ee273bf5
commit 5aad0bf340
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 33 additions and 10 deletions

View file

@ -154,6 +154,7 @@ pub struct ModalStory {
modal_overlay: bool, modal_overlay: bool,
model_show_close: bool, model_show_close: bool,
model_padding: bool, model_padding: bool,
model_keyboard: bool,
} }
impl super::Story for ModalStory { impl super::Story for ModalStory {
@ -274,6 +275,7 @@ impl ModalStory {
modal_overlay: true, modal_overlay: true,
model_show_close: true, model_show_close: true,
model_padding: true, model_padding: true,
model_keyboard: true,
} }
} }
@ -335,11 +337,13 @@ impl ModalStory {
let date_picker = self.date_picker.clone(); let date_picker = self.date_picker.clone();
let dropdown = self.dropdown.clone(); let dropdown = self.dropdown.clone();
let view = cx.view().clone(); let view = cx.view().clone();
let keyboard = self.model_keyboard;
cx.open_modal(move |modal, _| { cx.open_modal(move |modal, _| {
modal modal
.title("Form Modal") .title("Form Modal")
.overlay(overlay) .overlay(overlay)
.keyboard(keyboard)
.show_close(modal_show_close) .show_close(modal_show_close)
.when(!modal_padding, |this| this.p(px(0.))) .when(!modal_padding, |this| this.p(px(0.)))
.child( .child(
@ -448,6 +452,15 @@ impl Render for ModalStory {
view.model_padding = !view.model_padding; view.model_padding = !view.model_padding;
cx.notify(); cx.notify();
})), })),
)
.child(
Checkbox::new("modal-keyboard")
.label("Keyboard")
.checked(self.model_keyboard)
.on_click(cx.listener(|view, _, cx| {
view.model_keyboard = !view.model_keyboard;
cx.notify();
})),
), ),
) )
.child( .child(

View file

@ -34,6 +34,7 @@ pub struct Modal {
on_close: Rc<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>, on_close: Rc<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>,
show_close: bool, show_close: bool,
overlay: bool, overlay: bool,
keyboard: bool,
/// This will be change when open the modal, the focus handle is create when open the modal. /// This will be change when open the modal, the focus handle is create when open the modal.
pub(crate) focus_handle: FocusHandle, pub(crate) focus_handle: FocusHandle,
@ -75,6 +76,7 @@ impl Modal {
width: px(480.), width: px(480.),
max_width: None, max_width: None,
overlay: true, overlay: true,
keyboard: true,
layer_ix: 0, layer_ix: 0,
overlay_visible: true, overlay_visible: true,
on_close: Rc::new(|_, _| {}), on_close: Rc::new(|_, _| {}),
@ -133,6 +135,12 @@ impl Modal {
self self
} }
/// Set whether to support keyboard esc to close the modal, defaults to `true`.
pub fn keyboard(mut self, keyboard: bool) -> Self {
self.keyboard = keyboard;
self
}
pub(crate) fn has_overlay(&self) -> bool { pub(crate) fn has_overlay(&self) -> bool {
self.overlay self.overlay
} }
@ -185,7 +193,8 @@ impl RenderOnce for Modal {
.id(SharedString::from(format!("modal-{layer_ix}"))) .id(SharedString::from(format!("modal-{layer_ix}")))
.key_context(CONTEXT) .key_context(CONTEXT)
.track_focus(&self.focus_handle) .track_focus(&self.focus_handle)
.on_action({ .when(self.keyboard, |this| {
this.on_action({
let on_close = self.on_close.clone(); let on_close = self.on_close.clone();
move |_: &Escape, cx| { move |_: &Escape, cx| {
// FIXME: // FIXME:
@ -196,6 +205,7 @@ impl RenderOnce for Modal {
cx.close_modal(); cx.close_modal();
} }
}) })
})
.absolute() .absolute()
.occlude() .occlude()
.relative() .relative()