-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Migrate
ResizeObserver
to composition API (no-changelog) (#…
- Loading branch information
Showing
1 changed file
with
49 additions
and
72 deletions.
There are no files selected for viewing
121 changes: 49 additions & 72 deletions
121
packages/design-system/src/components/ResizeObserver/ResizeObserver.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,63 @@ | ||
<template> | ||
<div ref="root"> | ||
<slot :bp="bp"></slot> | ||
</div> | ||
</template> | ||
<script setup lang="ts"> | ||
import { ref, onMounted, onBeforeUnmount, computed } from 'vue'; | ||
<script lang="ts"> | ||
import { defineComponent } from 'vue'; | ||
export type BreakpointDefinition = { bp: string; width: number }; | ||
export default defineComponent({ | ||
name: 'ResizeObserver', | ||
props: { | ||
enabled: { | ||
type: Boolean, | ||
default: true, | ||
}, | ||
breakpoints: { | ||
type: Array, | ||
validator: (bps: Array<{ bp: string; width: number }>) => { | ||
return ( | ||
Array.isArray(bps) && | ||
bps.reduce( | ||
(accu, { width, bp }) => accu && typeof width === 'number' && typeof bp === 'string', | ||
true, | ||
) | ||
); | ||
}, | ||
}, | ||
const props = defineProps({ | ||
enabled: { | ||
type: Boolean, | ||
default: true, | ||
}, | ||
data(): { observer: ResizeObserver | null; bp: string } { | ||
return { | ||
observer: null, | ||
bp: '', | ||
}; | ||
breakpoints: { | ||
type: Array as () => BreakpointDefinition[], | ||
validator: (breakpoints: BreakpointDefinition[]) => { | ||
if (breakpoints.length === 0) return true; | ||
return breakpoints.every((bp) => typeof bp.width === 'number' && typeof bp.bp === 'string'); | ||
}, | ||
default: () => [], | ||
}, | ||
mounted() { | ||
if (!this.enabled) { | ||
return; | ||
} | ||
}); | ||
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 root = this.$refs.root as HTMLDivElement; | ||
const getBreakpointFromWidth = (width: number): string => { | ||
return ( | ||
sortedBreakpoints.value.find((sortedBreakpoint) => width < sortedBreakpoint.width)?.bp ?? | ||
'default' | ||
); | ||
}; | ||
if (!root) { | ||
return; | ||
} | ||
onMounted(() => { | ||
if (!props.enabled) return; | ||
if (!root.value) return; | ||
this.bp = this.getBreakpointFromWidth(root.offsetWidth); | ||
breakpoint.value = getBreakpointFromWidth(root.value.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); | ||
}); | ||
observer.value = new ResizeObserver((entries) => { | ||
entries.forEach((entry) => { | ||
requestAnimationFrame(() => { | ||
breakpoint.value = 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; | ||
} | ||
} | ||
observer.value.observe(root.value); | ||
}); | ||
return newBP; | ||
}, | ||
}, | ||
onBeforeUnmount(() => { | ||
if (observer.value) { | ||
observer.value.disconnect(); | ||
} | ||
}); | ||
</script> | ||
|
||
<template> | ||
<div ref="root"> | ||
<slot :bp="breakpoint"></slot> | ||
</div> | ||
</template> |