is-offscreen.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import getComposedParent from './get-composed-parent';
  2. import getElementCoordinates from './get-element-coordinates';
  3. import getViewportSize from './get-viewport-size';
  4. function noParentScrolled(element, offset) {
  5. element = getComposedParent(element);
  6. while (element && element.nodeName.toLowerCase() !== 'html') {
  7. if (element.scrollTop) {
  8. offset += element.scrollTop;
  9. if (offset >= 0) {
  10. return false;
  11. }
  12. }
  13. element = getComposedParent(element);
  14. }
  15. return true;
  16. }
  17. /**
  18. * Determines if element is off screen
  19. * @method isOffscreen
  20. * @memberof axe.commons.dom
  21. * @instance
  22. * @param {Element} element
  23. * @return {Boolean}
  24. */
  25. function isOffscreen(element) {
  26. let leftBoundary;
  27. const docElement = document.documentElement;
  28. const styl = window.getComputedStyle(element);
  29. const dir = window
  30. .getComputedStyle(document.body || docElement)
  31. .getPropertyValue('direction');
  32. const coords = getElementCoordinates(element);
  33. // bottom edge beyond
  34. if (
  35. coords.bottom < 0 &&
  36. (noParentScrolled(element, coords.bottom) || styl.position === 'absolute')
  37. ) {
  38. return true;
  39. }
  40. if (coords.left === 0 && coords.right === 0) {
  41. //This is an edge case, an empty (zero-width) element that isn't positioned 'off screen'.
  42. return false;
  43. }
  44. if (dir === 'ltr') {
  45. if (coords.right <= 0) {
  46. return true;
  47. }
  48. } else {
  49. leftBoundary = Math.max(
  50. docElement.scrollWidth,
  51. getViewportSize(window).width
  52. );
  53. if (coords.left >= leftBoundary) {
  54. return true;
  55. }
  56. }
  57. return false;
  58. }
  59. export default isOffscreen;