lib.rs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //! <div align="center">
  2. //! <h1>🌗🚀 📦 Dioxus</h1>
  3. //! <p>
  4. //! <strong>A concurrent, functional, virtual DOM for Rust</strong>
  5. //! </p>
  6. //! </div>
  7. //! Dioxus: a concurrent, functional, reactive virtual dom for any renderer in Rust.
  8. //!
  9. //! This crate aims to maintain a uniform hook-based, renderer-agnostic UI framework for cross-platform development.
  10. //!
  11. //! ## Components
  12. //! The base unit of Dioxus is the `component`. Components can be easily created from just a function - no traits required:
  13. //! ```
  14. //! use dioxus_core::prelude::*;
  15. //!
  16. //! #[derive(Properties)]
  17. //! struct Props { name: String }
  18. //!
  19. //! fn Example(ctx: &mut Context<Props>) -> VNode {
  20. //! html! { <div> "Hello {ctx.props.name}!" </div> }
  21. //! }
  22. //! ```
  23. //! Components need to take a "Context" parameter which is generic over some properties. This defines how the component can be used
  24. //! and what properties can be used to specify it in the VNode output. Component state in Dioxus is managed by hooks - if you're new
  25. //! to hooks, check out the hook guide in the official guide.
  26. //!
  27. //! Components can also be crafted as static closures, enabling type inference without all the type signature noise:
  28. //! ```
  29. //! use dioxus_core::prelude::*;
  30. //!
  31. //! #[derive(Properties)]
  32. //! struct Props { name: String }
  33. //!
  34. //! static Example: FC<Props> = |ctx, props| {
  35. //! html! { <div> "Hello {props.name}!" </div> }
  36. //! }
  37. //! ```
  38. //!
  39. //! If the properties struct is too noisy for you, we also provide a macro that converts variadic functions into components automatically.
  40. //! ```
  41. //! use dioxus_core::prelude::*;
  42. //!
  43. //! #[fc]
  44. //! static Example: FC = |ctx, name: String| {
  45. //! html! { <div> "Hello {name}!" </div> }
  46. //! }
  47. //! ```
  48. //!
  49. //! ## Hooks
  50. //! Dioxus uses hooks for state management. Hooks are a form of state persisted between calls of the function component. Instead of
  51. //! using a single struct to store data, hooks use the "use_hook" building block which allows the persistence of data between
  52. //! function component renders.
  53. //!
  54. //! This allows functions to reuse stateful logic between components, simplify large complex components, and adopt more clear context
  55. //! subscription patterns to make components easier to read.
  56. //!
  57. //! ## Supported Renderers
  58. //! Instead of being tightly coupled to a platform, browser, or toolkit, Dioxus implements a VirtualDOM object which
  59. //! can be consumed to draw the UI. The Dioxus VDOM is reactive and easily consumable by 3rd-party renderers via
  60. //! the `Patch` object. See [Implementing a Renderer](docs/8-custom-renderer.md) and the `StringRenderer` classes for information
  61. //! on how to implement your own custom renderer. We provide 1st-class support for these renderers:
  62. //! - dioxus-desktop (via WebView)
  63. //! - dioxus-web (via WebSys)
  64. //! - dioxus-ssr (via StringRenderer)
  65. //! - dioxus-liveview (SSR + StringRenderer)
  66. //!
  67. pub mod component; // Logic for extending FC
  68. pub mod context; // Logic for providing hook + context functionality to user components
  69. pub mod debug_renderer;
  70. pub mod diff;
  71. pub mod patch; // An "edit phase" described by transitions and edit operations // Test harness for validating that lifecycles and diffs work appropriately
  72. // the diffing algorithm that builds the ChangeList
  73. pub mod error; // Error type we expose to the renderers
  74. pub mod events; // Manages the synthetic event API
  75. pub mod hooks; // Built-in hooks
  76. pub mod nodebuilder; // Logic for building VNodes with a direct syntax
  77. pub mod nodes; // Logic for the VNodes
  78. pub mod virtual_dom; // Most fun logic starts here, manages the lifecycle and suspense
  79. pub mod builder {
  80. pub use super::nodebuilder::*;
  81. }
  82. // types used internally that are important
  83. pub(crate) mod innerlude {
  84. pub use crate::component::*;
  85. pub use crate::context::*;
  86. pub use crate::debug_renderer::*;
  87. pub use crate::diff::*;
  88. pub use crate::error::*;
  89. pub use crate::events::*;
  90. pub use crate::hooks::*;
  91. pub use crate::nodebuilder::*;
  92. pub use crate::nodes::*;
  93. pub use crate::patch::*;
  94. pub use crate::virtual_dom::*;
  95. pub type FC<P> = for<'scope> fn(Context<'scope>, &'scope P) -> DomTree;
  96. // TODO @Jon, fix this
  97. // hack the VNode type until VirtualNode is fixed in the macro crate
  98. pub type VirtualNode<'a> = VNode<'a>;
  99. // Re-export the FC macro
  100. pub use crate as dioxus;
  101. pub use crate::nodebuilder as builder;
  102. pub use dioxus_core_macro::{html, rsx};
  103. }
  104. /// Re-export common types for ease of development use.
  105. /// Essential when working with the html! macro
  106. pub mod prelude {
  107. pub use crate::component::{fc_to_builder, Properties};
  108. use crate::nodes;
  109. pub use crate::virtual_dom::Context;
  110. pub use nodes::*;
  111. pub use crate::nodebuilder::LazyNodes;
  112. pub use crate::virtual_dom::NodeCtx;
  113. // pub use nodes::iterables::IterableNodes;
  114. /// This type alias is an internal way of abstracting over the static functions that represent components.
  115. pub use crate::innerlude::FC;
  116. // TODO @Jon, fix this
  117. // hack the VNode type until VirtualNode is fixed in the macro crate
  118. // expose our bumpalo type
  119. pub use bumpalo;
  120. pub use bumpalo::Bump;
  121. // Re-export the FC macro
  122. pub use crate as dioxus;
  123. pub use crate::nodebuilder as builder;
  124. // pub use dioxus_core_macro::fc;
  125. pub use dioxus_core_macro::{format_args_f, html, rsx, Props};
  126. pub use crate::component::ScopeIdx;
  127. pub use crate::diff::DiffMachine;
  128. pub use crate::debug_renderer::DebugRenderer;
  129. pub use crate::hooks::*;
  130. }