From 5ea65b49d0c15833239ef90afc1aa96fec970825 Mon Sep 17 00:00:00 2001 From: melloware Date: Wed, 20 Jul 2022 13:04:45 -0400 Subject: [PATCH] Fix #3080: StyleClass improved handling of visible elements --- components/lib/styleclass/StyleClass.js | 5 +---- components/lib/utils/DomHandler.js | 7 ++++++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/components/lib/styleclass/StyleClass.js b/components/lib/styleclass/StyleClass.js index 4f6f5ea1a2..e4ff292047 100644 --- a/components/lib/styleclass/StyleClass.js +++ b/components/lib/styleclass/StyleClass.js @@ -55,10 +55,7 @@ export const StyleClass = React.forwardRef((props, ref) => { DomHandler.addClass(targetRef.current, props.toggleClassName); } else { - if (targetRef.current.offsetParent === null) - enter(); - else - leave(); + DomHandler.isVisible(targetRef.current) ? leave() : enter(); } } }); diff --git a/components/lib/utils/DomHandler.js b/components/lib/utils/DomHandler.js index b49324487b..42d568a7bb 100644 --- a/components/lib/utils/DomHandler.js +++ b/components/lib/utils/DomHandler.js @@ -746,7 +746,12 @@ export default class DomHandler { } static isVisible(element) { - return element && element.offsetParent != null; + // https://stackoverflow.com/a/59096915/502366 (in future use IntersectionObserver) + return element && ( + element.clientHeight !== 0 || + element.getClientRects().length !== 0 || + getComputedStyle(element).display !== 'none' + ); } static isExist(element) {