• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..22-Nov-2021-

LICENSEH A D22-Nov-20211 KiB2016

README.mdH A D22-Nov-20219.8 KiB259186

index.d.tsH A D22-Nov-2021801.2 KiB20,0073,746

index.js.flowH A D22-Nov-2021297.8 KiB6,0625,414

package.jsonH A D22-Nov-20211.9 KiB6362

README.md

1# CSSType
2
3[![npm](https://img.shields.io/npm/v/csstype.svg)](https://www.npmjs.com/package/csstype)
4
5TypeScript and Flow definitions for CSS, generated by [data from MDN](https://github.com/mdn/data). It provides autocompletion and type checking for CSS properties and values.
6
7**TypeScript**
8
9```ts
10import * as CSS from 'csstype';
11
12const style: CSS.Properties = {
13  colour: 'white', // Type error on property
14  textAlign: 'middle', // Type error on value
15};
16```
17
18**Flow**
19
20```js
21// @flow strict
22import * as CSS from 'csstype';
23
24const style: CSS.Properties<> = {
25  colour: 'white', // Type error on property
26  textAlign: 'middle', // Type error on value
27};
28```
29
30_Further examples below will be in TypeScript!_
31
32## Getting started
33
34```sh
35$ npm install csstype
36$ # or
37$ yarn add csstype
38```
39
40## Table of content
41
42- [Style types](#style-types)
43- [At-rule types](#at-rule-types)
44- [Pseudo types](#pseudo-types)
45- [Generics](#generics)
46- [Usage](#usage)
47- [What should I do when I get type errors?](#what-should-i-do-when-i-get-type-errors)
48- [Version 3.0](#version-30)
49- [Contributing](#contributing)
50
51## Style types
52
53Properties are categorized in different uses and in several technical variations to provide typings that suits as many as possible.
54
55|                | Default              | `Hyphen`                   | `Fallback`                   | `HyphenFallback`                   |
56| -------------- | -------------------- | -------------------------- | ---------------------------- | ---------------------------------- |
57| **All**        | `Properties`         | `PropertiesHyphen`         | `PropertiesFallback`         | `PropertiesHyphenFallback`         |
58| **`Standard`** | `StandardProperties` | `StandardPropertiesHyphen` | `StandardPropertiesFallback` | `StandardPropertiesHyphenFallback` |
59| **`Vendor`**   | `VendorProperties`   | `VendorPropertiesHyphen`   | `VendorPropertiesFallback`   | `VendorPropertiesHyphenFallback`   |
60| **`Obsolete`** | `ObsoleteProperties` | `ObsoletePropertiesHyphen` | `ObsoletePropertiesFallback` | `ObsoletePropertiesHyphenFallback` |
61| **`Svg`**      | `SvgProperties`      | `SvgPropertiesHyphen`      | `SvgPropertiesFallback`      | `SvgPropertiesHyphenFallback`      |
62
63Categories:
64
65- **All** - Includes `Standard`, `Vendor`, `Obsolete` and `Svg`
66- **`Standard`** - Current properties and extends subcategories `StandardLonghand` and `StandardShorthand` _(e.g. `StandardShorthandProperties`)_
67- **`Vendor`** - Vendor prefixed properties and extends subcategories `VendorLonghand` and `VendorShorthand` _(e.g. `VendorShorthandProperties`)_
68- **`Obsolete`** - Removed or deprecated properties
69- **`Svg`** - SVG-specific properties
70
71Variations:
72
73- **Default** - JavaScript (camel) cased property names
74- **`Hyphen`** - CSS (kebab) cased property names
75- **`Fallback`** - Also accepts array of values e.g. `string | string[]`
76
77## At-rule types
78
79At-rule interfaces with descriptors.
80
81**TypeScript**: These will be found in the `AtRule` namespace, e.g. `AtRule.Viewport`.
82**Flow**: These will be prefixed with `AtRule$`, e.g. `AtRule$Viewport`.
83
84|                      | Default        | `Hyphen`             | `Fallback`             | `HyphenFallback`             |
85| -------------------- | -------------- | -------------------- | ---------------------- | ---------------------------- |
86| **`@counter-style`** | `CounterStyle` | `CounterStyleHyphen` | `CounterStyleFallback` | `CounterStyleHyphenFallback` |
87| **`@font-face`**     | `FontFace`     | `FontFaceHyphen`     | `FontFaceFallback`     | `FontFaceHyphenFallback`     |
88| **`@viewport`**      | `Viewport`     | `ViewportHyphen`     | `ViewportFallback`     | `ViewportHyphenFallback`     |
89
90## Pseudo types
91
92String literals of pseudo classes and pseudo elements
93
94- `Pseudos`
95
96  Extends:
97
98  - `AdvancedPseudos`
99
100    Function-like pseudos e.g. `:not(:first-child)`. The string literal contains the value excluding the parenthesis: `:not`. These are separated because they require an argument that results in infinite number of variations.
101
102  - `SimplePseudos`
103
104    Plain pseudos e.g. `:hover` that can only be **one** variation.
105
106## Generics
107
108All interfaces has two optional generic argument to define length and time: `CSS.Properties<TLength = string | 0, TTime = string>`
109
110- **Length** is the first generic parameter and defaults to `string | 0` because `0` is the only [length where the unit identifier is optional](https://drafts.csswg.org/css-values-3/#lengths). You can specify this, e.g. `string | number`, for platforms and libraries that accepts any numeric value as length with a specific unit.
111  ```tsx
112  const style: CSS.Properties<string | number> = {
113    width: 100,
114  };
115  ```
116- **Time** is the second generic argument and defaults to `string`. You can specify this, e.g. `string | number`, for platforms and libraries that accepts any numeric value as length with a specific unit.
117  ```tsx
118  const style: CSS.Properties<string | number, number> = {
119    transitionDuration: 1000,
120  };
121  ```
122
123## Usage
124
125```ts
126import * as CSS from 'csstype';
127
128const style: CSS.Properties = {
129  width: '10px',
130  margin: '1em',
131};
132```
133
134In some cases, like for CSS-in-JS libraries, an array of values is a way to provide fallback values in CSS. Using `CSS.PropertiesFallback` instead of `CSS.Properties` will add the possibility to use any property value as an array of values.
135
136```ts
137import * as CSS from 'csstype';
138
139const style: CSS.PropertiesFallback = {
140  display: ['-webkit-flex', 'flex'],
141  color: 'white',
142};
143```
144
145There's even string literals for pseudo selectors and elements.
146
147```ts
148import * as CSS from 'csstype';
149
150const pseudos: { [P in CSS.SimplePseudos]?: CSS.Properties } = {
151  ':hover': {
152    display: 'flex',
153  },
154};
155```
156
157Hyphen cased (kebab cased) properties are provided in `CSS.PropertiesHyphen` and `CSS.PropertiesHyphenFallback`. It's not **not** added by default in `CSS.Properties`. To allow both of them, you can simply extend with `CSS.PropertiesHyphen` or/and `CSS.PropertiesHyphenFallback`.
158
159```ts
160import * as CSS from 'csstype';
161
162interface Style extends CSS.Properties, CSS.PropertiesHyphen {}
163
164const style: Style = {
165  'flex-grow': 1,
166  'flex-shrink': 0,
167  'font-weight': 'normal',
168  backgroundColor: 'white',
169};
170```
171
172## What should I do when I get type errors?
173
174The goal is to have as perfect types as possible and we're trying to do our best. But with CSS Custom Properties, the CSS specification changing frequently and vendors implementing their own specifications with new releases sometimes causes type errors even if it should work. Here's some steps you could take to get it fixed:
175
176_If you're using CSS Custom Properties you can step directly to step 3._
177
1781.  **First of all, make sure you're doing it right.** A type error could also indicate that you're not :wink:
179
180    - Some CSS specs that some vendors has implemented could have been officially rejected or haven't yet received any official acceptance and are therefor not included
181    - If you're using TypeScript, [type widening](https://blog.mariusschulz.com/2017/02/04/TypeScript-2-1-literal-type-widening) could be the reason you get `Type 'string' is not assignable to...` errors
182
1832.  **Have a look in [issues](https://github.com/frenic/csstype/issues) to see if an issue already has been filed. If not, create a new one.** To help us out, please refer to any information you have found.
1843.  Fix the issue locally with **TypeScript** (Flow further down):
185
186    - The recommended way is to use **module augmentation**. Here's a few examples:
187
188      ```ts
189      // My css.d.ts file
190      import * as CSS from 'csstype';
191
192      declare module 'csstype' {
193        interface Properties {
194          // Add a missing property
195          WebkitRocketLauncher?: string;
196
197          // Add a CSS Custom Property
198          '--theme-color'?: 'black' | 'white';
199
200          // ...or allow any other property
201          [index: string]: any;
202        }
203      }
204      ```
205
206    - The alternative way is to use **type assertion**. Here's a few examples:
207
208      ```ts
209      const style: CSS.Properties = {
210        // Add a missing property
211        ['WebkitRocketLauncher' as any]: 'launching',
212
213        // Add a CSS Custom Property
214        ['--theme-color' as any]: 'black',
215      };
216      ```
217
218    Fix the issue locally with **Flow**:
219
220    - Use **type assertion**. Here's a few examples:
221
222      ```js
223      const style: $Exact<CSS.Properties<*>> = {
224        // Add a missing property
225        [('WebkitRocketLauncher': any)]: 'launching',
226
227        // Add a CSS Custom Property
228        [('--theme-color': any)]: 'black',
229      };
230      ```
231
232## Version 3.0
233
234- **All property types are exposed with namespace**
235  TypeScript: `Property.AlignContent` (was `AlignContentProperty` before)
236  Flow: `Property$AlignContent`
237- **All at-rules are exposed with namespace**
238  TypeScript: `AtRule.FontFace` (was `FontFace` before)
239  Flow: `AtRule$FontFace`
240- **Data types are NOT exposed**
241  E.g. `Color` and `Box`. Because the generation of data types may suddenly be removed or renamed.
242- **TypeScript hack for autocompletion**
243  Uses `(string & {})` for literal string unions and `(number & {})` for literal number unions ([related issue](https://github.com/microsoft/TypeScript/issues/29729)). Utilize `PropertyValue<T>` to unpack types from e.g. `(string & {})` to `string`.
244- **New generic for time**
245  Read more on the ["Generics"](#generics) section.
246- **Flow types improvements**
247  Flow Strict enabled and exact types are used.
248
249## Contributing
250
251**Never modify `index.d.ts` and `index.js.flow` directly. They are generated automatically and committed so that we can easily follow any change it results in.** Therefor it's important that you run `$ git config merge.ours.driver true` after you've forked and cloned. That setting prevents merge conflicts when doing rebase.
252
253### Commands
254
255- `yarn build` Generates typings and type checks them
256- `yarn watch` Runs build on each save
257- `yarn test` Runs the tests
258- `yarn lazy` Type checks, lints and formats everything
259