11import React from 'react' ;
2+ import canUseDom from 'dom-helpers/util/inDOM' ;
23
4+ import getOwnerDocument from 'dom-helpers/ownerDocument' ;
5+ import getOwnerWindow from 'dom-helpers/ownerWindow' ;
36
4- let canUseDom = ! ! (
5- typeof window !== 'undefined' &&
6- window . document &&
7- window . document . createElement
8- ) ;
7+ import contains from 'dom-helpers/query/contains' ;
8+ import activeElement from 'dom-helpers/activeElement' ;
99
10+ import getOffset from 'dom-helpers/query/offset' ;
11+ import offsetParent from 'dom-helpers/query/offsetParent' ;
12+ import getPosition from 'dom-helpers/query/position' ;
13+
14+ import css from 'dom-helpers/style' ;
1015
11- /**
12- * Get elements owner document
13- *
14- * @param {ReactComponent|HTMLElement } componentOrElement
15- * @returns {HTMLElement }
16- */
1716function ownerDocument ( componentOrElement ) {
1817 let elem = React . findDOMNode ( componentOrElement ) ;
19- return ( elem && elem . ownerDocument ) || document ;
18+ return getOwnerDocument ( ( elem && elem . ownerDocument ) || document ) ;
2019}
2120
2221function ownerWindow ( componentOrElement ) {
2322 let doc = ownerDocument ( componentOrElement ) ;
24- return doc . defaultView
25- ? doc . defaultView
26- : doc . parentWindow ;
27- }
28-
29- /**
30- * get the active element, safe in IE
31- * @return {HTMLElement }
32- */
33- function getActiveElement ( componentOrElement ) {
34- let doc = ownerDocument ( componentOrElement ) ;
35-
36- try {
37- return doc . activeElement || doc . body ;
38- } catch ( e ) {
39- return doc . body ;
40- }
41- }
42-
43- /**
44- * Shortcut to compute element style
45- *
46- * @param {HTMLElement } elem
47- * @returns {CssStyle }
48- */
49- function getComputedStyles ( elem ) {
50- return ownerDocument ( elem ) . defaultView . getComputedStyle ( elem , null ) ;
51- }
52-
53- /**
54- * Get elements offset
55- *
56- * TODO: REMOVE JQUERY!
57- *
58- * @param {HTMLElement } DOMNode
59- * @returns {{top: number, left: number} }
60- */
61- function getOffset ( DOMNode ) {
62- if ( window . jQuery ) {
63- return window . jQuery ( DOMNode ) . offset ( ) ;
64- }
65-
66- let docElem = ownerDocument ( DOMNode ) . documentElement ;
67- let box = { top : 0 , left : 0 } ;
68-
69- // If we don't have gBCR, just use 0,0 rather than error
70- // BlackBerry 5, iOS 3 (original iPhone)
71- if ( typeof DOMNode . getBoundingClientRect !== 'undefined' ) {
72- box = DOMNode . getBoundingClientRect ( ) ;
73- }
74-
75- return {
76- top : box . top + window . pageYOffset - docElem . clientTop ,
77- left : box . left + window . pageXOffset - docElem . clientLeft
78- } ;
79- }
80-
81- /**
82- * Get elements position
83- *
84- * TODO: REMOVE JQUERY!
85- *
86- * @param {HTMLElement } elem
87- * @param {HTMLElement? } offsetParent
88- * @returns {{top: number, left: number} }
89- */
90- function getPosition ( elem , offsetParent ) {
91- let offset ,
92- parentOffset ;
93-
94- if ( window . jQuery ) {
95- if ( ! offsetParent ) {
96- return window . jQuery ( elem ) . position ( ) ;
97- }
98-
99- offset = window . jQuery ( elem ) . offset ( ) ;
100- parentOffset = window . jQuery ( offsetParent ) . offset ( ) ;
101-
102- // Get element offset relative to offsetParent
103- return {
104- top : offset . top - parentOffset . top ,
105- left : offset . left - parentOffset . left
106- } ;
107- }
108-
109- parentOffset = { top : 0 , left : 0 } ;
110-
111- // Fixed elements are offset from window (parentOffset = {top:0, left: 0}, because it is its only offset parent
112- if ( getComputedStyles ( elem ) . position === 'fixed' ) {
113- // We assume that getBoundingClientRect is available when computed position is fixed
114- offset = elem . getBoundingClientRect ( ) ;
115-
116- } else {
117- if ( ! offsetParent ) {
118- // Get *real* offsetParent
119- offsetParent = offsetParentFunc ( elem ) ;
120- }
121-
122- // Get correct offsets
123- offset = getOffset ( elem ) ;
124- if ( offsetParent . nodeName !== 'HTML' ) {
125- parentOffset = getOffset ( offsetParent ) ;
126- }
127-
128- // Add offsetParent borders
129- parentOffset . top += parseInt ( getComputedStyles ( offsetParent ) . borderTopWidth , 10 ) ;
130- parentOffset . left += parseInt ( getComputedStyles ( offsetParent ) . borderLeftWidth , 10 ) ;
131- }
132-
133- // Subtract parent offsets and element margins
134- return {
135- top : offset . top - parentOffset . top - parseInt ( getComputedStyles ( elem ) . marginTop , 10 ) ,
136- left : offset . left - parentOffset . left - parseInt ( getComputedStyles ( elem ) . marginLeft , 10 )
137- } ;
23+ return getOwnerWindow ( doc ) ;
13824}
13925
14026/**
@@ -156,57 +42,15 @@ function getSize(elem) {
15642 return rect ;
15743}
15844
159- /**
160- * Get parent element
161- *
162- * @param {HTMLElement? } elem
163- * @returns {HTMLElement }
164- */
165- function offsetParentFunc ( elem ) {
166- let docElem = ownerDocument ( elem ) . documentElement ;
167- let offsetParent = elem . offsetParent || docElem ;
168-
169- while ( offsetParent && ( offsetParent . nodeName !== 'HTML' &&
170- getComputedStyles ( offsetParent ) . position === 'static' ) ) {
171- offsetParent = offsetParent . offsetParent ;
172- }
173-
174- return offsetParent || docElem ;
175- }
176-
177- /**
178- * Cross browser .contains() polyfill
179- * @param {HTMLElement } elem
180- * @param {HTMLElement } inner
181- * @return {bool }
182- */
183- function contains ( elem , inner ) {
184- function ie8Contains ( root , node ) {
185- while ( node ) {
186- if ( node === root ) {
187- return true ;
188- }
189- node = node . parentNode ;
190- }
191- return false ;
192- }
193-
194- return ( elem && elem . contains )
195- ? elem . contains ( inner )
196- : ( elem && elem . compareDocumentPosition )
197- ? elem === inner || ! ! ( elem . compareDocumentPosition ( inner ) & 16 )
198- : ie8Contains ( elem , inner ) ;
199- }
200-
20145export default {
20246 canUseDom,
47+ css,
20348 contains,
20449 ownerWindow,
20550 ownerDocument,
206- getComputedStyles,
20751 getOffset,
20852 getPosition,
20953 getSize,
210- activeElement : getActiveElement ,
211- offsetParent : offsetParentFunc
54+ activeElement,
55+ offsetParent
21256} ;
0 commit comments