Skip to content

Commit b70dd33

Browse files
authored
refactor: getBgColor関数の切り出し + fix types (#291)
1 parent 87ada07 commit b70dd33

File tree

9 files changed

+96
-111
lines changed

9 files changed

+96
-111
lines changed

packages/frontend/src/components/MkContainer.vue

+14-10
Original file line numberDiff line numberDiff line change
@@ -66,26 +66,30 @@ const showBody = ref(props.expanded);
6666
const omitted = ref(false);
6767
const manuallyOperated = ref(false);
6868

69-
function enter(el) {
69+
function enter(el: Element) {
70+
if (!(el instanceof HTMLElement)) return;
7071
const elementHeight = el.getBoundingClientRect().height;
71-
el.style.height = 0;
72+
el.style.height = '0';
7273
el.offsetHeight; // reflow
73-
el.style.height = Math.min(elementHeight, props.maxHeight ?? Infinity) + 'px';
74+
el.style.height = `${Math.min(elementHeight, props.maxHeight ?? Infinity)}px`;
7475
}
7576

76-
function afterEnter(el) {
77-
el.style.height = null;
77+
function afterEnter(el: Element) {
78+
if (!(el instanceof HTMLElement)) return;
79+
el.style.height = '';
7880
}
7981

80-
function leave(el) {
82+
function leave(el: Element) {
83+
if (!(el instanceof HTMLElement)) return;
8184
const elementHeight = el.getBoundingClientRect().height;
82-
el.style.height = elementHeight + 'px';
85+
el.style.height = `${elementHeight}px`;
8386
el.offsetHeight; // reflow
84-
el.style.height = 0;
87+
el.style.height = '0';
8588
}
8689

87-
function afterLeave(el) {
88-
el.style.height = null;
90+
function afterLeave(el: Element) {
91+
if (!(el instanceof HTMLElement)) return;
92+
el.style.height = '';
8993
}
9094

9195
const showMore = () => {

packages/frontend/src/components/MkDateSeparatedList.vue

+4-4
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,14 @@ export default defineComponent({
126126
return children;
127127
};
128128

129-
function onBeforeLeave(element: Element) {
130-
const el = element as HTMLElement;
129+
function onBeforeLeave(el: Element) {
130+
if (!(el instanceof HTMLElement)) return;
131131
el.style.top = `${el.offsetTop}px`;
132132
el.style.left = `${el.offsetLeft}px`;
133133
}
134134

135-
function onLeaveCancelled(element: Element) {
136-
const el = element as HTMLElement;
135+
function onLeaveCancelled(el: Element) {
136+
if (!(el instanceof HTMLElement)) return;
137137
el.style.top = '';
138138
el.style.left = '';
139139
}

packages/frontend/src/components/MkFoldableSection.vue

+24-34
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ SPDX-License-Identifier: AGPL-3.0-only
55

66
<template>
77
<div ref="rootEl" :class="$style.root">
8-
<header :class="$style.header" class="_button" :style="{ background: bg }" @click="showBody = !showBody">
8+
<header :class="$style.header" class="_button" @click="showBody = !showBody">
99
<div :class="$style.title"><div><slot name="header"></slot></div></div>
1010
<div :class="$style.divider"></div>
1111
<button class="_button" :class="$style.button">
@@ -32,21 +32,23 @@ SPDX-License-Identifier: AGPL-3.0-only
3232

3333
<script lang="ts" setup>
3434
import { onMounted, ref, shallowRef, watch } from 'vue';
35-
import tinycolor from 'tinycolor2';
3635
import { miLocalStorage } from '@/local-storage.js';
3736
import { defaultStore } from '@/store.js';
37+
import { getBgColor } from '@/scripts/tms/get-bg-color.js';
3838

3939
const miLocalStoragePrefix = 'ui:folder:' as const;
4040

4141
const props = withDefaults(defineProps<{
4242
expanded?: boolean;
43-
persistKey?: string;
43+
persistKey?: string | null;
4444
}>(), {
4545
expanded: true,
46+
persistKey: null,
4647
});
4748

48-
const rootEl = shallowRef<HTMLDivElement>();
49-
const bg = ref<string>();
49+
const rootEl = shallowRef<HTMLElement>();
50+
const parentBg = ref<string | null>(null);
51+
// eslint-disable-next-line vue/no-setup-props-reactivity-loss
5052
const showBody = ref((props.persistKey && miLocalStorage.getItem(`${miLocalStoragePrefix}${props.persistKey}`)) ? (miLocalStorage.getItem(`${miLocalStoragePrefix}${props.persistKey}`) === 't') : props.expanded);
5153

5254
watch(showBody, () => {
@@ -55,55 +57,42 @@ watch(showBody, () => {
5557
}
5658
});
5759

58-
function enter(element: Element) {
59-
const el = element as HTMLElement;
60+
function enter(el: Element) {
61+
if (!(el instanceof HTMLElement)) return;
6062
const elementHeight = el.getBoundingClientRect().height;
6163
el.style.height = '0';
6264
el.offsetHeight; // reflow
63-
el.style.height = elementHeight + 'px';
65+
el.style.height = `${elementHeight}px`;
6466
}
6567

66-
function afterEnter(element: Element) {
67-
const el = element as HTMLElement;
68-
el.style.height = 'unset';
68+
function afterEnter(el: Element) {
69+
if (!(el instanceof HTMLElement)) return;
70+
el.style.height = '';
6971
}
7072

71-
function leave(element: Element) {
72-
const el = element as HTMLElement;
73+
function leave(el: Element) {
74+
if (!(el instanceof HTMLElement)) return;
7375
const elementHeight = el.getBoundingClientRect().height;
74-
el.style.height = elementHeight + 'px';
76+
el.style.height = `${elementHeight}px`;
7577
el.offsetHeight; // reflow
7678
el.style.height = '0';
7779
}
7880

79-
function afterLeave(element: Element) {
80-
const el = element as HTMLElement;
81-
el.style.height = 'unset';
81+
function afterLeave(el: Element) {
82+
if (!(el instanceof HTMLElement)) return;
83+
el.style.height = '';
8284
}
8385

8486
onMounted(() => {
85-
function getParentBg(el?: HTMLElement | null): string {
86-
if (el == null || el.tagName === 'BODY') return 'var(--bg)';
87-
const background = el.style.background || el.style.backgroundColor;
88-
if (background) {
89-
return background;
90-
} else {
91-
return getParentBg(el.parentElement);
92-
}
93-
}
94-
95-
const rawBg = getParentBg(rootEl.value);
96-
const _bg = tinycolor(rawBg.startsWith('var(') ? getComputedStyle(document.documentElement).getPropertyValue(rawBg.slice(4, -1)) : rawBg);
97-
_bg.setAlpha(0.85);
98-
bg.value = _bg.toRgbString();
87+
parentBg.value = getBgColor(rootEl.value?.parentElement);
9988
});
10089
</script>
10190

10291
<style lang="scss" module>
10392
.folderToggleEnterActive, .folderToggleLeaveActive {
10493
overflow-y: hidden; // fallback (overflow-y: clip)
10594
overflow-y: clip;
106-
transition: opacity 0.5s, height 0.5s !important;
95+
transition: opacity 0.3s, height 0.3s !important;
10796
}
10897

10998
.folderToggleEnterFrom, .folderToggleLeaveTo {
@@ -120,8 +109,9 @@ onMounted(() => {
120109
z-index: 10;
121110
position: sticky;
122111
top: var(--stickyTop, 0px);
123-
-webkit-backdrop-filter: var(--blur, blur(8px));
124-
backdrop-filter: var(--blur, blur(20px));
112+
-webkit-backdrop-filter: var(--blur, blur(15px));
113+
backdrop-filter: var(--blur, blur(15px));
114+
background-color: color(from v-bind("parentBg ?? 'var(--bg)'") srgb r g b / 0.85);
125115
}
126116

127117
.title {

packages/frontend/src/components/MkFolder.vue

+17-21
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ SPDX-License-Identifier: AGPL-3.0-only
5656
</template>
5757

5858
<script lang="ts" setup>
59-
import { nextTick, onMounted, shallowRef, ref } from 'vue';
59+
import { nextTick, onMounted, ref, shallowRef } from 'vue';
6060
import { defaultStore } from '@/store.js';
61+
import { getBgColor } from '@/scripts/tms/get-bg-color.js';
6162

6263
const props = withDefaults(defineProps<{
6364
defaultOpen?: boolean;
@@ -69,40 +70,35 @@ const props = withDefaults(defineProps<{
6970
withSpacer: true,
7071
});
7172

72-
const getBgColor = (el: HTMLElement) => {
73-
const style = window.getComputedStyle(el);
74-
if (style.backgroundColor && !['rgba(0, 0, 0, 0)', 'rgba(0,0,0,0)', 'transparent'].includes(style.backgroundColor)) {
75-
return style.backgroundColor;
76-
} else {
77-
return el.parentElement ? getBgColor(el.parentElement) : 'transparent';
78-
}
79-
};
80-
8173
const rootEl = shallowRef<HTMLElement>();
8274
const bgSame = ref(false);
8375
const opened = ref(props.defaultOpen);
8476
const openedAtLeastOnce = ref(props.defaultOpen);
8577

86-
function enter(el) {
78+
function enter(el: Element) {
79+
if (!(el instanceof HTMLElement)) return;
8780
const elementHeight = el.getBoundingClientRect().height;
88-
el.style.height = 0;
81+
el.style.height = '0';
8982
el.offsetHeight; // reflow
90-
el.style.height = Math.min(elementHeight, props.maxHeight ?? Infinity) + 'px';
83+
el.style.height = `${Math.min(elementHeight, props.maxHeight ?? Infinity)}px`;
9184
}
9285

93-
function afterEnter(el) {
94-
el.style.height = null;
86+
function afterEnter(el: Element) {
87+
if (!(el instanceof HTMLElement)) return;
88+
el.style.height = '';
9589
}
9690

97-
function leave(el) {
91+
function leave(el: Element) {
92+
if (!(el instanceof HTMLElement)) return;
9893
const elementHeight = el.getBoundingClientRect().height;
99-
el.style.height = elementHeight + 'px';
94+
el.style.height = `${elementHeight}px`;
10095
el.offsetHeight; // reflow
101-
el.style.height = 0;
96+
el.style.height = '0';
10297
}
10398

104-
function afterLeave(el) {
105-
el.style.height = null;
99+
function afterLeave(el: Element) {
100+
if (!(el instanceof HTMLElement)) return;
101+
el.style.height = '';
106102
}
107103

108104
function toggle() {
@@ -117,7 +113,7 @@ function toggle() {
117113

118114
onMounted(() => {
119115
const computedStyle = getComputedStyle(document.documentElement);
120-
const parentBg = getBgColor(rootEl.value!.parentElement!);
116+
const parentBg = getBgColor(rootEl.value?.parentElement) ?? 'transparent';
121117
const myBg = computedStyle.getPropertyValue('--panel');
122118
bgSame.value = parentBg === myBg;
123119
});

packages/frontend/src/components/global/MkPageHeader.tabs.vue

+13-12
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export type Tab = {
5353
</script>
5454

5555
<script lang="ts" setup>
56-
import { onMounted, onUnmounted, watch, nextTick, shallowRef } from 'vue';
56+
import { nextTick, onMounted, onUnmounted, shallowRef, watch } from 'vue';
5757
import { defaultStore } from '@/store.js';
5858

5959
const props = withDefaults(defineProps<{
@@ -120,14 +120,14 @@ function onTabWheel(ev: WheelEvent) {
120120

121121
let entering = false;
122122

123-
async function enter(element: Element) {
123+
async function enter(el: Element) {
124+
if (!(el instanceof HTMLElement)) return;
124125
entering = true;
125-
const el = element as HTMLElement;
126126
const elementWidth = el.getBoundingClientRect().width;
127127
el.style.width = '0';
128128
el.style.paddingLeft = '0';
129-
el.offsetWidth; // force reflow
130-
el.style.width = elementWidth + 'px';
129+
el.offsetWidth; // reflow
130+
el.style.width = `${elementWidth}px`;
131131
el.style.paddingLeft = '';
132132
nextTick(() => {
133133
entering = false;
@@ -136,22 +136,23 @@ async function enter(element: Element) {
136136
setTimeout(renderTab, 170);
137137
}
138138

139-
function afterEnter(element: HTMLElement) {
139+
function afterEnter(el: Element) {
140+
if (!(el instanceof HTMLElement)) return;
140141
// element.style.width = '';
141142
}
142143

143-
async function leave(element: Element) {
144-
const el = element as HTMLElement;
144+
async function leave(el: Element) {
145+
if (!(el instanceof HTMLElement)) return;
145146
const elementWidth = el.getBoundingClientRect().width;
146-
el.style.width = elementWidth + 'px';
147+
el.style.width = `${elementWidth}px`;
147148
el.style.paddingLeft = '';
148-
el.offsetWidth; // force reflow
149+
el.offsetWidth; // reflow
149150
el.style.width = '0';
150151
el.style.paddingLeft = '0';
151152
}
152153

153-
function afterLeave(element: Element) {
154-
const el = element as HTMLElement;
154+
function afterLeave(el: Element) {
155+
if (!(el instanceof HTMLElement)) return;
155156
el.style.width = '';
156157
}
157158

packages/frontend/src/directives/adaptive-bg.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,12 @@
44
*/
55

66
import { Directive } from 'vue';
7+
import { getBgColor } from '@/scripts/tms/get-bg-color.js';
78

89
// eslint-disable-next-line import/no-default-export
910
export default {
1011
mounted(src, binding, vn) {
11-
const getBgColor = (el: HTMLElement) => {
12-
const style = window.getComputedStyle(el);
13-
if (style.backgroundColor && !['rgba(0, 0, 0, 0)', 'rgba(0,0,0,0)', 'transparent'].includes(style.backgroundColor)) {
14-
return style.backgroundColor;
15-
} else {
16-
return el.parentElement ? getBgColor(el.parentElement) : 'transparent';
17-
}
18-
};
19-
20-
const parentBg = getBgColor(src.parentElement);
12+
const parentBg = getBgColor(src.parentElement) ?? 'transparent';
2113

2214
const myBg = window.getComputedStyle(src).backgroundColor;
2315

packages/frontend/src/directives/adaptive-border.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,12 @@
44
*/
55

66
import { Directive } from 'vue';
7+
import { getBgColor } from '@/scripts/tms/get-bg-color.js';
78

89
// eslint-disable-next-line import/no-default-export
910
export default {
1011
mounted(src, binding, vn) {
11-
const getBgColor = (el: HTMLElement) => {
12-
const style = window.getComputedStyle(el);
13-
if (style.backgroundColor && !['rgba(0, 0, 0, 0)', 'rgba(0,0,0,0)', 'transparent'].includes(style.backgroundColor)) {
14-
return style.backgroundColor;
15-
} else {
16-
return el.parentElement ? getBgColor(el.parentElement) : 'transparent';
17-
}
18-
};
19-
20-
const parentBg = getBgColor(src.parentElement);
12+
const parentBg = getBgColor(src.parentElement) ?? 'transparent';
2113

2214
const myBg = window.getComputedStyle(src).backgroundColor;
2315

packages/frontend/src/directives/panel.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,12 @@
44
*/
55

66
import { Directive } from 'vue';
7+
import { getBgColor } from '@/scripts/tms/get-bg-color.js';
78

89
// eslint-disable-next-line import/no-default-export
910
export default {
1011
mounted(src, binding, vn) {
11-
const getBgColor = (el: HTMLElement) => {
12-
const style = window.getComputedStyle(el);
13-
if (style.backgroundColor && !['rgba(0, 0, 0, 0)', 'rgba(0,0,0,0)', 'transparent'].includes(style.backgroundColor)) {
14-
return style.backgroundColor;
15-
} else {
16-
return el.parentElement ? getBgColor(el.parentElement) : 'transparent';
17-
}
18-
};
19-
20-
const parentBg = getBgColor(src.parentElement);
12+
const parentBg = getBgColor(src.parentElement) ?? 'transparent';
2113

2214
const myBg = getComputedStyle(document.documentElement).getPropertyValue('--panel');
2315

0 commit comments

Comments
 (0)