tachys/html/attribute/
global.rs

1use super::Lang;
2use crate::{
3    html::{
4        attribute::*,
5        class::{class, Class, IntoClass},
6        element::{ElementType, HasElementType, HtmlElement},
7        event::{on, on_target, EventDescriptor, On, Targeted},
8        property::{prop, IntoProperty, Property},
9        style::{style, IntoStyle, Style},
10    },
11    prelude::RenderHtml,
12    view::add_attr::AddAnyAttr,
13};
14use core::convert::From;
15
16/// Adds an attribute that modifies the `class`.
17pub trait ClassAttribute<C>
18where
19    C: IntoClass,
20{
21    /// The type of the element with the new attribute added.
22    type Output;
23
24    /// Adds a CSS class to an element.
25    fn class(self, value: C) -> Self::Output;
26}
27
28impl<E, At, Ch, C> ClassAttribute<C> for HtmlElement<E, At, Ch>
29where
30    E: ElementType + Send,
31    At: Attribute + Send,
32    Ch: RenderHtml + Send,
33    C: IntoClass,
34{
35    type Output = <Self as AddAnyAttr>::Output<Class<C>>;
36
37    fn class(self, value: C) -> Self::Output {
38        self.add_any_attr(class(value))
39    }
40}
41
42/// Adds an attribute that modifies the DOM properties.
43pub trait PropAttribute<K, P>
44where
45    P: IntoProperty,
46{
47    /// The type of the element with the new attribute added.
48    type Output;
49
50    /// Adds a DOM property to an element.
51    fn prop(self, key: K, value: P) -> Self::Output;
52}
53
54impl<E, At, Ch, K, P> PropAttribute<K, P> for HtmlElement<E, At, Ch>
55where
56    E: ElementType + Send,
57    At: Attribute + Send,
58    Ch: RenderHtml + Send,
59    K: AsRef<str> + Send,
60    P: IntoProperty,
61{
62    type Output = <Self as AddAnyAttr>::Output<Property<K, P>>;
63
64    fn prop(self, key: K, value: P) -> Self::Output {
65        self.add_any_attr(prop(key, value))
66    }
67}
68
69/// Adds an attribute that modifies the CSS styles.
70pub trait StyleAttribute<S>
71where
72    S: IntoStyle,
73{
74    /// The type of the element with the new attribute added.
75    type Output;
76
77    /// Adds a CSS style to an element.
78    fn style(self, value: S) -> Self::Output;
79}
80
81impl<E, At, Ch, S> StyleAttribute<S> for HtmlElement<E, At, Ch>
82where
83    E: ElementType + Send,
84    At: Attribute + Send,
85    Ch: RenderHtml + Send,
86    S: IntoStyle,
87{
88    type Output = <Self as AddAnyAttr>::Output<Style<S>>;
89
90    fn style(self, value: S) -> Self::Output {
91        self.add_any_attr(style(value))
92    }
93}
94
95/// Adds an event listener to an element definition.
96pub trait OnAttribute<E, F> {
97    /// The type of the element with the event listener added.
98    type Output;
99
100    /// Adds an event listener to an element.
101    fn on(self, event: E, cb: F) -> Self::Output;
102}
103
104impl<El, At, Ch, E, F> OnAttribute<E, F> for HtmlElement<El, At, Ch>
105where
106    El: ElementType + Send,
107    At: Attribute + Send,
108    Ch: RenderHtml + Send,
109    E: EventDescriptor + Send + 'static,
110    E::EventType: 'static,
111    E::EventType: From<crate::renderer::types::Event>,
112    F: FnMut(E::EventType) + 'static,
113{
114    type Output = <Self as AddAnyAttr>::Output<On<E, F>>;
115
116    fn on(self, event: E, cb: F) -> Self::Output {
117        self.add_any_attr(on(event, cb))
118    }
119}
120
121/// Adds an event listener with a typed target to an element definition.
122pub trait OnTargetAttribute<E, F, T> {
123    /// The type of the element with the new attribute added.
124    type Output;
125
126    /// Adds an event listener with a typed target to an element definition.
127    fn on_target(self, event: E, cb: F) -> Self::Output;
128}
129
130impl<El, At, Ch, E, F> OnTargetAttribute<E, F, Self> for HtmlElement<El, At, Ch>
131where
132    El: ElementType + Send,
133    At: Attribute + Send,
134    Ch: RenderHtml + Send,
135    E: EventDescriptor + Send + 'static,
136    E::EventType: 'static,
137    E::EventType: From<crate::renderer::types::Event>,
138    F: FnMut(Targeted<E::EventType, <Self as HasElementType>::ElementType>)
139        + 'static,
140{
141    type Output =
142        <Self as AddAnyAttr>::Output<On<E, Box<dyn FnMut(E::EventType)>>>;
143
144    fn on_target(self, event: E, cb: F) -> Self::Output {
145        self.add_any_attr(on_target::<E, HtmlElement<El, At, Ch>, F>(event, cb))
146    }
147}
148
149/// Global attributes can be added to any HTML element.
150pub trait GlobalAttributes<V>
151where
152    Self: Sized + AddAnyAttr,
153    V: AttributeValue,
154{
155    /// The `accesskey` global attribute provides a hint for generating a keyboard shortcut for the current element.
156    fn accesskey(
157        self,
158        value: V,
159    ) -> <Self as AddAnyAttr>::Output<Attr<Accesskey, V>> {
160        self.add_any_attr(accesskey(value))
161    }
162
163    /// The `autocapitalize` global attribute controls whether and how text input is automatically capitalized as it is entered/edited by the user.
164    fn autocapitalize(
165        self,
166        value: V,
167    ) -> <Self as AddAnyAttr>::Output<Attr<Autocapitalize, V>> {
168        self.add_any_attr(autocapitalize(value))
169    }
170
171    /// The `autofocus` global attribute is a Boolean attribute indicating that an element should receive focus as soon as the page is loaded.
172    fn autofocus(
173        self,
174        value: V,
175    ) -> <Self as AddAnyAttr>::Output<Attr<Autofocus, V>> {
176        self.add_any_attr(autofocus(value))
177    }
178
179    /// The `contenteditable` global attribute is an enumerated attribute indicating if the element should be editable by the user.
180    fn contenteditable(
181        self,
182        value: V,
183    ) -> <Self as AddAnyAttr>::Output<Attr<Contenteditable, V>> {
184        self.add_any_attr(contenteditable(value))
185    }
186
187    /// The `dir` global attribute is an enumerated attribute indicating the directionality of the element's text.
188    fn dir(self, value: V) -> <Self as AddAnyAttr>::Output<Attr<Dir, V>> {
189        self.add_any_attr(dir(value))
190    }
191
192    /// The `draggable` global attribute is an enumerated attribute indicating whether the element can be dragged.
193    fn draggable(
194        self,
195        value: V,
196    ) -> <Self as AddAnyAttr>::Output<Attr<Draggable, V>> {
197        self.add_any_attr(draggable(value))
198    }
199
200    /// The `enterkeyhint` global attribute is used to customize the enter key on virtual keyboards.
201    fn enterkeyhint(
202        self,
203        value: V,
204    ) -> <Self as AddAnyAttr>::Output<Attr<Enterkeyhint, V>> {
205        self.add_any_attr(enterkeyhint(value))
206    }
207
208    /// The `exportparts` attribute enables the sharing of parts of an element's shadow DOM with a containing document.
209    fn exportparts(
210        self,
211        value: V,
212    ) -> <Self as AddAnyAttr>::Output<Attr<Exportparts, V>> {
213        self.add_any_attr(exportparts(value))
214    }
215
216    /// The `hidden` global attribute is a Boolean attribute indicating that the element is not yet, or is no longer, relevant.
217    fn hidden(self, value: V) -> <Self as AddAnyAttr>::Output<Attr<Hidden, V>> {
218        self.add_any_attr(hidden(value))
219    }
220
221    /// The `id` global attribute defines a unique identifier (ID) which must be unique in the whole document.
222    fn id(self, value: V) -> <Self as AddAnyAttr>::Output<Attr<Id, V>> {
223        self.add_any_attr(id(value))
224    }
225
226    /// The `inert` global attribute is a Boolean attribute that makes an element behave inertly.
227    fn inert(self, value: V) -> <Self as AddAnyAttr>::Output<Attr<Inert, V>> {
228        self.add_any_attr(inert(value))
229    }
230
231    /// The `inputmode` global attribute provides a hint to browsers for which virtual keyboard to display.
232    fn inputmode(
233        self,
234        value: V,
235    ) -> <Self as AddAnyAttr>::Output<Attr<Inputmode, V>> {
236        self.add_any_attr(inputmode(value))
237    }
238
239    /// The `is` global attribute allows you to specify that a standard HTML element should behave like a custom built-in element.
240    fn is(self, value: V) -> <Self as AddAnyAttr>::Output<Attr<Is, V>> {
241        self.add_any_attr(is(value))
242    }
243
244    /// The `itemid` global attribute is used to specify the unique, global identifier of an item.
245    fn itemid(self, value: V) -> <Self as AddAnyAttr>::Output<Attr<Itemid, V>> {
246        self.add_any_attr(itemid(value))
247    }
248
249    /// The `itemprop` global attribute is used to add properties to an item.
250    fn itemprop(
251        self,
252        value: V,
253    ) -> <Self as AddAnyAttr>::Output<Attr<Itemprop, V>> {
254        self.add_any_attr(itemprop(value))
255    }
256
257    /// The `itemref` global attribute is used to refer to other elements.
258    fn itemref(
259        self,
260        value: V,
261    ) -> <Self as AddAnyAttr>::Output<Attr<Itemref, V>> {
262        self.add_any_attr(itemref(value))
263    }
264
265    /// The `itemscope` global attribute is used to create a new item.
266    fn itemscope(
267        self,
268        value: V,
269    ) -> <Self as AddAnyAttr>::Output<Attr<Itemscope, V>> {
270        self.add_any_attr(itemscope(value))
271    }
272
273    /// The `itemtype` global attribute is used to specify the types of items.
274    fn itemtype(
275        self,
276        value: V,
277    ) -> <Self as AddAnyAttr>::Output<Attr<Itemtype, V>> {
278        self.add_any_attr(itemtype(value))
279    }
280
281    /// The `lang` global attribute helps define the language of an element.
282    fn lang(self, value: V) -> <Self as AddAnyAttr>::Output<Attr<Lang, V>> {
283        self.add_any_attr(lang(value))
284    }
285
286    /// The `nonce` global attribute is used to specify a cryptographic nonce.
287    fn nonce(self, value: V) -> <Self as AddAnyAttr>::Output<Attr<Nonce, V>> {
288        self.add_any_attr(nonce(value))
289    }
290
291    /// The `part` global attribute identifies the element as a part of a component.
292    fn part(self, value: V) -> <Self as AddAnyAttr>::Output<Attr<Part, V>> {
293        self.add_any_attr(part(value))
294    }
295
296    /// The `popover` global attribute defines the popover's behavior.
297    fn popover(
298        self,
299        value: V,
300    ) -> <Self as AddAnyAttr>::Output<Attr<Popover, V>> {
301        self.add_any_attr(popover(value))
302    }
303
304    /// The `role` global attribute defines the role of an element in ARIA.
305    fn role(self, value: V) -> <Self as AddAnyAttr>::Output<Attr<Role, V>> {
306        self.add_any_attr(role(value))
307    }
308
309    /// The `slot` global attribute assigns a slot in a shadow DOM.
310    fn slot(self, value: V) -> <Self as AddAnyAttr>::Output<Attr<Slot, V>> {
311        self.add_any_attr(slot(value))
312    }
313
314    /// The `spellcheck` global attribute is an enumerated attribute that defines whether the element may be checked for spelling errors.
315    fn spellcheck(
316        self,
317        value: V,
318    ) -> <Self as AddAnyAttr>::Output<Attr<Spellcheck, V>> {
319        self.add_any_attr(spellcheck(value))
320    }
321
322    /// The `tabindex` global attribute indicates if the element can take input focus.
323    fn tabindex(
324        self,
325        value: V,
326    ) -> <Self as AddAnyAttr>::Output<Attr<Tabindex, V>> {
327        self.add_any_attr(tabindex(value))
328    }
329
330    /// The `title` global attribute contains text representing advisory information.
331    fn title(self, value: V) -> <Self as AddAnyAttr>::Output<Attr<Title, V>> {
332        self.add_any_attr(title(value))
333    }
334
335    /// The `translate` global attribute is an enumerated attribute that specifies whether an element's attribute values and text content should be translated when the page is localized.
336    fn translate(
337        self,
338        value: V,
339    ) -> <Self as AddAnyAttr>::Output<Attr<Translate, V>> {
340        self.add_any_attr(translate(value))
341    }
342
343    /// The `virtualkeyboardpolicy` global attribute specifies the behavior of the virtual keyboard.
344    fn virtualkeyboardpolicy(
345        self,
346        value: V,
347    ) -> <Self as AddAnyAttr>::Output<Attr<Virtualkeyboardpolicy, V>> {
348        self.add_any_attr(virtualkeyboardpolicy(value))
349    }
350}
351
352impl<El, At, Ch, V> GlobalAttributes<V> for HtmlElement<El, At, Ch>
353where
354    El: ElementType + Send,
355    At: Attribute + Send,
356    Ch: RenderHtml + Send,
357    V: AttributeValue,
358{
359}
360
361macro_rules! on_definitions {
362	($(#[$meta:meta] $key:ident $html:literal),* $(,)?) => {
363        paste::paste! {
364            $(
365                #[doc = concat!("Adds the HTML `", $html, "` attribute to the element.\n\n**Note**: This is the HTML attribute, which takes a JavaScript string, not an `on:` listener that takes application logic written in Rust.")]
366                #[track_caller]
367                fn $key(
368                    self,
369                    value: V,
370                ) -> <Self as AddAnyAttr>::Output<Attr<[<$key:camel>], V>>
371                {
372                    self.add_any_attr($key(value))
373                }
374            )*
375		}
376    }
377}
378
379/// Provides methods for HTML event listener attributes.
380pub trait GlobalOnAttributes<V>
381where
382    Self: Sized + AddAnyAttr,
383    V: AttributeValue,
384{
385    on_definitions! {
386        /// The `onabort` attribute specifies the event handler for the abort event.
387        onabort "onabort",
388        /// The `onautocomplete` attribute specifies the event handler for the autocomplete event.
389        onautocomplete "onautocomplete",
390        /// The `onautocompleteerror` attribute specifies the event handler for the autocompleteerror event.
391        onautocompleteerror "onautocompleteerror",
392        /// The `onblur` attribute specifies the event handler for the blur event.
393        onblur "onblur",
394        /// The `oncancel` attribute specifies the event handler for the cancel event.
395        oncancel "oncancel",
396        /// The `oncanplay` attribute specifies the event handler for the canplay event.
397        oncanplay "oncanplay",
398        /// The `oncanplaythrough` attribute specifies the event handler for the canplaythrough event.
399        oncanplaythrough "oncanplaythrough",
400        /// The `onchange` attribute specifies the event handler for the change event.
401        onchange "onchange",
402        /// The `onclick` attribute specifies the event handler for the click event.
403        onclick "onclick",
404        /// The `onclose` attribute specifies the event handler for the close event.
405        onclose "onclose",
406        /// The `oncontextmenu` attribute specifies the event handler for the contextmenu event.
407        oncontextmenu "oncontextmenu",
408        /// The `oncuechange` attribute specifies the event handler for the cuechange event.
409        oncuechange "oncuechange",
410        /// The `ondblclick` attribute specifies the event handler for the double click event.
411        ondblclick "ondblclick",
412        /// The `ondrag` attribute specifies the event handler for the drag event.
413        ondrag "ondrag",
414        /// The `ondragend` attribute specifies the event handler for the dragend event.
415        ondragend "ondragend",
416        /// The `ondragenter` attribute specifies the event handler for the dragenter event.
417        ondragenter "ondragenter",
418        /// The `ondragleave` attribute specifies the event handler for the dragleave event.
419        ondragleave "ondragleave",
420        /// The `ondragover` attribute specifies the event handler for the dragover event.
421        ondragover "ondragover",
422        /// The `ondragstart` attribute specifies the event handler for the dragstart event.
423        ondragstart "ondragstart",
424        /// The `ondrop` attribute specifies the event handler for the drop event.
425        ondrop "ondrop",
426        /// The `ondurationchange` attribute specifies the event handler for the durationchange event.
427        ondurationchange "ondurationchange",
428        /// The `onemptied` attribute specifies the event handler for the emptied event.
429        onemptied "onemptied",
430        /// The `onended` attribute specifies the event handler for the ended event.
431        onended "onended",
432        /// The `onerror` attribute specifies the event handler for the error event.
433        onerror "onerror",
434        /// The `onfocus` attribute specifies the event handler for the focus event.
435        onfocus "onfocus",
436        /// The `onformdata` attribute specifies the event handler for the formdata event.
437        onformdata "onformdata",
438        /// The `oninput` attribute specifies the event handler for the input event.
439        oninput "oninput",
440        /// The `oninvalid` attribute specifies the event handler for the invalid event.
441        oninvalid "oninvalid",
442        /// The `onkeydown` attribute specifies the event handler for the keydown event.
443        onkeydown "onkeydown",
444        /// The `onkeypress` attribute specifies the event handler for the keypress event.
445        onkeypress "onkeypress",
446        /// The `onkeyup` attribute specifies the event handler for the keyup event.
447        onkeyup "onkeyup",
448        /// The `onlanguagechange` attribute specifies the event handler for the languagechange event.
449        onlanguagechange "onlanguagechange",
450        /// The `onload` attribute specifies the event handler for the load event.
451        onload "onload",
452        /// The `onloadeddata` attribute specifies the event handler for the loadeddata event.
453        onloadeddata "onloadeddata",
454        /// The `onloadedmetadata` attribute specifies the event handler for the loadedmetadata event.
455        onloadedmetadata "onloadedmetadata",
456        /// The `onloadstart` attribute specifies the event handler for the loadstart event.
457        onloadstart "onloadstart",
458        /// The `onmousedown` attribute specifies the event handler for the mousedown event.
459        onmousedown "onmousedown",
460        /// The `onmouseenter` attribute specifies the event handler for the mouseenter event.
461        onmouseenter "onmouseenter",
462        /// The `onmouseleave` attribute specifies the event handler for the mouseleave event.
463        onmouseleave "onmouseleave",
464        /// The `onmousemove` attribute specifies the event handler for the mousemove event.
465        onmousemove "onmousemove",
466        /// The `onmouseout` attribute specifies the event handler for the mouseout event.
467        onmouseout "onmouseout",
468        /// The `onmouseover` attribute specifies the event handler for the mouseover event.
469        onmouseover "onmouseover",
470        /// The `onmouseup` attribute specifies the event handler for the mouseup event.
471        onmouseup "onmouseup",
472        /// The `onpause` attribute specifies the event handler for the pause event.
473        onpause "onpause",
474        /// The `onplay` attribute specifies the event handler for the play event.
475        onplay "onplay",
476        /// The `onplaying` attribute specifies the event handler for the playing event.
477        onplaying "onplaying",
478        /// The `onprogress` attribute specifies the event handler for the progress event.
479        onprogress "onprogress",
480        /// The `onratechange` attribute specifies the event handler for the ratechange event.
481        onratechange "onratechange",
482        /// The `onreset` attribute specifies the event handler for the reset event.
483        onreset "onreset",
484        /// The `onresize` attribute specifies the event handler for the resize event.
485        onresize "onresize",
486        /// The `onscroll` attribute specifies the event handler for the scroll event.
487        onscroll "onscroll",
488        /// The `onsecuritypolicyviolation` attribute specifies the event handler for the securitypolicyviolation event.
489        onsecuritypolicyviolation "onsecuritypolicyviolation",
490        /// The `onseeked` attribute specifies the event handler for the seeked event.
491        onseeked "onseeked",
492        /// The `onseeking` attribute specifies the event handler for the seeking event.
493        onseeking "onseeking",
494        /// The `onselect` attribute specifies the event handler for the select event.
495        onselect "onselect",
496        /// The `onslotchange` attribute specifies the event handler for the slotchange event.
497        onslotchange "onslotchange",
498        /// The `onstalled` attribute specifies the event handler for the stalled event.
499        onstalled "onstalled",
500        /// The `onsubmit` attribute specifies the event handler for the submit event.
501        onsubmit "onsubmit",
502        /// The `onsuspend` attribute specifies the event handler for the suspend event.
503        onsuspend "onsuspend",
504        /// The `ontimeupdate` attribute specifies the event handler for the timeupdate event.
505        ontimeupdate "ontimeupdate",
506        /// The `ontoggle` attribute specifies the event handler for the toggle event.
507        ontoggle "ontoggle",
508        /// The `onvolumechange` attribute specifies the event handler for the volumechange event.
509        onvolumechange "onvolumechange",
510        /// The `onwaiting` attribute specifies the event handler for the waiting event.
511        onwaiting "onwaiting",
512        /// The `onwebkitanimationend` attribute specifies the event handler for the webkitanimationend event.
513        onwebkitanimationend "onwebkitanimationend",
514        /// The `onwebkitanimationiteration` attribute specifies the event handler for the webkitanimationiteration event.
515        onwebkitanimationiteration "onwebkitanimationiteration",
516        /// The `onwebkitanimationstart` attribute specifies the event handler for the webkitanimationstart event.
517        onwebkitanimationstart "onwebkitanimationstart",
518        /// The `onwebkittransitionend` attribute specifies the event handler for the webkittransitionend event.
519        onwebkittransitionend "onwebkittransitionend",
520        /// The `onwheel` attribute specifies the event handler for the wheel event.
521        onwheel "onwheel",
522
523    }
524}
525
526impl<El, At, Ch, V> GlobalOnAttributes<V> for HtmlElement<El, At, Ch>
527where
528    El: ElementType + Send,
529    At: Attribute + Send,
530    Ch: RenderHtml + Send,
531    V: AttributeValue,
532{
533}