question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

doubt about editing returned raw VNodes

See original GitHub issue

Im trying to edit raw ‘h’ nodes before they get constructed

Hope u get a bit of background how im trying to do that image

Im trying to create a @withStyle(cssObj) decorator using a @afterMethod Join Point with kaop-ts to plug a <style> ... content </style> inside some component before it gets evaluated by preact.

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:

import { h, Component } from "preact";
// styles will be replaced with component scoped selectors like angular 2+ does
const styleContent = `
  button {
    color: gray;
  }
  button:hover {
    color: white;
  }
`;

export class MyButton extends Component {

  @withStyle(styleContent)
  render(props) {
    return (
        <button>{props.children}</button>
    )
  } 
}

or

export const MyButton = withStyle(styleContent)(props => (
  <button>{props.children}</button>
));

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
k1r0scommented, Oct 14, 2017

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) ]
  );
});
1reaction
k1r0scommented, Oct 12, 2017

Awesome answer, thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Antd customRender return raw html - vue.js
If you dont have JSX support, you can return a Vnode. ... Edit: Here, another exemple, to show you where this piece of...
Read more >
Response Editing
Nearly all question types and fields are editable via the response editing tool, including text entry questions, multiple choice questions, slider questions, ...
Read more >
Adding & Editing Questions | SurveyMonkey Help
After you create your survey and name it, you can start setting up your questions. Editing existing questions is easy—just click on a...
Read more >
Signed Up for SSH-only Web Hosting? Don't Worry
Conclusion The Linux command line can be intimidating at first. There's no doubt about that. If you've accidentally found yourself saddled with SSH-only...
Read more >
3.4.3 Editing
Data editing begins by asking the question, “What could be the ... The same information could be available from the tax returns of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found