11<template >
2- <div :class =" sidebarClasses" >
3- <slot >Sidebar </slot >
2+ <div :class =" sidebarClasses" @click = " sidebarClick " >
3+ <slot ></slot >
44 </div >
55</template >
66
77<script >
8- import elementResizeDetectorMaker from ' element-resize-detector'
9- import { getStyle } from ' @coreui/coreui/dist/js/coreui-utilities'
10-
118export default {
129 name: ' CSidebar' ,
1310 props: {
1411 fixed: Boolean ,
12+ unfoldable: Boolean ,
1513 breakpoint: {
16- type: String ,
14+ type: [ String , Boolean ] ,
1715 default: ' lg' ,
18- validator : val => [' sm' , ' md' , ' lg' , ' xl' ].includes (val)
16+ validator : val => [false , ' ' , ' sm' , ' md' , ' lg' , ' xl' ].includes (val)
1917 },
2018 minimize: Boolean ,
2119 show: {
20+ type: Boolean ,
21+ default: null
22+ },
23+ hideOnMobileClick: {
2224 type: Boolean ,
2325 default: true
2426 },
25- showOnMobile: Boolean ,
26- noHideOnMobileClick: Boolean ,
2727 aside: Boolean ,
28- light: Boolean ,
29- dropdownStateOnRouteChange: {
28+ colorScheme: {
29+ type: String ,
30+ default: ' dark'
31+ },
32+ dropdownMode: {
3033 type: String ,
3134 default: ' openActive' ,
3235 validator : (mode ) => {
@@ -37,142 +40,68 @@ export default {
3740 }
3841 },
3942 provide () {
40- const state = {}
41- Object .defineProperty (state, ' minimized' , {
42- get : () => this .minimized
43- })
44- Object .defineProperty (state, ' open' , {
45- get : () => this .open
46- })
47- Object .defineProperty (state, ' mobileOpen' , {
48- get : () => this .mobileOpen
49- })
50- const dropdownStateOnRouteChange = this .dropdownStateOnRouteChange
51- return { state, dropdownStateOnRouteChange }
43+ return { dropdownMode: this .dropdownMode }
5244 },
5345 data () {
5446 return {
5547 open: this .show ,
56- mobileOpen: this .showOnMobile ,
57- minimized: this .minimize ,
58- erd: elementResizeDetectorMaker (),
59- bodyWidth: undefined ,
6048 }
6149 },
62- mounted () {
63- this .erd .listenTo (document .body , (el ) => this .bodyWidth = el .clientWidth )
64-
65- this .$root .$on (this .listenedEvents , () => {
66- if (this .isOnMobile ) {
67- this .switchState (' mobileOpen' )
68- } else {
69- this .switchState (' open' )
70- }
71- })
72- },
7350 watch: {
7451 show (val ) {
7552 this .open = val
76- },
77- open: {
78- immediate: true ,
79- handler (val ) {
80- this .emitCurrentState (' show' , val)
81- }
82- },
83- showOnMobile (val ) {
84- this .mobileOpen = val
85- },
86- mobileOpen: {
87- immediate: true ,
88- handler (val ) {
89- this .emitCurrentState (' showOnMobile' , val)
90- }
91- },
92- minimize (val ) {
93- this .minimized = val
94- },
95- minimized: {
96- immediate: true ,
97- handler (val ) {
98- this .emitCurrentState (' minimize' , val)
99- }
100- },
101- isOnMobile: {
102- immediate: true ,
103- handler (val , oldVal ) {
104- if (val === true && val !== oldVal) {
105- document .body .addEventListener (' click' , this .mobileClick )
106- } else if (oldVal === true ) {
107- document .body .removeEventListener (' click' , this .mobileClick )
108- }
109-
110- if (val !== undefined ) {
111- this .$root .$emit (' c-sidebar-mobile-state' , val)
112- }
113- }
11453 }
11554 },
11655 computed: {
117- listenedEvents () {
118- const componentEvent = this .aside ? ' c-aside-toggle' : ' c-sidebar-toggle'
119- if (this .$attrs .id ) {
120- return [componentEvent, this .$attrs .id ]
56+ displayClasses () {
57+ const breakpointPrefix = this .breakpoint === false ? ' ' : ` -${ this .breakpoint } `
58+ const responsiveClass = ` c-sidebar${ breakpointPrefix} -show`
59+ if (this .open === false ) {
60+ return null
61+ } else if (this .open === true ) {
62+ return [' c-sidebar-show' , breakpointPrefix ? responsiveClass : ' ' ]
63+ } else if (this .open === null ) {
64+ return responsiveClass
12165 }
122- return componentEvent
12366 },
67+
12468 sidebarClasses () {
125- const mobileClasses = {
126- ' c-sidebar-show' : this .mobileOpen ,
127- }
128- const desktopClasses = {
129- ' c-sidebar-minimized' : this .minimized ,
130- [` c-sidebar-${ this .breakpoint } -show` ]: this .open ,
131- }
13269 return [
133- this .isOnMobile ? mobileClasses : desktopClasses,
13470 ' c-sidebar' ,
135- ` c-sidebar-${ this .light ? ' light' : ' dark' } ` ,
71+ ` c-sidebar-${ this .colorScheme } ` ,
72+ this .displayClasses ,
13673 {
13774 ' c-sidebar-fixed' : this .fixed ,
138- ' c-sidebar-right' : this .aside
75+ ' c-sidebar-right' : this .aside ,
76+ ' c-sidebar-minimized' : this .minimize && ! this .unfoldable ,
77+ ' c-sidebar-minimized-unfoldable' : this .minimize && this .unfoldable
13978 }
14079 ]
141- },
142- breakpoints () {
143- return {
144- ' sm' : getStyle (' --breakpoint-sm' ) || ' 576px' ,
145- ' md' : getStyle (' --breakpoint-md' ) || ' 768px' ,
146- ' lg' : getStyle (' --breakpoint-lg' ) || ' 992px' ,
147- ' xl' : getStyle (' --breakpoint-xl' ) || ' 1200px'
148- }
149- },
150- isOnMobile () {
151- const mobileWidth = parseFloat (this .breakpoints [this .breakpoint ])
152- return this .bodyWidth && (this .bodyWidth < mobileWidth)
15380 }
15481 },
15582 methods: {
156- mobileClick (event ) {
157- if (! this .mobileOpen || this .noHideOnMobileClick ) {
158- return
159- }
160- const classList = Array .from (event .target .classList ).join ()
161- const clickedOutsideSidebar = ! this .$el .contains (event .target )
83+ sidebarClick (event ) {
84+ const hiddingElementClicked = event .target .tagName === ' A'
16285 if (
163- (clickedOutsideSidebar && ! classList .includes (' c-header-toggler' )) ||
164- (! clickedOutsideSidebar && event .target .tagName === ' A' )
86+ hiddingElementClicked &&
87+ this .hideOnMobileClick &&
88+ this .isOnMobile (event )
16589 ) {
166- this .switchState (' mobileOpen' )
90+ this .open = false
91+ this .$emit (' update:show' , false )
16792 }
16893 },
169- switchState (variable ) {
170- this [variable] = ! this [variable]
171- },
172- emitCurrentState (variableName , value ) {
173- this .$emit (` update:${ variableName} ` , value)
174- this .$root .$emit (` c-sidebar-${ variableName} -state` , value)
94+ isOnMobile (event ) {
95+ return Boolean (getComputedStyle (this .$el ).getPropertyValue (' --on-mobile' ))
17596 }
17697 }
17798}
17899 </script >
100+
101+ <style scoped>
102+ @media (max-width : 992px ) {
103+ .c-sidebar-lg-show {
104+ --on-mobile : true;
105+ }
106+ }
107+ </style >
0 commit comments