Installation
Install the component
templui add sidebarAdd the JavaScript to your layout
@sidebar.Script() @dialog.Script() @popover.Script()Call this template in your base layout file (e.g., in the <head> section).
Persisted State
The sidebar supports persisting its state across page reloads using cookies. When the sidebar state changes, a cookie named sidebar_state is set with the current open/closed state ("true" for expanded or "false" for collapsed).
To persist sidebar state in your Go application and avoid flicker on page loads, read the cookie server-side and set the Collapsed prop:
func MyHandler(w http.ResponseWriter, r *http.Request) {
// Read sidebar state from cookie
cookie, _ := r.Cookie("sidebar_state")
// "false" means collapsed, "true" or no cookie means expanded
collapsed := cookie != nil && cookie.Value == "false"
// Pass to template
component := MyPage(collapsed)
component.Render(context.Background(), w)
}
templ MyPage(collapsed bool) {
@sidebar.Layout() {
@sidebar.Sidebar(sidebar.Props{
Collapsed: collapsed,
}) {
// Sidebar content
}
@sidebar.Inset() {
// Main content
}
}
}Note: The JavaScript will not automatically restore the state from cookies to prevent flicker. Always read the cookie server-side for the best user experience.
Custom Width
The sidebar width is controlled via the CSS variable --sidebar-width (default: 16rem). You can set it to any valid CSS value using the Attributes prop.
Example
@sidebar.Sidebar(sidebar.Props{
Attributes: templ.Attributes{
"style": "--sidebar-width:20rem", // Any CSS unit works: px, %, vw, etc.
},
}) {
// sidebar content
}Note: You can use any valid CSS unit (rem, px, %, vw) or even dynamic values like calc(), min(), or clamp() for responsive designs.
API Reference
Inset
Main content wrapper that adapts to sidebar state.
| Name | Type | Default |
|---|---|---|
Unique identifier for the inset element. | | - |
Additional CSS classes to apply to the inset. | | - |
Additional HTML attributes to apply to the inset element. | | - |
Group
Groups related menu items together with optional label.
| Name | Type | Default |
|---|---|---|
Unique identifier for the group element. | | - |
Additional CSS classes to apply to the group. | | - |
Additional HTML attributes to apply to the group element. | | - |
GroupLabel
Label for a sidebar group, typically used to name sections like 'Platform' or 'Projects'.
| Name | Type | Default |
|---|---|---|
Unique identifier for the group label element. | | - |
Additional CSS classes to apply to the group label. | | - |
Additional HTML attributes to apply to the group label element. | | - |
Separator
Visual separator between sidebar sections.
| Name | Type | Default |
|---|---|---|
Unique identifier for the separator element. | | - |
Additional CSS classes to apply to the separator. | | - |
Additional HTML attributes to apply to the separator element. | | - |