safari不adoptedStyleSheets,降级为style标签

pull/1/head
yutent 2023-03-14 19:03:57 +08:00
parent 4463d0bc5e
commit f1830910aa
2 changed files with 20 additions and 4 deletions

View File

@ -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",

View File

@ -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)
}
}