Skip to content

Commit 61e7041

Browse files
CopilotTechQuery
andcommitted
Refactor SessionBox: extract ResponsiveDrawer module, use PropsWithChildren, Tailwind CSS
Co-authored-by: TechQuery <[email protected]>
1 parent 4e92e7e commit 61e7041

File tree

2 files changed

+49
-84
lines changed

2 files changed

+49
-84
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { Drawer, useMediaQuery, useTheme } from '@mui/material';
2+
import { FC, PropsWithChildren } from 'react';
3+
4+
const DESKTOP_DRAWER_STYLES = {
5+
position: 'sticky',
6+
top: '5rem',
7+
height: 'calc(100vh - 5rem)',
8+
border: 'none',
9+
boxShadow: 'none',
10+
} as const;
11+
12+
export interface ResponsiveDrawerProps extends PropsWithChildren {
13+
open: boolean;
14+
onClose: () => void;
15+
}
16+
17+
export const ResponsiveDrawer: FC<ResponsiveDrawerProps> = ({ open, onClose, children }) => {
18+
const theme = useTheme();
19+
const isMobile = useMediaQuery(theme.breakpoints.down('md'));
20+
21+
return (
22+
<Drawer
23+
anchor="left"
24+
open={isMobile ? open : true}
25+
variant={isMobile ? 'temporary' : 'permanent'}
26+
onClose={onClose}
27+
sx={{
28+
display: { xs: isMobile ? 'block' : 'none', md: isMobile ? 'none' : 'block' },
29+
'& .MuiDrawer-paper': {
30+
width: 250,
31+
...(isMobile ? {} : DESKTOP_DRAWER_STYLES),
32+
},
33+
}}
34+
>
35+
{children}
36+
</Drawer>
37+
);
38+
};

components/User/SessionBox.tsx

Lines changed: 11 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
import { User } from '@idea2app/data-server';
22
import {
33
Box,
4-
Drawer,
54
IconButton,
65
List,
76
ListItem,
87
ListItemButton,
98
ListItemText,
109
Modal,
11-
useMediaQuery,
12-
useTheme,
1310
} from '@mui/material';
1411
import { observable } from 'mobx';
1512
import { observer } from 'mobx-react';
1613
import Link from 'next/link';
1714
import { JWTProps } from 'next-ssr-middleware';
18-
import { Component, FC, HTMLAttributes, JSX } from 'react';
15+
import { Component, HTMLAttributes, JSX } from 'react';
1916

2017
import { SymbolIcon } from '../Icon';
18+
import { ResponsiveDrawer } from './ResponsiveDrawer';
2119
import { SessionForm } from './SessionForm';
2220

2321
export type MenuItem = Pick<JSX.IntrinsicElements['a'], 'href' | 'title'>;
@@ -27,43 +25,6 @@ export interface SessionBoxProps extends HTMLAttributes<HTMLDivElement>, JWTProp
2725
menu?: MenuItem[];
2826
}
2927

30-
interface ResponsiveDrawerProps {
31-
open: boolean;
32-
onClose: () => void;
33-
children: JSX.Element;
34-
}
35-
36-
const ResponsiveDrawer: FC<ResponsiveDrawerProps> = ({ open, onClose, children }) => {
37-
const theme = useTheme();
38-
const isMobile = useMediaQuery(theme.breakpoints.down('md'));
39-
40-
return (
41-
<Drawer
42-
anchor="left"
43-
open={isMobile ? open : true}
44-
variant={isMobile ? 'temporary' : 'permanent'}
45-
onClose={onClose}
46-
sx={{
47-
display: { xs: isMobile ? 'block' : 'none', md: isMobile ? 'none' : 'block' },
48-
'& .MuiDrawer-paper': {
49-
width: 250,
50-
...(isMobile
51-
? {}
52-
: {
53-
position: 'sticky',
54-
top: '5rem',
55-
height: 'calc(100vh - 5rem)',
56-
border: 'none',
57-
boxShadow: 'none',
58-
}),
59-
},
60-
}}
61-
>
62-
{children}
63-
</Drawer>
64-
);
65-
};
66-
6728
@observer
6829
export class SessionBox extends Component<SessionBoxProps> {
6930
@observable
@@ -110,27 +71,9 @@ export class SessionBox extends Component<SessionBoxProps> {
11071
const { className = '', children, jwtPayload, ...props } = this.props;
11172

11273
return (
113-
<Box
114-
sx={{
115-
display: 'flex',
116-
flexDirection: { xs: 'column', md: 'row' },
117-
}}
118-
className={className}
119-
{...props}
120-
>
74+
<div className={`flex flex-col md:flex-row ${className}`} {...props}>
12175
{/* Mobile Menu Button */}
122-
<Box
123-
sx={{
124-
display: { xs: 'flex', md: 'none' },
125-
position: 'sticky',
126-
top: 0,
127-
zIndex: 1100,
128-
bgcolor: 'background.paper',
129-
borderBottom: 1,
130-
borderColor: 'divider',
131-
p: 1,
132-
}}
133-
>
76+
<div className="sticky top-0 z-[1100] flex border-b p-1 md:hidden bg-background-paper border-divider">
13477
<IconButton
13578
edge="start"
13679
color="inherit"
@@ -139,43 +82,27 @@ export class SessionBox extends Component<SessionBoxProps> {
13982
>
14083
<SymbolIcon name="menu" />
14184
</IconButton>
142-
</Box>
85+
</div>
14386

14487
{/* Unified Responsive Drawer */}
14588
<ResponsiveDrawer open={this.drawerOpen} onClose={this.closeDrawer}>
146-
<Box sx={{ width: 250 }}>{this.renderMenuItems()}</Box>
89+
<div className="w-[250px]">{this.renderMenuItems()}</div>
14790
</ResponsiveDrawer>
14891

14992
{/* Main Content */}
150-
<Box
151-
component="main"
152-
sx={{
153-
flex: 1,
154-
pb: 3,
155-
px: { xs: 2, sm: 3 },
156-
}}
157-
>
93+
<main className="flex-1 px-2 pb-3 sm:px-3">
15894
{children}
15995

16096
<Modal open={this.modalShown}>
16197
<Box
162-
sx={{
163-
position: 'absolute',
164-
top: '50%',
165-
left: '50%',
166-
transform: 'translate(-50%, -50%)',
167-
width: { xs: '90vw', sm: 400 },
168-
bgcolor: 'background.paper',
169-
borderRadius: 1,
170-
boxShadow: 24,
171-
p: 4,
172-
}}
98+
className="absolute left-1/2 top-1/2 w-[90vw] -translate-x-1/2 -translate-y-1/2 rounded p-4 shadow-2xl sm:w-[400px] bg-background-paper"
99+
sx={{ boxShadow: 24 }}
173100
>
174101
<SessionForm onSignIn={() => (this.modalShown = false)} />
175102
</Box>
176103
</Modal>
177-
</Box>
178-
</Box>
104+
</main>
105+
</div>
179106
);
180107
}
181108
}

0 commit comments

Comments
 (0)