📚ReDocs
ReDocs

Import from:

1import { createSlot, Template } from "@reface/recast/slots";

Slots System provides a mechanism for content distribution and management across components.

Content Distribution

Recast provides a slot system for content distribution:

1import { createSlot } from "@reface/slots";
2import { Template } from "@reface/slots";
3
4// Create named slot
5const HeadSlot = createSlot("head");
6const TitleSlot = createSlot(
7 ​"head.title",
8 ​(content) => title`${content}` // Optional render transform
9);
10
11// Fill slots with content using ComponentTemplate
12const Page = component((props: Props, children: Children) => (
13 ​<html>
14 ​ ​<head>
15 ​ ​ ​<HeadSlot /> {/* Slot placeholder */}
16 ​ ​ ​<TitleSlot>Page Title</TitleSlot>
17 ​ ​</head>
18 ​ ​<body>
19 ​ ​ ​<main>{children}</main>
20 ​ ​</body>
21 ​</html>
22));
23
24// Provide content for slots
25const Layout = component((props: Props, children: Children) => (
26 ​<>
27 ​ ​<Template slot="head">
28 ​ ​ ​<meta charset="utf-8" />
29 ​ ​ ​<link rel="stylesheet" href="/style.css" />
30 ​ ​</Template>
31
32 ​ ​<Template slot="head.title" key="main">
33 ​ ​ ​My Website
34 ​ ​</Template>
35 ​ ​{children}
36 ​</>
37));

Key Features

  1. Named Slots

    • Unique slot identifiers

    • Optional render transforms

    • Hierarchical naming (e.g., "head.title")

  2. Multiple Providers

    • Multiple content providers for same slot

    • Content deduplication via keys

    • Custom content merging logic

  3. Scope Control

    • Global slots

    • Component-scoped slots

    • Inheritance control

  4. Content Management

    • Content collection

    • Key-based deduplication

    • Transform pipeline

    • Injection system

Built-in Slots

Recast provides several built-in slots:

1// Head content
2const HeadSlot = createSlot("head");
3
4// Page title
5const TitleSlot = createSlot("head.title", (content) => title`${content}`);
6
7// End of body content
8const BodyEndSlot = createSlot("body.end");
9
10// Usage with Template
11const Page = component((props: Props, children: Children) => (
12 ​<html>
13 ​ ​<head>
14 ​ ​ ​<HeadSlot />
15 ​ ​ ​<TitleSlot>Default Title</TitleSlot>
16 ​ ​</head>
17 ​ ​<body>
18 ​ ​ ​{children}
19 ​ ​ ​<BodyEndSlot />
20 ​ ​</body>
21 ​</html>
22));

Render Process

During render, slots follow this process:

  1. Slots collect content from Templates

  2. Content is deduplicated by keys

  3. Optional render transforms are applied

  4. Content is injected into slot placeholders

Best Practices

  1. Use meaningful slot names

  2. Always provide keys for content deduplication

  3. Use hierarchical naming for related slots

  4. Keep transforms pure and simple

  5. Consider scope when designing slot structure