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

Clear css properties when passed undefined #3862

Merged
merged 2 commits into from
Jan 19, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 is passed an empty string, null, and 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);
});
}
});