-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(widgets): handle css variable and camel cased keys passed to widg…
…et style prop
- Loading branch information
1 parent
5ce18d5
commit 456d1eb
Showing
8 changed files
with
72 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export function applyStyles(element: HTMLElement, style?: Partial<CSSStyleDeclaration>): void { | ||
if (style) { | ||
Object.entries(style).map(([key, value]) => { | ||
if (key.startsWith('--')) { | ||
// Assume CSS variable | ||
element.style.setProperty(key, value as string); | ||
} else { | ||
// Assume camelCase | ||
element.style[key] = value; | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* global document */ | ||
import test from 'tape-promise/tape'; | ||
import {applyStyles} from '@deck.gl/core/utils/apply-styles'; | ||
|
||
const TEST_CASES = [ | ||
{ | ||
title: 'CSS variable', | ||
argument: {property: '--my-var', value: 'red'}, | ||
result: {property: '--my-var', value: 'red'} | ||
}, | ||
{ | ||
title: 'camelCase property', | ||
argument: {property: 'backgroundColor', value: 'red'}, | ||
result: {property: 'background-color', value: 'red'} | ||
} | ||
]; | ||
|
||
test('applyStyles', t => { | ||
const container = document.body.appendChild(document.createElement('div')); | ||
for (const tc of TEST_CASES) { | ||
const {argument, result} = tc; | ||
applyStyles(container, {[argument.property]: argument.value}); | ||
t.deepEqual( | ||
container.style.getPropertyValue(result.property), | ||
result.value, | ||
`applyStyles ${tc.title} returned expected result` | ||
); | ||
} | ||
t.end(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters