Style.js.flow 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * Copyright (c) 2013-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @providesModule Style
  8. * @typechecks
  9. */
  10. var getStyleProperty = require('./getStyleProperty');
  11. /**
  12. * @param {DOMNode} element [description]
  13. * @param {string} name Overflow style property name.
  14. * @return {boolean} True if the supplied ndoe is scrollable.
  15. */
  16. function _isNodeScrollable(element, name) {
  17. var overflow = Style.get(element, name);
  18. return overflow === 'auto' || overflow === 'scroll';
  19. }
  20. /**
  21. * Utilities for querying and mutating style properties.
  22. */
  23. var Style = {
  24. /**
  25. * Gets the style property for the supplied node. This will return either the
  26. * computed style, if available, or the declared style.
  27. *
  28. * @param {DOMNode} node
  29. * @param {string} name Style property name.
  30. * @return {?string} Style property value.
  31. */
  32. get: getStyleProperty,
  33. /**
  34. * Determines the nearest ancestor of a node that is scrollable.
  35. *
  36. * NOTE: This can be expensive if used repeatedly or on a node nested deeply.
  37. *
  38. * @param {?DOMNode} node Node from which to start searching.
  39. * @return {?DOMWindow|DOMElement} Scroll parent of the supplied node.
  40. */
  41. getScrollParent: function (node) {
  42. if (!node) {
  43. return null;
  44. }
  45. var ownerDocument = node.ownerDocument;
  46. while (node && node !== ownerDocument.body) {
  47. if (_isNodeScrollable(node, 'overflow') || _isNodeScrollable(node, 'overflowY') || _isNodeScrollable(node, 'overflowX')) {
  48. return node;
  49. }
  50. node = node.parentNode;
  51. }
  52. return ownerDocument.defaultView || ownerDocument.parentWindow;
  53. }
  54. };
  55. module.exports = Style;