Skip to content

Commit

Permalink
feat(toolkit/observable): warn if document root is not positioned whe…
Browse files Browse the repository at this point in the history
…n using `fromBoundingClientRect$`
  • Loading branch information
danielwiehl committed Oct 10, 2024
1 parent 9979db9 commit de8d31a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/site/tools/observable.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fromBoundingClientRect$(element).subscribe((clientRect: DOMRect) => {
```

***
The target element and the document root (`<html>`) must be positioned (`relative`, `absolute`, or `fixed`). If not, positioning is changed to `relative`.
The target element and the document root (`<html>`) must be positioned `relative` or `absolute`. If not, a warning is logged and positioning is changed to `relative`.
***

*Note:*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import {fromResize$} from './resize.observable';
*
* Upon subscription, emits the current bounding box, and then continuously when the bounding box changes. The Observable never completes.
*
* The target element and the document root (`<html>`) must be positioned (`relative`, `absolute`, or `fixed`). If not, positioning is changed to `relative`.
*
* The target element and the document root (`<html>`) must be positioned `relative` or `absolute`.
* If not, a warning is logged and positioning is changed to `relative`.
* Note:
* As of 2024, there is no native browser API to observe the position of an element. This implementation uses {@link IntersectionObserver} and
* {@link ResizeObserver} to detect position changes. For tracking only size changes, use {@link fromResize$} instead.
Expand Down Expand Up @@ -217,6 +218,7 @@ function ensureElementPositioned(element: HTMLElement): void {
const styleSheet = new CSSStyleSheet({});
styleSheet.insertRule(`html { position: relative; }`);
document.adoptedStyleSheets.push(styleSheet);
console?.warn?.('[@scion/toolkit] fromBoundingClientRect$ requires the document root element (<html>) to be positioned relative or absolute. Changing positioning to relative.');
}
else {
setStyle(element, {position: 'relative'});
Expand Down

0 comments on commit de8d31a

Please sign in to comment.