Slots
Filling a component's slots from Rust.
A component that renders a <slot> or <RouterView> gets a Slots struct, and its render takes one after the props. Between generated components the compiler fills it; from Rust, you do.
Plain slots
Each <slot> is an Option<Slot> field: head for <slot name="head">, default for <slot>. None renders the slot's fallback. A Slot::new wraps a closure that writes the content:
<!-- Frame.vue -->
<template>
<div class="frame"><header><slot name="head">{{ title }}</slot></header><main><slot>nothing here</slot></main></div>
</template>/// What a parent puts in the slots `Frame.vue` renders.
#[derive(Clone, Copy, Default)]
pub struct Slots<'s> {
/// `<slot name="head">`
pub head: Option<fv::render::Slot<'s>>,
/// `<slot>`
pub default: Option<fv::render::Slot<'s>>,
let body = |out: &mut String| out.push_str("<p>three new messages</p>");
let mut out = String::new();
frame::render(
&mut out,
&frame::Props::new("Inbox"),
frame::Slots { default: Some(Slot::new(&body)), ..Default::default() },
);
// The head shows its fallback, the title; every slot sits between fragment markers.
assert_eq!(
out,
r#"<div class="frame"><header><!--[-->Inbox<!--]--></header><main><!--[--><p>three new messages</p><!--]--></main></div>"#
);The closure writes raw markup: it is your own code, which nothing escapes for you, so escape any text it writes with escape_into. To put another component in the slot, call its renderer from the closure: |out: &mut String| badge::render(out, &badge_props), or |out: &mut String| badge::island(&badge_props).render_to(out) to make it an island.
Content given with Slot::new always counts as content, as a component in a slot always does to Vue: the fallback never replaces it, even when it writes nothing. To show the fallback, pass None.
<RouterView>
A component with <RouterView> has a router_view: Slot<'s> field that is not optional, and its Slots has no Default: the page the route shows is always given. It is written with Slot::render_to, without fragment markers, as <RouterView> writes its component. See routing for a full example.
Scoped slots
A slot that passes props to its content, <slot name="row" :row="r" :index="i" :first="i === 0">, gets a struct of those props, borrowed for the render, and a closure type:
/// The props `<slot name="row">` passes the content a parent gives it, borrowed for the render.
pub struct RowSlotProps<'v> {
pub row: &'v Row<'v>,
pub index: i64,
pub label: &'v str,
pub tags: &'v [Cow<'v, str>],
pub first: bool,
}
/// A parent's content for `<slot name="row">`, given its props: returns whether it wrote anything but comments.
pub type RowSlot<'s> = dyn for<'v> Fn(&mut String, &RowSlotProps<'v>) -> bool + 's;
pub struct Slots<'s> {
/// `<slot name="row">`
pub row: Option<&'s RowSlot<'s>>,
// …
}Strings arrive as &str, lists as slices, objects by reference. The closure is called once per render of the outlet (once per row of a v-for) and returns whether it wrote content. Returning false discards whatever it wrote and shows the slot's fallback, as Vue does with content that is empty or only comments:
// DataList.vue, in part:
// <li v-for="(r, i) in rows"><slot name="row" :row="r" :index="i" :label="r.label" :tags="r.tags" :first="i === 0">{{ i }}. {{ r.label }}</slot></li>
let props = data_list::Props::new(
vec![data_list::Row::new(1, "a<b", ["x"]), data_list::Row::new(2, "c", ["y", "z"])],
"Rows",
);
let row = |out: &mut String, p: &data_list::RowSlotProps<'_>| {
if p.first {
return false; // the first row keeps the fallback
}
out.push_str("<b>");
ferrovue::core::escape_into(out, p.label);
out.push_str("</b> ");
ferrovue::core::push_int(out, p.tags.len() as i64);
true
};
let mut out = String::new();
data_list::render(&mut out, &props, data_list::Slots { row: Some(&row), ..Default::default() });
assert_eq!(
out,
concat!(
"<section><header><!--[-->Rows<!--]--></header><ul><!--[-->",
"<li><!--[-->0. a<b<!--]--></li>",
"<li><!--[--><b>c</b> 2<!--]--></li>",
"<!--]--></ul><footer><!--[--><!--]--></footer></section>",
)
);The closure is passed by reference, Some(&row), so one closure serves every row and borrows whatever the page holds.
What generated parents do
When one generated component fills another's slot, the compiler writes the closure for you. Slot content from a template uses a variant of Slot that reports whether it wrote anything but comments, so that a <template #head><b v-if="show">…</b></template> that renders only <!----> gives way to the fallback as it does in Vue. A slot forwarded through several components keeps that answer. slot_into and scoped_slot_into are the functions the outlet calls.
Components with :slotted() styles
A component whose <style scoped> uses :slotted() passes a slot scope id, data-v-…-s, to the content of each of its slots, which writes it onto its elements. Its outlets call slot_into_slotted and scoped_slot_into_slotted instead, and generated parents fill its plain slots with Slot::slotted, content that is given the id.
From Rust nothing changes for a plain slot: Slot::new content is not given the id, as static markup is not in Vue. A scoped slot's closure type takes the id as a third parameter, data-v-…-s with its leading space, which the closure may write onto its elements or ignore:
pub type FooterSlot<'s> = dyn Fn(&mut String, &FooterSlotProps, &str) -> bool + 's;scoped_styles has the whole example.
What is refused
Slot props that are array literals, defaults in destructured slot props (#row="{ label = 'x' }"), and two outlets of the same slot that pass different props.