最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ResizeObserver only firing when web component added to the DOM and not on Resize - Stack Overflow

programmeradmin4浏览0评论

I've created a web ponent and am attempting to add a ResizeObserver to the element.

ResizeObserver is added on connectedCallback which appears to fire only once upon the element being added to the DOM, but never again when the element resizes.

I would expect that the ResizeObserver would be fired when the ponent is forced to resize by it's parent element. I've checked and watched the element size in the developer tools, and it definitely resizes.

I have also tested the same code with standard HTML element (id: #foo) and the callback works. So I can only assume the problem is related to the shadow DOM.

the specification says:

  1. Observation will fire when watched Element is inserted/removed from DOM
  2. Observation will fire when watched Element display gets set to none.
  3. Observations do not fire for non-replaced inline Elements.
  4. Observations will not be triggered by CSS transforms.
  5. Observation will fire when observation starts if Element is being rendered, and Element’s size is not 0,0.

I can confirm that point #1 happens.

I expect that point #5 should happen, but it never does. If Inspect the element, it's size is not 0,0 and does resize with the page.

I've gone over the MDN documentation for web ponents and ResizeObserver and a number of other websites and as far as I understand, I have everything correct.

Here is a JSFiddle of the code: /

I've created a web ponent and am attempting to add a ResizeObserver to the element.

ResizeObserver is added on connectedCallback which appears to fire only once upon the element being added to the DOM, but never again when the element resizes.

I would expect that the ResizeObserver would be fired when the ponent is forced to resize by it's parent element. I've checked and watched the element size in the developer tools, and it definitely resizes.

I have also tested the same code with standard HTML element (id: #foo) and the callback works. So I can only assume the problem is related to the shadow DOM.

the specification says:

  1. Observation will fire when watched Element is inserted/removed from DOM
  2. Observation will fire when watched Element display gets set to none.
  3. Observations do not fire for non-replaced inline Elements.
  4. Observations will not be triggered by CSS transforms.
  5. Observation will fire when observation starts if Element is being rendered, and Element’s size is not 0,0.

I can confirm that point #1 happens.

I expect that point #5 should happen, but it never does. If Inspect the element, it's size is not 0,0 and does resize with the page.

I've gone over the MDN documentation for web ponents and ResizeObserver and a number of other websites and as far as I understand, I have everything correct.

Here is a JSFiddle of the code: https://jsfiddle/h15e48fx/1/

Share Improve this question edited Nov 26, 2022 at 18:28 leoplaw asked Nov 26, 2022 at 15:41 leoplawleoplaw 19213 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

The custom element style defaultes to display: inline.

Setting it to display: block causes the ResizeObserver to fire when the ponent resizes.

发布评论

评论列表(0)

  1. 暂无评论