-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae9864c
commit 156b491
Showing
5 changed files
with
84 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@vanilla-extract/css': minor | ||
--- | ||
|
||
Add `composeStyles` function |
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,25 @@ | ||
import { composeStyles } from './composeStyles'; | ||
|
||
describe('composeStyles', () => { | ||
it.each([ | ||
{ args: ['1'], output: '1' }, | ||
{ args: ['1 1'], output: '1' }, | ||
{ args: ['1 2 3'], output: '1 2 3' }, | ||
{ args: ['1', '2', '3'], output: '1 2 3' }, | ||
{ args: [['1', '2'], '3'], output: '1 2 3' }, | ||
{ args: [['1', ['2', '3']], '4'], output: '1 2 3 4' }, | ||
{ | ||
args: [['1', ['2', ['3', '4']]], ['5', '6'], '7'], | ||
output: '1 2 3 4 5 6 7', | ||
}, | ||
{ args: ['1', '1', '1'], output: '1' }, | ||
{ | ||
args: ['1', ['1', '2'], ['1', '2', '3'], ['1', '2', '3', '4']], | ||
output: '1 2 3 4', | ||
}, | ||
{ args: ['1 2 3', '2 3 4', '1 5'], output: '1 2 3 4 5' }, | ||
{ args: [' 1 2 3 2 ', ' 2 3 4 2 ', ' 1 5 1 '], output: '1 2 3 4 5' }, | ||
])('composeStyles', ({ args, output }) => { | ||
expect(composeStyles(...args)).toBe(output); | ||
}); | ||
}); |
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 @@ | ||
type ClassNames = string | Array<ClassNames>; | ||
|
||
function composeStylesIntoSet( | ||
set: Set<string>, | ||
...classNames: Array<ClassNames> | ||
) { | ||
for (const className of classNames) { | ||
if (className.length === 0) { | ||
continue; | ||
} | ||
|
||
if (typeof className === 'string') { | ||
if (className.includes(' ')) { | ||
composeStylesIntoSet(set, ...className.trim().split(' ')); | ||
} else { | ||
set.add(className); | ||
} | ||
} else if (Array.isArray(className)) { | ||
composeStylesIntoSet(set, ...className); | ||
} | ||
} | ||
} | ||
|
||
export function composeStyles(...classNames: Array<ClassNames>) { | ||
const set: Set<string> = new Set(); | ||
|
||
composeStylesIntoSet(set, ...classNames); | ||
|
||
return Array.from(set).join(' '); | ||
} |
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