-
Notifications
You must be signed in to change notification settings - Fork 225
[UI] Add DashboardLayout and WidgetPicker components #1706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
NSTKrishna
wants to merge
6
commits into
layer5io:master
Choose a base branch
from
NSTKrishna:feat/dashboard-layout-mobile-responsive
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8ce6b10
Add DashboardLayout and WidgetPicker components
NSTKrishna c1fd1f8
Call onClose on drawer close; tighten WidgetPicker types
NSTKrishna 7d0b48d
Fix mobile drawer visibility
NSTKrishna 5a65f9d
Fix mobile dashboard drawer behavior
NSTKrishna 7acdc32
Remove DashboardLayout onClose prop
NSTKrishna eaba5c9
Merge branch 'master' into feat/dashboard-layout-mobile-responsive
Rajesh-Nagarajan-11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| import React, { useState, useEffect } from 'react'; | ||
| import { Box } from '../../base'; | ||
| import { useTheme, useMediaQuery } from '../../theme'; | ||
| import { SwipeableDrawer } from '@mui/material'; | ||
|
|
||
| export interface DashboardLayoutProps { | ||
| /** The main dashboard content (typically the React-Grid-Layout) */ | ||
| children: React.ReactNode; | ||
|
|
||
| /** Whether the right-hand sidebar should be visible */ | ||
| isSidebarOpen: boolean; | ||
|
|
||
| /** The content to render inside the sidebar (e.g., Widget Gallery) */ | ||
| sidebarContent: React.ReactNode; | ||
|
|
||
| /** Optional custom width for the sidebar. Defaults to responsive width. */ | ||
| sidebarWidth?: string | number | Partial<Record<'xs' | 'sm' | 'md' | 'lg' | 'xl', string | number>>; | ||
|
|
||
| /** Optional sticky top offset for the sidebar (useful if page has a top navbar) */ | ||
| sidebarTopOffset?: string | number; | ||
|
|
||
| /** Optional fixed height for the sticky sidebar. Defaults to 100vh */ | ||
| sidebarHeight?: string | number; | ||
|
|
||
|
|
||
| } | ||
|
|
||
| export const DashboardLayout: React.FC<DashboardLayoutProps> = ({ | ||
| children, | ||
| isSidebarOpen, | ||
| sidebarContent, | ||
| sidebarWidth = { xs: '100%', md: '350px' }, | ||
| sidebarTopOffset = '0', | ||
| sidebarHeight = '100vh', | ||
| }) => { | ||
| const theme = useTheme(); | ||
| // We use the 'md' breakpoint (900px default) to switch between mobile and desktop layout | ||
| const isMobile = useMediaQuery(theme.breakpoints.down('md')); | ||
|
|
||
| const [isMobileDrawerOpen, setIsMobileDrawerOpen] = useState(false); | ||
| const drawerBleeding = 56; | ||
|
|
||
| useEffect(() => { | ||
| if (isSidebarOpen) { | ||
| setIsMobileDrawerOpen(true); | ||
| } else { | ||
| setIsMobileDrawerOpen(false); | ||
| } | ||
| }, [isSidebarOpen]); | ||
|
|
||
| return ( | ||
| <Box sx={{ display: 'flex', flexDirection: 'row', gap: '1rem', width: '100%' }}> | ||
| <Box sx={{ flex: 1, padding: 0, minWidth: 0 }}> | ||
| {children} | ||
| </Box> | ||
|
|
||
| {isMobile && ( | ||
| <> | ||
| <SwipeableDrawer | ||
| anchor="bottom" | ||
| open={isMobileDrawerOpen} | ||
| onClose={() => { | ||
| setIsMobileDrawerOpen(false); | ||
| }} | ||
| onOpen={() => setIsMobileDrawerOpen(true)} | ||
|
NSTKrishna marked this conversation as resolved.
|
||
| swipeAreaWidth={isSidebarOpen ? drawerBleeding : 0} | ||
| disableSwipeToOpen={false} | ||
| ModalProps={{ | ||
| keepMounted: true, | ||
| }} | ||
| sx={{ | ||
| '& .MuiPaper-root': { | ||
| height: `calc(50% - ${drawerBleeding}px)`, | ||
| overflow: 'visible', | ||
| }, | ||
| '& .MuiDrawer-paper': { | ||
| borderTopLeftRadius: '16px', | ||
| borderTopRightRadius: '16px', | ||
| }, | ||
| }} | ||
| > | ||
| <Box | ||
| sx={{ | ||
| position: 'absolute', | ||
| top: -drawerBleeding, | ||
| borderTopLeftRadius: 16, | ||
| borderTopRightRadius: 16, | ||
| visibility: 'visible', | ||
| right: 0, | ||
| left: 0, | ||
| backgroundColor: theme.palette.background.paper, | ||
| height: drawerBleeding, | ||
| display: 'flex', | ||
| alignItems: 'center', | ||
| justifyContent: 'center', | ||
| borderBottom: `1px solid ${theme.palette.divider}`, | ||
| cursor: 'pointer', | ||
| }} | ||
| onClick={() => { | ||
| const nextState = !isMobileDrawerOpen; | ||
| setIsMobileDrawerOpen(nextState); | ||
| }} | ||
| > | ||
| <Box | ||
| sx={{ | ||
| width: 30, | ||
| height: 6, | ||
| backgroundColor: theme.palette.mode === 'light' ? '#e0e0e0' : '#424242', | ||
| borderRadius: 3, | ||
| }} | ||
| /> | ||
| </Box> | ||
| <Box sx={{ px: 2, pb: 2, height: '100%', overflow: 'auto' }}> | ||
| {sidebarContent} | ||
| </Box> | ||
| </SwipeableDrawer> | ||
| </> | ||
| )} | ||
|
|
||
| {isSidebarOpen && !isMobile && ( | ||
| <Box | ||
| sx={{ | ||
| width: sidebarWidth, | ||
| flexShrink: 0, | ||
| position: 'sticky', | ||
| top: sidebarTopOffset, | ||
| alignSelf: 'flex-start', | ||
| height: sidebarHeight, | ||
| maxHeight: sidebarHeight, | ||
| }} | ||
| > | ||
| {sidebarContent} | ||
| </Box> | ||
| )} | ||
| </Box> | ||
| ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| import React from 'react'; | ||
| import { Box, IconButton, Stack, Typography } from '../../base'; | ||
| import { AddIcon, CloseIcon } from '../../icons'; | ||
| import { useTheme } from '../../theme'; | ||
| import { SxProps, Theme } from '@mui/material'; | ||
|
|
||
| export interface WidgetItem { | ||
| key: string; | ||
| title: string; | ||
| thumbnail?: string; | ||
| [key: string]: unknown; // Allow passing extra widget properties | ||
| } | ||
|
|
||
| export interface WidgetPickerProps { | ||
| /** The list of widgets available to add */ | ||
| widgetsToAdd: WidgetItem[]; | ||
|
|
||
| /** Callback when a widget is clicked to be added */ | ||
| onAddWidget: (widget: Omit<WidgetItem, 'key'>, key: string) => void; | ||
|
|
||
| /** Optional callback to close the picker (renders a Close icon if provided) */ | ||
| onClose?: () => void; | ||
|
|
||
| /** Custom background color for the header. Defaults to theme.palette.background.default */ | ||
| headerBackgroundColor?: string; | ||
|
|
||
| /** Custom text color for the header. Defaults to theme.palette.text.primary */ | ||
| headerTextColor?: string; | ||
|
|
||
| /** Custom styles for the outer container (e.g. for custom box shadows or borders) */ | ||
| containerSx?: SxProps<Theme>; | ||
| } | ||
|
|
||
| export const WidgetPicker: React.FC<WidgetPickerProps> = ({ | ||
| widgetsToAdd, | ||
| onAddWidget, | ||
| onClose, | ||
| headerBackgroundColor, | ||
| headerTextColor, | ||
| containerSx = {}, | ||
| }) => { | ||
| const theme = useTheme(); | ||
|
|
||
| return ( | ||
| <Box | ||
| sx={{ | ||
| display: 'flex', | ||
| flexDirection: 'column', | ||
| height: '100%', | ||
| width: '100%', | ||
| backgroundColor: theme.palette.background.paper, | ||
| ...containerSx, | ||
| }} | ||
| > | ||
| <Stack | ||
| direction="row" | ||
| sx={{ | ||
| alignItems: 'center', | ||
| justifyContent: 'space-between', | ||
| px: 2, | ||
| py: 1.5, | ||
| borderBottom: `1px solid ${theme.palette.divider}`, | ||
| background: headerBackgroundColor || theme.palette.background.default, | ||
| flexShrink: 0, | ||
| zIndex: 1, | ||
| }} | ||
| > | ||
| <Typography variant="h5" sx={{ color: headerTextColor || theme.palette.text.primary }}> | ||
| Widgets | ||
| </Typography> | ||
| {onClose && ( | ||
| <IconButton aria-label="Close widget picker" onClick={onClose} size="small"> | ||
| <CloseIcon fill={headerTextColor || theme.palette.text.primary} width="20" /> | ||
| </IconButton> | ||
| )} | ||
| </Stack> | ||
|
|
||
| <Box | ||
| sx={{ | ||
| display: 'flex', | ||
| flexDirection: 'column', | ||
| gap: '0.75rem', | ||
| p: 2, | ||
| overflowY: 'auto', | ||
| flex: 1, | ||
| minHeight: 0, | ||
| scrollbarWidth: 'thin', | ||
| scrollbarColor: `${theme.palette.divider} transparent`, | ||
| '&::-webkit-scrollbar': { | ||
| width: '8px', | ||
| height: '8px', | ||
| }, | ||
| '&::-webkit-scrollbar-track': { | ||
| background: 'transparent', | ||
| }, | ||
| '&::-webkit-scrollbar-thumb': { | ||
| backgroundColor: theme.palette.divider, | ||
| borderRadius: '4px', | ||
| border: '2px solid transparent', | ||
| backgroundClip: 'content-box', | ||
| '&:hover': { | ||
| backgroundColor: theme.palette.text.secondary, | ||
| }, | ||
| }, | ||
| }} | ||
| > | ||
| {widgetsToAdd.length === 0 && ( | ||
| <Typography variant="body2" sx={{ color: theme.palette.text.primary, textAlign: 'center', mt: 2 }}> | ||
| All widgets added to the layout. | ||
| </Typography> | ||
| )} | ||
|
|
||
| {widgetsToAdd.map(({ key, ...widget }) => ( | ||
| <Box | ||
| key={key} | ||
| sx={{ | ||
| width: '100%', | ||
| minWidth: '16rem', | ||
| p: 1.5, | ||
| height: '18rem', | ||
| flexShrink: 0, | ||
| backgroundColor: theme.palette.background.secondary, | ||
| boxShadow: theme.shadows[1], | ||
| borderRadius: 1, | ||
| }} | ||
| > | ||
| <Stack | ||
| direction="row" | ||
| sx={{ alignItems: "center", justifyContent: "space-between" }} | ||
| > | ||
| <Typography variant="button">{widget.title}</Typography> | ||
| <IconButton | ||
| aria-label={`Add ${widget.title} widget`} | ||
| onClick={() => onAddWidget(widget, key)} | ||
| > | ||
| <AddIcon fill={theme.palette.text.primary} width="30" /> | ||
| </IconButton> | ||
| </Stack> | ||
| {widget.thumbnail && ( | ||
| <img | ||
| src={widget.thumbnail} | ||
| alt={widget.title} | ||
| style={{ | ||
| width: '100%', | ||
| height: 'auto', | ||
| maxHeight: '180px', | ||
| objectFit: 'contain', | ||
| marginTop: '0.5rem', | ||
| }} | ||
| /> | ||
| )} | ||
| </Box> | ||
| ))} | ||
| </Box> | ||
| </Box> | ||
| ); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './WidgetPicker'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| // Export all custom components | ||
| export * from './CustomTooltip'; | ||
| export * from './DashboardLayout'; | ||
| export * from './HelperTextPopover'; | ||
| export * from './Markdown'; | ||
| export * from './Modal'; | ||
| export * from './RJSFFormWrapper'; | ||
| export * from './StyledAccordion'; | ||
| export * from './WidgetPicker'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my understanding, the
DashboardLayoutcan be opened or closed in two different ways:isSidebarOpenprop.DashboardLayoutalso updates its own state when the user interacts with theSwipeableDrawer.Could this lead to state inconsistencies or bugs in the future? For example, the drawer could be closed from within
DashboardLayoutwhile the parent component still considers it open. Would it make sense to have a single source of truth for the open state (for example, by notifying the parent of open/close events) so both remain synchronized?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Katotodan normally you'd be absolutely right about the state desync, but in this specific case, it's actually intentional. In the parent component, isSidebarOpen is tied to isEditMode. On mobile, we need the user to be able to swipe down and minimize the drawer so they can see the dashboard and drag widgets around. If we kept a strict single source of truth and fired onClose when swiping down,
it would force the parent to exit Edit Mode completely, preventing them from rearranging their dashboard and also deleting the widget