Skip to content

Commit ebcce02

Browse files
ranilelukechu10
andauthored
Add wrappers for alert, confirm, and prompt functions (#123)
* Add wrappers for alert, confirm, and prompt functions Fixes: #122 * Update crates/dialogs/src/lib.rs Co-authored-by: Luke Chu <[email protected]> * Docs + README with `update-readmes.sh` script * Fix licence Co-authored-by: Luke Chu <[email protected]>
1 parent 2c9e776 commit ebcce02

File tree

6 files changed

+101
-4
lines changed

6 files changed

+101
-4
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ gloo-timers = { version = "0.2.0", path = "crates/timers" }
1515
gloo-console-timer = { version = "0.1.0", path = "crates/console-timer" }
1616
gloo-events = { version = "0.1.0", path = "crates/events" }
1717
gloo-file = { version = "0.1.0", path = "crates/file" }
18+
gloo-dialogs = { version = "0.1.0", path = "crates/dialogs" }
1819

1920
[features]
2021
default = []

crates/dialogs/Cargo.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "gloo-dialogs"
3+
description = "Convenience crate for working with dialogs in browser"
4+
version = "0.1.0"
5+
authors = ["Rust and WebAssembly Working Group"]
6+
edition = "2018"
7+
license = "MIT OR Apache-2.0"
8+
readme = "README.md"
9+
repository = "https://github.com/rustwasm/gloo/tree/master/crates/dialogs"
10+
homepage = "https://github.com/rustwasm/gloo"
11+
categories = ["api-bindings", "asynchronous", "wasm"]
12+
13+
[dependencies]
14+
wasm-bindgen = "0.2"
15+
16+
[dependencies.web-sys]
17+
version = "0.3"
18+
features = [
19+
"Window"
20+
]

crates/dialogs/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<div align="center">
2+
3+
<h1><code>gloo-dialogs</code></h1>
4+
5+
<p>
6+
<a href="https://dev.azure.com/rustwasm/gloo/_build?definitionId=6"><img src="https://img.shields.io/azure-devops/build/rustwasm/gloo/6.svg?style=flat-square" alt="Build Status" /></a>
7+
<a href="https://crates.io/crates/gloo-dialogs"><img src="https://img.shields.io/crates/v/gloo-dialogs.svg?style=flat-square" alt="Crates.io version" /></a>
8+
<a href="https://crates.io/crates/gloo-dialogs"><img src="https://img.shields.io/crates/d/gloo-dialogs.svg?style=flat-square" alt="Download" /></a>
9+
<a href="https://docs.rs/gloo-dialogs"><img src="https://img.shields.io/badge/docs-latest-blue.svg?style=flat-square" alt="docs.rs docs" /></a>
10+
</p>
11+
12+
<h3>
13+
<a href="https://docs.rs/gloo-dialogs">API Docs</a>
14+
<span> | </span>
15+
<a href="https://github.com/rustwasm/gloo/blob/master/CONTRIBUTING.md">Contributing</a>
16+
<span> | </span>
17+
<a href="https://discordapp.com/channels/442252698964721669/443151097398296587">Chat</a>
18+
</h3>
19+
20+
<sub>Built with 🦀🕸 by <a href="https://rustwasm.github.io/">The Rust and WebAssembly Working Group</a></sub>
21+
</div>
22+
23+
This crate provides wrapper for `alert`, `prompt` and `confirm` functions.
24+
`web-sys` provides a raw API which is hard to use. This crate provides an easy-to-use,
25+
idiomatic Rust API for these functions.
26+
27+
See the documentation for [`alert`], [`prompt`] and [`confirm`] for more information.

crates/dialogs/src/lib.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//! This crate provides wrapper for `alert`, `prompt` and `confirm` functions.
2+
//! `web-sys` provides a raw API which is hard to use. This crate provides an easy-to-use,
3+
//! idiomatic Rust API for these functions.
4+
//!
5+
//! See the documentation for [`alert`], [`prompt`] and [`confirm`] for more information.
6+
7+
use wasm_bindgen::prelude::*;
8+
9+
/// Calls the alert function.
10+
///
11+
/// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Window/alert)
12+
pub fn alert(message: &str) {
13+
window().alert_with_message(message).unwrap_throw()
14+
}
15+
16+
/// Calls the confirm function.
17+
///
18+
/// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Window/confirm)
19+
pub fn confirm(message: &str) -> bool {
20+
window().confirm_with_message(message).unwrap_throw()
21+
}
22+
23+
/// Calls the `prompt` function.
24+
///
25+
/// A default value can be supplied which will be returned if the user doesn't input anything.
26+
/// This function will return `None` if the value of `default` is `None` and the user cancels
27+
/// the operation.
28+
///
29+
/// [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt)
30+
pub fn prompt(message: &str, default: Option<&str>) -> Option<String> {
31+
match default {
32+
Some(default) => window()
33+
.prompt_with_message_and_default(message, default)
34+
.expect_throw("can't read input"),
35+
None => window()
36+
.prompt_with_message(message)
37+
.expect_throw("can't read input"),
38+
}
39+
}
40+
41+
#[inline]
42+
fn window() -> web_sys::Window {
43+
web_sys::window().expect_throw("can't access window")
44+
}

crates/file/README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@
2020
<sub>Built with 🦀🕸 by <a href="https://rustwasm.github.io/">The Rust and WebAssembly Working Group</a></sub>
2121
</div>
2222

23+
Provides wrappers for working with `Blob`s and `File`s from JavaScript.
2324

24-
Working with files and blobs on the Web.
25+
A `File` is just a `Blob` with some extra data: a name and a last modified time.
2526

26-
These APIs come in two flavors:
27+
In the File API, `Blob`s are opaque objects that lazily fetch their contained data when
28+
asked. This allows a `Blob` to represent some resource that isn't completely available, for
29+
example a WebSocket message that is being received or a file that needs to be read from disk.
2730

28-
1. a callback style (that more directly mimics the JavaScript APIs), and
29-
2. a `Future` API.
31+
You can asynchronously access the contents of the `Blob` through callbacks,
32+
but that is rather inconvenient, so this crate provides some functions which
33+
return a `Future` instead.

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ pub use gloo_console_timer as console_timer;
88
pub use gloo_events as events;
99
pub use gloo_file as file;
1010
pub use gloo_timers as timers;
11+
pub use gloo_dialogs as dialogs;

0 commit comments

Comments
 (0)