Bug 216240 - vw units doesn't get updated inside shadow trees as frame is resized
Summary: vw units doesn't get updated inside shadow trees as frame is resized
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Layout and Rendering (show other bugs)
Version: Safari 13
Hardware: Mac macOS 10.14
: P2 Major
Assignee: Ryosuke Niwa
URL:
Keywords: InRadar
Depends on:
Blocks: 148695
  Show dependency treegraph
 
Reported: 2020-09-07 03:49 PDT by Roman Prudnikov
Modified: 2022-07-05 12:10 PDT (History)
8 users (show)

See Also:


Attachments
WIP (1.01 KB, patch)
2022-07-04 10:55 PDT, Ryosuke Niwa
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Roman Prudnikov 2020-09-07 03:49:52 PDT
Summary:
Elements inside a custom element with a shadow tree render strangely if they're positioned fixed/absolute with vw units from left/right. First render looks correct, but if the page has been resized in width, the element stays the same, not following its left/right property, resulting in being outside of the viewport.

Reproduce:
https://jsfiddle.net/tuk1905w/3/

1. Create a custom element
2. Attach Shadow with "mode: open"
3. Append an element
4. Add styling to that element so it has "position: fixed" and "left: calc(100vw - 100px)"
5. Resize the window changing its width
6. The element positioned strangely, not the way when it's outside a shadow tree

Workaround (the way I fixed it):
https://jsfiddle.net/y4azr6t5/

On the resize handler I set a custom value to `window.innerWidth * 0.01` and use that custom value to position the element inside the shadow tree.

JS:
```
  const vw = window.innerWidth * 0.01
  document.documentElement.style.setProperty("--vw", `${vw}px`)
```
CSS:
```
button {
  left: calc((var(--vw, 1vw) * 100) - 200px);
}
```

Note:
Interesting that if I open web inspector and change any value of that positioned element Safari rerenders it correctly. But it works only until next resize.
Comment 1 Radar WebKit Bug Importer 2020-09-08 09:43:36 PDT
<rdar://problem/68513285>
Comment 2 Vahan Hartooni 2022-06-03 17:32:02 PDT
This doesn't just affect positioning, but dimensions as well. Take for example this example: https://jsfiddle.net/auzekghL/

When you resize the window, the blue box doesn't update it's width or height to fit the window.

Compare that to this blue box which does: https://jsfiddle.net/1jyacbzh/

Both blue boxes have these two css rules:

  height: calc(100vh - 50px);
  width: 100vw;

But the first one is in a shadow dom.
Comment 3 Vahan Hartooni 2022-06-06 11:35:16 PDT
There is a workaround where you can apply `width: 100vw` on the `:host`: https://jsfiddle.net/3kxn0veh/

Unfortunately, you can't utilize the same technique on height: https://jsfiddle.net/3nktrqw9/
Comment 4 Ryosuke Niwa 2022-07-04 10:48:02 PDT
This is a bug in Document::updateViewportUnitsOnResize(). It needs to iterate over all shadow roots & invalidate styles instead of just at the document level.
Comment 5 Ryosuke Niwa 2022-07-04 10:55:49 PDT
Created attachment 460657 [details]
WIP
Comment 6 Ryosuke Niwa 2022-07-04 11:21:13 PDT
Pull request: https://github.com/WebKit/WebKit/pull/2061
Comment 7 Ryosuke Niwa 2022-07-04 12:14:58 PDT
Pull request: https://github.com/WebKit/WebKit/pull/2062
Comment 8 EWS 2022-07-05 12:10:32 PDT
Committed 252149@main (50df76018d84): <https://commits.webkit.org/252149@main>

Reviewed commits have been landed. Closing PR #2062 and removing active labels.