Skip to content

Commit

Permalink
Replace ResizeSensor
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexVelezLl committed Feb 28, 2024
1 parent 7971ea4 commit 501e37c
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions lib/composables/_useKResponsiveElement.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import ResizeSensor from 'css-element-queries/src/ResizeSensor';
import { onMounted, onUnmounted, ref, getCurrentInstance } from '@vue/composition-api';

let _resizeObserver;

if (typeof window !== 'undefined' && window.ResizeObserver) {
_resizeObserver = new ResizeObserver(entries => {
for (const entry of entries) {
if (entry.target._resizeListener) {
entry.target._resizeListener();
}
}
});
}

export default function useKResponsiveElement() {
const elementWidth = ref(0);
const elementHeight = ref(0);
const instance = getCurrentInstance();

let _resizeSensor;

function updateEl() {
const { $el } = instance.proxy || {};
const { clientHeight, clientWidth } = $el || {};
Expand All @@ -17,14 +26,17 @@ export default function useKResponsiveElement() {

onMounted(() => {
updateEl();
_resizeSensor = new ResizeSensor(instance.proxy.$el, updateEl);
if (_resizeObserver) {
instance.proxy.$el._resizeListener = updateEl;

_resizeObserver.observe(instance.proxy.$el);
}
});

onUnmounted(() => {
if (!_resizeSensor) {
return;
if (_resizeObserver) {
_resizeObserver.unobserve(instance.proxy.$el);
}
_resizeSensor.detach(instance.proxy.$el, updateEl);
});

return {
Expand Down

0 comments on commit 501e37c

Please sign in to comment.