terminalCode Example
Razor Panel Structure and Syntax
Razor Panel Structure
s&box uses Razor syntax for UI panels with .razor files and optional .razor.scss stylesheets.
Basic Panel Component
CSHARP
// MyHud.razor
@inherits PanelComponent
<root>
<div class="health-bar">
<div class="fill" style="width: @(HealthPercent)%"></div>
</div>
<label>@PlayerName</label>
</root>
@code {
[Property] public float HealthPercent { get; set; }
[Property] public string PlayerName { get; set; }
}File Structure
CODE
Code/
UI/
MyHud.razor // Panel markup
MyHud.razor.scss // Auto-linked stylesheet
HealthBar.razor // Child panelThe <root> Element
The <root> element is required and becomes the PanelComponent root:
RAZOR
<root>
<div class="container">
<label>Health: @Health</label>
</div>
</root>If no <root> is present, all elements become children of the panel root automatically.
Child Panels
Create child panel classes that inherit from Panel:
CSHARP
// HealthBar.razor
@inherits Panel
<root>
<div class="bar">
<div class="fill" style="width: @(Percent)%"></div>
</div>
<label>@Label</label>
</root>
@code {
public float Percent { get; set; }
public string Label { get; set; }
}Use in parent panel:
CSHARP
// MyHud.razor
@inherits PanelComponent
<root>
<HealthBar Percent="@Health" Label="@"HP"" />
</root>Conditional Rendering
RAZOR
<root>
@if (IsAlive)
{
<div class="hud">
<label>Health: @Health</label>
</div>
}
else
{
<div class="death-screen">
<label>You Died</label>
<button @onclick="@Respawn">Respawn</button>
</div>
}
</root>Lists
RAZOR
<root>
<div class="inventory">
@foreach (var item in InventoryItems)
{
<div class="slot" @onclick="@(() => SelectItem(item))">
<label>@item.Name</label>
</div>
}
</div>
</root>
Was this helpful?