-
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): widget style prop keys should accept camelCase css prop…
…erties and dashed css variables (#8991) * fix(widgets): handle css variable and camel cased keys passed to widget style prop --------- Co-authored-by: Chris Gervang <[email protected]>
- Loading branch information
1 parent
e9eada6
commit 691baea
Showing
11 changed files
with
149 additions
and
23 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
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,27 @@ | ||
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; | ||
} | ||
}); | ||
} | ||
} | ||
|
||
export function removeStyles(element: HTMLElement, style?: Partial<CSSStyleDeclaration>): void { | ||
if (style) { | ||
Object.keys(style).map(key => { | ||
if (key.startsWith('--')) { | ||
// Assume CSS variable | ||
element.style.removeProperty(key); | ||
} else { | ||
// Assume camelCase | ||
element.style[key] = ''; | ||
} | ||
}); | ||
} | ||
} |
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,59 @@ | ||
/* global document */ | ||
import test from 'tape-promise/tape'; | ||
import {applyStyles, removeStyles} from '@deck.gl/core/utils/apply-styles'; | ||
|
||
const APPLY_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 APPLY_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(); | ||
}); | ||
|
||
const REMOVE_TEST_CASES = [ | ||
{ | ||
title: 'CSS variable', | ||
argument: {property: '--my-var', value: 'red'}, | ||
result: {property: '--my-var', value: ''} | ||
}, | ||
{ | ||
title: 'camelCase property', | ||
argument: {property: 'backgroundColor', value: 'red'}, | ||
result: {property: 'background-color', value: ''} | ||
} | ||
]; | ||
|
||
test('removeStyles', t => { | ||
const container = document.body.appendChild(document.createElement('div')); | ||
for (const tc of REMOVE_TEST_CASES) { | ||
const {argument, result} = tc; | ||
// setProperty expects kabab-case | ||
container.style.setProperty(result.property, argument.value); | ||
removeStyles(container, {[argument.property]: argument.value}); | ||
t.deepEqual( | ||
container.style.getPropertyValue(result.property), | ||
result.value, | ||
`removeStyles ${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