pub struct Island { /* private fields */ }Expand description
One React island: an element that the loader mounts a component into.
It renders as:
<div data-react-island="Counter" data-react-props="{...}"
data-react-mount="visible" id="..." class="...">fallback</div>The loader replaces the fallback with the component. Without JavaScript, or after an error, the fallback stays.
use autumn_plugin_react::{Island, MountWhen};
use maud::{Render, html};
let island = Island::new("Counter")
.props(&serde_json::json!({ "start": 3 }))?
.mount_when(MountWhen::Visible)
.fallback(html! { p { "Count: 3" } });
let html = island.render().into_string();
assert!(html.starts_with(r#"<div data-react-island="Counter""#));Props go in an HTML attribute. Keep them small. The component name, the
id and the class are trusted input from your code.
Implementations§
Source§impl Island
impl Island
Sourcepub fn new(name: impl Into<String>) -> Self
pub fn new(name: impl Into<String>) -> Self
Makes an island for the component that an app bundle registers as
name.
Sourcepub fn props<T: Serialize + ?Sized>(self, props: &T) -> Result<Self, PropsError>
pub fn props<T: Serialize + ?Sized>(self, props: &T) -> Result<Self, PropsError>
Sets the component props. The value must serialize to a JSON object.
§Errors
PropsError::Serializewhenserde_jsoncannot serialize the value (for example, a map with tuple keys).PropsError::NotAnObjectwhen the JSON is not an object.PropsError::ProtoKeywhen the object has a__proto__key.
serde_json writes a non-finite float (NaN, infinity) as null.
Sourcepub const fn mount_when(self, when: MountWhen) -> Self
pub const fn mount_when(self, when: MountWhen) -> Self
Sets when the loader mounts the island. The default is
MountWhen::Load.
Sourcepub fn fallback(self, fallback: Markup) -> Self
pub fn fallback(self, fallback: Markup) -> Self
Sets the content to show before the mount, without JavaScript, and after an error.
Sourcepub fn attr(
self,
name: impl Into<String>,
value: impl Into<String>,
) -> Result<Self, AttrError>
pub fn attr( self, name: impl Into<String>, value: impl Into<String>, ) -> Result<Self, AttrError>
Adds an attribute, for example role, aria-label or
hx-preserve. A second call with the same name replaces the value.
§Errors
AttrError::InvalidNamefor a name that is not a plain attribute name.AttrError::Reservedforid,class(useIsland::idandIsland::class),data-react-*(the loader owns them) andon*event handlers (the default CSP blocks them).
Trait Implementations§
Auto Trait Implementations§
impl Forget for Island
impl Freeze for Island
impl RefUnwindSafe for Island
impl Send for Island
impl Sync for Island
impl Unpin for Island
impl UnsafeUnpin for Island
impl UnwindSafe for Island
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T> Scoped for T
impl<T> Scoped for T
Source§fn scope(ctx: &PolicyContext) -> ScopeQuery<'_, Self>
fn scope(ctx: &PolicyContext) -> ScopeQuery<'_, Self>
ScopeQuery for this type. Resolves the
registered scope at .load() time, not here.