diff --git a/addon/src/-private/element.js b/addon/src/-private/element.js new file mode 100644 index 00000000..63492695 --- /dev/null +++ b/addon/src/-private/element.js @@ -0,0 +1,18 @@ +/** + * Borrowed from jquery https://github.com/jquery/jquery/blob/a684e6ba836f7c553968d7d026ed7941e1a612d8/src/css/hiddenVisibleSelectors.js + * + * Elements are considered visible if they consume space in the document. Visible elements have a width or height that is greater than zero. + * + * Elements with visibility: hidden or opacity: 0 are considered visible, since they still consume space in the layout. + * + * @private + * @param {Element} element + * @returns boolean + */ +export function isVisible(element) { + return !!( + element.offsetWidth || + element.offsetHeight || + element.getClientRects().length + ); +} diff --git a/addon/src/properties/is-hidden.js b/addon/src/properties/is-hidden.js index bab0704b..ce8b226c 100644 --- a/addon/src/properties/is-hidden.js +++ b/addon/src/properties/is-hidden.js @@ -1,6 +1,7 @@ -import { guardMultiple, $ } from '../-private/helpers'; +import { guardMultiple } from '../-private/helpers'; import { findMany } from '../-private/finders'; import { getter } from '../macros/index'; +import { isVisible } from '../-private/element'; /** * Validates if an element or set of elements is hidden or does not exist in the DOM. @@ -82,6 +83,6 @@ export function isHidden(selector, userOptions = {}) { guardMultiple(elements, selector); - return elements.length === 0 || $(elements[0]).is(':hidden'); + return elements.length === 0 || !isVisible(elements[0]); }); } diff --git a/addon/src/properties/is-visible.js b/addon/src/properties/is-visible.js index 6cb2f485..70ea9dcc 100644 --- a/addon/src/properties/is-visible.js +++ b/addon/src/properties/is-visible.js @@ -1,6 +1,7 @@ -import { guardMultiple, $ } from '../-private/helpers'; +import { guardMultiple } from '../-private/helpers'; import { findMany } from '../-private/finders'; import { getter } from '../macros/index'; +import { isVisible as isElementVisible } from '../-private/element'; /** * Validates if an element or set of elements are visible. @@ -87,6 +88,6 @@ export function isVisible(selector, userOptions = {}) { let elements = findMany(this, selector, options); guardMultiple(elements, selector, options.multiple); - return elements.length === 1 && $(elements[0]).is(':visible'); + return elements.length === 1 && isElementVisible(elements[0]); }); }