Skip to content

Commit 82c55e8

Browse files
authored
feat: Minor changes and added use_preferred_color_scheme example (#2)
* Updated dioxus, clean up code and added use_preferred_color_scheme example * add readmes
1 parent 3a228c9 commit 82c55e8

File tree

9 files changed

+117
-14
lines changed

9 files changed

+117
-14
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
/target
1+
target
2+
dist
23
/Cargo.lock
34
/.cargo

Cargo.toml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,25 @@ repository = "https://github.com/DioxusLabs/dioxus-std/"
99
homepage = "https://dioxuslabs.com"
1010
keywords = ["dom", "gui", "dioxus", "standard", "hooks"]
1111

12+
[workspace]
13+
members = ["./examples/color_scheme"]
14+
1215
[features]
16+
# Features
1317
clipboard = ["dep:clipboard"]
1418
notifications = ["dep:notify-rust"]
1519
camera = ["dep:nokhwa"]
1620
hooks-use_preferred_color_scheme = ["dep:dioxus", "dep:web-sys", "dep:wasm-bindgen"]
1721

22+
# Platform-specific features
23+
web = ["hooks-use_preferred_color_scheme"]
24+
desktop = ["camera", "notifications", "clipboard"]
25+
26+
# Default features
27+
default = ["web"]
28+
1829
[dependencies]
19-
dioxus = { version = "0.2", optional = true }
30+
dioxus = { version = "0.3", optional = true }
2031
web-sys = { version = "0.3.60", features = ["Window", "MediaQueryList"], optional = true }
2132
wasm-bindgen = { version = "0.2.83", optional = true }
2233

examples/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Examples
2+
3+
### [`color_scheme`](./color_scheme/)
4+
Learn how to use `use_preferred_color_scheme`.

examples/color_scheme/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "color_scheme"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
dioxus-std = { path="../../" }
8+
dioxus = "0.3"
9+
dioxus-web = "0.3"
10+
11+
log = "0.4.6"
12+
13+
# WebAssembly Debug
14+
wasm-logger = "0.2.0"
15+
console_error_panic_hook = "0.1.7"

examples/color_scheme/Dioxus.toml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[application]
2+
3+
# App (Project) Name
4+
name = "color_scheme"
5+
6+
# Dioxus App Default Platform
7+
# desktop, web, mobile, ssr
8+
default_platform = "web"
9+
10+
# `build` & `serve` dist path
11+
out_dir = "dist"
12+
13+
# resource (public) file folder
14+
asset_dir = "public"
15+
16+
[web.app]
17+
18+
# HTML title tag content
19+
title = "dioxus | ⛺"
20+
21+
[web.watcher]
22+
23+
# when watcher trigger, regenerate the `index.html`
24+
reload_html = true
25+
26+
# which files or dirs will be watcher monitoring
27+
watch_path = ["src", "public"]
28+
29+
# include `assets` in web platform
30+
[web.resource]
31+
32+
# CSS style file
33+
style = []
34+
35+
# Javascript code file
36+
script = []
37+
38+
[web.resource.dev]
39+
40+
# Javascript code file
41+
# serve: [dev-server] only
42+
script = []

examples/color_scheme/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# color_scheme
2+
3+
Learn how to use `use_preferred_color_scheme`.
4+
5+
Run:
6+
7+
```dioxus serve```
130 KB
Binary file not shown.

examples/color_scheme/src/main.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use dioxus::prelude::*;
2+
use dioxus_std::hooks::use_preferred_color_scheme;
3+
4+
fn main() {
5+
// init debug tool for WebAssembly
6+
wasm_logger::init(wasm_logger::Config::default());
7+
console_error_panic_hook::set_once();
8+
9+
dioxus_web::launch(app);
10+
}
11+
12+
fn app(cx: Scope) -> Element {
13+
let color_scheme = use_preferred_color_scheme(cx);
14+
15+
render!(
16+
div {
17+
style: "text-align: center;",
18+
h1 { "🌗 Dioxus 🚀" }
19+
if let Ok(color_scheme) = color_scheme {
20+
rsx!(
21+
h3 { "You preferred color scheme is {color_scheme:?}." }
22+
)
23+
} else {
24+
rsx!(
25+
h3 { "There was an error when reading your preferred color scheme."}
26+
)
27+
}
28+
}
29+
)
30+
}

src/hooks/use_preferred_color_scheme.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,29 @@ use wasm_bindgen::{prelude::Closure, JsCast};
66
pub enum PreferredColorScheme {
77
Light,
88
Dark,
9-
Err(String),
109
}
1110

1211
static INIT: Once = Once::new();
1312

1413
/// Retrieves (as well as listens for changes) to the user's preferred color scheme (dark or light) so your application can adapt accordingly.
15-
pub fn use_preferred_color_scheme(cx: &ScopeState) -> PreferredColorScheme {
14+
pub fn use_preferred_color_scheme(cx: &ScopeState) -> Result<PreferredColorScheme, String> {
1615
// This code is kinda messy..
1716
let window = match web_sys::window() {
1817
Some(w) => w,
1918
None => {
20-
return PreferredColorScheme::Err(
21-
"not running in wasm context: window doesn't exist".to_string(),
22-
)
19+
return Err("Not running in wasm context: window doesn't exist".to_string())
2320
}
2421
};
2522

2623
let media_query_list = match window.match_media("(prefers-color-scheme: dark)") {
2724
Ok(opt) => match opt {
2825
Some(m) => m,
2926
None => {
30-
return PreferredColorScheme::Err(
31-
"failed to determine preferred scheme".to_string(),
32-
)
27+
return Err("Failed to determine preferred scheme".to_string())
3328
}
3429
},
3530
Err(e) => {
36-
return PreferredColorScheme::Err(e.as_string().unwrap_or(
37-
"failed to determine preferred scheme and couldn't retrieve error".to_string(),
38-
))
31+
return Err(e.as_string().unwrap_or("Failed to determine preferred scheme and couldn't retrieve error".to_string()))
3932
}
4033
};
4134

@@ -57,7 +50,7 @@ pub fn use_preferred_color_scheme(cx: &ScopeState) -> PreferredColorScheme {
5750
media_query_list.set_onchange(Some(cb.unchecked_ref()));
5851
});
5952

60-
determine_scheme(media_query_list.matches())
53+
Ok(determine_scheme(media_query_list.matches()))
6154
}
6255

6356
fn determine_scheme(value: bool) -> PreferredColorScheme {

0 commit comments

Comments
 (0)