Skip to content

Commit

Permalink
feat(runtime): custom inject function (#3028)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
exside and antfu committed Aug 31, 2023
1 parent 4f4148e commit 77ea190
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ export interface RuntimeOptions {
* Callback to modify config
*/
configResolved?: (config: UserConfig, defaults: UserConfigDefaults) => void
/**
* Optional function to control UnoCSS style element(s) injection into DOM.
* When provided, the default injection logic will be overridden.
*/
inject?: (styleElement: HTMLStyleElement) => void
/**
* Callback when the runtime is ready. Returning false will prevent default extraction
*/
Expand Down Expand Up @@ -138,6 +143,7 @@ export default function init(inlineConfig: RuntimeOptions = {}) {

runtimeOptions.configResolved?.(userConfig, userConfigDefaults)
const uno = createGenerator(userConfig, userConfigDefaults)
const inject = (styleElement) => runtimeOptions.inject ? runtimeOptions.inject(styleElement) : html().prepend(styleElement)

Check failure on line 146 in packages/runtime/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected parentheses around single function argument having a body with no curly braces
const styleElements = new Map<string, HTMLStyleElement>()

let paused = true
Expand Down Expand Up @@ -177,15 +183,15 @@ export default function init(inlineConfig: RuntimeOptions = {}) {
styleElements.set(layer, styleElement)

if (previousLayer == null) {
html().prepend(styleElement)
inject(styleElement)
}
else {
const previousStyle = getStyleElement(previousLayer)
const parentNode = previousStyle.parentNode
if (parentNode)
parentNode.insertBefore(styleElement, previousStyle.nextSibling)
else
html().prepend(styleElement)
inject(styleElement)
}
}

Expand Down

0 comments on commit 77ea190

Please sign in to comment.