You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit fed17f8
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: docs/components/compositions/index.md
+14-27Lines changed: 14 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,9 +5,7 @@ description: Build complex UIs by combining components through nesting and slots
5
5
6
6
# Compositions
7
7
8
-
Component composition in Nordcraft refers to combining components in various ways to create more complex interfaces. By using different composition techniques, you can build flexible, maintainable, and reusable user interfaces.
9
-
10
-
The primary composition patterns in Nordcraft include:
8
+
Component composition in Nordcraft refers to combining components in various ways. The primary composition patterns in Nordcraft include:
11
9
12
10
-[Nesting components](#nesting-components): Adding components inside other components
13
11
-[Slots](#slots): Creating placeholders that can be filled with external content
@@ -32,42 +30,35 @@ This example shows two different ways to build a component:
32
30
33
31
- Provides empty placeholder areas (slots)
34
32
- You must manually add elements into slots
35
-
- Different instances can contain different elements
33
+
- Different component instances can contain different elements
36
34
37
35
## Nesting components
38
36
39
-
Nesting is the most basic form of component composition, where you add components inside other components:
37
+
Nesting is where you add components inside other components:
40
38
41
39
1. Open a component in the editor
42
40
2. Add another component from the [element catalog](/the-editor/element-tree#element-catalog)
43
41
3. Configure the nested component's attributes as needed
44
42
45
43
Benefits of nesting include:
46
44
47
-
- Breaking complex interfaces into manageable pieces
45
+
- Breaking more complex interfaces into manageable pieces
48
46
- Reusing functionality at different levels of your project
49
47
- Creating clear relationships between UI elements
50
48
51
-
### Nesting considerations
52
-
53
49
When nesting components, consider the following:
54
50
55
51
-**Responsibility boundaries**: Each component should have a clear, focused responsibility
56
52
-**Data flow**: Parent components pass data down to child components through [attributes](/components/interface-and-lifecycle#defining-attributes) or [contexts](/contexts/overview). Child components send data up to parents through [events](/components/interface-and-lifecycle#adding-events) or [contexts](/contexts/overview)
57
53
-**Styling**: Parent components can [style the root element](/styling/conditional-styles#component-style-overrides) of nested components
58
54
59
55
::: tip
60
-
As your project grows, you'll naturally develop deeper component hierarchies. While there's no strict limit to nesting depth, it's good practice to maintain clear boundaries of responsibility between components to ensure maintainability.
56
+
As your project grows, you may develop deeper component hierarchies (e.g. components within comonents within components). While there's no strict limit to nesting depth, it's good practice to maintain clear boundaries of responsibility between components to ensure maintainability.
61
57
:::
62
58
63
59
## Slots
64
60
65
-
Slots are a powerful feature in Nordcraft that allow components to receive and display content provided when the component is used. They act as placeholders that can be filled with content passed into the component from the outside.
66
-
67
-
- Slots build upon native web standards
68
-
- Slots create areas in your component that can be filled with external content
69
-
- Slots allow for flexible, composable component architectures
70
-
- Slots help maintain component encapsulation while enabling customization
61
+
Components have access to a special element called a slot, which you can find in the element catalogue. Slots allow you to create placeholders for child content of components. This means that when you use the component in an element tree, you can nest other elements inside that component in situ and render them in a specific area in the component, rather than having to set up a complex system of attributes and configuration inside the component to decide what goes where.
71
62
72
63
::: info
73
64
The slot element in Nordcraft follows the web components standard. For more technical details, see the [MDN documentation on the slot element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/slot).
@@ -79,20 +70,20 @@ When [exporting a component](/components/export-a-component) as a web component,
79
70
80
71
### Adding slots to components
81
72
82
-
Slots are added to components like any other element using the [element catalog](/the-editor/element-tree#adding-elements):
73
+
You can add slots to components using the [element catalog](/the-editor/element-tree#adding-elements):
83
74
84
-
1. Select where you want to add the slot in your component
75
+
1. Select where you want to add the slot in your component in the element tree
85
76
2. Add a slot element from the [element catalog](/the-editor/element-tree#adding-elements)
86
77
3. Optional: add a name attribute (without a name, the slot is named `default`)
87
78
88
79
{https://editor.nordcraft.com/projects/docs_examples/branches/main/components/screenshot-card-container?canvas-width=800&rightpanel=attributes&selection=nodes.Iry7BeAO7AjcLcJEL-sPT&canvas-height=800}
89
80
90
81
### Naming
91
82
92
-
Slots in Nordcraft work with a naming system:
83
+
Slots have a naming system. To give a slot a name, select it in the element tree, and name the slot in the right panel in the editor.
93
84
94
-
-**Default slot**: A slot without a name attribute that receives any unassigned content
95
-
-**Named slot**: A slot with a name attribute that receives content specifically targeted to it
85
+
-**Default slot**: displays any child content of that component
86
+
-**Named slot**: receives any child content specifically targeted to it.
96
87
97
88
{https://editor.nordcraft.com/projects/docs_examples/branches/main/components/screenshot-card?canvas-width=800&rightpanel=attributes&selection=nodes.47Ndqw5Wmw5WAwpiV13p8&canvas-height=800}
98
89
@@ -108,17 +99,13 @@ Elements without a specified slot are placed in the `default` slot
108
99
Slots can contain default content that appears when no content is provided. This default content:
109
100
110
101
- Is shown when no matching content is provided
111
-
- Gets replaced when matching content is provided
112
-
- Provides visual cues during development
102
+
- Is replaced when matching content is provided to that slot
113
103
114
104
{https://editor.nordcraft.com/projects/docs_examples/branches/main/components/screenshot-card-container?canvas-width=800&rightpanel=events&selection=nodes.Iry7BeAO7AjcLcJEL-sPT&canvas-height=800}
115
105
116
106
## Comparing composition approaches
117
107
118
-
Both nested components and slot-based assembly involve nesting components within other components. The key difference is in _how_ and _when_ this composition happens:
119
-
120
-
- With nesting, composition happens when creating the component
121
-
- With slots, composition happens when using the component
108
+
Both nested components and slot-based assembly involve nesting components within other components.
122
109
123
110
**When to use fixed nested components:**
124
111
@@ -134,4 +121,4 @@ Both nested components and slot-based assembly involve nesting components within
134
121
- You want to allow completely different elements in each instance
135
122
- You need to compose from existing components rather than building everything from scratch
136
123
137
-
Many complex components combine both approaches, using nested components for core functionality, and slots for customizable areas.
124
+
You can also combine both approaches, using nested components for core functionality, and slots for customizable areas.
0 commit comments