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

feat: driver-universal support CSS custom properties #1335

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions packages/driver-dom/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ export function setStyle(node, style) {
// Support CSS custom properties
const convertedValue = convertUnit(style[prop]);
if (prop[0] === '-' && prop[1] === '-') {
// reference: https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration/setProperty. style.setProperty do not support Camel-Case style properties.
node.style.setProperty(prop, convertedValue);
} else {
node.style[prop] = convertedValue;
Expand Down
39 changes: 39 additions & 0 deletions packages/driver-universal/src/__tests__/css-custom-properties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { createElement, render } from 'rax';
import DriverDOM from '../';

describe('Support CSS custom properties', () => {
let container;
let logs;
const setProperty = CSSStyleDeclaration.prototype.setProperty;

beforeAll(() => {
CSSStyleDeclaration.prototype.setProperty = (prop, value) => {
logs.push([prop, value]);
};
});

afterAll(() => {
CSSStyleDeclaration.prototype.setProperty = setProperty;
});

beforeEach(() => {
container = document.createElement('div');
(document.body || document.documentElement).appendChild(container);
logs = [];
});

it('CSS custom properties works.', () => {
render((
<div style={{
'--backgroundColor': 'red',
backgroundColor: 'var(--backgroundColor)',
}}>
Test CSS custom properties.
</div>
), container, {
driver: DriverDOM
});

expect(logs).toEqual([['--backgroundColor', 'red']]);
});
});
7 changes: 6 additions & 1 deletion packages/driver-universal/src/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ const driver = Object.assign({}, DriverDOM, {
if (Array.isArray(transformValue)) {
for (let i = 0; i < transformValue.length; i++) node.style[prop] = transformValue[i];
} else {
node.style[prop] = transformValue;
if (prop[0] === '-' && prop[1] === '-') {
BakuJun marked this conversation as resolved.
Show resolved Hide resolved
// reference: https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration/setProperty. style.setProperty do not support Camel-Case style properties.
node.style.setProperty(prop, transformValue);
} else {
node.style[prop] = transformValue;
}
}
}
}
Expand Down