lib.rs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 hook-based, renderer-agnostic framework for cross-platform UI 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: Context, props: &Props) -> VNode {
  20. //! html! { <div> "Hello {ctx.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| {
  35. //! html! { <div> "Hello {ctx.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. //! Many people don't like the magic of proc macros, so this is entirely optional. Under-the-hood, we simply transplant the
  41. //! function arguments into a struct, so there's very little actual magic happening.
  42. //!
  43. //! ```
  44. //! use dioxus_core::prelude::*;
  45. //!
  46. //! #[derive_props]
  47. //! static Example: FC = |ctx, name: &String| {
  48. //! html! { <div> "Hello {name}!" </div> }
  49. //! }
  50. //! ```
  51. //!
  52. //! ## Hooks
  53. //! Dioxus uses hooks for state management. Hooks are a form of state persisted between calls of the function component. Instead of
  54. //! using a single struct to store data, hooks use the "use_hook" building block which allows the persistence of data between
  55. //! function component renders. Each hook stores some data in a "memory cell" and needs to be called in a consistent order.
  56. //! This means hooks "anything with `use_x`" may not be called conditionally.
  57. //!
  58. //! This allows functions to reuse stateful logic between components, simplify large complex components, and adopt more clear context
  59. //! subscription patterns to make components easier to read.
  60. //!
  61. //! ## Supported Renderers
  62. //! Instead of being tightly coupled to a platform, browser, or toolkit, Dioxus implements a VirtualDOM object which
  63. //! can be consumed to draw the UI. The Dioxus VDOM is reactive and easily consumable by 3rd-party renderers via
  64. //! the `Patch` object. See [Implementing a Renderer](docs/8-custom-renderer.md) and the `StringRenderer` classes for information
  65. //! on how to implement your own custom renderer. We provide 1st-class support for these renderers:
  66. //! - dioxus-desktop (via WebView)
  67. //! - dioxus-web (via WebSys)
  68. //! - dioxus-ssr (via StringRenderer)
  69. //! - dioxus-liveview (SSR + StringRenderer)
  70. //!
  71. pub mod arena;
  72. pub mod component; // Logic for extending FC
  73. pub mod debug_renderer;
  74. pub mod diff;
  75. pub mod patch; // An "edit phase" described by transitions and edit operations // Test harness for validating that lifecycles and diffs work appropriately
  76. // the diffing algorithm that builds the ChangeList
  77. pub mod error; // Error type we expose to the renderers
  78. pub mod events; // Manages the synthetic event API
  79. pub mod hooks; // Built-in hooks
  80. pub mod nodebuilder; // Logic for building VNodes with a direct syntax
  81. pub mod nodes; // Logic for the VNodes
  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 use crate::component::*;
  89. pub use crate::debug_renderer::*;
  90. pub use crate::diff::*;
  91. pub use crate::error::*;
  92. pub use crate::events::*;
  93. pub use crate::hooks::*;
  94. pub use crate::nodebuilder::*;
  95. pub use crate::nodes::*;
  96. pub use crate::patch::*;
  97. pub use crate::virtual_dom::*;
  98. pub type FC<P> = fn(Context<P>) -> VNode;
  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 crate::virtual_dom::Scoped;
  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. }