Skip to content

Support passing arrays of styles to globalStyle #1559

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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/seven-singers-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@vanilla-extract/css': minor
---

Support passing arrays of styles to `globalStyle`
1 change: 1 addition & 0 deletions packages/css/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type {
StyleRule,
ComplexStyleRule,
GlobalStyleRule,
ComplexGlobalStyleRule,
Adapter,
FileScope,
CSSProperties,
Expand Down
25 changes: 19 additions & 6 deletions packages/css/src/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type {
GlobalStyleRule,
ClassNames,
ComplexStyleRule,
ComplexGlobalStyleRule,
CSSPropertiesWithVars,
} from './types';
import {
registerClassName,
Expand All @@ -20,6 +22,13 @@ import { getFileScope, hasFileScope } from './fileScope';
import { generateIdentifier } from './identifier';
import { dudupeAndJoinClassList } from './utils';

function composedStyleRule(rules: Array<CSSPropertiesWithVars>) {
return deepmerge.all(rules, {
// Replace arrays rather than merging
arrayMerge: (_, sourceArray) => sourceArray,
});
}

function composedStyle(rules: Array<StyleRule | ClassNames>, debugId?: string) {
const className = generateIdentifier(debugId);
registerClassName(className, getFileScope());
Expand Down Expand Up @@ -56,10 +65,10 @@ function composedStyle(rules: Array<StyleRule | ClassNames>, debugId?: string) {
}

if (styleRules.length > 0) {
const rule = deepmerge.all(styleRules, {
// Replace arrays rather than merging
arrayMerge: (_, sourceArray) => sourceArray,
});
// Remove type cast below (and this assertion) when this assertion becomes invalid
// @ts-expect-error
styleRules satisfies Array<StyleRule>;
const rule = composedStyleRule(styleRules as Array<StyleRule>);

appendCss({ type: 'local', selector: className, rule }, getFileScope());
}
Expand Down Expand Up @@ -89,8 +98,12 @@ export function composeStyles(...classNames: Array<ClassNames>) {
return compose(classNames);
}

export function globalStyle(selector: string, rule: GlobalStyleRule) {
appendCss({ type: 'global', selector, rule }, getFileScope());
export function globalStyle(selector: string, rule: ComplexGlobalStyleRule) {
const composedRule: GlobalStyleRule = Array.isArray(rule)
? composedStyleRule(rule)
: rule;

appendCss({ type: 'global', selector, rule: composedRule }, getFileScope());
}

export function fontFace(
Expand Down
1 change: 1 addition & 0 deletions packages/css/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ export type ThemeVars<ThemeContract extends NullableTokens> = MapLeafNodes<
export type ClassNames = string | Array<ClassNames>;

export type ComplexStyleRule = StyleRule | Array<StyleRule | ClassNames>;
export type ComplexGlobalStyleRule = GlobalStyleRule | Array<GlobalStyleRule>;

type _PropertySyntax =
| '<angle>'
Expand Down