Purpose: Manages sliding drawer/sidebar components with position control, backdrop handling, and swipe gestures.

Features

  • Four placement positions (left, right, top, bottom)
  • Backdrop and body scrolling control
  • Edge positioning with offset
  • Auto-show capability
  • Proper cleanup on disconnect

Static Values

static values = {
  autoShow: false,          // Boolean - Auto show drawer on connect
  placement: 'left',        // String - Drawer position ('left', 'right', 'top', 'bottom')
  backdrop: true,           // Boolean - Show backdrop
  bodyScrolling: false,     // Boolean - Allow body scrolling when open
  edge: false,              // Boolean - Position at screen edge
  edgeOffset: String,       // String - Offset from edge
  backdropClasses: 'bg-gray-900/50 dark:bg-gray-900/80 fixed inset-0 z-30',
  onHide: Object,           // Object - Hide callback
  onShow: Object,           // Object - Show callback
  onToggle: Object          // Object - Toggle callback
}

Targets

  • drawer: Drawer elements to control

Actions

  • show: Show the drawer
  • hide: Hide the drawer
  • toggle: Toggle drawer visibility

Usage Examples

Basic Left Drawer

<%= fx_drawer(id: "nav-drawer", data: { controller: "fx-drawer" }) do %>
  <nav class="p-4">
    <ul>
      <li><a href="/">Home</a></li>
      <li><a href="/about">About</a></li>
      <li><a href="/contact">Contact</a></li>
    </ul>
  </nav>
<% end %>

<%= fx_button(data: { action: "fx-drawer#toggle", "fx-drawer-id": "nav-drawer" }) do %>
  Menu
<% end %>

Right Drawer with Custom Settings

<%= fx_drawer(
  id: "settings-drawer",
  data: {
    controller: "fx-drawer",
    "fx-drawer-placement-value": "right",
    "fx-drawer-backdrop-value": false,
    "fx-drawer-body-scrolling-value": true
  }
) do %>
  <div class="p-6">
    <h3>Settings</h3>
    <!-- Settings form -->
  </div>
<% end %>

Bottom Drawer with Edge Positioning

<%= fx_drawer(
  id: "bottom-sheet",
  data: {
    controller: "fx-drawer",
    "fx-drawer-placement-value": "bottom",
    "fx-drawer-edge-value": true,
    "fx-drawer-edge-offset-value": "64px"
  }
) do %>
  <div class="p-4">
    <h4>Quick Actions</h4>
    <!-- Action buttons -->
  </div>
<% end %>

Event System

// Custom events
document.addEventListener("showDrawer:my-drawer", () => {
  // Drawer will be shown
});

document.dispatchEvent(new CustomEvent("hideDrawer:my-drawer"));