Skip to content

Commit a9b5091

Browse files
committed
feat: CCollapse directive.
1 parent f94a346 commit a9b5091

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

src/directives/Collapse/collapse.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
export default {
2+
attachAttributes (el, binding) {
3+
let item = document.getElementById(binding.arg)
4+
item.style.overflow = 'hidden'
5+
item.classList.add('collapse')
6+
if (!binding.modifiers.show && !binding.value) {
7+
el.classList.add('collapsed')
8+
el.setAttribute('aria-expanded', 'false')
9+
} else {
10+
el.setAttribute('aria-expanded', 'true')
11+
item.classList.add('show')
12+
}
13+
},
14+
inserted (el, binding) {
15+
binding.def.attachAttributes(el, binding)
16+
if(binding.value == undefined)
17+
el.addEventListener('click', () => binding.def.toggle(el, binding))
18+
},
19+
update (el, binding) {
20+
if(binding.oldValue !== binding.value)
21+
binding.def.toggle(el, binding)
22+
},
23+
toggle (el, binding) {
24+
let item = document.getElementById(binding.arg)
25+
let duration = el.dataset.duration ? Number(el.dataset.duration) : 400
26+
if(item.classList.contains('show'))
27+
binding.def.hide(item, el, duration)
28+
else
29+
binding.def.show(item, el, duration)
30+
},
31+
hide(item, el, duration){
32+
item.style.transition = `all ${duration}ms ease-in-out`
33+
item.style.height = item.scrollHeight + 'px'
34+
setTimeout(function () {
35+
item.style.paddingTop = 0
36+
item.style.paddingBottom = 0
37+
item.style.height = 0
38+
}, 1)
39+
setTimeout(function () {
40+
item.classList.remove('show');
41+
item.style.transition = ''
42+
item.style.paddingTop = ''
43+
item.style.paddingBottom = ''
44+
el.setAttribute('aria-expanded', 'false')
45+
el.classList.add('collapsed')
46+
item.style.height = ''
47+
}, duration)
48+
},
49+
show (item, el, duration) {
50+
var getHeight = function () {
51+
item.style.display = 'block'
52+
let style = []
53+
style['height'] = item.scrollHeight + 'px'
54+
style['pt'] = window.getComputedStyle(item).paddingTop
55+
style['pb'] = window.getComputedStyle(item).paddingBottom
56+
item.style.display = ''
57+
return style
58+
}
59+
var style = getHeight()
60+
item.style.height = 0
61+
item.style.paddingTop = 0
62+
item.style.paddingBottom = 0
63+
item.classList.add('show')
64+
65+
setTimeout(function () {
66+
item.style.transition = `all ${duration}ms ease-in-out`
67+
item.style.height = style['height']
68+
item.style.paddingTop = style['pt']
69+
item.style.paddingBottom = style['pb']
70+
}, 1);
71+
setTimeout(function () {
72+
item.style.height = ''
73+
item.style.transition = ''
74+
el.setAttribute('aria-expanded', 'true')
75+
el.classList.remove('collapsed')
76+
}, duration)
77+
}
78+
}

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*eslint import/namespace: [2, { allowComputed: true }]*/
22
import * as Components from './components'
3+
import Collapse from './directives/Collapse/collapse'
34

45
const CoreuiVue = {
56

@@ -9,6 +10,7 @@ const CoreuiVue = {
910
if(comp.name)
1011
Vue.component(comp.name, comp)
1112
}
13+
Vue.directive('CCollapse', Collapse)
1214
}
1315
};
1416
// Export library

src/index.umd.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import CoreuiVueModule, * as LibraryComponents from "./index";
1+
import CoreuiVue, * as LibraryComponents from "./index";
22

33
// Automatically register components if Vue is available globally
44
if (typeof window !== "undefined" && window.Vue) {
5-
window.Vue.use(CoreuiVueModule);
5+
window.Vue.use(CoreuiVue);
66
}
77

88
export default LibraryComponents;

0 commit comments

Comments
 (0)