frenic / csstype

Strict TypeScript and Flow types for style based on MDN data
MIT License
1.7k stars 69 forks source link

Allow CSS var function to inject custom properties values #126

Open sathishlxg opened 3 years ago

sathishlxg commented 3 years ago

Currently CSS properties do not accept var function to inject values from custom properties. As a workaround we are using the template literal type and create new type. I'm aware of the workaround for type error but can this be supported natively.

import * as CSS from 'csstype';

type GlobalsWithVars = `var(--${string})` | `var(--${string}, ${string | number})`;

// css variable should be applied to all css properties
type CSSPropertiesWithVar<T extends CSS.Properties = CSS.Properties> = {
    [P in keyof T]: T[P] | GlobalsWithVar
}

const style: CSSPropertiesWithVar = {
   fontWeight: 'var(--font-weight)'
}
frenic commented 3 years ago

Good idea! 👌 Thanks

joriswitteman commented 1 year ago

Would you recommend libraries that use csstype to work around this until the assigned milestone comes around? Could FontWeight be changed to take arbitrary strings in the meantime?

kylemh commented 1 year ago

I was curious if anybody has a workaround in terms of declaration merging.

I ended up with:

import type * as CSS from 'csstype';

declare type LiteralUnion<T extends TPrimitive, TPrimitive = string> = T | (TPrimitive & {});

declare module 'csstype' {
  interface StandardLonghandProperties<TLength = LiteralUnion<string> | 0, TTime = LiteralUnion<string>>
    extends CSS.StandardLonghandProperties<TLength, TTime> {
    [propertyName: string]: `var(--${string})`;
  }
}

but it's not working :(