lib.rs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 arena;
  68. pub mod component; // Logic for extending FC
  69. pub mod context; // Logic for providing hook + context functionality to user components
  70. pub mod debug_renderer;
  71. pub mod diff;
  72. pub mod patch; // An "edit phase" described by transitions and edit operations // Test harness for validating that lifecycles and diffs work appropriately
  73. // the diffing algorithm that builds the ChangeList
  74. pub mod error; // Error type we expose to the renderers
  75. pub mod events; // Manages the synthetic event API
  76. pub mod hooks; // Built-in hooks
  77. pub mod nodebuilder; // Logic for building VNodes with a direct syntax
  78. pub mod nodes; // Logic for the VNodes
  79. pub mod virtual_dom; // Most fun logic starts here, manages the lifecycle and suspense
  80. pub mod builder {
  81. pub use super::nodebuilder::*;
  82. }
  83. // types used internally that are important
  84. pub(crate) mod innerlude {
  85. pub use crate::component::*;
  86. pub use crate::context::*;
  87. pub use crate::debug_renderer::*;
  88. pub use crate::diff::*;
  89. pub use crate::error::*;
  90. pub use crate::events::*;
  91. pub use crate::hooks::*;
  92. pub use crate::nodebuilder::*;
  93. pub use crate::nodes::*;
  94. pub use crate::patch::*;
  95. pub use crate::virtual_dom::*;
  96. pub type FC<P> = for<'scope> fn(Context<'scope>, &'scope P) -> DomTree;
  97. // TODO @Jon, fix this
  98. // hack the VNode type until VirtualNode is fixed in the macro crate
  99. pub type VirtualNode<'a> = VNode<'a>;
  100. // Re-export the FC macro
  101. pub use crate as dioxus;
  102. pub use crate::nodebuilder as builder;
  103. pub use dioxus_core_macro::{html, rsx};
  104. }
  105. /// Re-export common types for ease of development use.
  106. /// Essential when working with the html! macro
  107. pub mod prelude {
  108. pub use crate::component::{fc_to_builder, Properties};
  109. use crate::nodes;
  110. pub use crate::virtual_dom::Context;
  111. pub use nodes::*;
  112. pub use crate::nodebuilder::LazyNodes;
  113. pub use crate::virtual_dom::NodeCtx;
  114. // pub use nodes::iterables::IterableNodes;
  115. /// This type alias is an internal way of abstracting over the static functions that represent components.
  116. pub use crate::innerlude::FC;
  117. // TODO @Jon, fix this
  118. // hack the VNode type until VirtualNode is fixed in the macro crate
  119. // expose our bumpalo type
  120. pub use bumpalo;
  121. pub use bumpalo::Bump;
  122. // Re-export the FC macro
  123. pub use crate as dioxus;
  124. pub use crate::nodebuilder as builder;
  125. // pub use dioxus_core_macro::fc;
  126. pub use dioxus_core_macro::{format_args_f, html, rsx, Props};
  127. pub use crate::component::ScopeIdx;
  128. pub use crate::diff::DiffMachine;
  129. pub use crate::debug_renderer::DebugRenderer;
  130. pub use crate::dioxus_main;
  131. pub use crate::hooks::*;
  132. }
  133. #[macro_export]
  134. macro_rules! dioxus_main {
  135. ($i:ident) => {
  136. fn main() {
  137. todo!("this macro is a placeholder for launching a dioxus app on different platforms. \nYou probably don't want to use this, but it's okay for small apps.")
  138. }
  139. };
  140. }