Skip to content

Commit

Permalink
Merge pull request #1939 from DioxusLabs/jk/examples-overhaul
Browse files Browse the repository at this point in the history
Add more examples, document examples, add css to most examples
  • Loading branch information
jkelleyrtp authored Feb 14, 2024
2 parents 739ffae + 72bef22 commit 77c80ea
Show file tree
Hide file tree
Showing 76 changed files with 1,452 additions and 954 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ publish = false
manganis = { workspace = true, optional = true}
reqwest = { version = "0.11.9", features = ["json"], optional = true}
http-range = {version = "0.1.5", optional = true }
warp = { version = "0.3.0", optional = true }

[dev-dependencies]
dioxus = { workspace = true, features = ["router"] }
Expand Down Expand Up @@ -154,7 +155,7 @@ server = ["dioxus/axum"]
default = ["dioxus/desktop"]
web = ["dioxus/web"]
collect-assets = ["manganis"]
http = ["reqwest", "http-range"]
http = ["reqwest", "http-range", "warp"]

[[example]]
name = "login_form"
Expand Down
44 changes: 16 additions & 28 deletions examples/all_events.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,33 @@
//! This example shows how to listen to all events on a div and log them to the console.
//!
//! The primary demonstration here is the properties on the events themselves, hoping to give you some inspiration
//! on adding interactivity to your own application.
use dioxus::prelude::*;
use std::{collections::VecDeque, fmt::Debug, rc::Rc};

fn main() {
launch(app);
}

const MAX_EVENTS: usize = 8;

const CONTAINER_STYLE: &str = r#"
display: flex;
flex-direction: column;
align-items: center;
"#;

const RECT_STYLE: &str = r#"
background: deepskyblue;
height: 50vh;
width: 50vw;
color: white;
padding: 20px;
margin: 20px;
text-aligh: center;
"#;

fn app() -> Element {
let mut events = use_signal(|| VecDeque::new() as VecDeque<Rc<dyn Debug>>);
// Using a VecDeque so its cheap to pop old events off the front
let mut events = use_signal(VecDeque::new);

// All events and their data implement Debug, so we can re-cast them as Rc<dyn Debug> instead of their specific type
let mut log_event = move |event: Rc<dyn Debug>| {
let mut events = events.write();

if events.len() >= MAX_EVENTS {
events.pop_front();
// Only store the last 20 events
if events.read().len() >= 20 {
events.write().pop_front();
}

events.push_back(event);
events.write().push_back(event);
};

rsx! {
div { style: "{CONTAINER_STYLE}",
style { {include_str!("./assets/events.css")} }
div { id: "container",
// focusing is necessary to catch keyboard events
div { style: "{RECT_STYLE}", tabindex: 0,
div { id: "receiver", tabindex: 0,
onmousemove: move |event| log_event(event.data()),
onclick: move |event| log_event(event.data()),
ondoubleclick: move |event| log_event(event.data()),
Expand All @@ -57,7 +45,7 @@ fn app() -> Element {

"Hover, click, type or scroll to see the info down below"
}
div {
div { id: "log",
for event in events.read().iter() {
div { "{event:?}" }
}
Expand Down
23 changes: 23 additions & 0 deletions examples/assets/clock.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
html body {
margin: 0;
padding: 0;
height: 100vh;
font-family: 'Courier New', Courier, monospace;
}

#app {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
height: 100vh;
background-color: plum;
font-size: 6em;
color: aliceblue;
}

#title {
font-size: 0.5em;
color: black;
margin-bottom: 0.5em;
}
26 changes: 26 additions & 0 deletions examples/assets/counter.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
html body {
font-family: Arial, sans-serif;
margin: 20px;
padding: 0;
display: flex;
justify-content: center;
font-size: 2rem;
height: 100vh;
background-color: #f0f0f0;
}

#controls {
display: flex;
justify-content: center;
align-items: center;
margin-top: 20px;
}

button {
padding: 5px 10px;
margin: 0 5px;
}

input {
width: 50px;
}
16 changes: 16 additions & 0 deletions examples/assets/crm.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
body {
background-color: #f4f4f4;
font-family: 'Open Sans', sans-serif;
font-size: 14px;
line-height: 1.42857143;
color: #333;
margin: 20px;
padding: 20px;
display: flex;
flex-direction: column;
align-items: center;
}

.red {
background-color: rgb(202, 60, 60) !important;
}
12 changes: 12 additions & 0 deletions examples/assets/custom_assets.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
body {
background-color: #f0f0f0;
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
height: 100vh;
width: 100vw;
}
Empty file added examples/assets/dog_app.css
Empty file.
24 changes: 24 additions & 0 deletions examples/assets/events.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#container {
display: flex;
flex-direction: column;
align-items: center;
}

#receiver {
background: deepskyblue;
height: 30vh;
width: 80vw;
color: white;
padding: 20px;
margin: 20px;
text-align: center;
}

#log {
background: lightgray;
padding: 20px;
margin: 20px;
overflow-y: scroll;
align-items: start;
text-align: left;
}
22 changes: 22 additions & 0 deletions examples/assets/file_upload.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
flex-direction: column;
gap: 20px;
}

#drop-zone {
border: 2px dashed #ccc;
border-radius: 3px;
padding: 20px;
text-align: center;
cursor: pointer;
margin: 20px;
background-color: rgba(225, 124, 225, 0);
}
40 changes: 40 additions & 0 deletions examples/assets/flat_router.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
body {
font-family: Arial, sans-serif;
margin: 20px;
padding: 20px;
background-color: #f4f4f4;
height: 100vh;
}

nav {
display: flex;
justify-content: space-around;
}

.nav-btn {
text-decoration: none;
color: black;
}

a {
padding: 10px;
border: none;
border-radius: 5px;
cursor: pointer;
}

/* button hover effect */
a:hover {
background-color: #dd6a6a;
}

#content {
border: 2px dashed #ccc;
padding-top: 20px;
display: flex;
justify-content: center;
align-items: center;
height: 100%;
flex-direction: column;
gap: 20px;
}
12 changes: 12 additions & 0 deletions examples/assets/links.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#external-links {
display: flex;
flex-direction: column;
}

#nav {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem;
background-color: #f4f4f4;
}
43 changes: 43 additions & 0 deletions examples/assets/radio.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
}

button {
margin: 10px;
padding: 10px;
border: none;
border-radius: 5px;
background-color: #f0f0f0;
cursor: pointer;
}

#pause {
background-color: #ff0000;
}

#play {
background-color: #00ff00;
}


.bounce {
animation: boomBox 0.5s infinite;
}

@keyframes boomBox {
0% {
transform: scale(1.0);
}

50% {
transform: scale(2);
}

100% {
transform: scale(1.0);
}
}
29 changes: 29 additions & 0 deletions examples/assets/roulette.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#main {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}

#roulette-grid {
margin: 10px;
display: grid;
grid-template-columns: repeat(9, 1fr);
grid-gap: 10px;
margin: 0 auto;
padding: 20px;
}

#roulette-grid>input {
color: white;
font-size: 20px;
width: 50px;
}

#roulette-grid>input:nth-child(odd) {
background-color: red;
}

#roulette-grid>input:nth-child(even) {
background-color: black;
}
13 changes: 13 additions & 0 deletions examples/assets/router.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#navbar {
display: flex;
justify-content: flex-start;
gap: 1rem;
align-items: center;
}

#blog-list {
display: flex;
flex-direction: column;
align-items: start;
gap: 1rem;
}
Loading

0 comments on commit 77c80ea

Please sign in to comment.