Skip to content

Commit 0e4ea08

Browse files
authored
feat[sidebar]: add resonsive sidebar (PanJiaChen#636)
1 parent 88429bd commit 0e4ea08

File tree

5 files changed

+104
-4
lines changed

5 files changed

+104
-4
lines changed

src/store/getters.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const getters = {
22
sidebar: state => state.app.sidebar,
33
language: state => state.app.language,
4+
device: state => state.app.device,
45
visitedViews: state => state.tagsView.visitedViews,
56
cachedViews: state => state.tagsView.cachedViews,
67
token: state => state.user.token,

src/store/modules/app.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import Cookies from 'js-cookie'
33
const app = {
44
state: {
55
sidebar: {
6-
opened: !+Cookies.get('sidebarStatus')
6+
opened: !+Cookies.get('sidebarStatus'),
7+
withoutAnimation: false
78
},
9+
device: 'desktop',
810
language: Cookies.get('language') || 'en'
911
},
1012
mutations: {
@@ -15,6 +17,15 @@ const app = {
1517
Cookies.set('sidebarStatus', 0)
1618
}
1719
state.sidebar.opened = !state.sidebar.opened
20+
state.sidebar.withoutAnimation = false
21+
},
22+
CLOSE_SIDEBAR: (state, withoutAnimation) => {
23+
Cookies.set('sidebarStatus', 1)
24+
state.sidebar.opened = false
25+
state.sidebar.withoutAnimation = withoutAnimation
26+
},
27+
TOGGLE_DEVICE: (state, device) => {
28+
state.device = device
1829
},
1930
SET_LANGUAGE: (state, language) => {
2031
state.language = language
@@ -25,6 +36,12 @@ const app = {
2536
toggleSideBar({ commit }) {
2637
commit('TOGGLE_SIDEBAR')
2738
},
39+
closeSideBar({ commit }, { withoutAnimation }) {
40+
commit('CLOSE_SIDEBAR', withoutAnimation)
41+
},
42+
toggleDevice({ commit }, device) {
43+
commit('TOGGLE_DEVICE', device)
44+
},
2845
setLanguage({ commit }, language) {
2946
commit('SET_LANGUAGE', language)
3047
}

src/styles/sidebar.scss

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#app {
2+
23
// 主体区域
34
.main-container {
45
min-height: 100%;
56
transition: margin-left .28s;
67
margin-left: 180px;
78
}
8-
// 侧边栏
9+
10+
// 侧边栏
911
.sidebar-container {
1012
.horizontal-collapse-transition {
1113
transition: 0s width ease-in-out, 0s padding-left ease-in-out, 0s padding-right ease-in-out;
@@ -32,6 +34,7 @@
3234
width: 100% !important;
3335
}
3436
}
37+
3538
.hideSidebar {
3639
.sidebar-container {
3740
width: 36px !important;
@@ -62,7 +65,8 @@
6265
}
6366
}
6467
}
65-
.sidebar-container .nest-menu .el-submenu > .el-submenu__title,
68+
69+
.sidebar-container .nest-menu .el-submenu>.el-submenu__title,
6670
.sidebar-container .el-submenu .el-menu-item {
6771
min-width: 180px !important;
6872
background-color: $subMenuBg !important;
@@ -73,4 +77,29 @@
7377
.el-menu--collapse .el-menu .el-submenu {
7478
min-width: 180px !important;
7579
}
80+
81+
//适配移动端
82+
.mobile {
83+
.main-container {
84+
margin-left: 0px;
85+
}
86+
.sidebar-container {
87+
top: 50px;
88+
transition: transform .28s;
89+
width: 180px !important;
90+
}
91+
&.hideSidebar {
92+
.sidebar-container {
93+
transition-duration: 0.3s;
94+
transform: translate3d(-180px, 0, 0);
95+
}
96+
}
97+
}
98+
99+
.withoutAnimation {
100+
.main-container,
101+
.sidebar-container {
102+
transition: none;
103+
}
104+
}
76105
}

src/views/layout/Layout.vue

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<div class="app-wrapper" :class="{hideSidebar:!sidebar.opened}">
2+
<div class="app-wrapper" :class="classObj">
33
<sidebar class="sidebar-container"></sidebar>
44
<div class="main-container">
55
<navbar></navbar>
@@ -11,6 +11,7 @@
1111

1212
<script>
1313
import { Navbar, Sidebar, AppMain, TagsView } from './components'
14+
import ResizeMixin from './mixin/ResizeHandler'
1415
1516
export default {
1617
name: 'layout',
@@ -20,9 +21,20 @@ export default {
2021
AppMain,
2122
TagsView
2223
},
24+
mixins: [ResizeMixin],
2325
computed: {
2426
sidebar() {
2527
return this.$store.state.app.sidebar
28+
},
29+
device() {
30+
return this.$store.state.app.device
31+
},
32+
classObj() {
33+
return {
34+
hideSidebar: !this.sidebar.opened,
35+
withoutAnimation: this.sidebar.withoutAnimation,
36+
mobile: this.device === 'mobile'
37+
}
2638
}
2739
}
2840
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import store from '@/store'
2+
3+
const { body } = document
4+
const WIDTH = 1024
5+
const RATIO = 3
6+
7+
export default {
8+
watch: {
9+
$route(route) {
10+
if (this.device === 'mobile' && this.sidebar.opened) {
11+
store.dispatch('closeSideBar', { withoutAnimation: false })
12+
}
13+
}
14+
},
15+
beforeMount() {
16+
window.addEventListener('resize', this.resizeHandler)
17+
},
18+
mounted() {
19+
const isMobile = this.isMobile()
20+
if (isMobile) {
21+
store.dispatch('toggleDevice', 'mobile')
22+
store.dispatch('closeSideBar', { withoutAnimation: true })
23+
}
24+
},
25+
methods: {
26+
isMobile() {
27+
const rect = body.getBoundingClientRect()
28+
return rect.width - RATIO < WIDTH
29+
},
30+
resizeHandler() {
31+
if (!document.hidden) {
32+
const isMobile = this.isMobile()
33+
store.dispatch('toggleDevice', isMobile ? 'mobile' : 'desktop')
34+
35+
if (isMobile) {
36+
store.dispatch('closeSideBar', { withoutAnimation: true })
37+
}
38+
}
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)