Skip to content

Commit

Permalink
feat(runtime): update & allow access to updateStyle() (#1770)
Browse files Browse the repository at this point in the history
  • Loading branch information
chu121su12 committed Oct 18, 2022
1 parent 002b1ad commit 1b8cc7b
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions packages/runtime/src/index.ts
@@ -1,4 +1,4 @@
import type { UnoGenerator, UserConfig, UserConfigDefaults } from '@unocss/core'
import type { GenerateResult, UnoGenerator, UserConfig, UserConfigDefaults } from '@unocss/core'
import { createGenerator } from '@unocss/core'
import { autoPrefixer, decodeHtml } from './utils'

Expand Down Expand Up @@ -69,6 +69,13 @@ export interface RuntimeContext {
*/
toggleObserver: (state?: boolean) => void

/**
* Manually run the update cycle.
*
* @returns {GenerateResult & { styleElement: HTMLStyleElement}}
*/
update: () => Promise<GenerateResult & { styleElement: HTMLStyleElement }>

/**
* The UnoCSS version.
*
Expand Down Expand Up @@ -137,14 +144,23 @@ export default function init(inlineConfig: RuntimeOptions = {}) {
})
}

async function updateStyle() {
const result = await uno.generate(tokens)
function getStyleElement() {
if (!styleElement) {
styleElement = document.createElement('style')
document.documentElement.prepend(styleElement)
}
return styleElement
}

async function updateStyle() {
const result = await uno.generate(tokens)
const styleElement = getStyleElement()
styleElement.innerHTML = result.css
tokens = result.matched
return {
...result,
styleElement,
}
}

async function extract(str: string) {
Expand Down Expand Up @@ -247,6 +263,7 @@ export default function init(inlineConfig: RuntimeOptions = {}) {
if (!observing && !paused)
ready()
},
update: updateStyle,
}

if (runtime?.ready?.(unoCssRuntime) !== false) {
Expand Down

0 comments on commit 1b8cc7b

Please sign in to comment.