dioxus/examples/link.rs
Evan Almloff cab573eefd
Synchronous prevent default (#2792)
* add prevent default methods to the event

* sync prevent default almost working

* sync prevent default working

* Move event handling into the runtime

* update core tests

* restore desktop file dialog

* implement prevent default on web

* add a hint about the new prevent default method

* fix web prevent default

* Fix CTRL+click on links

* fix values memorize in place test

* Fix a few more tests

* Add a playwright test for sync prevent default

* Fix core doc tests

* create a deprecated VirtualDom::handle_event

* fix macos imports in desktop

* Fix onmounted event

* Fix liveview support

* switch to RefCell for metadata

* Remove println

* remove prevent default attribute

* remove web specific link behavior

* Fix liveview links

* more liveview fixes for link

* Fix merge conflicts

* Fix clippy

* use the new prevent default in the file upload example
2024-08-13 11:57:54 -07:00

81 lines
2.1 KiB
Rust

//! How to use links in Dioxus
//!
//! The `router` crate gives us a `Link` component which is a much more powerful version of the standard HTML link.
//! However, you can use the traditional `<a>` tag if you want to build your own `Link` component.
//!
//! The `Link` component integrates with the Router and is smart enough to detect if the link is internal or external.
//! It also allows taking any `Route` as a target, making your links typesafe
use dioxus::prelude::*;
const STYLE: &str = asset!("./examples/assets/links.css");
fn main() {
launch(app);
}
fn app() -> Element {
rsx! (
head::Link { rel: "stylesheet", href: STYLE }
Router::<Route> {}
)
}
#[derive(Routable, Clone)]
#[rustfmt::skip]
enum Route {
#[layout(Header)]
#[route("/")]
Home {},
#[route("/default-links")]
DefaultLinks {},
#[route("/settings")]
Settings {},
}
#[component]
fn Header() -> Element {
rsx! {
h1 { "Your app here" }
nav { id: "nav",
Link { to: Route::Home {}, "home" }
Link { to: Route::DefaultLinks {}, "default links" }
Link { to: Route::Settings {}, "settings" }
}
Outlet::<Route> {}
}
}
#[component]
fn Home() -> Element {
rsx!( h1 { "Home" } )
}
#[component]
fn Settings() -> Element {
rsx!( h1 { "Settings" } )
}
#[component]
fn DefaultLinks() -> Element {
rsx! {
// Just some default links
div { id: "external-links",
// This link will open in a webbrowser
a { href: "http://dioxuslabs.com/", "Default link - links outside of your app" }
// This link will do nothing - we're preventing the default behavior
// It will just log "Hello Dioxus" to the console
a {
href: "http://dioxuslabs.com/",
onclick: |event| {
event.prevent_default();
println!("Hello Dioxus")
},
"Custom event link - links inside of your app"
}
}
}
}