Skip to content

Commit 105175b

Browse files
committed
feat: make isVisible more comprehensive (backport from v2)
1 parent c6d28ef commit 105175b

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

packages/shared/is-visible.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@
55
*/
66

77
function isStyleVisible(element) {
8-
const { display, visibility, opacity } = element.style
8+
if (!(element instanceof HTMLElement) && !(element instanceof SVGElement)) {
9+
return false
10+
}
11+
12+
// Per https://lists.w3.org/Archives/Public/www-style/2018May/0031.html
13+
// getComputedStyle should only work with connected elements.
14+
const { display, visibility, opacity } = element.isConnected
15+
? getComputedStyle(element)
16+
: element.style
917
return (
1018
display !== 'none' &&
1119
visibility !== 'hidden' &&
1220
visibility !== 'collapse' &&
13-
opacity !== '0' &&
14-
opacity !== 0
21+
opacity !== '0'
1522
)
1623
}
1724

test/specs/wrapper/isVisible.spec.js

+15
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,19 @@ describeWithShallowAndMount('isVisible', mountingMethod => {
176176

177177
expect(wrapper.find('.child.ready').isVisible()).toEqual(true)
178178
})
179+
180+
it('returns false if element has class with opacity: 0', async () => {
181+
const style = document.createElement('style')
182+
style.type = 'text/css'
183+
document.head.appendChild(style)
184+
style.sheet.insertRule('.opacity-0 { opacity: 0; }')
185+
186+
const compiled = compileToFunctions('<div id="my-div" class="opacity-0" />')
187+
const wrapper = mountingMethod(compiled, {
188+
attachTo: document.body
189+
})
190+
expect(wrapper.get('#my-div').isVisible()).toBe(false)
191+
192+
document.head.removeChild(style)
193+
})
179194
})

0 commit comments

Comments
 (0)