Skip to content

Commit

Permalink
* core: add dom helper to listen resize of elements.
Browse files Browse the repository at this point in the history
  • Loading branch information
catouse committed Jun 17, 2024
1 parent 3e8c4b8 commit dbec684
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/core/src/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './run-js';
export * from './scroll-into-view';
export * from './get-lib';
export * from './is-detached';
export * from './resize';
30 changes: 30 additions & 0 deletions lib/core/src/dom/resize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {$, Cash, Selector} from '../cash';

/**
* Listen element resize with ResizeObserver.
*
* @param selector Element selector to check.
* @returns True if the element is disabled.
*/
export function listenResize(selector: Selector, callback: ResizeObserverCallback): ResizeObserver {
const $elements = $(selector);
const observer = new ResizeObserver(callback);

$elements.each((_, ele) => {
observer.observe(ele);
});

return observer;
}

/* Declare types. */
declare module 'cash-dom' {
interface Cash {
resize(callback: ResizeObserverCallback): ResizeObserver;
}
}

/* Extend as $.fn.resize() */
$.fn.resize = function (this: Cash, callback: ResizeObserverCallback) {
return listenResize(this, callback);
};

0 comments on commit dbec684

Please sign in to comment.