Skip to content

Commit

Permalink
refactor: getBgColor関数の切り出し + fix types (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
taiyme authored Oct 11, 2024
1 parent 87ada07 commit b70dd33
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 111 deletions.
24 changes: 14 additions & 10 deletions packages/frontend/src/components/MkContainer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,30 @@ const showBody = ref(props.expanded);
const omitted = ref(false);
const manuallyOperated = ref(false);
function enter(el) {
function enter(el: Element) {
if (!(el instanceof HTMLElement)) return;
const elementHeight = el.getBoundingClientRect().height;
el.style.height = 0;
el.style.height = '0';
el.offsetHeight; // reflow
el.style.height = Math.min(elementHeight, props.maxHeight ?? Infinity) + 'px';
el.style.height = `${Math.min(elementHeight, props.maxHeight ?? Infinity)}px`;
}
function afterEnter(el) {
el.style.height = null;
function afterEnter(el: Element) {
if (!(el instanceof HTMLElement)) return;
el.style.height = '';
}
function leave(el) {
function leave(el: Element) {
if (!(el instanceof HTMLElement)) return;
const elementHeight = el.getBoundingClientRect().height;
el.style.height = elementHeight + 'px';
el.style.height = `${elementHeight}px`;
el.offsetHeight; // reflow
el.style.height = 0;
el.style.height = '0';
}
function afterLeave(el) {
el.style.height = null;
function afterLeave(el: Element) {
if (!(el instanceof HTMLElement)) return;
el.style.height = '';
}
const showMore = () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/frontend/src/components/MkDateSeparatedList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,14 @@ export default defineComponent({
return children;
};
function onBeforeLeave(element: Element) {
const el = element as HTMLElement;
function onBeforeLeave(el: Element) {
if (!(el instanceof HTMLElement)) return;
el.style.top = `${el.offsetTop}px`;
el.style.left = `${el.offsetLeft}px`;
}
function onLeaveCancelled(element: Element) {
const el = element as HTMLElement;
function onLeaveCancelled(el: Element) {
if (!(el instanceof HTMLElement)) return;
el.style.top = '';
el.style.left = '';
}
Expand Down
58 changes: 24 additions & 34 deletions packages/frontend/src/components/MkFoldableSection.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ SPDX-License-Identifier: AGPL-3.0-only

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

<script lang="ts" setup>
import { onMounted, ref, shallowRef, watch } from 'vue';
import tinycolor from 'tinycolor2';
import { miLocalStorage } from '@/local-storage.js';
import { defaultStore } from '@/store.js';
import { getBgColor } from '@/scripts/tms/get-bg-color.js';
const miLocalStoragePrefix = 'ui:folder:' as const;
const props = withDefaults(defineProps<{
expanded?: boolean;
persistKey?: string;
persistKey?: string | null;
}>(), {
expanded: true,
persistKey: null,
});
const rootEl = shallowRef<HTMLDivElement>();
const bg = ref<string>();
const rootEl = shallowRef<HTMLElement>();
const parentBg = ref<string | null>(null);
// eslint-disable-next-line vue/no-setup-props-reactivity-loss
const showBody = ref((props.persistKey && miLocalStorage.getItem(`${miLocalStoragePrefix}${props.persistKey}`)) ? (miLocalStorage.getItem(`${miLocalStoragePrefix}${props.persistKey}`) === 't') : props.expanded);
watch(showBody, () => {
Expand All @@ -55,55 +57,42 @@ watch(showBody, () => {
}
});
function enter(element: Element) {
const el = element as HTMLElement;
function enter(el: Element) {
if (!(el instanceof HTMLElement)) return;
const elementHeight = el.getBoundingClientRect().height;
el.style.height = '0';
el.offsetHeight; // reflow
el.style.height = elementHeight + 'px';
el.style.height = `${elementHeight}px`;
}
function afterEnter(element: Element) {
const el = element as HTMLElement;
el.style.height = 'unset';
function afterEnter(el: Element) {
if (!(el instanceof HTMLElement)) return;
el.style.height = '';
}
function leave(element: Element) {
const el = element as HTMLElement;
function leave(el: Element) {
if (!(el instanceof HTMLElement)) return;
const elementHeight = el.getBoundingClientRect().height;
el.style.height = elementHeight + 'px';
el.style.height = `${elementHeight}px`;
el.offsetHeight; // reflow
el.style.height = '0';
}
function afterLeave(element: Element) {
const el = element as HTMLElement;
el.style.height = 'unset';
function afterLeave(el: Element) {
if (!(el instanceof HTMLElement)) return;
el.style.height = '';
}
onMounted(() => {
function getParentBg(el?: HTMLElement | null): string {
if (el == null || el.tagName === 'BODY') return 'var(--bg)';
const background = el.style.background || el.style.backgroundColor;
if (background) {
return background;
} else {
return getParentBg(el.parentElement);
}
}
const rawBg = getParentBg(rootEl.value);
const _bg = tinycolor(rawBg.startsWith('var(') ? getComputedStyle(document.documentElement).getPropertyValue(rawBg.slice(4, -1)) : rawBg);
_bg.setAlpha(0.85);
bg.value = _bg.toRgbString();
parentBg.value = getBgColor(rootEl.value?.parentElement);
});
</script>

<style lang="scss" module>
.folderToggleEnterActive, .folderToggleLeaveActive {
overflow-y: hidden; // fallback (overflow-y: clip)
overflow-y: clip;
transition: opacity 0.5s, height 0.5s !important;
transition: opacity 0.3s, height 0.3s !important;
}
.folderToggleEnterFrom, .folderToggleLeaveTo {
Expand All @@ -120,8 +109,9 @@ onMounted(() => {
z-index: 10;
position: sticky;
top: var(--stickyTop, 0px);
-webkit-backdrop-filter: var(--blur, blur(8px));
backdrop-filter: var(--blur, blur(20px));
-webkit-backdrop-filter: var(--blur, blur(15px));
backdrop-filter: var(--blur, blur(15px));
background-color: color(from v-bind("parentBg ?? 'var(--bg)'") srgb r g b / 0.85);
}
.title {
Expand Down
38 changes: 17 additions & 21 deletions packages/frontend/src/components/MkFolder.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ SPDX-License-Identifier: AGPL-3.0-only
</template>

<script lang="ts" setup>
import { nextTick, onMounted, shallowRef, ref } from 'vue';
import { nextTick, onMounted, ref, shallowRef } from 'vue';
import { defaultStore } from '@/store.js';
import { getBgColor } from '@/scripts/tms/get-bg-color.js';
const props = withDefaults(defineProps<{
defaultOpen?: boolean;
Expand All @@ -69,40 +70,35 @@ const props = withDefaults(defineProps<{
withSpacer: true,
});
const getBgColor = (el: HTMLElement) => {
const style = window.getComputedStyle(el);
if (style.backgroundColor && !['rgba(0, 0, 0, 0)', 'rgba(0,0,0,0)', 'transparent'].includes(style.backgroundColor)) {
return style.backgroundColor;
} else {
return el.parentElement ? getBgColor(el.parentElement) : 'transparent';
}
};
const rootEl = shallowRef<HTMLElement>();
const bgSame = ref(false);
const opened = ref(props.defaultOpen);
const openedAtLeastOnce = ref(props.defaultOpen);
function enter(el) {
function enter(el: Element) {
if (!(el instanceof HTMLElement)) return;
const elementHeight = el.getBoundingClientRect().height;
el.style.height = 0;
el.style.height = '0';
el.offsetHeight; // reflow
el.style.height = Math.min(elementHeight, props.maxHeight ?? Infinity) + 'px';
el.style.height = `${Math.min(elementHeight, props.maxHeight ?? Infinity)}px`;
}
function afterEnter(el) {
el.style.height = null;
function afterEnter(el: Element) {
if (!(el instanceof HTMLElement)) return;
el.style.height = '';
}
function leave(el) {
function leave(el: Element) {
if (!(el instanceof HTMLElement)) return;
const elementHeight = el.getBoundingClientRect().height;
el.style.height = elementHeight + 'px';
el.style.height = `${elementHeight}px`;
el.offsetHeight; // reflow
el.style.height = 0;
el.style.height = '0';
}
function afterLeave(el) {
el.style.height = null;
function afterLeave(el: Element) {
if (!(el instanceof HTMLElement)) return;
el.style.height = '';
}
function toggle() {
Expand All @@ -117,7 +113,7 @@ function toggle() {
onMounted(() => {
const computedStyle = getComputedStyle(document.documentElement);
const parentBg = getBgColor(rootEl.value!.parentElement!);
const parentBg = getBgColor(rootEl.value?.parentElement) ?? 'transparent';
const myBg = computedStyle.getPropertyValue('--panel');
bgSame.value = parentBg === myBg;
});
Expand Down
25 changes: 13 additions & 12 deletions packages/frontend/src/components/global/MkPageHeader.tabs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export type Tab = {
</script>

<script lang="ts" setup>
import { onMounted, onUnmounted, watch, nextTick, shallowRef } from 'vue';
import { nextTick, onMounted, onUnmounted, shallowRef, watch } from 'vue';
import { defaultStore } from '@/store.js';
const props = withDefaults(defineProps<{
Expand Down Expand Up @@ -120,14 +120,14 @@ function onTabWheel(ev: WheelEvent) {
let entering = false;
async function enter(element: Element) {
async function enter(el: Element) {
if (!(el instanceof HTMLElement)) return;
entering = true;
const el = element as HTMLElement;
const elementWidth = el.getBoundingClientRect().width;
el.style.width = '0';
el.style.paddingLeft = '0';
el.offsetWidth; // force reflow
el.style.width = elementWidth + 'px';
el.offsetWidth; // reflow
el.style.width = `${elementWidth}px`;
el.style.paddingLeft = '';
nextTick(() => {
entering = false;
Expand All @@ -136,22 +136,23 @@ async function enter(element: Element) {
setTimeout(renderTab, 170);
}
function afterEnter(element: HTMLElement) {
function afterEnter(el: Element) {
if (!(el instanceof HTMLElement)) return;
// element.style.width = '';
}
async function leave(element: Element) {
const el = element as HTMLElement;
async function leave(el: Element) {
if (!(el instanceof HTMLElement)) return;
const elementWidth = el.getBoundingClientRect().width;
el.style.width = elementWidth + 'px';
el.style.width = `${elementWidth}px`;
el.style.paddingLeft = '';
el.offsetWidth; // force reflow
el.offsetWidth; // reflow
el.style.width = '0';
el.style.paddingLeft = '0';
}
function afterLeave(element: Element) {
const el = element as HTMLElement;
function afterLeave(el: Element) {
if (!(el instanceof HTMLElement)) return;
el.style.width = '';
}
Expand Down
12 changes: 2 additions & 10 deletions packages/frontend/src/directives/adaptive-bg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,12 @@
*/

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

// eslint-disable-next-line import/no-default-export
export default {
mounted(src, binding, vn) {
const getBgColor = (el: HTMLElement) => {
const style = window.getComputedStyle(el);
if (style.backgroundColor && !['rgba(0, 0, 0, 0)', 'rgba(0,0,0,0)', 'transparent'].includes(style.backgroundColor)) {
return style.backgroundColor;
} else {
return el.parentElement ? getBgColor(el.parentElement) : 'transparent';
}
};

const parentBg = getBgColor(src.parentElement);
const parentBg = getBgColor(src.parentElement) ?? 'transparent';

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

Expand Down
12 changes: 2 additions & 10 deletions packages/frontend/src/directives/adaptive-border.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,12 @@
*/

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

// eslint-disable-next-line import/no-default-export
export default {
mounted(src, binding, vn) {
const getBgColor = (el: HTMLElement) => {
const style = window.getComputedStyle(el);
if (style.backgroundColor && !['rgba(0, 0, 0, 0)', 'rgba(0,0,0,0)', 'transparent'].includes(style.backgroundColor)) {
return style.backgroundColor;
} else {
return el.parentElement ? getBgColor(el.parentElement) : 'transparent';
}
};

const parentBg = getBgColor(src.parentElement);
const parentBg = getBgColor(src.parentElement) ?? 'transparent';

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

Expand Down
12 changes: 2 additions & 10 deletions packages/frontend/src/directives/panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,12 @@
*/

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

// eslint-disable-next-line import/no-default-export
export default {
mounted(src, binding, vn) {
const getBgColor = (el: HTMLElement) => {
const style = window.getComputedStyle(el);
if (style.backgroundColor && !['rgba(0, 0, 0, 0)', 'rgba(0,0,0,0)', 'transparent'].includes(style.backgroundColor)) {
return style.backgroundColor;
} else {
return el.parentElement ? getBgColor(el.parentElement) : 'transparent';
}
};

const parentBg = getBgColor(src.parentElement);
const parentBg = getBgColor(src.parentElement) ?? 'transparent';

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

Expand Down
Loading

0 comments on commit b70dd33

Please sign in to comment.