refactor: Improve mobile responsiveness and scrolling behavior in UI layout

This commit is contained in:
1808837298@qq.com
2025-03-10 15:49:32 +08:00
parent d9cf0885f1
commit b2938ffe2c
3 changed files with 32 additions and 16 deletions

View File

@@ -84,7 +84,11 @@ const PageLayout = () => {
}}> }}>
<HeaderBar /> <HeaderBar />
</Header> </Header>
<Layout style={{ marginTop: '56px', height: 'calc(100vh - 56px)', overflow: 'hidden' }}> <Layout style={{
marginTop: '56px',
height: 'calc(100vh - 56px)',
overflow: styleState.isMobile ? 'auto' : 'hidden'
}}>
{styleState.showSider && ( {styleState.showSider && (
<Sider style={{ <Sider style={{
height: 'calc(100vh - 56px)', height: 'calc(100vh - 56px)',
@@ -105,13 +109,17 @@ const PageLayout = () => {
)} )}
<Layout style={{ <Layout style={{
marginLeft: styleState.showSider ? (isSidebarCollapsed ? '60px' : '200px') : '0', marginLeft: styleState.showSider ? (isSidebarCollapsed ? '60px' : '200px') : '0',
transition: 'margin-left 0.3s ease' transition: 'margin-left 0.3s ease',
height: '100%',
overflow: 'auto'
}}> }}>
<Content <Content
style={{ style={{
height: '100%', height: '100%',
overflowY: 'auto', overflowY: 'auto',
padding: styleState.shouldInnerPadding? '24px': '0' WebkitOverflowScrolling: 'touch',
padding: styleState.shouldInnerPadding? '24px': '0',
position: 'relative'
}} }}
> >
<App /> <App />

View File

@@ -305,7 +305,9 @@ const SiderBar = () => {
borderRadius: '0 8px 8px 0', borderRadius: '0 8px 8px 0',
transition: 'all 0.3s ease', transition: 'all 0.3s ease',
position: 'relative', position: 'relative',
zIndex: 95 zIndex: 95,
overflowY: 'auto',
WebkitOverflowScrolling: 'touch' // Improve scrolling on iOS devices
}} }}
defaultIsCollapsed={ defaultIsCollapsed={
localStorage.getItem('default_collapse_sidebar') === 'true' localStorage.getItem('default_collapse_sidebar') === 'true'

View File

@@ -9,7 +9,7 @@ export const StyleContext = React.createContext({
export const StyleProvider = ({ children }) => { export const StyleProvider = ({ children }) => {
const [state, setState] = useState({ const [state, setState] = useState({
isMobile: false, isMobile: isMobile(),
showSider: false, showSider: false,
shouldInnerPadding: false, shouldInnerPadding: false,
}); });
@@ -39,7 +39,13 @@ export const StyleProvider = ({ children }) => {
useEffect(() => { useEffect(() => {
const updateIsMobile = () => { const updateIsMobile = () => {
dispatch({ type: 'SET_MOBILE', payload: isMobile() }); const mobileDetected = isMobile();
dispatch({ type: 'SET_MOBILE', payload: mobileDetected });
// If on mobile, we might want to auto-hide the sidebar
if (mobileDetected && state.showSider) {
dispatch({ type: 'SET_SIDER', payload: false });
}
}; };
updateIsMobile(); updateIsMobile();
@@ -51,24 +57,24 @@ export const StyleProvider = ({ children }) => {
dispatch({ type: 'SET_SIDER', payload: false }); dispatch({ type: 'SET_SIDER', payload: false });
dispatch({ type: 'SET_INNER_PADDING', payload: false }); dispatch({ type: 'SET_INNER_PADDING', payload: false });
} else { } else {
dispatch({ type: 'SET_SIDER', payload: true }); // Only show sidebar on non-mobile devices by default
dispatch({ type: 'SET_SIDER', payload: !isMobile() });
dispatch({ type: 'SET_INNER_PADDING', payload: true }); dispatch({ type: 'SET_INNER_PADDING', payload: true });
} }
if (isMobile()) {
dispatch({ type: 'SET_SIDER', payload: false });
}
}; };
updateShowSider() updateShowSider();
// Add event listeners to handle window resize
// Optionally, add event listeners to handle window resize const handleResize = () => {
window.addEventListener('resize', updateIsMobile); updateIsMobile();
};
window.addEventListener('resize', handleResize);
// Cleanup event listener on component unmount // Cleanup event listener on component unmount
return () => { return () => {
window.removeEventListener('resize', updateIsMobile); window.removeEventListener('resize', handleResize);
}; };
}, []); }, []);