-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
doubt about editing returned raw VNodes #909
Comments
basically im asking for a way to 'deserialize' VNodes in hscript again :(( |
Would it be sufficient to instead mutate the resulting VNodes created by an unmodified h()? function withStyle(fn) {
return function() {
let vnode = fn.apply(this, arguments);
vnode.children = [ h('style', /*..etc..*/) ].concat(vnode.children)
return vnode;
};
} |
Awesome answer, thanks |
working decorator implementation: import { h, Component } from "preact";
import { afterMethod } from "kaop-ts";
export const stylesheet = (styleContent: string) =>
afterMethod<Component<any, any>, "render">((meta) => {
// remove all spaces, eols
styleContent = styleContent.replace(/(\r\n\s|\n|\r|\s)/gm, "");
// prefix all selectors to make stylesheet 'scoped'
styleContent = styleContent.replace(
/([^\r\n,{}]+)(,(?=[^}]*{)|\s*{)/g,
`${meta.target.constructor.name.toLowerCase()} $1$2`
)
// add stylesheet to vnode
meta.result = h(
meta.target.constructor.name, null,
[ meta.result, h("style", { 'scoped': true }, styleContent) ]
);
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Im trying to edit raw 'h' nodes before they get constructed
Hope u get a bit of background how im trying to do that
Im trying to create a
@withStyle(cssObj)
decorator using a@afterMethod
Join Point withkaop-ts
to plug a<style> ... content </style>
inside some component before it gets evaluated bypreact
.Because this occurs in runtime im trying to dynamicly edit component's render() hyperscript result before it become a VNode placing a
preact_1.h("style", null, scopedRules)));
as the last children.My question is if there is somehow a way more friendly to edit hyperscript rather than
str.replace(regexThatSearchOnHyperScriptBodyFunctionTheLastChildren, "preact_1.h("style", null, scopedRules)))")
:\The resulting implementation will look like this:
or
The text was updated successfully, but these errors were encountered: