Skip to content

Commit

Permalink
Support css variables #2
Browse files Browse the repository at this point in the history
  • Loading branch information
xuliangzhan committed Aug 26, 2024
1 parent 401a0d9 commit 2b5eb35
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ let currZIndex2 = 2000
currZIndex2 = domZIndex.getCurrent(currZIndex2) // 1500
```

## Css var

Using css variables.

* ```--dom-main-z-index``` be equivalent to ```getCurrent()```
* ```--dom-sub-z-index``` be equivalent to ```getSubCurrent()```

```css
.my-popup {
z-index: var(--dom-main-z-index);
}
.my-msg {
z-index: var(--dom-sub-z-index);
}
```

## Contributors

Thank you to everyone who contributed to this project.
Expand Down
30 changes: 30 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
let storeEl: HTMLElement | null = null
let storeId = 'z-index-manage'

let styleEl: HTMLStyleElement | null = null
let styleId = 'z-index-style'

let storeMainKey: 'm' = 'm'
let storeSubKey: 's' = 's'

Expand Down Expand Up @@ -29,6 +33,29 @@ function getDomMaxZIndex () {
return max
}

function getStyle () {
if (!styleEl) {
if (isDocument()) {
styleEl = document.getElementById(styleId) as HTMLStyleElement
if (!styleEl) {
styleEl = document.createElement('style')
styleEl.id = styleId
document.getElementsByTagName('head')[0].appendChild(styleEl)
}
}
}
return styleEl
}

function updateVar () {
let styEl = getStyle()
if (styEl) {
let prefixes = '--dom-'
let propKey = '-z-index'
styEl.innerHTML = ':root{' + prefixes + 'main' + propKey + ':' + getCurrent() + ';' + prefixes + 'sub' + propKey + ':' + getSubCurrent() + '}'
}
}

function getDom () {
if (!storeEl) {
if (isDocument()) {
Expand Down Expand Up @@ -60,6 +87,7 @@ function createSetHandle (key: keyof (typeof storeData)) {
}
}
}
updateVar()
return storeData[key]
}
}
Expand Down Expand Up @@ -121,4 +149,6 @@ const DomZIndex = {
getMax: getDomMaxZIndex
}

updateVar()

export default DomZIndex
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dom-zindex",
"version": "1.0.4",
"version": "1.0.5",
"description": "Web common z-index style management",
"files": [
"dist",
Expand Down

0 comments on commit 2b5eb35

Please sign in to comment.