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

#666 support css variables #827

Closed
wants to merge 10 commits into from
15 changes: 13 additions & 2 deletions src/dom/index.js
Expand Up @@ -83,10 +83,21 @@ export function setAccessor(node, name, old, value, isSvg) {
}
if (value && typeof value==='object') {
if (typeof old!=='string') {
for (let i in old) if (!(i in value)) node.style[i] = '';
for (let i in old) if (!(i in value)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Challenge: https://twitter.com/preactjs/status/998981752130588673

if (value && typeof value==='object') {
  let i; // placeholder
  if (typeof old!=='string') {
    for (i in old) {
      if (!(i in value)) node.style.setProperty(i, '');
    }
  }
  for (i in value) {
    node.style.setProperty(i, typeof value[i]==='number' && IS_NON_DIMENSIONAL.test(i)===false ? (value[i]+'px') : value[i]);
  }
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be backwards incompatible. Doesn't work for properties that are defined using camel-casing.

node.style.backgroundColor = value; // works
node.style['background-color'] = value; // works
node.style.setProperty('background-color', value); // works
node.style.setProperty('backgroundColor', value); // doesn't work

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PepsRyuu aye, saw this after 🙈 /delete

if (~i.indexOf('-')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should check what the size is when doing if (i[0]=='-') - we only care about using setProperty for custom properties, so it'll always be a leading --.

node.style.setProperty(i, '');
} else {
node.style[i] = '';
}
}
}
for (let i in value) {
node.style[i] = typeof value[i]==='number' && IS_NON_DIMENSIONAL.test(i)===false ? (value[i]+'px') : value[i];
if (~i.indexOf('-')) {
node.style.setProperty(i, value[i]);
}
else {
node.style[i] = typeof value[i]==='number' && IS_NON_DIMENSIONAL.test(i)===false ? (value[i]+'px') : value[i];
}
}
}
}
Expand Down
15 changes: 9 additions & 6 deletions test/browser/render.js
Expand Up @@ -244,10 +244,11 @@ describe('render()', () => {
});

it('should apply style as String', () => {
render(<div style="top:5px; position:relative;" />, scratch);
render(<div style="top:5px; position:relative; --var-font-size: 12px; font-size: var(--var-font-size);" />, scratch);
expect(scratch.childNodes[0].style.cssText)
.that.matches(/top\s*:\s*5px\s*/)
.and.matches(/position\s*:\s*relative\s*/);
.to.match(/top\s*:\s*5px\s*/)
.and.match(/position\s*:\s*relative\s*/)
.and.match(/--var-font-size\s*:\s*12px\s*/);
});

it('should only register on* functions as handlers', () => {
Expand Down Expand Up @@ -361,7 +362,8 @@ describe('render()', () => {
'background-size': 'cover',
padding: 5,
top: 100,
left: '100%'
left: '100%',
'--var-font-size': '12px'
}}>
test
</div>
Expand All @@ -375,12 +377,13 @@ describe('render()', () => {
expect(style).to.have.property('padding', '5px');
expect(style).to.have.property('top', '100px');
expect(style).to.have.property('left', '100%');
expect(style.getPropertyValue('--var-font-size')).to.equal('12px');

root = render((
<div style={{ color: 'rgb(0, 255, 255)' }}>test</div>
<div style={{ color: 'rgb(0, 255, 255)', '--var-font-size': '24px' }}>test</div>
), scratch, root);

expect(root.style.cssText).to.equal('color: rgb(0, 255, 255);');
expect(root.style.cssText).to.equal('color: rgb(0, 255, 255); --var-font-size:24px;');

root = render((
<div style="display: inline;">test</div>
Expand Down