lib.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #![allow(non_snake_case)]
  2. #![doc = include_str!("../README.md")]
  3. #![deny(missing_docs)]
  4. pub(crate) mod arbitrary_value;
  5. pub(crate) mod diff;
  6. pub(crate) mod events;
  7. pub(crate) mod lazynodes;
  8. pub(crate) mod mutations;
  9. pub(crate) mod nodes;
  10. pub(crate) mod properties;
  11. pub(crate) mod scopes;
  12. pub(crate) mod util;
  13. pub(crate) mod virtual_dom;
  14. pub(crate) mod innerlude {
  15. pub use crate::arbitrary_value::*;
  16. pub use crate::events::*;
  17. pub use crate::lazynodes::*;
  18. pub use crate::mutations::*;
  19. pub use crate::nodes::*;
  20. pub use crate::properties::*;
  21. pub use crate::scopes::*;
  22. pub use crate::util::*;
  23. pub use crate::virtual_dom::*;
  24. /// An [`Element`] is a possibly-none [`VNode`] created by calling `render` on [`Scope`] or [`ScopeState`].
  25. ///
  26. /// Any [`None`] [`Element`] will automatically be coerced into a placeholder [`VNode`] with the [`VNode::Placeholder`] variant.
  27. pub type Element<'a> = Option<VNode<'a>>;
  28. /// A [`Component`] is a function that takes a [`Scope`] and returns an [`Element`].
  29. ///
  30. /// Components can be used in other components with two syntax options:
  31. /// - lowercase as a function call with named arguments (rust style)
  32. /// - uppercase as an element (react style)
  33. ///
  34. /// ## Rust-Style
  35. ///
  36. /// ```rust, ignore
  37. /// fn example(cx: Scope<Props>) -> Element {
  38. /// // ...
  39. /// }
  40. ///
  41. /// rsx!(
  42. /// example()
  43. /// )
  44. /// ```
  45. /// ## React-Style
  46. /// ```rust, ignore
  47. /// fn Example(cx: Scope<Props>) -> Element {
  48. /// // ...
  49. /// }
  50. ///
  51. /// rsx!(
  52. /// Example {}
  53. /// )
  54. /// ```
  55. ///
  56. /// ## As a closure
  57. /// This particular type alias lets you even use static closures for pure/static components:
  58. ///
  59. /// ```rust, ignore
  60. /// static Example: Component<Props> = |cx| {
  61. /// // ...
  62. /// };
  63. /// ```
  64. pub type Component<P = ()> = fn(Scope<P>) -> Element;
  65. /// A list of attributes
  66. ///
  67. pub type Attributes<'a> = Option<&'a [Attribute<'a>]>;
  68. }
  69. pub use crate::innerlude::{
  70. AnyEvent, Attribute, AttributeValue, Component, DioxusElement, DomEdit, Element, ElementId,
  71. ElementIdIterator, EventHandler, EventPriority, IntoVNode, LazyNodes, Listener, Mutations,
  72. NodeFactory, Properties, SchedulerMsg, Scope, ScopeId, ScopeState, TaskId, UiEvent, UserEvent,
  73. VComponent, VElement, VFragment, VNode, VPlaceholder, VText, VirtualDom,
  74. };
  75. /// The purpose of this module is to alleviate imports of many common types
  76. ///
  77. /// This includes types like [`Scope`], [`Element`], and [`Component`].
  78. pub mod prelude {
  79. pub use crate::innerlude::{
  80. fc_to_builder, Attributes, Component, DioxusElement, Element, EventHandler, Fragment,
  81. LazyNodes, NodeFactory, Properties, Scope, ScopeId, ScopeState, VNode, VirtualDom,
  82. };
  83. }
  84. pub mod exports {
  85. //! Important dependencies that are used by the rest of the library
  86. //! Feel free to just add the dependencies in your own Crates.toml
  87. pub use bumpalo;
  88. pub use futures_channel;
  89. }
  90. /// Functions that wrap unsafe functionality to prevent us from misusing it at the callsite
  91. pub(crate) mod unsafe_utils {
  92. use crate::VNode;
  93. pub(crate) unsafe fn extend_vnode<'a, 'b>(node: &'a VNode<'a>) -> &'b VNode<'b> {
  94. std::mem::transmute(node)
  95. }
  96. }
  97. #[macro_export]
  98. /// A helper macro for using hooks in async environements.
  99. ///
  100. /// # Usage
  101. ///
  102. ///
  103. /// ```
  104. /// let (data) = use_ref(&cx, || {});
  105. ///
  106. /// let handle_thing = move |_| {
  107. /// to_owned![data]
  108. /// cx.spawn(async move {
  109. /// // do stuff
  110. /// });
  111. /// }
  112. /// ```
  113. macro_rules! to_owned {
  114. ($($es:ident),+) => {$(
  115. #[allow(unused_mut)]
  116. let mut $es = $es.to_owned();
  117. )*}
  118. }