mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-15 02:11:05 +00:00
feat(lib): add css utility to format style values
This commit is contained in:
parent
c2ef879862
commit
9c83fbda69
3 changed files with 26 additions and 0 deletions
9
src/lib/__tests__/css.spec.js
Normal file
9
src/lib/__tests__/css.spec.js
Normal file
|
@ -0,0 +1,9 @@
|
|||
import { css } from '../css';
|
||||
|
||||
test('css', () => {
|
||||
expect(css([])).toEqual('');
|
||||
expect(css(['width: 20px; height: 20px;'])).toEqual('width: 20px; height: 20px');
|
||||
expect(css(['width: 20px;', ['height', '20px']])).toEqual('width: 20px; height: 20px');
|
||||
expect(css(['width: 20px', ['height', '20px']])).toEqual('width: 20px; height: 20px');
|
||||
expect(css([undefined, null, 0, ['height', '20px']])).toEqual('height: 20px');
|
||||
});
|
16
src/lib/css.js
Normal file
16
src/lib/css.js
Normal file
|
@ -0,0 +1,16 @@
|
|||
function css(array) {
|
||||
return array
|
||||
.map((item, i) => {
|
||||
if (!item) return false;
|
||||
|
||||
return Array.isArray(item)
|
||||
? item.join(': ')
|
||||
: item.slice(-1) === ';'
|
||||
? item.slice(0, -1)
|
||||
: item;
|
||||
})
|
||||
.filter(Boolean)
|
||||
.join('; ');
|
||||
}
|
||||
|
||||
export { css };
|
|
@ -1 +1,2 @@
|
|||
export { cx } from './cx';
|
||||
export { css } from './css';
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue