Skip to content

Commit

Permalink
Clear css properties when passed undefined (#3862)
Browse files Browse the repository at this point in the history
* Clear css properties when passed undefined
  • Loading branch information
andrewiggins authored Jan 19, 2023
1 parent 2ce7b0a commit c483d96
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/diff/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function diffProps(dom, newProps, oldProps, isSvg, hydrate) {

function setStyle(style, key, value) {
if (key[0] === '-') {
style.setProperty(key, value);
style.setProperty(key, value == null ? '' : value);
} else if (value == null) {
style[key] = '';
} else if (typeof value != 'number' || IS_NON_DIMENSIONAL.test(key)) {
Expand Down
2 changes: 1 addition & 1 deletion test/_util/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export function clearOptions() {

/**
* Teardown test environment and reset preact's internal state
* @param {HTMLDivElement} scratch
* @param {HTMLElement} scratch
*/
export function teardown(scratch) {
if (scratch) {
Expand Down
52 changes: 52 additions & 0 deletions test/browser/style.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { setupScratch, teardown, sortCss } from '../_util/helpers';
/** @jsx createElement */

describe('style attribute', () => {
/** @type {HTMLElement} */
let scratch;

beforeEach(() => {
Expand Down Expand Up @@ -221,5 +222,56 @@ describe('style attribute', () => {
'10px'
);
});

it('should clear css properties when passed an empty string, null, or undefined', () => {
const red = 'rgb(255, 0, 0)';
const blue = 'rgb(0, 0, 255)';
const green = 'rgb(0, 128, 0)';
const yellow = 'rgb(255, 255, 0)';
const violet = 'rgb(238, 130, 238)';

function App({ color }) {
return (
<div style={{ '--color': color }}>
<span style={{ color: 'var(--color, red)' }}>Hello World!</span>
</div>
);
}

const getDiv = () => /** @type {HTMLDivElement} */ (scratch.firstChild);
const getSpan = () =>
/** @type {HTMLSpanElement} */ (scratch.firstChild.firstChild);
const getCSSVariableValue = () =>
getDiv().style.getPropertyValue('--color');
const getTextColor = () => window.getComputedStyle(getSpan()).color;

render(<App color="blue" />, scratch);
expect(getCSSVariableValue()).to.equal('blue');
expect(getTextColor()).to.equal(blue);

render(<App color="" />, scratch);
expect(getCSSVariableValue(), 'empty string').to.equal('');
expect(getTextColor(), 'empty string').to.equal(red);

render(<App color="green" />, scratch);
expect(getCSSVariableValue()).to.equal('green');
expect(getTextColor()).to.equal(green);

render(<App color={undefined} />, scratch);
expect(getCSSVariableValue(), 'undefined').to.equal('');
expect(getTextColor(), 'undefined').to.equal(red);

render(<App color="yellow" />, scratch);
expect(getCSSVariableValue()).to.equal('yellow');
expect(getTextColor()).to.equal(yellow);

render(<App color={null} />, scratch);
expect(getCSSVariableValue(), 'null').to.equal('');
expect(getTextColor(), 'null').to.equal(red);

render(<App color="violet" />, scratch);
expect(getCSSVariableValue()).to.equal('violet');
expect(getTextColor()).to.equal(violet);
});
}
});

0 comments on commit c483d96

Please sign in to comment.