Be patient, trust me, you need to modifiy the style tag's textcontent.
// add style tag
export function addFontSizeModeStyle(modeStr: EFontSizeMode) {
const sizeObj = SIZE_MODE_DATA[modeStr]?.size
if (!sizeObj) return
appendStyle({ id: FONT_STYELE_ID, cssStr: `:root{${filterStyleStr(JSON.stringify(sizeObj))}}` })
}
// handle :root format
function filterStyleStr(txt: string) {
return txt.replace(/\"/gi, '').replace(/\,/gi, ';').replace(/{|}/gi, '').concat(';')
}
// create style tag
function appendStyle({ id = '', cssStr }: { id?: string; cssStr: string }) {
const styleEl = document.createElement('style')
styleEl.type = 'text/css'
styleEl.id = id
styleEl.textContent = cssStr
document.head.appendChild(styleEl)
}
// set fontSize mode
export function setFontSizeMode(modeStr: EFontSizeMode) {
const sizeObj = SIZE_MODE_DATA[modeStr]?.size
if (!sizeObj) return
let $style = document.querySelector(`#${FONT_STYELE_ID}`)
const cssStr = `:root{${filterStyleStr(JSON.stringify(sizeObj))}}`
if ($style) {
$style.textContent = cssStr
} else {
appendStyle({ id: FONT_STYELE_ID, cssStr })
}
window.localStorage.setItem(FONT_THEME, modeStr)
}
usually, we can find the answer how to change css :root variable, it is
// not good
document.documentElement.style.setProperty(key, value)
but works on html tag's inline style in essence, use the level to cover :root variable, not change its variable
