safari不adoptedStyleSheets,降级为style标签
parent
4463d0bc5e
commit
f1830910aa
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@bd/core",
|
||||
"version": "1.1.0",
|
||||
"version": "1.2.0",
|
||||
"type": "module",
|
||||
"description": "百搭UI组件库的核心",
|
||||
"main": "dist/index.js",
|
||||
|
@ -9,7 +9,7 @@
|
|||
],
|
||||
"scripts": {
|
||||
"build": "esbuild src/index.js --minify --bundle --format=esm --target=esnext --outfile=dist/index.js",
|
||||
"build-es6": "esbuild src/index.js --minify --bundle --format=esm --target=es6 --outfile=dist/index.es6.js"
|
||||
"build:es6": "esbuild src/index.js --minify --bundle --format=esm --target=es6 --outfile=dist/index.es6.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
|
20
src/css.js
20
src/css.js
|
@ -6,6 +6,18 @@
|
|||
|
||||
import { RESET_CSS_STYLE } from './constants.js'
|
||||
|
||||
let MyCSSStyleSheet = CSSStyleSheet
|
||||
|
||||
if (!document.adoptedStyleSheets) {
|
||||
MyCSSStyleSheet = class {
|
||||
elem = document.createElement('style')
|
||||
|
||||
replaceSync(css) {
|
||||
this.elem.textContent = css.replace(/\s+/g, ' ')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function css(strs, ...args) {
|
||||
let output = ''
|
||||
let tmp = Array.from(strs)
|
||||
|
@ -16,7 +28,7 @@ export function css(strs, ...args) {
|
|||
}
|
||||
|
||||
export function adoptStyles(root, styles = '') {
|
||||
let sheet = new CSSStyleSheet()
|
||||
let sheet = new MyCSSStyleSheet()
|
||||
if (typeof styles === 'string') {
|
||||
styles = [styles]
|
||||
} else {
|
||||
|
@ -24,5 +36,9 @@ export function adoptStyles(root, styles = '') {
|
|||
}
|
||||
styles = (RESET_CSS_STYLE + styles.join(' ')).trim()
|
||||
sheet.replaceSync(styles)
|
||||
root.adoptedStyleSheets.push(sheet)
|
||||
if (root.adoptedStyleSheets) {
|
||||
root.adoptedStyleSheets.push(sheet)
|
||||
} else {
|
||||
root.appendChild(sheet.elem)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue