lib.rs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 patch; // An "edit phase" described by transitions and edit operations // Test harness for validating that lifecycles and diffs work appropriately
  71. // pub mod diff;
  72. // pub mod patch; // The diffing algorithm that builds the ChangeList
  73. pub mod diff;
  74. // the diffing algorithm that builds the ChangeList
  75. pub mod error; // Error type we expose to the renderers
  76. pub mod events; // Manages the synthetic event API
  77. pub mod hooks; // Built-in hooks
  78. pub mod nodebuilder; // Logic for building VNodes with a direct syntax
  79. pub mod nodes; // Logic for the VNodes
  80. pub mod scope; // Logic for single components
  81. // pub mod validation; // Logic for validating trees
  82. pub mod virtual_dom; // Most fun logic starts here, manages the lifecycle and suspense
  83. pub mod builder {
  84. pub use super::nodebuilder::*;
  85. }
  86. // types used internally that are important
  87. pub(crate) mod innerlude {
  88. // pub(crate) use crate::component::Properties;
  89. pub(crate) use crate::component::Properties;
  90. pub(crate) use crate::context::Context;
  91. pub use crate::diff::LifeCycleEvent;
  92. pub(crate) use crate::error::Result;
  93. pub use crate::events::{EventTrigger, VirtualEvent};
  94. use crate::nodes;
  95. pub use crate::component::ScopeIdx;
  96. pub use crate::context::hooks::Hook;
  97. pub use crate::nodes::VNode;
  98. pub(crate) use nodes::*;
  99. pub use crate::diff::DiffMachine;
  100. pub use crate::patch::{EditList, EditMachine};
  101. // pub use crate::patchdx;
  102. // pub use crate::patchtList;
  103. // pub use nodes::iterables::IterableNodes;
  104. /// This type alias is an internal way of abstracting over the static functions that represent components.
  105. pub type FC<P> = for<'scope> fn(Context<'scope>, &'scope P) -> DomTree;
  106. mod fc2 {}
  107. // pub type FC<'a, P: 'a> = for<'scope> fn(Context<'scope>, &'scope P) -> DomTree;
  108. // pub type FC<P> = for<'scope, 'r> fn(Context<'scope>, &'scope P) -> DomTree;
  109. // pub type FC<P> = for<'scope, 'r> fn(Context<'scope>, &'r P) -> VNode<'scope>;
  110. // pub type FC<P> = for<'scope, 'r> fn(Context<'scope>, &'r P) -> VNode<'scope>;
  111. // pub type FC<P> = for<'a> fn(Context<'a, P>) -> VNode<'a>;
  112. // TODO @Jon, fix this
  113. // hack the VNode type until VirtualNode is fixed in the macro crate
  114. pub type VirtualNode<'a> = VNode<'a>;
  115. // Re-export the FC macro
  116. pub use crate as dioxus;
  117. pub use crate::nodebuilder as builder;
  118. pub use dioxus_core_macro::{html, rsx};
  119. // pub use dioxus_core_macro::{fc, html, rsx};
  120. }
  121. /// Re-export common types for ease of development use.
  122. /// Essential when working with the html! macro
  123. pub mod prelude {
  124. pub use crate::component::{fc_to_builder, Properties};
  125. pub use crate::context::Context;
  126. use crate::nodes;
  127. pub use nodes::*;
  128. // pub use nodes::iterables::IterableNodes;
  129. /// This type alias is an internal way of abstracting over the static functions that represent components.
  130. pub use crate::innerlude::FC;
  131. // TODO @Jon, fix this
  132. // hack the VNode type until VirtualNode is fixed in the macro crate
  133. // expose our bumpalo type
  134. pub use bumpalo;
  135. pub use bumpalo::Bump;
  136. // Re-export the FC macro
  137. pub use crate as dioxus;
  138. pub use crate::nodebuilder as builder;
  139. // pub use dioxus_core_macro::fc;
  140. pub use dioxus_core_macro::{format_args_f, html, rsx, Props};
  141. pub use crate::component::ScopeIdx;
  142. pub use crate::diff::DiffMachine;
  143. pub use crate::debug_renderer::DebugRenderer;
  144. pub use crate::hooks::*;
  145. }