Skip to content

Commit ae9864c

Browse files
Rename mapToStyles to styleVariants (#37)
1 parent 756d9b0 commit ae9864c

File tree

7 files changed

+70
-52
lines changed

7 files changed

+70
-52
lines changed

.changeset/spicy-panthers-deny.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
'@vanilla-extract/babel-plugin': minor
3+
'@vanilla-extract/css': minor
4+
---
5+
6+
Rename `mapToStyles` to `styleVariants`
7+
8+
**BREAKING CHANGE**
9+
10+
```diff
11+
-import { mapToStyles } from '@vanilla-extract/css';
12+
+import { styleVariants } from '@vanilla-extract/css';
13+
14+
-export const variant = mapToStyles({
15+
+export const variant = styleVariants({
16+
primary: { background: 'blue' },
17+
secondary: { background: 'aqua' },
18+
});
19+
```

README.md

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ document.write(`
8282
- [Gatsby](#gatsby)
8383
- [API](#api)
8484
- [style](#style)
85+
- [styleVariants](#styleVariants)
8586
- [globalStyle](#globalstyle)
86-
- [mapToStyles](#maptostyles)
8787
- [createTheme](#createtheme)
8888
- [createGlobalTheme](#createglobaltheme)
8989
- [createThemeVars](#createthemevars)
@@ -263,60 +263,59 @@ export const childClass = style({
263263
>
264264
>If you want to globally target child nodes within the current element (e.g. `'& > a'`), you should use [`globalStyle`](#globalstyle) instead.
265265
266-
### globalStyle
266+
### styleVariants
267267

268-
Creates styles attached to a global selector.
268+
Creates a collection of named style variants.
269269

270270
```ts
271-
import { globalStyle } from '@vanilla-extract/css';
271+
import { styleVariants } from '@vanilla-extract/css';
272272

273-
globalStyle('html, body', {
274-
margin: 0
273+
export const variant = styleVariants({
274+
primary: { background: 'blue' },
275+
secondary: { background: 'aqua' },
275276
});
276277
```
277278

278-
Global selectors can also contain references to other scoped class names.
279+
> 💡 This is useful for mapping component props to styles, e.g. `<button className={styles.variant[props.variant]}>`
280+
281+
You can also transform the values by providing a map function as the second argument.
279282

280283
```ts
281-
import { globalStyle } from '@vanilla-extract/css';
284+
import { styleVariants } from '@vanilla-extract/css';
282285

283-
export const parentClass = style({});
286+
const spaceScale = {
287+
small: 4,
288+
medium: 8,
289+
large: 16
290+
};
284291

285-
globalStyle(`${parentClass} > a`, {
286-
color: 'pink'
287-
});
292+
export const padding = styleVariants(spaceScale, (space) => ({
293+
padding: space
294+
}));
288295
```
289296

290-
### mapToStyles
291-
292-
Creates an object that maps style names to hashed class names.
297+
### globalStyle
293298

294-
> 💡 This is useful for mapping to component props, e.g. `<div className={styles.padding[props.padding]}>`
299+
Creates styles attached to a global selector.
295300

296301
```ts
297-
import { mapToStyles } from '@vanilla-extract/css';
302+
import { globalStyle } from '@vanilla-extract/css';
298303

299-
export const padding = mapToStyles({
300-
small: { padding: 4 },
301-
medium: { padding: 8 },
302-
large: { padding: 16 }
304+
globalStyle('html, body', {
305+
margin: 0
303306
});
304307
```
305308

306-
You can also transform the values by providing a map function as the second argument.
309+
Global selectors can also contain references to other scoped class names.
307310

308311
```ts
309-
import { mapToStyles } from '@vanilla-extract/css';
312+
import { globalStyle } from '@vanilla-extract/css';
310313

311-
const spaceScale = {
312-
small: 4,
313-
medium: 8,
314-
large: 16
315-
};
314+
export const parentClass = style({});
316315

317-
export const padding = mapToStyles(spaceScale, (space) => ({
318-
padding: space
319-
}));
316+
globalStyle(`${parentClass} > a`, {
317+
color: 'pink'
318+
});
320319
```
321320

322321
### createTheme

docs/treat-migration-guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ You would then update your styles to use this new CSS Variable instead.
318318

319319
## `styleMap`
320320

321-
You can use [`mapToStyles`](https://github.com/seek-oss/vanilla-extract#maptostyles) as a drop-in replacement. Note that it now accepts a map function as the second argument, so there may be some opportunities to simplify your code if you were mapping over objects before passing them to `styleMap`.
321+
You can use [`styleVariants`](https://github.com/seek-oss/vanilla-extract#stylevariants) as a drop-in replacement. Note that it now accepts a map function as the second argument, so there may be some opportunities to simplify your code if you were mapping over objects before passing them to `styleMap`.
322322

323323
## `styleTree`
324324

fixtures/themed/src/styles.css.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
22
style,
3-
mapToStyles,
3+
styleVariants,
44
createVar,
55
fallbackVar,
66
fontFace,
@@ -77,7 +77,7 @@ export const button = [
7777
const undefinedVar1 = createVar();
7878
const undefinedVar2 = createVar();
7979

80-
export const opacity = mapToStyles(
80+
export const opacity = styleVariants(
8181
{
8282
'1/2': fallbackVar(undefinedVar1, '0.5'),
8383
'1/4': fallbackVar(undefinedVar1, undefinedVar2, '0.25'),

packages/babel-plugin/src/index.test.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,20 @@ describe('babel plugin', () => {
4242
`);
4343
});
4444

45-
it('should handle mapToStyles assigned to const', () => {
45+
it('should handle styleVariants assigned to const', () => {
4646
const source = `
47-
import { mapToStyles } from '@vanilla-extract/css';
47+
import { styleVariants } from '@vanilla-extract/css';
4848
49-
const colors = mapToStyles({
49+
const colors = styleVariants({
5050
red: { color: 'red' }
5151
});
5252
`;
5353

5454
expect(transform(source)).toMatchInlineSnapshot(`
5555
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
5656
setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
57-
import { mapToStyles } from '@vanilla-extract/css';
58-
const colors = mapToStyles({
57+
import { styleVariants } from '@vanilla-extract/css';
58+
const colors = styleVariants({
5959
red: {
6060
color: 'red'
6161
}
@@ -64,20 +64,20 @@ describe('babel plugin', () => {
6464
`);
6565
});
6666

67-
it('should handle mapToStyles with mapper assigned to const', () => {
67+
it('should handle styleVariants with mapper assigned to const', () => {
6868
const source = `
69-
import { mapToStyles } from '@vanilla-extract/css';
69+
import { styleVariants } from '@vanilla-extract/css';
7070
71-
const colors = mapToStyles({
71+
const colors = styleVariants({
7272
red: 'red'
7373
}, (color) => ({ color }));
7474
`;
7575

7676
expect(transform(source)).toMatchInlineSnapshot(`
7777
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
7878
setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
79-
import { mapToStyles } from '@vanilla-extract/css';
80-
const colors = mapToStyles({
79+
import { styleVariants } from '@vanilla-extract/css';
80+
const colors = styleVariants({
8181
red: 'red'
8282
}, color => ({
8383
color
@@ -406,29 +406,29 @@ describe('babel plugin', () => {
406406

407407
it('should ignore functions that already supply a debug name', () => {
408408
const source = `
409-
import { style, mapToStyles } from '@vanilla-extract/css';
409+
import { style, styleVariants } from '@vanilla-extract/css';
410410
411411
const three = style({
412412
testStyle: {
413413
zIndex: 2,
414414
}
415415
}, 'myDebugValue');
416416
417-
const four = mapToStyles({
417+
const four = styleVariants({
418418
red: { color: 'red' }
419419
}, 'myDebugValue');
420420
`;
421421

422422
expect(transform(source)).toMatchInlineSnapshot(`
423423
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
424424
setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
425-
import { style, mapToStyles } from '@vanilla-extract/css';
425+
import { style, styleVariants } from '@vanilla-extract/css';
426426
const three = style({
427427
testStyle: {
428428
zIndex: 2
429429
}
430430
}, 'myDebugValue');
431-
const four = mapToStyles({
431+
const four = styleVariants({
432432
red: {
433433
color: 'red'
434434
}

packages/babel-plugin/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const debuggableFunctionConfig = {
1818
createTheme: {
1919
maxParams: 3,
2020
},
21-
mapToStyles: {
21+
styleVariants: {
2222
maxParams: 3,
2323
},
2424
fontFace: {

packages/css/src/style.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ export function globalKeyframes(name: string, rule: CSSKeyframes) {
6868
appendCss({ type: 'keyframes', name, rule }, getFileScope());
6969
}
7070

71-
export function mapToStyles<
71+
export function styleVariants<
7272
StyleMap extends Record<string | number, StyleRule>
7373
>(styleMap: StyleMap, debugId?: string): Record<keyof StyleMap, string>;
74-
export function mapToStyles<Data extends Record<string | number, unknown>>(
74+
export function styleVariants<Data extends Record<string | number, unknown>>(
7575
data: Data,
7676
mapData: <Key extends keyof Data>(value: Data[Key], key: Key) => StyleRule,
7777
debugId?: string,
7878
): Record<keyof Data, string>;
79-
export function mapToStyles(...args: any[]) {
79+
export function styleVariants(...args: any[]) {
8080
if (typeof args[1] === 'function') {
8181
const data = args[0];
8282
const mapData = args[1];

0 commit comments

Comments
 (0)