Fragments
A Fragment is a special type of widget designed to implement flexible caching and handle parts of the UI that can be created in advance and do not require data at runtime. Using this type of widget allows for effective decomposition of large screens and optimization of application performance.
What Fragment Is For
Fragment provides a mechanism for pre-creating and caching reusable UI elements. The main advantages are:
- Performance: Pre-built fragments do not require repeated processing
- Decomposition: Simplifies the structure of large screens by breaking them down into reusable parts
- Caching: Static parts of the UI are created once and used multiple times
- Optimization: Reduces runtime load by shifting processing to the build stage
How to Use Fragment
Fragment uses two parameters:
tag(required) - unique fragment identifierid(optional) - widget instance identifier
Fragments must be registered in DuitRegistry before use. When processing the widget, the Fragment will be replaced with the content associated with the specified tag.
- TypeScript
- Go
import { DuitView, Column, Container, Text, Fragment } from "duit_js";
function headerFragment() {
return Container({
attributes: {
color: "#4285F4",
padding: 16,
}
}).addChild(
Text({
attributes: {
data: "Welcome to Duit!",
style: {
fontSize: 24,
color: "#FFFFFF",
}
}
})
);
}
function mainScreen() {
const builder = DuitView.builder();
// Регистрируем фрагмент
builder.addComponents([
{
tag: "header",
layoutRoot: headerFragment()
}
]);
const root = builder.createRootOfExactType(DuitElementType.column, {
mainAxisAlignment: "start"
});
root.addChildren([
// Используем зарегистрированный фрагмент
Fragment("header", "header-1"),
Container({
attributes: {
padding: 16,
}
}).addChild(
Text({
attributes: {
data: "Main content goes here",
}
})
)
]);
return builder.build();
}
package main
import (
"github.com/Duit-Foundation/duit_go/v4/pkg/duit_widget"
"github.com/Duit-Foundation/duit_go/v4/pkg/duit_attributes"
)
func headerFragment() *duit_core.DuitElementModel {
return duit_widget.Container(
&duit_attributes.ContainerAttributes{
Color: "#4285F4",
Padding: &duit_attributes.EdgeInsets{
Left: 16,
Right: 16,
Top: 16,
Bottom: 16,
},
},
"header_container",
).AddChild(
duit_widget.Text(
&duit_attributes.TextAttributes{
Data: "Welcome to Duit!",
Style: &duit_attributes.TextStyle{
FontSize: 24,
Color: "#FFFFFF",
},
},
"header_text",
),
)
}
func mainScreen() string {
builder := DuitView.Builder()
root := builder.CreateRootOfExactType(duit_core.Column, &duit_attributes.ColumnAttributes{
MainAxisAlignment: duit_props.MainAxisAlignmentStart,
}, "root")
root.AddChildren(
// Используем зарегистрированный фрагмент
duit_widget.Fragment("header", "header-1"),
duit_widget.Container(
&duit_attributes.ContainerAttributes{
Padding: &duit_attributes.EdgeInsets{
All: 16,
},
},
"content_container",
).AddChild(
duit_widget.Text(
&duit_attributes.TextAttributes{
Data: "Main content goes here",
},
"content_text",
),
),
)
// Регистрируем фрагмент
embedded := []*ComponentDescription{
{
Tag: "header",
LayoutRoot: headerFragment(),
},
}
builder.SetEmbedded(embedded)
return builder.Build()
}
Fragment Registration
Fragments must be registered before they can be used in the widget tree. This is done through the embedded field in the server response structure.
Data Structure
A fragment is described by an object with the following fields:
tag- unique fragment identifierlayoutRoot- widget structure contained within the fragment
Example with Multiple Fragments
- TypeScript
- Go
import {
DuitView,
Column,
Row,
Container,
Text,
SizedBox,
Fragment,
DuitElementType
} from "duit_js";
function cardFragment() {
return Container({
attributes: {
color: "#F5F5F5",
borderRadius: 8,
padding: 12,
boxShadow: [{
color: "#CCCCCC",
blurRadius: 4,
}]
}
}).addChild(
Column({
attributes: {
crossAxisAlignment: "start",
}
}).addChildren([
Text({
attributes: {
data: "Card Title",
style: { fontSize: 18, fontWeight: 600 }
}
}),
SizedBox({ attributes: { height: 8 } }),
Text({
attributes: {
data: "Card description goes here",
style: { fontSize: 14, color: "#666666" }
}
})
])
);
}
function sidebarFragment() {
return Container({
attributes: {
width: 200,
color: "#2C3E50",
padding: 16,
}
}).addChild(
Column({
attributes: {
mainAxisAlignment: "start",
}
}).addChildren([
Text({
attributes: {
data: "Navigation",
style: {
fontSize: 20,
fontWeight: 700,
color: "#FFFFFF"
}
}
}),
SizedBox({ attributes: { height: 16 } }),
// ... navigation items
])
);
}
function complexLayout() {
const builder = DuitView.builder();
// Регистрируем несколько фрагм ентов
builder.addComponents([
{
tag: "card",
layoutRoot: cardFragment()
},
{
tag: "sidebar",
layoutRoot: sidebarFragment()
}
]);
const root = builder.createRootOfExactType(
DuitElementType.row,
{}
);
root.addChildren([
// Используем фрагмент сайдбара
Fragment("sidebar", "sidebar-1"),
// Используем фрагмент карточки
Fragment("card", "card-1"),
SizedBox({ attributes: { width: 16 } }),
Fragment("card", "card-2"),
]);
return builder.build();
}
package main
import (
"github.com/Duit-Foundation/duit_go/v4/pkg/duit_widget"
"github.com/Duit-Foundation/duit_go/v4/pkg/duit_attributes"
"github.com/Duit-Foundation/duit_go/v4/pkg/duit_props"
)
func cardFragment() *duit_core.DuitElementModel {
return duit_widget.Container(
&duit_attributes.ContainerAttributes{
Color: "#F5F5F5",
BorderRadius: &duit_attributes.BorderRadius{
All: 8,
},
Padding: &duit_attributes.EdgeInsets{
All: 12,
},
BoxShadow: []*duit_attributes.BoxShadow{
{
Color: "#CCCCCC",
BlurRadius: 4,
},
},
},
"card_container",
).AddChild(
duit_widget.Column(
&duit_attributes.ColumnAttributes{
CrossAxisAlignment: duit_props.CrossAxisAlignmentStart,
},
"card_column",
).AddChildren(
duit_widget.Text(
&duit_attributes.TextAttributes{
Data: "Card Title",
Style: &duit_attributes.TextStyle{
FontSize: 18,
FontWeight: 600,
},
},
"card_title",
),
duit_widget.SizedBox(
&duit_attributes.SizedBoxAttributes{
Height: 8,
},
"card_spacer",
),
duit_widget.Text(
&duit_attributes.TextAttributes{
Data: "Card description goes here",
Style: &duit_attributes.TextStyle{
FontSize: 14,
Color: "#666666",
},
},
"card_description",
),
),
)
}
func sidebarFragment() *duit_core.DuitElementModel {
return duit_widget.Container(
&duit_attributes.ContainerAttributes{
Width: 200,
Color: "#2C3E50",
Padding: &duit_attributes.EdgeInsets{
All: 16,
},
},
"sidebar_container",
).AddChild(
duit_widget.Column(
&duit_attributes.ColumnAttributes{
MainAxisAlignment: duit_props.MainAxisAlignmentStart,
},
"sidebar_column",
).AddChildren(
duit_widget.Text(
&duit_attributes.TextAttributes{
Data: "Navigation",
Style: &duit_attributes.TextStyle{
FontSize: 20,
FontWeight: 700,
Color: "#FFFFFF",
},
},
"sidebar_title",
),
// ... navigation items
),
)
}
func complexLayout() string {
builder := DuitView.Builder()
// Регистрируем несколько фрагментов
embedded := []*ComponentDescription{
{
Tag: "card",
LayoutRoot: cardFragment(),
},
{
Tag: "sidebar",
LayoutRoot: sidebarFragment(),
},
}
builder.SetEmbedded(embedded)
root := builder.CreateRootOfExactType(
duit_core.Row,
&duit_attributes.RowAttributes{},
"root",
)
root.AddChildren(
// Используем фрагмент сайдбара
duit_widget.Fragment("sidebar", "sidebar-1"),
// Используем фрагмент карточки
duit_widget.Fragment("card", "card-1"),
duit_widget.SizedBox(
&duit_attributes.SizedBoxAttributes{Width: 16},
"spacer",
),
duit_widget.Fragment("card", "card-2"),
)
return builder.Build()
}
Important Notes
-
Tag is Required: The
tagparameter is mandatory. If it is not specified, the function will cause a panic (Go) or an error (TypeScript). -
Registration Before Use: Fragments must be registered before they are used in the widget tree.
-
Reusability: The same fragment can be used multiple times with different
ids. -
Static Content: Fragment is intended for static content. For dynamic data, use Component.
Differences Between Fragment and Component
| Characteristic | Fragment | Component |
|---|---|---|
| Data | Static content | Dynamic content |
| Parameters | Only tag and id | tag, id and data |
| Usage | Caching and decomposition | Reusability with data |
When to Use Fragment
Fragment should be used when:
- You have a static UI part that repeats on the screen
- You need to optimize performance through preprocessing
- You need to decompose a large screen into reusable parts
- The content does not depend on runtime data
For dynamic components with data, use Component.