Sidebar

Navigation that slides in from the side
Includes a Ruby component and corresponding Rails helper
Ruby
This is a paid-only premium component
Premium
Includes a Stimulus controller
Stimulus

A flexible sidebar navigation component with mobile overlay behavior. Use it to build application navigation with sections, user controls, and smooth animations that automatically adapts between desktop and mobile patterns.

Basic Example

Usage

nk_sidebar_layout(title: nil, **attrs) { |sidebar| ... }
PropertyDefaultDescription
titleString Optional default title for the title bar. Can be overridden with content_for(:title).
**attrsHTML attributes for <div> element

Layout Methods

sidebar.sidebar { |s| ... }
PropertyDefaultDescription
blockProc Content for the sidebar navigation area. Use sidebar builder methods within this block.
sidebar.main { |m| ... }
PropertyDefaultDescription
blockProc Content for the main content area. Automatically handles responsive padding for sidebar.
sidebar.title_bar(**attrs) { ... }
PropertyDefaultDescription
blockProc Optional content for the title bar. Includes hamburger menu for mobile.
**attrsHTML attributes for <nav> element

Sidebar Builder Methods

sidebar.sidebar_item_to(*args, active: false, **attrs) { ... }
PropertyDefaultDescription
argslink_to arguments Standard Rails link_to arguments (text, path, options).
activeBoolean Whether this item represents the current active page.
**attrsHTML attributes for <button> element
sidebar.sidebar_section { ... }
PropertyDefaultDescription
blockProc Groups navigation items with consistent spacing.
sidebar.sidebar_space
PropertyDefaultDescription
Flexible spacer to push subsequent items to bottom of sidebar.

Content Areas

The sidebar layout supports several content areas that can be customized:

  • title_bar_left_action: Content before the title (use content_for)
  • title_bar_right_action: Content after the title (use content_for)
  • title: Override the default title text (use content_for)
<% content_for :title_bar_left_action do %>
  <%= nk_button "Back", variant: :ghost, size: :sm %>
<% end %>

<% content_for :title, "Custom Page Title" %>

<% content_for :title_bar_right_action do %>
  <%= nk_button "Save", size: :sm %>
<% end %>

Responsive Design

The sidebar component automatically handles responsive behavior:

  • Desktop (lg+): Sidebar permanently visible, content has left padding
  • Mobile: Sidebar hidden by default, slides in as overlay when opened
  • Backdrop: Semi-transparent backdrop on mobile prevents interaction with main content
  • Click Outside: Automatically closes sidebar when clicking outside on mobile
  • Keyboard Navigation: Proper ARIA labels and keyboard accessibility

CSS Custom Properties

The sidebar uses CSS custom properties for consistent spacing:

  • --sidebar-width: Width of the sidebar (default: 18rem/288px)
  • --layout-padding: Internal content padding (responsive: 1.25rem/2rem)

Override these in your CSS for custom sizing:

:root { --sidebar-width: 20rem; --layout-padding: 3rem; }