dioxus_document/elements/
link.rs1use 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 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 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#[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}