Skip to content

Commit

Permalink
fix(cdk:popper): display: none reference is not treat as hidden (#1659
Browse files Browse the repository at this point in the history
)
  • Loading branch information
sallerli1 committed Aug 21, 2023
1 parent f691999 commit 830be33
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions packages/cdk/popper/src/middlewares/refenceHidden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* found in the LICENSE file at https://github.com/IDuxFE/idux/blob/main/LICENSE
*/

import { type Middleware, type ReferenceElement, type Side, type SideObject, hide } from '@floating-ui/dom'
import { type Middleware, type SideObject, hide } from '@floating-ui/dom'

import { isVisibleElement } from '@idux/cdk/utils'

Expand All @@ -15,8 +15,6 @@ export interface ReferenceHiddenData {
referenceHiddenOffsets: SideObject
}

const sides: Side[] = ['top', 'bottom', 'left', 'right']

export function referenceHidden(): Middleware {
const { fn: hideFn, ...rest } = hide({ elementContext: 'reference' })

Expand All @@ -32,17 +30,12 @@ export function referenceHidden(): Middleware {
data: ReferenceHiddenData
}
const {
data: { referenceHidden, referenceHiddenOffsets },
data: { referenceHidden },
} = res

const shouldHideReference =
referenceHidden &&
!(
reference instanceof HTMLElement &&
getComputedStyle(reference).display === 'none' &&
sides.some(side => referenceHiddenOffsets[side] === 0) &&
checkParentsVisible(reference)
)
!(reference instanceof HTMLElement && isReferenceDispalyNone(reference) && checkParentsVisible(reference))

if (shouldHideReference) {
floating.setAttribute('data-popper-reference-hidden', '')
Expand All @@ -61,11 +54,21 @@ export function referenceHidden(): Middleware {
}
}

function checkParentsVisible(el: ReferenceElement): boolean {
if (!(el instanceof HTMLElement)) {
function isReferenceDispalyNone(reference: HTMLElement): boolean {
const referenceDisplay = getComputedStyle(reference).display

if (referenceDisplay === 'none') {
return true
}

if (referenceDisplay === 'contents') {
return !!reference.firstElementChild && getComputedStyle(reference.firstElementChild).display === 'none'
}

return false
}

function checkParentsVisible(el: HTMLElement): boolean {
let parent = el.parentElement
while (parent) {
if (getComputedStyle(parent).display !== 'contents' && !isVisibleElement(parent)) {
Expand Down

0 comments on commit 830be33

Please sign in to comment.