lib.rs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //! Dioxus Server-Side-Rendering
  2. //!
  3. //! This crate demonstrates how to implement a custom renderer for Dioxus VNodes via the `TextRenderer` renderer.
  4. //! The `TextRenderer` consumes a Dioxus Virtual DOM, progresses its event queue, and renders the VNodes to a String.
  5. //!
  6. //! While `VNode` supports "to_string" directly, it renders child components as the RSX! macro tokens. For custom components,
  7. //! an external renderer is needed to progress the component lifecycles. The `TextRenderer` shows how to use the Virtual DOM
  8. //! API to progress these lifecycle events to generate a fully-mounted Virtual DOM instance which can be renderer in the
  9. //! `render` method.
  10. //!
  11. //! ```ignore
  12. //! fn main() {
  13. //! let renderer = TextRenderer::<()>::new(|_| html! {<div> "Hello world" </div>});
  14. //! let output = renderer.render();
  15. //! assert_eq!(output, "<div>Hello World</div>");
  16. //! }
  17. //! ```
  18. //!
  19. //! The `TextRenderer` is particularly useful when needing to cache a Virtual DOM in between requests
  20. //!
  21. use dioxus_core::prelude::{VNode, FC};
  22. pub mod tostring;
  23. pub mod prelude {
  24. pub use dioxus_core::prelude::*;
  25. }
  26. /// The `TextRenderer` provides a way of rendering a Dioxus Virtual DOM to a String.
  27. ///
  28. ///
  29. ///
  30. pub struct TextRenderer<T> {
  31. _root_type: std::marker::PhantomData<T>,
  32. }
  33. impl<T> TextRenderer<T> {
  34. /// Create a new text-renderer instance from a functional component root.
  35. /// Automatically progresses the creation of the VNode tree to completion.
  36. ///
  37. /// A VDom is automatically created. If you want more granular control of the VDom, use `from_vdom`
  38. pub fn new(root: FC<T>) -> Self {
  39. Self {
  40. _root_type: std::marker::PhantomData {},
  41. }
  42. }
  43. /// Create a new text renderer from an existing Virtual DOM.
  44. /// This will progress the existing VDom's events to completion.
  45. pub fn from_vdom() -> Self {
  46. todo!()
  47. }
  48. /// Pass new args to the root function
  49. pub fn update(&mut self, new_val: T) {
  50. todo!()
  51. }
  52. /// Modify the root function in place, forcing a re-render regardless if the props changed
  53. pub fn update_mut(&mut self, modifier: impl Fn(&mut T)) {
  54. todo!()
  55. }
  56. /// Immediately render a VNode to string
  57. pub fn to_text(root: VNode) -> String {
  58. todo!()
  59. }
  60. /// Render the virtual DOM to a string
  61. pub fn render(&self) -> String {
  62. let mut buffer = String::new();
  63. // iterate through the internal patch queue of virtual dom, and apply them to the buffer
  64. /*
  65. */
  66. todo!()
  67. }
  68. /// Render VDom to an existing buffer
  69. /// TODO @Jon, support non-string buffers to actually make this useful
  70. /// Currently, this only supports overwriting an existing buffer, instead of just
  71. pub fn render_mut(&self, buf: &mut String) {
  72. todo!()
  73. }
  74. }