Skip to content

Commit 0ed084c

Browse files
committed
⚡ Improved collapsible logic
1 parent 0d66e54 commit 0ed084c

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

src/components/LinkItems/Collapsable.vue

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
:id="sectionKey"
1111
class="toggle"
1212
type="checkbox"
13-
:checked="isExpanded"
14-
@change="collapseChanged"
13+
v-model="checkboxState"
1514
tabIndex="-1"
1615
>
1716
<label :for="sectionKey" class="lbl-toggle" tabindex="-1"
@@ -75,34 +74,42 @@ export default {
7574
const { rows, cols, checkSpanNum } = this;
7675
return `${checkSpanNum(cols, 'col')} ${checkSpanNum(rows, 'row')}`;
7776
},
77+
/* Used to fetch initial collapse state, and set new collapse state on change */
7878
isExpanded: {
79-
// getter
8079
get() {
8180
if (this.collapsed !== undefined) return !this.collapsed;
82-
const collapseStateObject = this.initialiseStorage(); // Check local storage
81+
const collapseStateObject = this.locallyStoredCollapseStates();
8382
if (collapseStateObject[this.uniqueKey] !== undefined) {
8483
return collapseStateObject[this.uniqueKey];
8584
}
86-
return true; // Nothing specified, return Open
85+
return true;
8786
},
88-
// setter
8987
set(newState) {
90-
// Get the current localstorage collapse state object
91-
const collapseState = JSON.parse(localStorage[localStorageKeys.COLLAPSE_STATE]);
92-
// Add the new state to it
88+
const collapseState = this.locallyStoredCollapseStates();
9389
collapseState[this.uniqueKey] = newState;
94-
// Stringify, and set the new object into local storage
9590
localStorage.setItem(localStorageKeys.COLLAPSE_STATE, JSON.stringify(collapseState));
9691
},
9792
},
9893
},
9994
data: () => ({
100-
// isExpanded: false,
95+
checkboxState: true,
10196
}),
10297
mounted() {
103-
// this.isExpanded = this.getCollapseState();
98+
this.checkboxState = this.isExpanded;
99+
},
100+
watch: {
101+
checkboxState(newState) {
102+
this.isExpanded = newState;
103+
},
104+
uniqueKey() {
105+
this.checkboxState = this.isExpanded;
106+
},
104107
},
105108
methods: {
109+
/* Either expand or collapse section, based on it's current state */
110+
toggle() {
111+
this.checkboxState = !this.checkboxState;
112+
},
106113
/* Check that row & column span is valid, and not over the max */
107114
checkSpanNum(span, classPrefix) {
108115
const maxSpan = 6;
@@ -115,7 +122,7 @@ export default {
115122
return userCss ? userCss.replace(/[^a-zA-Z0-9- :;.]/g, '') : '';
116123
},
117124
/* Returns local storage collapse state data, and if not yet set then initialized is */
118-
initialiseStorage() {
125+
locallyStoredCollapseStates() {
119126
// If not yet set, then call initialize
120127
if (!localStorage[localStorageKeys.COLLAPSE_STATE]) {
121128
localStorage.setItem(localStorageKeys.COLLAPSE_STATE, JSON.stringify({}));
@@ -124,10 +131,6 @@ export default {
124131
// Otherwise, return value of local storage
125132
return JSON.parse(localStorage[localStorageKeys.COLLAPSE_STATE]);
126133
},
127-
// /* Called when collapse state changes, trigger local storage update if needed */
128-
collapseChanged(whatChanged) {
129-
this.isExpanded = whatChanged.srcElement.checked;
130-
},
131134
openEditModal() {
132135
this.$emit('openEditSection');
133136
},
@@ -214,6 +217,8 @@ export default {
214217
margin-right: .7rem;
215218
transform: translateY(-2px);
216219
transition: transform .2s ease-out;
220+
opacity: 0.3;
221+
transition: all 0.4s ease-in-out;
217222
}
218223
}
219224
@@ -251,6 +256,16 @@ export default {
251256
top: 0.5rem;
252257
margin-left: 0.2rem;
253258
margin-right: 0.2rem;
259+
opacity: 0.3;
260+
transition: all 0.4s ease-in-out;
261+
}
262+
263+
/* On section hover, set interface icons to full visible */
264+
&:hover {
265+
.edit-mode-item, label.lbl-toggle::before {
266+
opacity: 1;
267+
transition: all 0.2s ease-out;
268+
}
254269
}
255270
256271
/* Makes sections fill available space */

0 commit comments

Comments
 (0)