Ship faster with goilerplate templUI Pro

Sidebar

A composable, themeable and customizable sidebar component.

Source Tailwind CSS JavaScript

Installation

  1. Install the component

    templui add sidebar
  2. Add 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

Required parameter
Hover for description

Inset

Main content wrapper that adapts to sidebar state.

Name Type Default
ID
string
-
Class
string
-
Attributes
templ.Attributes
-

Group

Groups related menu items together with optional label.

Name Type Default
ID
string
-
Class
string
-
Attributes
templ.Attributes
-

GroupLabel

Label for a sidebar group, typically used to name sections like 'Platform' or 'Projects'.

Name Type Default
ID
string
-
Class
string
-
Attributes
templ.Attributes
-

Separator

Visual separator between sidebar sections.

Name Type Default
ID
string
-
Class
string
-
Attributes
templ.Attributes
-