Git Product home page Git Product logo

Comments (2)

polytypic avatar polytypic commented on May 26, 2024

If I understand correctly, then I actually haven't ran into that specific scenario. I have had cases where I've used non-React components that handle their own rendering given a DOM node, such as the CodeMirror component in the GitHub snippet recorder example.

So, currently Karet doesn't provide explicit support for the scenario where one wants to perform ad hoc DOM manipulation after each update. You can, of course, do rendering on your own. Something like this (untested draft code):

const Code = ({code}) => {
  const element = U.variable()
  const updateEffect = U.thru(
    U.template({element, code}),
    U.consume(({element, code}) => {
      element.innerHTML = code.replace(/[\u00A0-\u9999<>&]/gim, i => '&#' + i.charCodeAt(0) + ';')
      syntaxHighlight(element)
    })
  )
  return <pre ref={U.refTo(element)} {...{updateEffect}}/>
}

If there is enough need for such a thing, then one possibility would be to e.g. support a special property, say karet-onDidUpdate, which would allow one to perform updates after rendering. Something like this:

const Code = ({code}) =>
  <pre karet-onDidUpdate={element => syntaxHighlight(element)}>
    {code}
  </pre>

There are probably other plausible APIs for this sort of thing.

However, it is not clear to me whether it makes sense to provide explicit support for this. I suspect that this kind of thing probably only works for very simple cases (such as the above) in the sense that one can't e.g. reliably and portably (as in having the code work with future React versions) render non-trivial DOM using React and then modify that arbitrarily after each update.

from karet.

polytypic avatar polytypic commented on May 26, 2024

FYI, I added a Highlight component to the GitHub Snippet Recorder example that uses Highlight.js to highlight a code block after changes (just edit the text in the CodeMirror editor on top).

from karet.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.