diff --git a/lib/core/src/dom/index.ts b/lib/core/src/dom/index.ts index 40eb38f5d8..175868144b 100644 --- a/lib/core/src/dom/index.ts +++ b/lib/core/src/dom/index.ts @@ -3,3 +3,4 @@ export * from './run-js'; export * from './scroll-into-view'; export * from './get-lib'; export * from './is-detached'; +export * from './resize'; diff --git a/lib/core/src/dom/resize.ts b/lib/core/src/dom/resize.ts new file mode 100644 index 0000000000..6b9a82d116 --- /dev/null +++ b/lib/core/src/dom/resize.ts @@ -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); +};