Skip to content

Commit

Permalink
include element in onResize directive callback
Browse files Browse the repository at this point in the history
  • Loading branch information
electrovir committed Oct 14, 2023
1 parent d29ff18 commit 3b84f07
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "element-vir",
"version": "16.4.7",
"version": "16.5.0",
"keywords": [
"custom",
"web",
Expand Down
17 changes: 11 additions & 6 deletions src/declarative-element/directives/on-resize.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {assertIsElementPartInfo} from './directive-helpers';

export type OnResizeCallback = (
/** Only these two properties are supported in all major modern browsers */
entry: Readonly<Pick<ResizeObserverEntry, 'target' | 'contentRect'>>,
size: Readonly<Pick<ResizeObserverEntry, 'target' | 'contentRect'>>,
element: Element,
) => void;

const directiveName = 'onResize';
Expand All @@ -29,20 +30,24 @@ export const onResize = directive(
`${directiveName} observation triggered but the first entry was empty.`,
);
}
this.callback?.({target: resizeEntry.target, contentRect: resizeEntry.contentRect});
this.callback?.(
{target: resizeEntry.target, contentRect: resizeEntry.contentRect},
this.element!,
);
}

override update(partInfo: PartInfo, [callback]: [OnResizeCallback]) {
assertIsElementPartInfo(partInfo, directiveName);
this.callback = callback;
const newElement = partInfo.element;
const oldElement = this.element;
// if the element changes we need to observe the new one
if (newElement !== this.element) {
if (this.element) {
this.resizeObserver.unobserve(this.element);
if (newElement !== oldElement) {
this.element = newElement;
if (oldElement) {
this.resizeObserver.unobserve(oldElement);
}
this.resizeObserver.observe(newElement);
this.element = newElement;
}
return this.render(callback);
}
Expand Down

0 comments on commit 3b84f07

Please sign in to comment.