mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
refactor: Migrate ResizeObserver
to composition API (no-changelog) (#9745)
This commit is contained in:
parent
8b4a9dbced
commit
2dad9ce44c
|
@ -1,86 +1,63 @@
|
||||||
<template>
|
<script setup lang="ts">
|
||||||
<div ref="root">
|
import { ref, onMounted, onBeforeUnmount, computed } from 'vue';
|
||||||
<slot :bp="bp"></slot>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<script lang="ts">
|
export type BreakpointDefinition = { bp: string; width: number };
|
||||||
import { defineComponent } from 'vue';
|
|
||||||
|
|
||||||
export default defineComponent({
|
const props = defineProps({
|
||||||
name: 'ResizeObserver',
|
enabled: {
|
||||||
props: {
|
type: Boolean,
|
||||||
enabled: {
|
default: true,
|
||||||
type: Boolean,
|
},
|
||||||
default: true,
|
breakpoints: {
|
||||||
},
|
type: Array as () => BreakpointDefinition[],
|
||||||
breakpoints: {
|
validator: (breakpoints: BreakpointDefinition[]) => {
|
||||||
type: Array,
|
if (breakpoints.length === 0) return true;
|
||||||
validator: (bps: Array<{ bp: string; width: number }>) => {
|
|
||||||
return (
|
return breakpoints.every((bp) => typeof bp.width === 'number' && typeof bp.bp === 'string');
|
||||||
Array.isArray(bps) &&
|
|
||||||
bps.reduce(
|
|
||||||
(accu, { width, bp }) => accu && typeof width === 'number' && typeof bp === 'string',
|
|
||||||
true,
|
|
||||||
)
|
|
||||||
);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
data(): { observer: ResizeObserver | null; bp: string } {
|
|
||||||
return {
|
|
||||||
observer: null,
|
|
||||||
bp: '',
|
|
||||||
};
|
|
||||||
},
|
|
||||||
mounted() {
|
|
||||||
if (!this.enabled) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const root = this.$refs.root as HTMLDivElement;
|
|
||||||
|
|
||||||
if (!root) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.bp = this.getBreakpointFromWidth(root.offsetWidth);
|
|
||||||
|
|
||||||
const observer = new ResizeObserver((entries) => {
|
|
||||||
entries.forEach((entry) => {
|
|
||||||
// We wrap it in requestAnimationFrame to avoid this error - ResizeObserver loop limit exceeded
|
|
||||||
requestAnimationFrame(() => {
|
|
||||||
this.bp = this.getBreakpointFromWidth(entry.contentRect.width);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
this.observer = observer;
|
|
||||||
observer.observe(root);
|
|
||||||
},
|
|
||||||
beforeUnmount() {
|
|
||||||
if (this.enabled) {
|
|
||||||
this.observer?.disconnect();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
methods: {
|
|
||||||
getBreakpointFromWidth(width: number): string {
|
|
||||||
let newBP = 'default';
|
|
||||||
const unsortedBreakpoints = [...(this.breakpoints || [])] as Array<{
|
|
||||||
width: number;
|
|
||||||
bp: string;
|
|
||||||
}>;
|
|
||||||
|
|
||||||
const bps = unsortedBreakpoints.sort((a, b) => a.width - b.width);
|
|
||||||
for (let i = 0; i < bps.length; i++) {
|
|
||||||
if (width < bps[i].width) {
|
|
||||||
newBP = bps[i].bp;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return newBP;
|
|
||||||
},
|
},
|
||||||
|
default: () => [],
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const observer = ref<ResizeObserver | null>(null);
|
||||||
|
const breakpoint = ref('');
|
||||||
|
const root = ref<HTMLDivElement | null>(null);
|
||||||
|
|
||||||
|
const sortedBreakpoints = computed(() => [...props.breakpoints].sort((a, b) => a.width - b.width));
|
||||||
|
|
||||||
|
const getBreakpointFromWidth = (width: number): string => {
|
||||||
|
return (
|
||||||
|
sortedBreakpoints.value.find((sortedBreakpoint) => width < sortedBreakpoint.width)?.bp ??
|
||||||
|
'default'
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
if (!props.enabled) return;
|
||||||
|
if (!root.value) return;
|
||||||
|
|
||||||
|
breakpoint.value = getBreakpointFromWidth(root.value.offsetWidth);
|
||||||
|
|
||||||
|
observer.value = new ResizeObserver((entries) => {
|
||||||
|
entries.forEach((entry) => {
|
||||||
|
requestAnimationFrame(() => {
|
||||||
|
breakpoint.value = getBreakpointFromWidth(entry.contentRect.width);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
observer.value.observe(root.value);
|
||||||
|
});
|
||||||
|
|
||||||
|
onBeforeUnmount(() => {
|
||||||
|
if (observer.value) {
|
||||||
|
observer.value.disconnect();
|
||||||
|
}
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div ref="root">
|
||||||
|
<slot :bp="breakpoint"></slot>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
Loading…
Reference in a new issue