LiveAtlas/src/components/sidebar/CollapsibleSection.vue

81 lines
1.8 KiB
Vue
Raw Normal View History

2021-05-25 18:26:14 +00:00
<template>
2021-05-26 01:10:34 +00:00
<section :class="{'sidebar__section': true, 'section--collapsible': true, 'section--collapsed': collapsed}">
<button :id="`${name}-heading`" type="button" class="section__heading"
2021-05-25 22:39:04 +00:00
@click.prevent="toggle" @keydown="handleKeydown" :title="title"
:aria-expanded="!collapsed" :aria-controls="`${name}-content`">
2021-05-26 01:10:34 +00:00
<span>
<slot name="heading"></slot>
</span>
<SvgIcon name="arrow"></SvgIcon>
2021-05-25 18:26:14 +00:00
</button>
<div :id="`${name}-content`" role="region" :aria-labelledby="`${name}-heading`" :aria-hidden="collapsed">
2021-05-25 18:26:14 +00:00
<slot></slot>
</div>
2021-05-25 18:26:14 +00:00
</section>
</template>
<script lang="ts">
import {useStore} from "@/store";
import {LiveAtlasSidebarSection} from "@/index";
import {defineComponent} from "@vue/runtime-core";
2021-05-26 01:10:34 +00:00
import SvgIcon from "@/components/SvgIcon.vue";
import '@/assets/icons/arrow.svg';
2021-05-25 18:26:14 +00:00
export default defineComponent({
name: 'CollapsibleSection',
2021-05-26 01:10:34 +00:00
components: {SvgIcon},
2021-05-25 18:26:14 +00:00
props: {
name: {
type: String as () => LiveAtlasSidebarSection,
2021-05-25 18:26:14 +00:00
required: true,
}
},
computed: {
title(): string {
return useStore().state.messages.toggleTitle;
},
collapsed(): boolean {
return useStore().state.ui.collapsedSections.has(this.name);
},
},
methods: {
handleKeydown(e: KeyboardEvent) {
2021-05-25 18:26:14 +00:00
if(e.key !== ' ' && e.key !== 'Enter') {
return;
}
2021-05-25 22:39:04 +00:00
e.preventDefault();
2021-05-25 18:26:14 +00:00
this.toggle();
},
toggle() {
const store = useStore();
if(this.collapsed) {
store.state.ui.collapsedSections.delete(this.name);
} else {
store.state.ui.collapsedSections.add(this.name);
}
}
}
});
</script>
2021-05-26 01:10:34 +00:00
<style lang="scss" scoped>
.section--collapsible {
.section__heading .svg-icon {
transform: rotate(180deg);
}
&.section--collapsed {
.section__heading .svg-icon {
transform: none;
}
}
}
</style>