Slots provide an alternative way for defining component structures within Store Framework themes.
Slots composition is an alternative paradigm for defining component structures within the Store Framework themes. Unlike the conventional block composition method, slots enable developers to pass React props directly to components, providing enhanced flexibility in storefront design.
When using slots, consider the following:
- The requirement for the
blocks
attribute is eliminated when implementing components that utilize slots in the Store Theme. Instead, developers can pass the desired blocks as regularprops
. - Slots grant the ability to include any block from any app as a regular prop. Hence, unlike the blocks composition method, declaring the
allowed
interface attribute is not necessary.
Blocks vs. Slots
To better understand the distinctions between blocks and slots, let's delve into the specifics of each method using a hypothetical hello-world
component.
Blocks
When using the blocks composition, the focus revolves around defining allowed
blocks and structuring their arrangement. In this approach, the blocks
attribute specifies which blocks can be used within the hello-world
component. Consider the following example:
HelloWorld.tsx | interfaces.json | blocks.json |
---|---|---|
|
|
|
Note that, in this example, the icon-caret
block is the only child block allowed for hello-world
. Other limitations of this method include:
- The
hello-world
block only accepts theicon-caret
block as a child. Any othericon-*
block or any block beyond that scope is incompatible, necessitating the explicit allowance of each distinct block in theinterfaces.json
file. - Implementing the block-based approach within
HelloWorld.tsx
involves conditional logic to identify the block passed by the developer and subsequently convey its ID to theExtensionPoint
component. - Passing multiple instances of the same block within the
HelloWorld
component may lead to rendering problems. For example, passing and rendering bothicon-caret#point-up
andicon-caret#point-down
in theblocks.json
file would lead to an ambiguous scenario.
Slots
Now, let's reimagine the same scenario using slots composition:
HelloWorld.tsx | interfaces.json | blocks.json |
---|---|---|
|
|
|
Note that, by directly passing required components as props
, the slots approach eliminates the need for the allowed
attribute. This method introduces flexibility into the component integration process, allowing any component to be included as a prop without prior restrictions.
This example demonstrates how slots composition simplifies the component declaration process and enhances flexibility. You can adjust the composition by passing different components as props, making the structure more adaptable to your needs.
To ensure consistent and effective usage of slots, it is important to adhere to the following guidelines:
- Slots are always exposed via
PascalCased
props.
✅ Do | ❌ Don't |
---|---|
|
|
- Nested slots are not supported. This means that you can't have a slot prop inside of an object, such as shown below:
_12{_12 "props": {_12 "somePropThatsAnObject": {_12 "p1": "this",_12 "p2": "will",_12 "p3": "not",_12 "p4": "work",_12 // This will NOT be picked up by the store builder_12 "Slot": "some-block"_12 }_12 }_12}