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

[lit-html] styleMap fails when update contains priority directive #3768

Merged
merged 2 commits into from Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/popular-shoes-carry.md
@@ -0,0 +1,6 @@
---
'lit-html': patch
'lit': patch
---

[lit-html] styleMap fails when update contains priority directive
justinfagnani marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions .eslintignore
Expand Up @@ -10,6 +10,7 @@ node_modules/
.vscode/
.wireit
/temp
.idea

packages/benchmarks/generated/
packages/benchmarks/generator/build/
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -5,3 +5,4 @@ node_modules/
.vscode/
.wireit
/temp
.idea
1 change: 1 addition & 0 deletions .prettierignore
Expand Up @@ -10,6 +10,7 @@ node_modules/
.vscode/
.wireit
/temp
.idea

packages/benchmarks/generated/
packages/benchmarks/generator/build/
Expand Down
11 changes: 8 additions & 3 deletions packages/lit-html/src/directives/style-map.ts
Expand Up @@ -24,6 +24,9 @@ export interface StyleInfo {
[name: string]: string | undefined | null;
}

const IMPORTNAT_PRIORITY = 'important';
const IMPORTANT_DIRECTIVE = '!important';
justinfagnani marked this conversation as resolved.
Show resolved Hide resolved

class StyleMapDirective extends Directive {
_previousStyleProperties?: Set<string>;

Expand Down Expand Up @@ -95,11 +98,13 @@ class StyleMapDirective extends Directive {
const value = styleInfo[name];
if (value != null) {
this._previousStyleProperties.add(name);
if (name.includes('-')) {
style.setProperty(name, value);
const valueToUse = value.replace(IMPORTANT_DIRECTIVE, '');
const priority = valueToUse !== value ? IMPORTNAT_PRIORITY : '';
if (name.includes('-') || priority) {
style.setProperty(name, valueToUse, priority);
justinfagnani marked this conversation as resolved.
Show resolved Hide resolved
} else {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(style as any)[name] = value;
(style as any)[name] = valueToUse;
justinfagnani marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions packages/lit-html/src/test/directives/style-map_test.ts
Expand Up @@ -133,6 +133,21 @@ suite('styleMap', () => {
assert.equal(el.style.getPropertyValue('--size'), '');
});

test('adds priority in updated properties', () => {
renderStyleMap({color: 'blue !important'});
const el = container.firstElementChild as HTMLElement;
assert.equal(el.style.getPropertyValue('color'), 'blue');
assert.equal(el.style.getPropertyPriority('color'), 'important');
renderStyleMap({color: 'green !important'});
assert.equal(el.style.getPropertyValue('color'), 'green');
assert.equal(el.style.getPropertyPriority('color'), 'important');
renderStyleMap({color: 'red'});
assert.equal(el.style.getPropertyValue('color'), 'red');
assert.equal(el.style.getPropertyPriority('color'), '');
renderStyleMap({});
assert.equal(el.style.getPropertyValue('color'), '');
});

test('works when used with the same object', () => {
const styleInfo: StyleInfo = {marginTop: '2px', 'padding-bottom': '4px'};
renderStyleMap(styleInfo);
Expand Down