summaryrefslogtreecommitdiff
path: root/packages/frontend/src/components/MkFoldableSection.vue
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2023-05-14 12:23:39 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2023-05-14 12:23:39 +0900
commit0717afc3124a5d7434af3df31dfa54cbf420f109 (patch)
tree6182a31c905872a6cd25f28f9c97322f6f1579e4 /packages/frontend/src/components/MkFoldableSection.vue
parentrefactor(frontend): use composition api (diff)
downloadmisskey-0717afc3124a5d7434af3df31dfa54cbf420f109.tar.gz
misskey-0717afc3124a5d7434af3df31dfa54cbf420f109.tar.bz2
misskey-0717afc3124a5d7434af3df31dfa54cbf420f109.zip
refactor(frontend): use composition api
Diffstat (limited to 'packages/frontend/src/components/MkFoldableSection.vue')
-rw-r--r--packages/frontend/src/components/MkFoldableSection.vue123
1 files changed, 55 insertions, 68 deletions
diff --git a/packages/frontend/src/components/MkFoldableSection.vue b/packages/frontend/src/components/MkFoldableSection.vue
index 475e01c8d4..f52c66a8be 100644
--- a/packages/frontend/src/components/MkFoldableSection.vue
+++ b/packages/frontend/src/components/MkFoldableSection.vue
@@ -1,5 +1,5 @@
<template>
-<div class="ssazuxis">
+<div ref="el" class="ssazuxis">
<header class="_button" :style="{ background: bg }" @click="showBody = !showBody">
<div class="title"><div><slot name="header"></slot></div></div>
<div class="divider"></div>
@@ -22,80 +22,67 @@
</div>
</template>
-<script lang="ts">
-import { defineComponent } from 'vue';
+<script lang="ts" setup>
+import { onMounted, ref, shallowRef, watch } from 'vue';
import tinycolor from 'tinycolor2';
import { miLocalStorage } from '@/local-storage';
import { defaultStore } from '@/store';
const miLocalStoragePrefix = 'ui:folder:' as const;
-export default defineComponent({
- props: {
- expanded: {
- type: Boolean,
- required: false,
- default: true,
- },
- persistKey: {
- type: String,
- required: false,
- default: null,
- },
- },
- data() {
- return {
- defaultStore,
- bg: null,
- showBody: (this.persistKey && miLocalStorage.getItem(`${miLocalStoragePrefix}${this.persistKey}`)) ? (miLocalStorage.getItem(`${miLocalStoragePrefix}${this.persistKey}`) === 't') : this.expanded,
- };
- },
- watch: {
- showBody() {
- if (this.persistKey) {
- miLocalStorage.setItem(`${miLocalStoragePrefix}${this.persistKey}`, this.showBody ? 't' : 'f');
- }
- },
- },
- mounted() {
- function getParentBg(el: Element | null): string {
- if (el == null || el.tagName === 'BODY') return 'var(--bg)';
- const bg = el.style.background || el.style.backgroundColor;
- if (bg) {
- return bg;
- } else {
- return getParentBg(el.parentElement);
- }
- }
- const rawBg = getParentBg(this.$el);
- const bg = tinycolor(rawBg.startsWith('var(') ? getComputedStyle(document.documentElement).getPropertyValue(rawBg.slice(4, -1)) : rawBg);
- bg.setAlpha(0.85);
- this.bg = bg.toRgbString();
- },
- methods: {
- toggleContent(show: boolean) {
- this.showBody = show;
- },
+const props = withDefaults(defineProps<{
+ expanded?: boolean;
+ persistKey?: string;
+}>(), {
+ expanded: true,
+});
+
+const el = shallowRef<HTMLDivElement>();
+const bg = ref<string | null>(null);
+const showBody = ref((props.persistKey && miLocalStorage.getItem(`${miLocalStoragePrefix}${props.persistKey}`)) ? (miLocalStorage.getItem(`${miLocalStoragePrefix}${props.persistKey}`) === 't') : props.expanded);
+
+watch(showBody, () => {
+ if (props.persistKey) {
+ miLocalStorage.setItem(`${miLocalStoragePrefix}${props.persistKey}`, showBody.value ? 't' : 'f');
+ }
+});
+
+function enter(el: Element) {
+ const elementHeight = el.getBoundingClientRect().height;
+ el.style.height = 0;
+ el.offsetHeight; // reflow
+ el.style.height = elementHeight + 'px';
+}
+
+function afterEnter(el: Element) {
+ el.style.height = null;
+}
+
+function leave(el: Element) {
+ const elementHeight = el.getBoundingClientRect().height;
+ el.style.height = elementHeight + 'px';
+ el.offsetHeight; // reflow
+ el.style.height = 0;
+}
- enter(el) {
- const elementHeight = el.getBoundingClientRect().height;
- el.style.height = 0;
- el.offsetHeight; // reflow
- el.style.height = elementHeight + 'px';
- },
- afterEnter(el) {
- el.style.height = null;
- },
- leave(el) {
- const elementHeight = el.getBoundingClientRect().height;
- el.style.height = elementHeight + 'px';
- el.offsetHeight; // reflow
- el.style.height = 0;
- },
- afterLeave(el) {
- el.style.height = null;
- },
- },
+function afterLeave(el: Element) {
+ el.style.height = null;
+}
+
+onMounted(() => {
+ function getParentBg(el: HTMLElement | null): string {
+ if (el == null || el.tagName === 'BODY') return 'var(--bg)';
+ const bg = el.style.background || el.style.backgroundColor;
+ if (bg) {
+ return bg;
+ } else {
+ return getParentBg(el.parentElement);
+ }
+ }
+ const rawBg = getParentBg(el.value);
+ const _bg = tinycolor(rawBg.startsWith('var(') ? getComputedStyle(document.documentElement).getPropertyValue(rawBg.slice(4, -1)) : rawBg);
+ _bg.setAlpha(0.85);
+ bg.value = _bg.toRgbString();
});
</script>