-
-
Notifications
You must be signed in to change notification settings - Fork 778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(runtime): custom inject
function
#3028
Conversation
❌ Deploy Preview for unocss failed.
|
The insertion before Per your example, You can get each layer's unocss/test/runtime-dom.test.ts Lines 32 to 42 in 1073bea
|
@@ -161,15 +162,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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be handled. Maybe use previousStyle
/previousLayer
for context
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I undestand, I'm simply replacing the hardcoded html().prepend()
with an optional custom method that prepends/appends/inserts the style element(s) wherever the user needs it? What do you mean by "should be handled"? (I mean if a runtime user uses this config option he/she should know what it does IMHO 😉 )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah maybe i get what you mean: You think we should give users the ability to inject each layer separately wherever they want it?
If that is what you are proposing, I think this would maybe add unnecessary complexity, no? I didn't want to mess with the existing logic of how UnoCSS injects the different layers etc., just WHERE in the DOM it does all of that.
@@ -122,6 +122,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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add the type and some description
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added to RuntimeOptions interfarce
@@ -161,15 +162,15 @@ export default function init(inlineConfig: RuntimeOptions = {}) { | |||
styleElements.set(layer, styleElement) | |||
|
|||
if (previousLayer == null) { | |||
html().prepend(styleElement) | |||
inject(styleElement) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may want to send layer
for context
absolutely agree! This is the normal use case, but in mine I want to use Uno CSS within a third party vendor platform that does a lot of things i can't control, including markup & styles etc., and here the issue is that I DON'T want the platform's styles to override my utils (or shortcuts) defined in Uno, of course I can use
yes, but that doesn't matter if the third party code does not use @layer at all 😉 |
inject
function
Improve comment Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
First of all, amazing work with UnoCSS, keep it up!
I'm liking runtime, but one thing I'm missing is the ability to control where the generated styles get injected into the DOM, right now they are basically even before the
head
tag. I'm using it in a context where i'd like to override existing classes from HTML markup I don't control via UnoCSSshortcuts
, but I can't (without !important) because the styles from that platform are in thehead
(as usual) and single class selector's specificity is therefore higher than single class shortcuts from UnoCSS defined/injected earlier on.So the change is relatively simple, it's just a user configurable function that receives the style element and the user can then inject that wherever they want. Default behaviour doesn't change. Layering logic is untouched, the function just controls where the first layer is injected.
One can now do the following:
which results in:
If you don't like the way it's implemented or where, let me know, glad to adapt to your liking. Just threw this together for myself, but thought it might be helpful for others too.