Complete guide to making dashboards fully responsive for mobile, tablet, and desktop devices.
The responsive dashboard system provides:
Files:
Dashboard.Responsive.css - All responsive CSS stylesObj.Dashboard.Responsive.pas - TResponsiveManager component| Device | Width | Grid Columns | Sidebar |
|---|---|---|---|
| Desktop | > 1024px | 3 (configured) | Visible |
| Tablet | 768px - 1024px | 2 | Visible |
| Mobile | 480px - 768px | 2 | Burger menu |
| Small Mobile | < 480px | 1 | Burger menu |
/// CSS Variables for breakpoints
:root {
--mobile-breakpoint : 768px;
--tablet-breakpoint : 1024px;
--small-mobile-breakpoint : 480px;
}
Main component that handles responsive behavior.
TYPE
TDeviceMode = (dmDesktop, dmTablet, dmMobile, dmSmallMobile);
TResponsiveManager = CLASS(TComponent)
PUBLIC
PROCEDURE Initialize;
PROCEDURE OpenSidebar;
PROCEDURE CloseSidebar;
PROCEDURE ToggleSidebar;
PROCEDURE CheckDeviceMode;
FUNCTION IsMobile: Boolean;
FUNCTION IsTablet: Boolean;
FUNCTION IsDesktop: Boolean;
FUNCTION GetResponsiveColumns: Integer;
PROPERTY CurrentMode: TDeviceMode READ FCurrentMode;
PROPERTY SidebarOpen: Boolean READ FSidebarOpen;
PUBLISHED
PROPERTY LayoutContainer: TCoreDiv;
PROPERTY Sidebar: TControl;
PROPERTY Canvas: TControl;
PROPERTY ReadOnlyOnMobile: Boolean DEFAULT True;
PROPERTY MobileBreakpoint: Integer DEFAULT 768;
PROPERTY TabletBreakpoint: Integer DEFAULT 1024;
PROPERTY SmallMobileBreakpoint: Integer DEFAULT 480;
PROPERTY OnDeviceModeChanged: TDeviceModeChangedEvent;
END;
Include this CSS file in your application for responsive styles.
| Class | Purpose |
|---|---|
.dashboard-layout | Main container - add to parent of sidebar + canvas |
.mobile-mode | Added automatically when on mobile |
.dashboard-sidebar | Sidebar container |
.sidebar-open | Shows sidebar on mobile |
.burger-menu-btn | Hamburger menu button |
.sidebar-overlay | Dark overlay when menu open |
.dashboard-readonly | Disables editing controls |
/// Tablet: Force 2 columns
@media screen and (max-width: 1024px) {
.dashboard-container {
grid-template-columns: repeat(2, 1fr) !important;
}
}
/// Mobile: 2 columns, padding for burger
@media screen and (max-width: 768px) {
.dashboard-container {
grid-template-columns: repeat(2, 1fr) !important;
padding: 60px 10px 10px 10px;
}
/// Cards span single column only
.dashboard-card {
grid-column: span 1 !important;
}
}
/// Small Mobile: Single column
@media screen and (max-width: 480px) {
.dashboard-container {
grid-template-columns: 1fr !important;
}
}
Add the responsive CSS to your HTML:
<link rel="stylesheet" href="Dashboard.Responsive.css">
<div class="dashboard-layout">
<div class="dashboard-sidebar">
<!-- TSideBar component here -->
</div>
<div class="dashboard-main-content">
<div class="dashboard-container">
<!-- TDashboardCanvas component here -->
</div>
</div>
</div>
PROCEDURE TMainForm.WebFormCreate(Sender: TObject);
BEGIN
FResponsiveManager := TResponsiveManager.Create(Self);
FResponsiveManager.LayoutContainer := pnlMain; /// Main container div
FResponsiveManager.Sidebar := pnlSidebar; /// Sidebar panel
FResponsiveManager.Canvas := DashboardCanvas; /// Dashboard canvas
FResponsiveManager.ReadOnlyOnMobile := True; /// Disable editing on mobile
FResponsiveManager.OnDeviceModeChanged := HandleModeChanged;
FResponsiveManager.Initialize;
END;
PROCEDURE TMainForm.HandleModeChanged(Sender: TObject; NewMode: TDeviceMode);
BEGIN
CASE NewMode OF
dmDesktop:
Console.log('Switched to desktop mode');
dmTablet:
Console.log('Switched to tablet mode');
dmMobile, dmSmallMobile:
Console.log('Switched to mobile mode');
END;
END;
When ReadOnlyOnMobile is True (default), the following is disabled on mobile:
.dashboard-readonly .card-resize-handle {
display: none !important;
}
.dashboard-readonly .dashboard-card {
cursor: default !important;
}
.dashboard-readonly .btn-expand,
.dashboard-readonly .btn-shrink,
.dashboard-readonly .btn-delete {
display: none !important;
}
/// Check if currently mobile
IF ResponsiveManager.IsMobile THEN
ShowMessage('You are on a mobile device');
/// Get recommended columns for current mode
Columns := ResponsiveManager.GetResponsiveColumns;
DashboardCanvas.SpanAcross := Columns;
!important flags to override inline styles set by JavaScript. This ensures the responsive grid rules take precedence on mobile devices.