dioxus_document/elements/
meta.rs

1use super::*;
2use crate::document;
3use dioxus_core::{use_hook, VNode};
4use dioxus_html as dioxus_elements;
5
6#[non_exhaustive]
7/// Props for the [`Meta`] component
8#[derive(Clone, Props, PartialEq)]
9pub struct MetaProps {
10    pub property: Option<String>,
11    pub name: Option<String>,
12    pub charset: Option<String>,
13    pub http_equiv: Option<String>,
14    pub content: Option<String>,
15    pub data: Option<String>,
16    #[props(extends = meta, extends = GlobalAttributes)]
17    pub additional_attributes: Vec<Attribute>,
18}
19
20impl MetaProps {
21    /// Get all the attributes for the meta tag
22    pub fn attributes(&self) -> Vec<(&'static str, String)> {
23        let mut attributes = Vec::new();
24        extend_attributes(&mut attributes, &self.additional_attributes);
25        if let Some(property) = &self.property {
26            attributes.push(("property", property.clone()));
27        }
28        if let Some(name) = &self.name {
29            attributes.push(("name", name.clone()));
30        }
31        if let Some(charset) = &self.charset {
32            attributes.push(("charset", charset.clone()));
33        }
34        if let Some(http_equiv) = &self.http_equiv {
35            attributes.push(("http-equiv", http_equiv.clone()));
36        }
37        if let Some(content) = &self.content {
38            attributes.push(("content", content.clone()));
39        }
40        if let Some(data) = &self.data {
41            attributes.push(("data", data.clone()));
42        }
43        attributes
44    }
45}
46
47/// Render a [`<meta>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/meta) tag into the head of the page.
48///
49/// # Example
50///
51/// ```rust, no_run
52/// # use dioxus::prelude::*;
53/// fn RedirectToDioxusHomepageWithoutJS() -> Element {
54///     rsx! {
55///         // You can use the meta component to render a meta tag into the head of the page
56///         // This meta tag will redirect the user to the dioxuslabs homepage in 10 seconds
57///         document::Meta {
58///             http_equiv: "refresh",
59///             content: "10;url=https://dioxuslabs.com",
60///         }
61///     }
62/// }
63/// ```
64///
65/// <div class="warning">
66///
67/// Any updates to the props after the first render will not be reflected in the head.
68///
69/// </div>
70#[component]
71#[doc(alias = "<meta>")]
72pub fn Meta(props: MetaProps) -> Element {
73    use_update_warning(&props, "Meta {}");
74
75    use_hook(|| {
76        let document = document();
77        let insert_link = document.create_head_component();
78
79        if !insert_link {
80            return;
81        }
82
83        document.create_meta(props);
84    });
85
86    VNode::empty()
87}