Skip to content
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

Style modules API #429

Closed
LarsDenBakker opened this issue Jan 11, 2019 · 5 comments
Closed

Style modules API #429

LarsDenBakker opened this issue Jan 11, 2019 · 5 comments

Comments

@LarsDenBakker
Copy link
Contributor

LarsDenBakker commented Jan 11, 2019

Now that there is a new style system, I think it's good to consider how to create shared style modules. In Polymer we used to do this:

<!-- file: style-module-a.html -->
<dom-module id="style-module-a">
  <template>
    <style>/* ... */</style>
  </template>
</dom-module>

<!-- file: style-module-b.html -->
<dom-module id="style-module-b">
  <template>
    <style include="style-module-a">/* ... */</style>
  </template>
</dom-module>

This way you could use style-module-b without knowing that it also composes style-module-a.

With the new LitElement style system, it would translate to this:

// file: style-module-a.js
export const styleModuleA = css`
  ...
`;

// file: style-module-b.js
import { styleModuleA } from './style-module-a.js';

export const styleModuleB = [
  styleModuleA,
  css`
    ...
  `;
];

This is a bit awkward, as the consumer of style-module-b now has to know that it is an array of styles and has to spread it:

class MyElement extends LitElement {
  static get styles() {
    return [
      ..styleModuleB,
      styleModuleC
    ];
  }
}

But this also means that styleModuleB could never change back to not being an array, and styleModuleC can never change to being an array of styles.

We could solve this by making style modules always an array, even if there is only one entry. Another solution would be if LitElement flattened the styles array so that you don't need to worry about this.

@sorvell
Copy link
Member

sorvell commented Jan 11, 2019

The reason that static get styles must return an array to make composition easier.

I think it's reasonable to flatten the array of styles and we'd accept a PR for that.

@justinfagnani
Copy link
Contributor

Why do we need to support this when we have array spread syntax? It's prefer to not add code for things the language already does for us.

@justinfagnani
Copy link
Contributor

justinfagnani commented Jan 11, 2019

One thing that would make flattening worth it to me is if we changed the styles property to allow for a single CssResult. Then merging in super-class styles would always be [super.styles, css`...`] and writing styles would be simpler for the single-stylesheet case.

@logicalphase
Copy link
Contributor

I agree with @justinfagnani last comment. That provides a good relief solution for those who really want/need a single stylesheet method.

@LarsDenBakker
Copy link
Contributor Author

Yes definitely make sense to support single CSSResult values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants