Code Monkey home page Code Monkey logo

Comments (2)

johanalkstal avatar johanalkstal commented on May 14, 2024 1

Thanks for the discussion. I was sure there were things I had not considered and expected as much.

from hybrids.

smalluban avatar smalluban commented on May 14, 2024

Styling limitations exist because of older browser support. It is not a library limitation. You can use expressions in <style> element if you target modern browsers. However, then you should use <style> element in your template rather than passing a string value to .style() helper. Why?

Again, because of polyfills, .style() helper changes the signature of the template (under the hood ShadyCSS creates style element and put it into the head of the document with a special prefix, which is added to all elements of the template. I didn't check, but your example might break with more changes). It means that a template with a different style, like in your example (when changing isRed value) creates a new, unique template ID. When engine takes new template ID, it wipes out "old" template and replaces it with the new one.

Even though your example works, it brings a big performance cost. When you change isRed value, your <h1> element will be replaced with new <h1> element.

For CSS in JS like approach, I recommend using inline styles, which are supported by the template engine:

const App = {
  isRed: true,
  render: ({ isRed }) => html`
    <h1 style={{ color: isRed ? "red" : "blue" }}>Hello World</h1>
  `,
};

You can also create "helper" method, for creating your styles:

const fontSize = {
  normal: "1rem",
};

const style = isRed => ({
  fontSize: fontSize.normal,
  color: isRed ? "red" : "blue",
});

const App = {
  isRed: true,
  render: ({ isRed }) => html`
    <h1 style={style(isRed)}>Hello World</h1>
  `,
};

Although, creating dynamic styles at scale might have some performance impact. Creating a static style, and then using classes or other selectors for switching styles will always be faster.

from hybrids.

Related Issues (20)

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.