Bring the power of slots to your React components effortlessly.
In modern React applications, building reusable and flexible components is key to scaling efficiently. However, as the complexity of components increases, the need for a slot-based architecture becomes apparent. The concept of slots, familiar to developers from frameworks like Svelte and Vue, allows for seamless content injection and greater customization of component behavior. But in React, this pattern isn’t natively supported and often leads to verbose or suboptimal solutions.
react-slots introduces a streamlined way to implement slots, bringing familiar concepts into the React ecosystem with minimal effort. It provides developers with a clear and consistent API to define and use slots, enhancing flexibility while reducing boilerplate code. The library ensures components remain decoupled, making it easier to manage nested or complex content structures.
This example demonstrates how to create and use slots in React using the @grlt-hub/react-slots library.
- Creating Slot Identifiers
import { createSlots, createSlotIdentifier } from '@grlt-hub/react-slots';
const slots = {
Bottom: createSlotIdentifier(),
} as const;We import createSlots and createSlotIdentifier from the library. Then, we define a slots object, where each key represents a unique slot. In this case, we create a slot named Bottom.
- Creating the Slot API
const { slotsApi: footerSlots, Slots: FooterSlots } = createSlots(slots);createSlots takes the slots object and returns two values:
slotsApi(renamed tofooterSlots): an API for managing slot content.Slots(renamed toFooterSlots): a component used to render the slot content in the specified location.
- Defining the Footer Component
const Footer = () => (
<footer>
Hello
<FooterSlots.Bottom />
</footer>
);Using footerSlots.insert.into.Bottom, we insert content into the Bottom slot. Here, we add a component that renders <code>World</code>.
After executing the code, the rendered output will be:
<footer>
Hello
<code>World</code>
</footer>This way, the @grlt-hub/react-slots library provides an efficient way to define and use slots in React components, making content injection simple and flexible.
npm i @grlt-hub/react-slots
# or
yarn add @grlt-hub/react-slots
# or
pnpm add @grlt-hub/react-slotsIn this guide, we'll walk through how to define and pass props to components inserted into a slot using @grlt-hub/react-slots.
You can specify the props a slot should accept by providing a type to createSlotIdentifier. For example, if you want a slot that requires a text prop, you can define it like this:
import { createSlotIdentifier } from '@grlt-hub/react-slots';
const slots = {
Bottom: createSlotIdentifier<{ text: string }>(),
} as const;This type definition ensures that any usage of <FooterSlots.Bottom /> must include a text prop.
When you use the slot component in your layout, you must pass the required props directly:
const Footer = () => (
<footer>
Footer content
<FooterSlots.Bottom text='Hello from the slot!' />
</footer>
);The text prop passed here will be provided to any component inserted into the Bottom slot.
You use footerSlots.insert.into.Bottom to insert a component. The component will automatically receive the props passed to <FooterSlots.Bottom />:
footerSlots.insert.into.Bottom({
fn: ({ text }) => ({ doubleText: `${text} ${text}` }),
component: ({ doubleText }) => <p>{doubleText}</p>,
});fn: This function is optional. If provided, it receives the props from<FooterSlots.Bottom />(e.g.,{ text }) and allows you to transform them before passing them tocomponent. In the example above,fntakestextand creates a new propdoubleText, which repeats thetextvalue twice.- Without
fn: Iffnis not provided, the props from<FooterSlots.Bottom />are passed directly to component without any transformation, one-to-one. component: This function receives either the transformed props (iffnis used) or the original props and renders the component accordingly.
This flexibility allows you to choose whether to modify props or pass them through unchanged, depending on your use case.
Inserting multiple components into a slot is straightforward. You can call footerSlots.insert.into.Bottom multiple times to add different components. The components will be added in the order in which they are inserted.
Here's how you can insert multiple components into the Bottom slot:
footerSlots.insert.into.Bottom({
component: () => <p>First Component</p>,
});
footerSlots.insert.into.Bottom({
component: () => <p>Second Component</p>,
});In this example:
- The first call to
footerSlots.insert.into.Bottominserts a component that renders<p>First Component</p>. - The second call inserts a component that renders
<p>Second Component</p>.
The components will appear in the order they are inserted, so the rendered output will look like this:
<footer>First Component Second Component</footer>You can control the order in which components are rendered within a slot using the optional order property. By default, components are added in the order they are inserted. However, you can specify a custom order to rearrange them.
Let's build on the previous example and introduce the order property:
footerSlots.insert.into.Bottom({
component: () => <p>First Component</p>,
});
footerSlots.insert.into.Bottom({
component: () => <p>Second Component</p>,
order: 0,
});- In this case, the first call inserts
<p>First Component</p>without anorderproperty, so it gets the default position. - The second call inserts
<p>Second Component</p>and specifiesorder: 0. This causes the "Second Component" to be rendered before the "First Component".
With the order property applied, the rendered output will look like this:
<footer>Second Component First Component</footer>- Type:
orderis always a number. - Default Behavior: If
orderis not provided, the components are rendered in the order they are inserted. - Custom Order: Components with a lower
ordervalue are rendered before those with a higher value. If multiple components have the sameordervalue, they maintain the order of insertion.