Skip to content

Commit 7c1191d

Browse files
authored
[lit-html] styleMap fails when update contains priority directive (#3768)
1 parent a4eb5e6 commit 7c1191d

6 files changed

Lines changed: 37 additions & 2 deletions

File tree

.changeset/popular-shoes-carry.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'lit-html': patch
3+
'lit': patch
4+
---
5+
6+
Fix styleMap's handling of important flags

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ node_modules/
1010
.vscode/
1111
.wireit
1212
/temp
13+
.idea
1314

1415
packages/benchmarks/generated/
1516
packages/benchmarks/generator/build/

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ node_modules/
55
.vscode/
66
.wireit
77
/temp
8+
.idea

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ node_modules/
1010
.vscode/
1111
.wireit
1212
/temp
13+
.idea
1314

1415
packages/benchmarks/generated/
1516
packages/benchmarks/generator/build/

packages/lit-html/src/directives/style-map.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ export interface StyleInfo {
2424
[name: string]: string | undefined | null;
2525
}
2626

27+
const important = 'important';
28+
// The leading space is important
29+
const importantFlag = ' !' + important;
30+
// How many characters to remove from a value, as a negative number
31+
const flagTrim = 0 - importantFlag.length;
32+
2733
class StyleMapDirective extends Directive {
2834
_previousStyleProperties?: Set<string>;
2935

@@ -95,8 +101,13 @@ class StyleMapDirective extends Directive {
95101
const value = styleInfo[name];
96102
if (value != null) {
97103
this._previousStyleProperties.add(name);
98-
if (name.includes('-')) {
99-
style.setProperty(name, value);
104+
const isImportant = value.endsWith(importantFlag);
105+
if (name.includes('-') || isImportant) {
106+
style.setProperty(
107+
name,
108+
isImportant ? value.slice(0, flagTrim) : value,
109+
isImportant ? important : ''
110+
);
100111
} else {
101112
// eslint-disable-next-line @typescript-eslint/no-explicit-any
102113
(style as any)[name] = value;

packages/lit-html/src/test/directives/style-map_test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,21 @@ suite('styleMap', () => {
133133
assert.equal(el.style.getPropertyValue('--size'), '');
134134
});
135135

136+
test('adds priority in updated properties', () => {
137+
renderStyleMap({color: 'blue !important'});
138+
const el = container.firstElementChild as HTMLElement;
139+
assert.equal(el.style.getPropertyValue('color'), 'blue');
140+
assert.equal(el.style.getPropertyPriority('color'), 'important');
141+
renderStyleMap({color: 'green !important'});
142+
assert.equal(el.style.getPropertyValue('color'), 'green');
143+
assert.equal(el.style.getPropertyPriority('color'), 'important');
144+
renderStyleMap({color: 'red'});
145+
assert.equal(el.style.getPropertyValue('color'), 'red');
146+
assert.equal(el.style.getPropertyPriority('color'), '');
147+
renderStyleMap({});
148+
assert.equal(el.style.getPropertyValue('color'), '');
149+
});
150+
136151
test('works when used with the same object', () => {
137152
const styleInfo: StyleInfo = {marginTop: '2px', 'padding-bottom': '4px'};
138153
renderStyleMap(styleInfo);

0 commit comments

Comments
 (0)