Skip to main content

dioxus_html/
lib.rs

1#![doc = include_str!("../README.md")]
2#![doc(html_logo_url = "https://avatars.githubusercontent.com/u/79236386")]
3#![doc(html_favicon_url = "https://avatars.githubusercontent.com/u/79236386")]
4
5//! # Dioxus Namespace for HTML
6//!
7//! This crate provides a set of compile-time correct HTML elements that can be used with the Rsx and Html macros.
8//! This system allows users to easily build new tags, new types, and customize the output of the Rsx and Html macros.
9//!
10//! An added benefit of this approach is the ability to lend comprehensive documentation on how to use these elements inside
11//! of the Rsx and Html macros. Each element comes with a substantial amount of documentation on how to best use it, hopefully
12//! making the development cycle quick.
13//!
14//! All elements are used as zero-sized unit structs with trait impls.
15//!
16//! Currently, we don't validate for structures, but do validate attributes.
17
18/// The shared root every element associated-const hangs off of. Both the built-in
19/// `define_elements!` invocation and user `define_elements!` invocations `impl` their
20/// per-element traits for this type, so `html::div`, `html::main`, and user-defined
21/// `html::mycustom` all live under one namespace and aggregate in `html::` autocomplete.
22#[allow(non_camel_case_types)]
23pub enum html {}
24
25#[macro_use]
26pub mod elements;
27#[cfg(feature = "hot-reload-context")]
28pub use elements::HtmlCtx;
29#[cfg(feature = "html-to-rsx")]
30pub use elements::{map_html_attribute_to_rsx, map_html_element_to_rsx};
31pub mod events;
32pub(crate) mod file_data;
33pub use file_data::*;
34mod attribute_groups;
35mod data_transfer;
36pub mod geometry;
37pub mod input_data;
38pub mod point_interaction;
39pub use data_transfer::*;
40
41pub use bytes;
42
43#[doc(hidden)]
44pub use dioxus_core;
45#[doc(hidden)]
46pub use dioxus_html_internal_macro::define_elements as __define_elements;
47
48/// Define typed custom elements (and their typed attributes) for use in `rsx!`.
49///
50/// Reach for `define_elements!` when your app needs project-specific element
51/// names or attributes that should type-check just like the built-in Dioxus
52/// HTML elements. Each element becomes a typed builder, and each listed
53/// attribute becomes a typed method you can set in `rsx!`.
54///
55/// Element and attribute identifiers are written in Rust (`snake_case` or
56/// `camelCase`). Use `#[element(name = "...")]` / `#[attr(name = "...")]` to
57/// control the rendered HTML name when it differs from the Rust identifier -
58/// for example to emit a hyphenated custom-element tag or a `data-*` attribute.
59///
60/// # Example
61///
62/// ```rust, ignore
63/// use dioxus::prelude::*;
64///
65/// dioxus::html::define_elements! {
66///     // Renders as `<analytics-panel>` even though the Rust name is `analyticsPanel`.
67///     #[element(name = "analytics-panel")]
68///     analyticsPanel {
69///         metric,
70///         // Renders as the `data-region` attribute.
71///         #[attr(name = "data-region")]
72///         region,
73///     }
74/// }
75///
76/// fn app() -> Element {
77///     rsx! {
78///         analyticsPanel {
79///             metric: "conversion-rate",
80///             region: "north-america",
81///             "Revenue dashboard"
82///         }
83///     }
84/// }
85/// ```
86#[macro_export]
87macro_rules! define_elements {
88    ($($tokens:tt)*) => {
89        $crate::__dioxus_html_define_elements_with_detected_gated_attributes! {
90            $crate::__define_elements,
91            core = $crate::dioxus_core,
92            html = $crate;
93            $($tokens)*
94        }
95    };
96}
97
98#[cfg(feature = "serialize")]
99mod transit;
100
101#[cfg(feature = "serialize")]
102pub use transit::*;
103
104pub use crate::attribute_groups::{GlobalAttributesExtension, SvgAttributesExtension};
105pub use crate::point_interaction::*;
106pub use attribute_groups::*;
107pub use elements::*;
108pub use events::*;
109pub use keyboard_types::{self, Code, Key, Location, Modifiers};
110
111pub mod traits {
112    pub use crate::events::*;
113    pub use crate::point_interaction::*;
114}
115
116pub mod extensions {
117    pub use crate::attribute_groups::*;
118    pub use crate::elements::extensions::*;
119    pub use crate::events::EventsExtension;
120}