-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cf302ac
commit 7187e67
Showing
4 changed files
with
91 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* @license | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://github.com/IDuxFE/idux/blob/main/LICENSE | ||
*/ | ||
|
||
import { Logger } from '@idux/cdk/utils' | ||
|
||
class ZIndexManager { | ||
private zIndexMap: Map<HTMLElement, number> = new Map() | ||
protected initZIndex = 10000 | ||
private nextZIndex: number | ||
|
||
constructor() { | ||
this.nextZIndex = this.initZIndex | ||
} | ||
|
||
private get elementCount() { | ||
return this.zIndexMap.size | ||
} | ||
|
||
public register(el: HTMLElement, zIndex?: number) { | ||
const { zIndexMap } = this | ||
if (typeof zIndex !== 'undefined') { | ||
el.style.zIndex = `${zIndex}` | ||
zIndexMap.delete(el) | ||
return | ||
} | ||
const { nextZIndex } = this | ||
if (zIndexMap.has(el)) { | ||
const currentZIndex = zIndexMap.get(el)! | ||
if (currentZIndex + 1 === this.nextZIndex) { | ||
return | ||
} | ||
} | ||
el.style.zIndex = `${nextZIndex}` | ||
zIndexMap.set(el, nextZIndex) | ||
this.nextZIndex = nextZIndex + 1 | ||
} | ||
|
||
public unregister(el: HTMLElement) { | ||
const { zIndexMap } = this | ||
if (zIndexMap.has(el)) { | ||
zIndexMap.delete(el) | ||
} else if (__DEV__) { | ||
Logger.warn('cdk/zIndex', `Element not found when unregister`) | ||
} | ||
if (this.canResetState()) { | ||
this.resetState() | ||
} | ||
} | ||
|
||
private canResetState() { | ||
return this.elementCount === 0 | ||
} | ||
|
||
private resetState() { | ||
this.nextZIndex = this.initZIndex | ||
} | ||
} | ||
|
||
const modalInitZIndex = 1000 | ||
|
||
class ModalZIndexManager extends ZIndexManager { | ||
protected initZIndex: number = modalInitZIndex | ||
|
||
public setInitZIndex(newZIndex: number) { | ||
this.initZIndex = newZIndex | ||
} | ||
} | ||
|
||
export const modalZIndexManager = new ModalZIndexManager() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters