|
11 | 11 | */
|
12 | 12 |
|
13 | 13 | import {Axis, Placement, PlacementAxis, SizeAxis} from '@react-types/overlays';
|
14 |
| -import getCss from 'dom-helpers/css'; |
15 |
| -import getOffset from 'dom-helpers/offset'; |
16 |
| -import getPosition from 'dom-helpers/position'; |
17 |
| -import getScrollLeft from 'dom-helpers/scrollLeft'; |
18 |
| -import getScrollTop from 'dom-helpers/scrollTop'; |
19 |
| -import ownerDocument from 'dom-helpers/ownerDocument'; |
20 | 14 |
|
21 | 15 | interface Position {
|
22 | 16 | top?: number,
|
@@ -104,19 +98,16 @@ function getContainerDimensions(containerNode: HTMLElement): Dimensions {
|
104 | 98 | let scroll: Position = {};
|
105 | 99 |
|
106 | 100 | if (containerNode.tagName === 'BODY') {
|
107 |
| - width = visualViewport?.width ?? document.documentElement.clientWidth; |
108 |
| - height = visualViewport?.height ?? document.documentElement.clientHeight; |
109 |
| - |
110 |
| - scroll.top = |
111 |
| - getScrollTop(ownerDocument(containerNode).documentElement) || |
112 |
| - getScrollTop(containerNode); |
113 |
| - scroll.left = |
114 |
| - getScrollLeft(ownerDocument(containerNode).documentElement) || |
115 |
| - getScrollLeft(containerNode); |
| 101 | + let documentElement = document.documentElement; |
| 102 | + width = visualViewport?.width ?? documentElement.clientWidth; |
| 103 | + height = visualViewport?.height ?? documentElement.clientHeight; |
| 104 | + |
| 105 | + scroll.top = documentElement.scrollTop || containerNode.scrollTop; |
| 106 | + scroll.left = documentElement.scrollLeft || containerNode.scrollLeft; |
116 | 107 | } else {
|
117 | 108 | ({width, height, top, left} = getOffset(containerNode));
|
118 |
| - scroll.top = getScrollTop(containerNode); |
119 |
| - scroll.left = getScrollLeft(containerNode); |
| 109 | + scroll.top = containerNode.scrollTop; |
| 110 | + scroll.left = containerNode.scrollLeft; |
120 | 111 | }
|
121 | 112 |
|
122 | 113 | return {width, height, scroll, top, left};
|
@@ -380,8 +371,7 @@ export function calculatePosition(opts: PositionOpts): PositionResult {
|
380 | 371 | let childOffset: Offset = isBodyContainer ? getOffset(targetNode) : getPosition(targetNode, container);
|
381 | 372 |
|
382 | 373 | if (!isBodyContainer) {
|
383 |
| - let marginTop = String(getCss(targetNode, 'marginTop')); |
384 |
| - let marginLeft = String(getCss(targetNode, 'marginLeft')); |
| 374 | + let {marginTop, marginLeft} = window.getComputedStyle(targetNode); |
385 | 375 | childOffset.top += parseInt(marginTop, 10) || 0;
|
386 | 376 | childOffset.left += parseInt(marginLeft, 10) || 0;
|
387 | 377 | }
|
@@ -411,3 +401,35 @@ export function calculatePosition(opts: PositionOpts): PositionResult {
|
411 | 401 | maxHeight
|
412 | 402 | );
|
413 | 403 | }
|
| 404 | + |
| 405 | +function getOffset(node: Element): Offset { |
| 406 | + let {top, left, width, height} = node.getBoundingClientRect(); |
| 407 | + let {scrollTop, scrollLeft, clientTop, clientLeft} = document.documentElement; |
| 408 | + return { |
| 409 | + top: top + scrollTop - clientTop, |
| 410 | + left: left + scrollLeft - clientLeft, |
| 411 | + width, |
| 412 | + height |
| 413 | + }; |
| 414 | +} |
| 415 | + |
| 416 | +function getPosition(node: Element, parent: Element): Offset { |
| 417 | + let style = window.getComputedStyle(node); |
| 418 | + let offset: Offset; |
| 419 | + if (style.position === 'fixed') { |
| 420 | + let {top, left, width, height} = node.getBoundingClientRect(); |
| 421 | + offset = {top, left, width, height}; |
| 422 | + } else { |
| 423 | + offset = getOffset(node); |
| 424 | + let parentOffset = getOffset(parent); |
| 425 | + let parentStyle = window.getComputedStyle(parent); |
| 426 | + parentOffset.top += (parseInt(parentStyle.borderTopWidth, 10) || 0) - parent.scrollTop; |
| 427 | + parentOffset.left += (parseInt(parentStyle.borderLeftWidth, 10) || 0) - parent.scrollLeft; |
| 428 | + offset.top -= parentOffset.top; |
| 429 | + offset.left -= parentOffset.left; |
| 430 | + } |
| 431 | + |
| 432 | + offset.top -= parseInt(style.marginTop, 10) || 0; |
| 433 | + offset.left -= parseInt(style.marginLeft, 10) || 0; |
| 434 | + return offset; |
| 435 | +} |
0 commit comments