-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for arbitrary node ref elements
- Loading branch information
1 parent
571af01
commit 6f57e8b
Showing
6 changed files
with
362 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod arrow; | ||
mod node_ref; | ||
mod types; | ||
mod use_floating; | ||
mod utils; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,251 @@ | ||
use std::ops::Deref; | ||
|
||
use leptos::{html::ElementDescriptor, HtmlElement, NodeRef}; | ||
use web_sys::Element; | ||
|
||
pub trait NodeRefAsElement<T: ElementDescriptor + Clone + 'static> { | ||
fn get_as_element(&self) -> Option<Element>; | ||
|
||
fn get_untracked_as_element(&self) -> Option<Element>; | ||
|
||
fn on_load<F>(self, f: F) | ||
where | ||
F: FnOnce(HtmlElement<T>) + 'static; | ||
} | ||
|
||
impl NodeRefAsElement<leptos::html::AnyElement> for NodeRef<leptos::html::AnyElement> { | ||
fn get_as_element(&self) -> Option<Element> { | ||
self.get().map(|html_element| { | ||
let element: &web_sys::Element = html_element.deref(); | ||
element.clone() | ||
}) | ||
} | ||
|
||
fn get_untracked_as_element(&self) -> Option<Element> { | ||
self.get_untracked().map(|html_element| { | ||
let element: &web_sys::Element = html_element.deref(); | ||
element.clone() | ||
}) | ||
} | ||
|
||
fn on_load<F>(self, f: F) | ||
where | ||
F: FnOnce(HtmlElement<leptos::html::AnyElement>) + 'static, | ||
{ | ||
self.on_load(f) | ||
} | ||
} | ||
|
||
macro_rules! generate_html_tags { | ||
($( | ||
$tag:ty | ||
),* $(,)?) => { | ||
paste::paste! { | ||
$( | ||
impl NodeRefAsElement<leptos::html::[<$tag>]> for NodeRef<leptos::html::[<$tag>]> { | ||
fn get_as_element(&self) -> Option<Element> { | ||
self.get().map(|html_element| { | ||
let element: &web_sys::Element = html_element.deref(); | ||
element.clone() | ||
}) | ||
} | ||
|
||
fn get_untracked_as_element(&self) -> Option<Element> { | ||
self.get_untracked().map(|html_element| { | ||
let element: &web_sys::Element = html_element.deref(); | ||
element.clone() | ||
}) | ||
} | ||
|
||
fn on_load<F>(self, f: F) | ||
where | ||
F: FnOnce(HtmlElement<leptos::html::[<$tag>]>) + 'static, | ||
{ | ||
self.on_load(f) | ||
} | ||
} | ||
)* | ||
} | ||
} | ||
} | ||
|
||
macro_rules! generate_math_tags { | ||
($( | ||
$tag:ty | ||
),* $(,)?) => { | ||
paste::paste! { | ||
$( | ||
impl NodeRefAsElement<leptos::math::[<$tag>]> for NodeRef<leptos::math::[<$tag>]> { | ||
fn get_as_element(&self) -> Option<Element> { | ||
self.get().map(|html_element| { | ||
let element: &web_sys::Element = html_element.deref(); | ||
element.clone() | ||
}) | ||
} | ||
|
||
fn get_untracked_as_element(&self) -> Option<Element> { | ||
self.get_untracked().map(|html_element| { | ||
let element: &web_sys::Element = html_element.deref(); | ||
element.clone() | ||
}) | ||
} | ||
|
||
fn on_load<F>(self, f: F) | ||
where | ||
F: FnOnce(HtmlElement<leptos::math::[<$tag>]>) + 'static, | ||
{ | ||
self.on_load(f) | ||
} | ||
} | ||
)* | ||
} | ||
} | ||
} | ||
|
||
macro_rules! generate_svg_tags { | ||
($( | ||
$tag:ty | ||
),* $(,)?) => { | ||
paste::paste! { | ||
$( | ||
impl NodeRefAsElement<leptos::svg::[<$tag>]> for NodeRef<leptos::svg::[<$tag>]> { | ||
fn get_as_element(&self) -> Option<Element> { | ||
self.get().map(|html_element| { | ||
let element: &web_sys::Element = html_element.deref(); | ||
element.clone() | ||
}) | ||
} | ||
|
||
fn get_untracked_as_element(&self) -> Option<Element> { | ||
self.get_untracked().map(|html_element| { | ||
let element: &web_sys::Element = html_element.deref(); | ||
element.clone() | ||
}) | ||
} | ||
|
||
fn on_load<F>(self, f: F) | ||
where | ||
F: FnOnce(HtmlElement<leptos::svg::[<$tag>]>) + 'static, | ||
{ | ||
self.on_load(f) | ||
} | ||
} | ||
)* | ||
} | ||
} | ||
} | ||
|
||
generate_html_tags![ | ||
Html, Base, Head, Link, Meta, Style, Title, Body, Address, Article, Aside, Footer, Header, | ||
Hgroup, H1, H2, H3, H4, H5, H6, Main, Nav, Section, Blockquote, Dd, Div, Dl, Dt, Figcaption, | ||
Figure, Hr, Li, Ol, P, Pre, Ul, A, Abbr, B, Bdi, Bdo, Br, Cite, Code, Data, Dfn, Em, I, Kbd, | ||
Mark, Q, Rp, Rt, Ruby, S, Samp, Small, Span, Strong, Sub, Sup, Time, U, Var, Wbr, Area, Audio, | ||
Img, Map, Track, Video, Embed, Iframe, Object, Param, Picture, Portal, Source, Svg, Math, | ||
Canvas, Noscript, Script, Del, Ins, Caption, Col, Colgroup, Table, Tbody, Td, Tfoot, Th, Thead, | ||
Tr, Button, Datalist, Fieldset, Form, Input, Label, Legend, Meter, Optgroup, Option_, Output, | ||
Progress, Select, Textarea, Details, Dialog, Menu, Summary, Slot, Template, | ||
]; | ||
|
||
generate_math_tags![ | ||
Math, | ||
Mi, | ||
Mn, | ||
Mo, | ||
Ms, | ||
Mspace, | ||
Mtext, | ||
Menclose, | ||
Merror, | ||
Mfenced, | ||
Mfrac, | ||
Mpadded, | ||
Mphantom, | ||
Mroot, | ||
Mrow, | ||
Msqrt, | ||
Mstyle, | ||
Mmultiscripts, | ||
Mover, | ||
Mprescripts, | ||
Msub, | ||
Msubsup, | ||
Msup, | ||
Munder, | ||
Munderover, | ||
Mtable, | ||
Mtd, | ||
Mtr, | ||
Maction, | ||
Annotation, | ||
AnnotationXml, | ||
Semantics, | ||
]; | ||
|
||
generate_svg_tags![ | ||
A, | ||
Animate, | ||
AnimateMotion, | ||
AnimateTransform, | ||
Circle, | ||
ClipPath, | ||
Defs, | ||
Desc, | ||
Discard, | ||
Ellipse, | ||
FeBlend, | ||
FeColorMatrix, | ||
FeComponentTransfer, | ||
FeComposite, | ||
FeConvolveMatrix, | ||
FeDiffuseLighting, | ||
FeDisplacementMap, | ||
FeDistantLight, | ||
FeDropShadow, | ||
FeFlood, | ||
FeFuncA, | ||
FeFuncB, | ||
FeFuncG, | ||
FeFuncR, | ||
FeGaussianBlur, | ||
FeImage, | ||
FeMerge, | ||
FeMergeNode, | ||
FeMorphology, | ||
FeOffset, | ||
FePointLight, | ||
FeSpecularLighting, | ||
FeSpotLight, | ||
FeTile, | ||
FeTurbulence, | ||
Filter, | ||
ForeignObject, | ||
G, | ||
Hatch, | ||
Hatchpath, | ||
Image, | ||
Line, | ||
LinearGradient, | ||
Marker, | ||
Mask, | ||
Metadata, | ||
Mpath, | ||
Path, | ||
Pattern, | ||
Polygon, | ||
Polyline, | ||
RadialGradient, | ||
Rect, | ||
Script, | ||
Set, | ||
Stop, | ||
Style, | ||
Svg, | ||
Switch, | ||
Symbol, | ||
Text, | ||
TextPath, | ||
Title, | ||
Tspan, | ||
Use, | ||
View, | ||
]; |
Oops, something went wrong.