一个在TypeScript中,带有连字符的有效CSS属性名称数组

huangapple go评论73阅读模式
英文:

An array of valid CSS property names in TypeScript, but hyphened

问题

我在组件中有一个属性,应该接受一个有效CSS属性的数组:

interface MyComponentProps {
    cssProperties: (keyof CSSStyleDeclaration)[];
}

const MyComponent = ({ cssProperties }: MyComponentProps) => {
    //
}

问题在于,CSSStyleDeclaration 将样式存储为具有驼峰式属性名称的对象。我需要真正的CSS属性值,带连字符的,例如 background-color 而不是 backgroundColor。我知道还有 React.CSSProperties 类型,但它也使用驼峰式属性,同时允许无单位的数字值。

是否有一种TypeScript类型,可以使用原始的、带连字符的CSS属性名称?

英文:

I have a property in my component that is supposed to get an array of valid CSS properties:

interface MyComponentProps {
    cssProperties: (keyof CSSStyleDeclaration)[];
}

const MyComponent = ({ cssProperties }: MyComponentProps) => {
    //
}

The problem is that CSSStyleDeclaration stores styles as an object with property names in camel case. I need real CSS property values, hyphened. So background-color instead of backgroundColor. I know there's also React.CSSProperties type, but it uses camel-cased properties too, while allowing for unitless numeric values.

Is there a TypeScript type to use original, hyphened CSS property names?

答案1

得分: 1

使用`Kebab`类型在[这里][1]可用。 _驼峰命名_的CSS属性将被转换为 _短横线命名_

    type Kebab<T extends string, A extends string = ""> =
        T extends `${infer F}${infer R}` ?
        Kebab<R, `${A}${F extends Lowercase<F> ? "" : "-"}${Lowercase<F>}`> :
        A
    
    type Props = {
        cssProps: Kebab<keyof React.CSSProperties>[];
    }
    
    const Component = (props: Props) => null;
    
    const App = () => (
      <Component cssProps={['min-width']} />
    );

[Typescript Playground][2]

  [1]: https://stackoverflow.com/a/66140779/6695924
  [2]: https://www.typescriptlang.org/play?target=8&ts=4.8.4#code/C4TwDgpgBA0hBGBDeAeAKlCAPYEB2AJgM5RHABOAlngOYA0UAgpjvsaRdTVALxQBE-AHy8AUFAlQM2XIRIADACQBvagDMI5KADEAvivWaoAJV3yoAfnGS4SVMYZLljfcu0tZ7ADIB7AO6aAMaIRBAo2iIWAvxQAFwCALT8rr4B5MGh4UJmIrHWEoyiopQAtmA+5MAmEIiBVWrkPiVQAOTkNXUtANxFoJBQAAqNYCR8yvlQgUREQz4j8bbIKADWECA+atW1wAB0AMIAygezkJWUEERCANoAuj26RYE+eGRQe03lePhVfAAUYMMiPETkQAJS8ER4ACuABsYT1RE8XlVGGAwLwoL9wTwRL9rCh3mVnt9JtMQTxlFcWiVqAk-JQCMAABYtG66KAAeiEolBXSAA
英文:

Use Kebab type available here. Camelcased CSS properties will be transformed into kebabcase.

type Kebab&lt;T extends string, A extends string = &quot;&quot;&gt; =
    T extends `${infer F}${infer R}` ?
    Kebab&lt;R, `${A}${F extends Lowercase&lt;F&gt; ? &quot;&quot; : &quot;-&quot;}${Lowercase&lt;F&gt;}`&gt; :
    A

type Props = {
    cssProps: Kebab&lt;keyof React.CSSProperties&gt;[];
}

const Component = (props: Props) =&gt; null;

const App = () =&gt; (
  &lt;Component cssProps={[&#39;min-width&#39;]} /&gt;
);

Typescript Playground

答案2

得分: 1

有一个名为csstype的库,被MUI、emotion和一些其他流行的组件库使用。

它允许你使用连字符属性。

import CSS from "csstype";

interface MyComponentProps {
  cssProperties: (keyof CSS.PropertiesHyphen)[];
}

const MyComponent = ({ cssProperties }: MyComponentProps) => {
  //
}

<MyComponent cssProperties={["color", "background-color"]} />;
英文:

There is this library csstype which is used by MUI, emotion and some other popular component libraries.

Which allows you to use the hyphen props

import CSS from &quot;csstype&quot;;

interface MyComponentProps {
  cssProperties: (keyof CSS.PropertiesHyphen)[];
}

const MyComponent = ({ cssProperties }: MyComponentProps) =&gt; {
  //
}

&lt;MyComponent cssProperties={[&quot;color&quot;, &quot;background-color&quot;]} /&gt;;

huangapple
  • 本文由 发表于 2023年2月6日 18:06:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359868.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定