Skip to content

Commit

Permalink
address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
z4o4z committed May 31, 2023
1 parent 81e8540 commit 11d4fa7
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 52 deletions.
27 changes: 25 additions & 2 deletions examples/next/components/HelloWorld.css.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
import { style } from '@vanilla-extract/css';
import { style, createVar, keyframes, getVarName, fallbackVar } from '@vanilla-extract/css';

const color = createVar();
const angle = createVar({
syntax: '<angle>',
inherits: false,
initialValue: '0deg',
});

const angleKeyframes = keyframes({
'100%': {
[getVarName(angle)]: '360deg',
},
});

export const root = style({
background: 'pink',
color: 'blue',
padding: '16px',
transition: 'opacity .1s ease', // Testing autoprefixer
transition: `opacity .1s ease, color .1s ease`, // Testing autoprefixer
backgroundImage: `linear-gradient(${angle}, rgba(153, 70, 198, 0.35) 0%, rgba(28, 56, 240, 0.46) 100%)`,
animation: `${angleKeyframes} 7s infinite ease-in-out both`,


':hover': {
opacity: 0.8,
color: color,
},

vars: {
[color]: '#fef',
[angle]: fallbackVar(angle, '138deg'),
}
});
3 changes: 2 additions & 1 deletion packages/css/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { getVarName } from '@vanilla-extract/private';

import './runtimeAdapter';

export type {
Expand All @@ -14,4 +16,3 @@ export * from './style';
export * from './vars';
export { createContainer } from './container';
export * from './layer';
export * from './properties';
46 changes: 0 additions & 46 deletions packages/css/src/properties.ts

This file was deleted.

41 changes: 38 additions & 3 deletions packages/css/src/vars.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import type { AtRule } from 'csstype';


import {
get,
walkObject,
Expand All @@ -9,20 +12,52 @@ import cssesc from 'cssesc';

import { Tokens, NullableTokens, ThemeVars } from './types';
import { validateContract } from './validateContract';
import { getFileScope } from './fileScope';
import { generateIdentifier } from './identifier';

export function createVar(debugId?: string): CSSVarFunction {
import { PropertySyntax } from './types';
import { appendCss } from './adapter';

type VarDeclaration = {
syntax: PropertySyntax | Array<PropertySyntax>;
inherits: boolean;
initialValue?: string
};

const buildPropertyRule = ({ syntax, inherits, initialValue }: VarDeclaration): AtRule.Property => ({
syntax: `"${Array.isArray(syntax) ? syntax.join(' | ') : syntax}"`,
inherits: inherits ? 'true' : 'false',
initialValue,
})

export function createVar(declaration: VarDeclaration, debugId?: string): CSSVarFunction
export function createVar(debugId?: string): CSSVarFunction
export function createVar(debugIdOrDeclaration?: string | VarDeclaration, debugId?: string): CSSVarFunction {
const cssVarName = cssesc(
generateIdentifier({
debugId,
debugId: typeof debugIdOrDeclaration === 'string' ? debugIdOrDeclaration : debugId,
debugFileName: false,
}),
{ isIdentifier: true },
);

if (debugIdOrDeclaration && typeof debugIdOrDeclaration === 'object') {
appendCss({ type: 'property', name: `--${cssVarName}`, rule: buildPropertyRule(debugIdOrDeclaration) }, getFileScope());
}

return `var(--${cssVarName})` as const;
}

export function createGlobalVar(name: string): CSSVarFunction
export function createGlobalVar(name: string, declaration: VarDeclaration): CSSVarFunction
export function createGlobalVar(name: string, declaration?: VarDeclaration): string {
if (declaration && typeof declaration === 'object') {
appendCss({ type: 'property', name: `--${name}`, rule: buildPropertyRule(declaration) }, getFileScope());
}


return `var(--${name})`;
}

export function assertVarName(value: unknown): asserts value is `var(--${string})` {
if (typeof value !== 'string' || !/^var\(--.*\)$/.test(value)) {
throw new Error(`Invalid variable name: ${value}`);
Expand Down
2 changes: 2 additions & 0 deletions packages/dynamic/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export { getVarName } from '@vanilla-extract/private';

export { assignInlineVars } from './assignInlineVars';
export { setElementVars } from './setElementVars';

0 comments on commit 11d4fa7

Please sign in to comment.