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