dioxus_document/elements/
link.rs

1use super::*;
2use crate::document;
3use dioxus_html as dioxus_elements;
4
5#[non_exhaustive]
6#[derive(Clone, Props, PartialEq)]
7pub struct OtherLinkProps {
8    pub rel: String,
9    #[props(extends = link, extends = GlobalAttributes)]
10    pub additional_attributes: Vec<Attribute>,
11}
12
13#[non_exhaustive]
14#[derive(Clone, Props, PartialEq)]
15pub struct LinkProps {
16    pub rel: Option<String>,
17    pub media: Option<String>,
18    pub title: Option<String>,
19    pub disabled: Option<bool>,
20    pub r#as: Option<String>,
21    pub sizes: Option<String>,
22    /// Links are deduplicated by their href attribute
23    pub href: Option<String>,
24    pub crossorigin: Option<String>,
25    pub referrerpolicy: Option<String>,
26    pub fetchpriority: Option<String>,
27    pub hreflang: Option<String>,
28    pub integrity: Option<String>,
29    pub r#type: Option<String>,
30    pub blocking: Option<String>,
31    #[props(extends = link, extends = GlobalAttributes)]
32    pub additional_attributes: Vec<Attribute>,
33    pub onload: Option<String>,
34}
35
36impl LinkProps {
37    /// Get all the attributes for the link tag
38    pub fn attributes(&self) -> Vec<(&'static str, String)> {
39        let mut attributes = Vec::new();
40        extend_attributes(&mut attributes, &self.additional_attributes);
41        if let Some(rel) = &self.rel {
42            attributes.push(("rel", rel.clone()));
43        }
44        if let Some(media) = &self.media {
45            attributes.push(("media", media.clone()));
46        }
47        if let Some(title) = &self.title {
48            attributes.push(("title", title.clone()));
49        }
50        if let Some(disabled) = &self.disabled {
51            attributes.push(("disabled", disabled.to_string()));
52        }
53        if let Some(r#as) = &self.r#as {
54            attributes.push(("as", r#as.clone()));
55        }
56        if let Some(sizes) = &self.sizes {
57            attributes.push(("sizes", sizes.clone()));
58        }
59        if let Some(href) = &self.href {
60            attributes.push(("href", href.clone()));
61        }
62        if let Some(crossorigin) = &self.crossorigin {
63            attributes.push(("crossOrigin", crossorigin.clone()));
64        }
65        if let Some(referrerpolicy) = &self.referrerpolicy {
66            attributes.push(("referrerPolicy", referrerpolicy.clone()));
67        }
68        if let Some(fetchpriority) = &self.fetchpriority {
69            attributes.push(("fetchPriority", fetchpriority.clone()));
70        }
71        if let Some(hreflang) = &self.hreflang {
72            attributes.push(("hrefLang", hreflang.clone()));
73        }
74        if let Some(integrity) = &self.integrity {
75            attributes.push(("integrity", integrity.clone()));
76        }
77        if let Some(r#type) = &self.r#type {
78            attributes.push(("type", r#type.clone()));
79        }
80        if let Some(blocking) = &self.blocking {
81            attributes.push(("blocking", blocking.clone()));
82        }
83        if let Some(onload) = &self.onload {
84            attributes.push(("onload", onload.clone()));
85        }
86        attributes
87    }
88}
89
90/// Render a [`<link>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/link) tag into the head of the page.
91///
92/// > The [Link](https://docs.rs/dioxus-router/latest/dioxus_router/components/fn.Link.html) component in dioxus router and this component are completely different.
93/// > This component links resources in the head of the page, while the router component creates clickable links in the body of the page.
94///
95/// # Example
96/// ```rust, no_run
97/// # use dioxus::prelude::*;
98/// fn RedBackground() -> Element {
99///     rsx! {
100///         // You can use the meta component to render a meta tag into the head of the page
101///         // This meta tag will redirect the user to the dioxuslabs homepage in 10 seconds
102///         document::Link {
103///             href: asset!("/assets/style.css"),
104///             rel: "stylesheet",
105///         }
106///     }
107/// }
108/// ```
109///
110/// <div class="warning">
111///
112/// Any updates to the props after the first render will not be reflected in the head.
113///
114/// </div>
115#[doc(alias = "<link>")]
116#[component]
117pub fn Link(props: LinkProps) -> Element {
118    use_update_warning(&props, "Link {}");
119
120    use_hook(|| {
121        let document = document();
122        let mut insert_link = document.create_head_component();
123        if let Some(href) = &props.href {
124            if !should_insert_link(href) {
125                insert_link = false;
126            }
127        }
128
129        if !insert_link {
130            return;
131        }
132
133        document.create_link(props);
134    });
135
136    VNode::empty()
137}
138
139#[derive(Default, Clone)]
140struct LinkContext(DeduplicationContext);
141
142fn should_insert_link(href: &str) -> bool {
143    get_or_insert_root_context::<LinkContext>()
144        .0
145        .should_insert(href)
146}