lib.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. pub type Component<P = ()> = fn(Scope<P>) -> Element;
  56. /// A list of attributes
  57. pub type Attributes<'a> = Option<&'a [Attribute<'a>]>;
  58. }
  59. pub use crate::innerlude::{
  60. AnyEvent, Attribute, AttributeValue, Component, DioxusElement, DomEdit, Element, ElementId,
  61. ElementIdIterator, EventHandler, EventPriority, IntoVNode, LazyNodes, Listener, Mutations,
  62. NodeFactory, Properties, SchedulerMsg, Scope, ScopeId, ScopeState, TaskId, UiEvent, UserEvent,
  63. VComponent, VElement, VFragment, VNode, VPlaceholder, VText, VirtualDom,
  64. };
  65. /// The purpose of this module is to alleviate imports of many common types
  66. ///
  67. /// This includes types like [`Scope`], [`Element`], and [`Component`].
  68. pub mod prelude {
  69. pub use crate::innerlude::{
  70. fc_to_builder, Attributes, Component, DioxusElement, Element, EventHandler, Fragment,
  71. LazyNodes, NodeFactory, Properties, Scope, ScopeId, ScopeState, VNode, VirtualDom,
  72. };
  73. }
  74. pub mod exports {
  75. //! Important dependencies that are used by the rest of the library
  76. //! Feel free to just add the dependencies in your own Crates.toml
  77. pub use bumpalo;
  78. pub use futures_channel;
  79. }
  80. /// Functions that wrap unsafe functionality to prevent us from misusing it at the callsite
  81. pub(crate) mod unsafe_utils {
  82. use crate::VNode;
  83. pub(crate) unsafe fn extend_vnode<'a, 'b>(node: &'a VNode<'a>) -> &'b VNode<'b> {
  84. std::mem::transmute(node)
  85. }
  86. }
  87. #[macro_export]
  88. /// A helper macro for using hooks in async environements.
  89. ///
  90. /// # Usage
  91. ///
  92. ///
  93. /// ```
  94. /// let (data) = use_ref(&cx, || {});
  95. ///
  96. /// let handle_thing = move |_| {
  97. /// to_owned![data]
  98. /// cx.spawn(async move {
  99. /// // do stuff
  100. /// });
  101. /// }
  102. /// ```
  103. macro_rules! to_owned {
  104. ($($es:ident),+) => {$(
  105. #[allow(unused_mut)]
  106. let mut $es = $es.to_owned();
  107. )*}
  108. }