- CSS for modal dialog
- User can click outside modal dialog to close it
This commit is contained in:
parent
3bc17cea79
commit
8be2aa0129
9 changed files with 93 additions and 52 deletions
|
|
@ -251,10 +251,7 @@ body {
|
||||||
|
|
||||||
input,
|
input,
|
||||||
button {
|
button {
|
||||||
// background-color: rgb(52, 40, 85);
|
|
||||||
border-width: 1px;
|
border-width: 1px;
|
||||||
// border-color: white;
|
|
||||||
// color: white;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
@use 'constants' as consts;
|
||||||
|
|
||||||
@mixin markdown {
|
@mixin markdown {
|
||||||
h1 {
|
h1 {
|
||||||
font-size: 140%;
|
font-size: 140%;
|
||||||
|
|
@ -23,4 +25,15 @@
|
||||||
h6 {
|
h6 {
|
||||||
font-size: 100%;
|
font-size: 100%;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Common style for popup box like modal dialog and toast.
|
||||||
|
@mixin popup {
|
||||||
|
border: 0.1em solid consts.$color-3;
|
||||||
|
border-radius: 0.5em;
|
||||||
|
background-color: consts.$color-2;
|
||||||
|
|
||||||
|
text-align: center;
|
||||||
|
padding: calc(2 * consts.$margin) calc(2 * consts.$margin);
|
||||||
|
box-shadow: -1px 1px 10px rgba(0, 0, 0, 0.3);
|
||||||
}
|
}
|
||||||
|
|
@ -1,19 +1,21 @@
|
||||||
|
@use 'mixins';
|
||||||
|
@use 'constants' as consts;
|
||||||
|
|
||||||
#modal-dialog {
|
#modal-dialog {
|
||||||
// visibility: hidden;
|
|
||||||
color: white;
|
|
||||||
width: 800px;
|
|
||||||
margin-left: -400px;
|
|
||||||
background-color: black;
|
|
||||||
text-align: center;
|
|
||||||
border-radius: 2px;
|
|
||||||
padding: 16px; // TODO: 'rem' better?
|
|
||||||
position: fixed;
|
position: fixed;
|
||||||
z-index: 1;
|
z-index: 1;
|
||||||
left: 50%;
|
left: 50%;
|
||||||
bottom: 30px;
|
width: 50%;
|
||||||
box-shadow: -1px 1px 10px rgba(0, 0, 0, 0.3);
|
transform: translate(-50%, 0%);
|
||||||
}
|
|
||||||
|
|
||||||
#modal-dialog.show {
|
@include mixins.popup;
|
||||||
visibility: visible;
|
|
||||||
|
// The inner div will cover the entire dialog and prevent user to
|
||||||
|
// click on it to close the dialog. Thus, The use can click outside
|
||||||
|
// the div and it will be considered as a click on the dialog.
|
||||||
|
padding: 0;
|
||||||
|
|
||||||
|
>div {
|
||||||
|
padding: calc(2 * consts.$margin) calc(2 * consts.$margin);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
@use 'constants' as consts;
|
@use 'constants' as consts;
|
||||||
|
@use 'mixins';
|
||||||
|
|
||||||
#toasts {
|
#toasts {
|
||||||
position: fixed;
|
position: fixed;
|
||||||
|
|
@ -11,18 +12,11 @@
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
|
|
||||||
.toast {
|
.toast {
|
||||||
// visibility: hidden;
|
|
||||||
display: none;
|
display: none;
|
||||||
width: fit-content;
|
width: fit-content;
|
||||||
align-self: center;
|
align-self: center;
|
||||||
|
|
||||||
border: 0.1em solid consts.$color-3;
|
@include mixins.popup;
|
||||||
border-radius: 0.5em;
|
|
||||||
background-color: consts.$color-2;
|
|
||||||
|
|
||||||
text-align: center;
|
|
||||||
padding: calc(2 * consts.$margin) calc(2 * consts.$margin);
|
|
||||||
box-shadow: -1px 1px 10px rgba(0, 0, 0, 0.3);
|
|
||||||
|
|
||||||
margin: consts.$margin;
|
margin: consts.$margin;
|
||||||
|
|
||||||
|
|
@ -36,14 +30,6 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// #toast.show {
|
|
||||||
// visibility: visible;
|
|
||||||
// animation:
|
|
||||||
// fadein 0.5s,
|
|
||||||
// fadeout 0.5s 9.5s;
|
|
||||||
// animation-iteration-count: 1;
|
|
||||||
// }
|
|
||||||
|
|
||||||
@keyframes fadein {
|
@keyframes fadein {
|
||||||
from {
|
from {
|
||||||
opacity: 0;
|
opacity: 0;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
{# Needed by the frontend modal_dialog module. #}
|
{# Needed by the frontend modal_dialog module. #}
|
||||||
<dialog id="modal-dialog">
|
<dialog id="modal-dialog">
|
||||||
<div class="content"></div>
|
<div>
|
||||||
<input type="button" class="ok" value="OK">
|
<div class="content"></div>
|
||||||
<input type="button" class="cancel" value="Cancel">
|
<input type="button" class="ok" value="OK">
|
||||||
|
<input type="button" class="cancel" value="Cancel">
|
||||||
|
</div>
|
||||||
</dialog>
|
</dialog>
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use futures::{future::FutureExt, pin_mut, select};
|
use futures::{future::FutureExt, pin_mut, select};
|
||||||
use web_sys::{Element, HtmlDialogElement};
|
use web_sys::{Element, HtmlDialogElement, Node};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
on_click,
|
on_click,
|
||||||
|
|
@ -25,20 +25,17 @@ where
|
||||||
/// Show a modal dialog with a copy of the given HTML element as content and execute an initilizer
|
/// Show a modal dialog with a copy of the given HTML element as content and execute an initilizer
|
||||||
/// to modify the given content.
|
/// to modify the given content.
|
||||||
/// Call the given function when OK is pressed.
|
/// Call the given function when OK is pressed.
|
||||||
pub async fn show_and_initialize_with_ok<T, V, W, U>(
|
pub async fn show_and_initialize_with_ok<FInit, TInit, FOk, TOk>(
|
||||||
element_selector: &str,
|
element_selector: &str,
|
||||||
initializer: T,
|
initializer: FInit,
|
||||||
ok: V,
|
ok: FOk,
|
||||||
) -> Option<W>
|
) -> Option<TOk>
|
||||||
where
|
where
|
||||||
T: AsyncFn(Element) -> U,
|
FInit: AsyncFn(Element) -> TInit,
|
||||||
V: Fn(Element, U) -> W,
|
FOk: Fn(Element, TInit) -> TOk,
|
||||||
{
|
{
|
||||||
let dialog: HtmlDialogElement = by_id("modal-dialog");
|
let dialog: HtmlDialogElement = by_id("modal-dialog");
|
||||||
|
|
||||||
let input_ok: Element = dialog.selector(".ok");
|
|
||||||
let input_cancel: Element = dialog.selector(".cancel");
|
|
||||||
|
|
||||||
let content_element = dialog.selector::<Element>(".content");
|
let content_element = dialog.selector::<Element>(".content");
|
||||||
|
|
||||||
let element: Element = selector_and_clone(element_selector);
|
let element: Element = selector_and_clone(element_selector);
|
||||||
|
|
@ -46,16 +43,25 @@ where
|
||||||
content_element.append_child(&element).unwrap();
|
content_element.append_child(&element).unwrap();
|
||||||
let init_result = initializer(element.clone()).await;
|
let init_result = initializer(element.clone()).await;
|
||||||
|
|
||||||
|
let input_ok: Element = dialog.selector(".ok");
|
||||||
|
let input_cancel: Element = dialog.selector(".cancel");
|
||||||
|
|
||||||
dialog.show_modal().unwrap();
|
dialog.show_modal().unwrap();
|
||||||
|
|
||||||
let click_ok = on_click::OnClick::new(&input_ok).fuse();
|
let click_ok = on_click::OnClick::new(&input_ok).fuse();
|
||||||
let click_cancel = on_click::OnClick::new(&input_cancel).fuse();
|
let click_cancel = on_click::OnClick::new(&input_cancel).fuse();
|
||||||
|
|
||||||
pin_mut!(click_ok, click_cancel);
|
let dialog_cloned = dialog.clone();
|
||||||
|
let click_dialog =
|
||||||
|
on_click::OnClick::new_with_filter(&dialog, move |node| node == &dialog_cloned as &Node)
|
||||||
|
.fuse();
|
||||||
|
|
||||||
|
pin_mut!(click_ok, click_cancel, click_dialog);
|
||||||
|
|
||||||
let result = select! {
|
let result = select! {
|
||||||
() = click_ok => Some(ok(element, init_result)),
|
() = click_ok => Some(ok(element, init_result)),
|
||||||
() = click_cancel => None,
|
() = click_cancel => None,
|
||||||
|
() = click_dialog => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
dialog.close();
|
dialog.close();
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ use std::{
|
||||||
task::{Context, Poll},
|
task::{Context, Poll},
|
||||||
};
|
};
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
use web_sys::EventTarget;
|
use web_sys::{EventTarget, Node};
|
||||||
|
|
||||||
// From: https://docs.rs/gloo-events/latest/gloo_events/struct.EventListener.html
|
// From: https://docs.rs/gloo-events/latest/gloo_events/struct.EventListener.html
|
||||||
|
|
||||||
|
|
@ -19,11 +19,25 @@ pub struct OnClick {
|
||||||
|
|
||||||
impl OnClick {
|
impl OnClick {
|
||||||
pub fn new(target: &EventTarget) -> Self {
|
pub fn new(target: &EventTarget) -> Self {
|
||||||
|
Self::new_with_filter(target, |_| true)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_with_filter<F>(target: &EventTarget, filter: F) -> Self
|
||||||
|
where
|
||||||
|
F: Fn(&Node) -> bool + 'static,
|
||||||
|
{
|
||||||
let (sender, receiver) = mpsc::unbounded();
|
let (sender, receiver) = mpsc::unbounded();
|
||||||
|
|
||||||
// Attach an event listener.
|
let listener = EventListener::new(target, "click", move |event| {
|
||||||
let listener = EventListener::new(target, "click", move |_event| {
|
let mut send = true;
|
||||||
sender.unbounded_send(()).unwrap_throw();
|
if let Some(event_target) = event.target() {
|
||||||
|
if let Some(node) = event_target.dyn_ref::<Node>() {
|
||||||
|
send = filter(node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if send {
|
||||||
|
sender.unbounded_send(()).unwrap_throw();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Self { receiver, listener }
|
Self { receiver, listener }
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use gloo::{events::EventListener, utils::document};
|
use gloo::{console::log, events::EventListener, utils::document};
|
||||||
use wasm_bindgen_futures::spawn_local;
|
use wasm_bindgen_futures::spawn_local;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
|
@ -61,7 +61,11 @@ pub fn setup_page() {
|
||||||
|
|
||||||
EventListener::new(&by_id("test-modal-dialog"), "click", move |_event| {
|
EventListener::new(&by_id("test-modal-dialog"), "click", move |_event| {
|
||||||
spawn_local(async move {
|
spawn_local(async move {
|
||||||
modal_dialog::show("#hidden-templates .modal-test-message").await;
|
if modal_dialog::show("#hidden-templates .modal-test-message").await {
|
||||||
|
log!("Ok");
|
||||||
|
} else {
|
||||||
|
log!("Cancel");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.forget();
|
.forget();
|
||||||
|
|
|
||||||
|
|
@ -49,6 +49,23 @@ impl SelectorExt for Element {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Not used anymore.
|
||||||
|
// pub trait NodeHelperExt {
|
||||||
|
// fn is_parent(&self, child: &Node) -> bool;
|
||||||
|
// }
|
||||||
|
// impl NodeHelperExt for Node {
|
||||||
|
// fn is_parent(&self, child: &Node) -> bool {
|
||||||
|
// let mut node = child.clone();
|
||||||
|
// while let Some(parent) = node.parent_node() {
|
||||||
|
// if &parent == self {
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
// node = parent;
|
||||||
|
// }
|
||||||
|
// false
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
pub fn selector<T>(selectors: &str) -> T
|
pub fn selector<T>(selectors: &str) -> T
|
||||||
where
|
where
|
||||||
T: JsCast,
|
T: JsCast,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue