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

Add composeStyles function #38

Merged
merged 11 commits into from
Apr 22, 2021
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
5 changes: 5 additions & 0 deletions .changeset/small-guests-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@vanilla-extract/css': minor
---

Add `composeStyles` function
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ document.write(`
- [globalFontFace](#globalfontface)
- [keyframes](#keyframes)
- [globalKeyframes](#globalkeyframes)
- [composeStyles](#composestyles)
- [Dynamic API](#dynamic-api)
- [createInlineTheme](#createinlinetheme)
- [setElementTheme](#setelementtheme)
Expand Down Expand Up @@ -580,6 +581,28 @@ export const animated = style({
});
```

### composeStyles

Combines mutliple styles into a single class string, while also deduplicating and removing unnecessary spaces.

```ts
import { style, composeStyles } from '@vanilla-extract/css';

const base = style({
padding: 12
});

export const blue = composeStyles(base, style({
background: 'blue'
}));

export const green = composeStyles(base, style({
background: 'green'
}));
```

> 💡 Styles can also be provided in shallow and deeply nested arrays. Think of it as a static version of [classnames.](https://github.com/JedWatson/classnames)

## Dynamic API

We also provide a lightweight standalone package to support dynamic runtime theming.
Expand Down
25 changes: 25 additions & 0 deletions packages/css/src/composeStyles.test.ts
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);
});
});
30 changes: 30 additions & 0 deletions packages/css/src/composeStyles.ts
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(' ');
}
1 change: 1 addition & 0 deletions packages/css/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './identifier';
export * from './theme';
export * from './style';
export * from './vars';
export { composeStyles } from './composeStyles';