Skip to content

Commit 660e448

Browse files
code (#369)
1 parent 10c9e2f commit 660e448

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

.changeset/happy-birds-breathe.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@kitajs/html': patch
3+
---
4+
5+
Allow undefined in style attributes

packages/html/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ function attributesToString(attributes) {
264264
// @ts-expect-error - this indexing is safe.
265265
value = attributes[key];
266266

267+
if (value === null || value === undefined) {
268+
continue;
269+
}
270+
267271
// React className compatibility.
268272
if (key === 'className') {
269273
// @ts-expect-error - both were provided, so use the class attribute.
@@ -317,10 +321,6 @@ function attributesToString(attributes) {
317321
continue;
318322
}
319323

320-
if (value === null || value === undefined) {
321-
continue;
322-
}
323-
324324
result += ' ' + key;
325325

326326
if (type !== 'string') {

packages/html/test/style.test.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ describe('Style', () => {
1515
);
1616
});
1717

18+
test('accepts undefined', () => {
19+
assert.equal(
20+
<div style={{ backgroundColor: undefined }}></div>,
21+
'<div style=""></div>'
22+
);
23+
24+
assert.equal(<div style={undefined}></div>, '<div></div>');
25+
});
26+
1827
test('CSSProperties', () => {
1928
const style: JSX.CSSProperties = {
2029
color: 'red',

0 commit comments

Comments
 (0)