-
Notifications
You must be signed in to change notification settings - Fork 190
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
Two small fixes for the styles doc #535
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ The value of the static `styles` class field can be: | |
|
||
The static `styles` class field is _almost always_ the best way to add styles to your component, but there are some use cases you can't handle this way—for example, customizing styles per instance. For alternate ways to add styles, see [Defining scoped styles in the template](#styles-in-the-template). | ||
|
||
|
||
### Using expressions in static styles {#expressions} | ||
|
||
Static styles apply to all instances of a component. Any expressions in CSS are evaluated **once**, then reused for all instances. | ||
|
@@ -74,6 +75,12 @@ Using an array of tagged template literals, a component can inherit the styles f | |
|
||
{% playground-ide "docs/components/style/superstyles" %} | ||
|
||
<div class="alert alert-info"> | ||
|
||
**Using super.styles**. You can also use `super.styles` to reference the superclass's styles property, but TypeScript doesn't convert this correctly when compiling for ES5. Explicitly referencing the superclass, as shown in the example, avoids this issue. | ||
|
||
</div> | ||
|
||
### Sharing styles | ||
|
||
You can share styles between components by creating a module that exports tagged styles: | ||
|
@@ -105,6 +112,22 @@ class MyElement extends LitElement { | |
} | ||
``` | ||
|
||
### Using unicode escapes in styles | ||
|
||
CSS's unicode escape sequence is a backslash followed by four or six hex digits: for example, `\2022` for a bullet character. This similar to the format of JavaScript's deprecated _octal_ escape sequences, so using these sequences in a `css` tagged template literal causes an error. | ||
|
||
There are two work-arounds for adding a unicode escape to your styles: | ||
|
||
* Add a second backslash (for example, `\\2022`). | ||
* Use the JavaScript escape sequence, starting with `\u` (for example, `\u2022`). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The third is to just include the unicode character in the JS source. |
||
|
||
```js | ||
static styles = css` | ||
div::before { | ||
content: '\u2022'; | ||
} | ||
``` | ||
|
||
## Shadow DOM styling overview {#shadow-dom} | ||
|
||
This section gives a brief overview of shadow DOM styling. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe clarify that this does work for ES2015 and above, though. Also I think we should show the
super.styles
approach in the examples. (@AndrewJakubowicz found that it works fine in TypeScript 4.4, we just need to update the Playground's TypeScript version).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, cool. Yeah, I agree that showing the
super.styles
approach as the default is preferable. Is there a bug for updating the Playground typescript version?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
google/playground-elements#217 to release with the new version. After this I'll send a PR to update lit.dev. Should be able to get that in before this PR merges.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#537
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh, bumping the TypeScript version fixed the type error, so there's no longer a squiggly when doing
super.styles
, but it didn't fix the output, which is still invalid note the(void 0).styles
:I wonder if we need to tweak the compiler options (https://github.com/google/playground-elements/blob/05538da0ee6fe13b3345d89b805dd06b2b8f30b3/src/typescript-worker/language-service-context.ts#L9)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see, the latest update seems fine (though maybe we should note it's also broken for ES2015+ with a class decorator, not just ES5?) I think we should file a typescript bug?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think under those circumstances we should just recommend against using it in TypeScript. If someone uses it and subsequently decides to add an IE11 build or to adopt decorators, their code will break and that seems bad.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... especially since we invariably show TypeScript with decorators, so the only circumstance where it works is off the well-lit path.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made a repro. Can look for an open issue in TypeScript or open a new one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another maybe interesting note, if you set
useDefineForClassFields
in the previous repro then the (void 0) bug no longer occurs.