Drawer (Offcanvas)
The Drawer component creates sliding side panels for navigation menus, settings, and additional content. Perfect for mobile-friendly navigation and contextual information displays.
Goal
Master creating drawer panels, understand Bootstrap offcanvas classes, and build responsive navigation that works beautifully on all screen sizes.
Bootstrap Reference
Quick Start
from faststrap import Drawer, Button
# Button to trigger drawer
Button(
"Open Menu",
variant="primary",
data_bs_toggle="offcanvas",
data_bs_target="#myDrawer"
)
# Drawer component
Drawer(
ListGroup(
ListGroupItem("Home", href="/"),
ListGroupItem("About", href="/about"),
ListGroupItem("Contact", href="/contact")
),
drawer_id="myDrawer",
title="Navigation",
placement="start" # Left side
)
Visual Examples & Use Cases
1. Placements - Four Directions
# Left drawer (default)
Drawer(content, drawer_id="left", title="Left Menu", placement="start")
# Right drawer
Drawer(content, drawer_id="right", title="Right Menu", placement="end")
# Top drawer
Drawer(content, drawer_id="top", title="Top Panel", placement="top")
# Bottom drawer
Drawer(content, drawer_id="bottom", title="Bottom Panel", placement="bottom")
2. Mobile Navigation Menu
from faststrap import Drawer, ListGroup, ListGroupItem, Icon, Button
# Trigger button
Button(
Icon("list"),
variant="outline-primary",
data_bs_toggle="offcanvas",
data_bs_target="#mobileNav"
)
# Drawer
Drawer(
ListGroup(
ListGroupItem(Icon("house"), " Home", href="/", action=True),
ListGroupItem(Icon("grid"), " Products", href="/products", action=True),
ListGroupItem(Icon("info-circle"), " About", href="/about", action=True),
ListGroupItem(Icon("envelope"), " Contact", href="/contact", action=True),
flush=True
),
drawer_id="mobileNav",
title="Menu",
placement="start"
)
3. Settings Panel
Drawer(
H5("Preferences"),
Switch("dark_mode", label="Dark Mode"),
Switch("notifications", label="Notifications"),
Select(
"language",
("en", "English"),
("es", "Spanish"),
label="Language"
),
Button("Save", variant="primary", cls="w-100 mt-3"),
drawer_id="settings",
title="Settings",
placement="end"
)
Focus Trap (Accessibility)
Trap keyboard focus inside the drawer and optionally set autofocus:
Drawer(
"Drawer content",
drawer_id="settings",
title="Settings",
focus_trap=True,
autofocus_selector="#first-input",
)
Bootstrap CSS Classes Explained
| Class | Purpose |
|---|---|
.offcanvas |
Base drawer container |
.offcanvas-start |
Left placement |
.offcanvas-end |
Right placement |
.offcanvas-top |
Top placement |
.offcanvas-bottom |
Bottom placement |
.offcanvas-header |
Drawer header |
.offcanvas-body |
Drawer content |
.offcanvas-title |
Title text |
Parameter Reference
| Parameter | Type | Default | Description |
|---|---|---|---|
*children |
Any |
Required | Drawer body content |
drawer_id |
str \| None |
Auto-generated | Unique ID for drawer |
title |
str \| None |
None |
Drawer header title |
placement |
"start" \| "end" \| "top" \| "bottom" |
"start" |
Drawer position |
backdrop |
bool \| None |
True |
Show backdrop overlay |
scroll |
bool \| None |
False |
Allow body scroll when open |
dark |
bool \| None |
False |
Dark variant |
**kwargs |
Any |
- | Additional HTML attributes |
faststrap.components.navigation.drawer.Drawer(*children, drawer_id=None, title=None, footer=None, placement=None, backdrop=None, scroll=None, dark=None, focus_trap=None, autofocus_selector=None, header_cls=None, body_cls=None, footer_cls=None, title_cls=None, close_cls=None, **kwargs)
Bootstrap Offcanvas (Drawer) component for side panels and menus.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*children
|
Any
|
Drawer body content |
()
|
drawer_id
|
str | None
|
Unique ID for the drawer (required for Bootstrap JS) |
None
|
title
|
str | None
|
Drawer header title |
None
|
footer
|
Any | None
|
Drawer footer content |
None
|
placement
|
PlacementType | None
|
Drawer position (start=left, end=right, top, bottom) |
None
|
backdrop
|
bool | None
|
Show backdrop overlay |
None
|
scroll
|
bool | None
|
Allow body scroll when drawer is open |
None
|
dark
|
bool | None
|
Use dark variant (Bootstrap 5.3+) |
None
|
focus_trap
|
bool | None
|
Trap keyboard focus inside the drawer |
None
|
autofocus_selector
|
str | None
|
CSS selector for the element to autofocus |
None
|
**kwargs
|
Any
|
Additional HTML attributes (cls, hx-, data-, etc.) |
{}
|
Returns:
| Type | Description |
|---|---|
Div
|
FastHTML Div element with offcanvas structure |
Source code in src/faststrap/components/navigation/drawer.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | |