Skip to content

Commit

Permalink
feat: Added confirm and prompt template modal
Browse files Browse the repository at this point in the history
  • Loading branch information
emirror-de committed Mar 9, 2024
1 parent e47aa94 commit 4f31b52
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/modules/modal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ extern "C" {
handler: &Closure<dyn Fn()>,
) -> Modal;

/// Internal function to create the modal confirm template.
#[wasm_bindgen(js_namespace=["$"], js_name="modal")]
fn new_modal_confirm(
props: &JsValue,
title: String,
content: String,
handler: &Closure<dyn Fn(bool)>,
) -> Modal;

/// Internal function to create the modal prompt template.
#[wasm_bindgen(js_namespace=["$"], js_name="modal")]
fn new_modal_prompt(
props: &JsValue,
title: String,
content: String,
handler: &Closure<dyn Fn(String)>,
) -> Modal;

#[wasm_bindgen(method, js_name = "modal")]
pub fn modal(this: &Modal, behavior: &JsValue);

Expand All @@ -79,6 +97,36 @@ impl Modal {
Ok(result)
}

/// Creates a `Confirm` modal.
pub fn new_confirm<H: 'static>(
title: String,
content: String,
handler: H,
) -> anyhow::Result<Self>
where
H: Fn(bool),
{
let handler = Closure::new(handler);
let result = new_modal_confirm(&JsValue::from("confirm"), title, content, &handler);
handler.forget();
Ok(result)
}

/// Creates a `Prompt` modal.
pub fn new_prompt<H: 'static>(
title: String,
content: String,
handler: H,
) -> anyhow::Result<Self>
where
H: Fn(String),
{
let handler = Closure::new(handler);
let result = new_modal_prompt(&JsValue::from("prompt"), title, content, &handler);
handler.forget();
Ok(result)
}

/// Queries the modal by the given selector.
pub fn query_from_selector(selector: &str) -> anyhow::Result<Self> {
Ok(new_modal_from_selector(&JsValue::from(selector))
Expand Down

0 comments on commit 4f31b52

Please sign in to comment.