1 | # CSSType
|
---|
2 |
|
---|
3 | [![npm](https://img.shields.io/npm/v/csstype.svg)](https://www.npmjs.com/package/csstype)
|
---|
4 |
|
---|
5 | TypeScript 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
|
---|
10 | import type * as CSS from 'csstype';
|
---|
11 |
|
---|
12 | const 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
|
---|
22 | import * as CSS from 'csstype';
|
---|
23 |
|
---|
24 | const 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 | ```
|
---|
37 |
|
---|
38 | ## Table of content
|
---|
39 |
|
---|
40 | - [Style types](#style-types)
|
---|
41 | - [At-rule types](#at-rule-types)
|
---|
42 | - [Pseudo types](#pseudo-types)
|
---|
43 | - [Generics](#generics)
|
---|
44 | - [Usage](#usage)
|
---|
45 | - [What should I do when I get type errors?](#what-should-i-do-when-i-get-type-errors)
|
---|
46 | - [Version 3.0](#version-30)
|
---|
47 | - [Contributing](#contributing)
|
---|
48 |
|
---|
49 | ## Style types
|
---|
50 |
|
---|
51 | Properties are categorized in different uses and in several technical variations to provide typings that suits as many as possible.
|
---|
52 |
|
---|
53 | | | Default | `Hyphen` | `Fallback` | `HyphenFallback` |
|
---|
54 | | -------------- | -------------------- | -------------------------- | ---------------------------- | ---------------------------------- |
|
---|
55 | | **All** | `Properties` | `PropertiesHyphen` | `PropertiesFallback` | `PropertiesHyphenFallback` |
|
---|
56 | | **`Standard`** | `StandardProperties` | `StandardPropertiesHyphen` | `StandardPropertiesFallback` | `StandardPropertiesHyphenFallback` |
|
---|
57 | | **`Vendor`** | `VendorProperties` | `VendorPropertiesHyphen` | `VendorPropertiesFallback` | `VendorPropertiesHyphenFallback` |
|
---|
58 | | **`Obsolete`** | `ObsoleteProperties` | `ObsoletePropertiesHyphen` | `ObsoletePropertiesFallback` | `ObsoletePropertiesHyphenFallback` |
|
---|
59 | | **`Svg`** | `SvgProperties` | `SvgPropertiesHyphen` | `SvgPropertiesFallback` | `SvgPropertiesHyphenFallback` |
|
---|
60 |
|
---|
61 | Categories:
|
---|
62 |
|
---|
63 | - **All** - Includes `Standard`, `Vendor`, `Obsolete` and `Svg`
|
---|
64 | - **`Standard`** - Current properties and extends subcategories `StandardLonghand` and `StandardShorthand` _(e.g. `StandardShorthandProperties`)_
|
---|
65 | - **`Vendor`** - Vendor prefixed properties and extends subcategories `VendorLonghand` and `VendorShorthand` _(e.g. `VendorShorthandProperties`)_
|
---|
66 | - **`Obsolete`** - Removed or deprecated properties
|
---|
67 | - **`Svg`** - SVG-specific properties
|
---|
68 |
|
---|
69 | Variations:
|
---|
70 |
|
---|
71 | - **Default** - JavaScript (camel) cased property names
|
---|
72 | - **`Hyphen`** - CSS (kebab) cased property names
|
---|
73 | - **`Fallback`** - Also accepts array of values e.g. `string | string[]`
|
---|
74 |
|
---|
75 | ## At-rule types
|
---|
76 |
|
---|
77 | At-rule interfaces with descriptors.
|
---|
78 |
|
---|
79 | **TypeScript**: These will be found in the `AtRule` namespace, e.g. `AtRule.Viewport`.
|
---|
80 | **Flow**: These will be prefixed with `AtRule$`, e.g. `AtRule$Viewport`.
|
---|
81 |
|
---|
82 | | | Default | `Hyphen` | `Fallback` | `HyphenFallback` |
|
---|
83 | | -------------------- | -------------- | -------------------- | ---------------------- | ---------------------------- |
|
---|
84 | | **`@counter-style`** | `CounterStyle` | `CounterStyleHyphen` | `CounterStyleFallback` | `CounterStyleHyphenFallback` |
|
---|
85 | | **`@font-face`** | `FontFace` | `FontFaceHyphen` | `FontFaceFallback` | `FontFaceHyphenFallback` |
|
---|
86 | | **`@viewport`** | `Viewport` | `ViewportHyphen` | `ViewportFallback` | `ViewportHyphenFallback` |
|
---|
87 |
|
---|
88 | ## Pseudo types
|
---|
89 |
|
---|
90 | String literals of pseudo classes and pseudo elements
|
---|
91 |
|
---|
92 | - `Pseudos`
|
---|
93 |
|
---|
94 | Extends:
|
---|
95 |
|
---|
96 | - `AdvancedPseudos`
|
---|
97 |
|
---|
98 | 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.
|
---|
99 |
|
---|
100 | - `SimplePseudos`
|
---|
101 |
|
---|
102 | Plain pseudos e.g. `:hover` that can only be **one** variation.
|
---|
103 |
|
---|
104 | ## Generics
|
---|
105 |
|
---|
106 | All interfaces has two optional generic argument to define length and time: `CSS.Properties<TLength = string | 0, TTime = string>`
|
---|
107 |
|
---|
108 | - **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.
|
---|
109 | ```tsx
|
---|
110 | const style: CSS.Properties<string | number> = {
|
---|
111 | width: 100,
|
---|
112 | };
|
---|
113 | ```
|
---|
114 | - **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.
|
---|
115 | ```tsx
|
---|
116 | const style: CSS.Properties<string | number, number> = {
|
---|
117 | transitionDuration: 1000,
|
---|
118 | };
|
---|
119 | ```
|
---|
120 |
|
---|
121 | ## Usage
|
---|
122 |
|
---|
123 | ```ts
|
---|
124 | import type * as CSS from 'csstype';
|
---|
125 |
|
---|
126 | const style: CSS.Properties = {
|
---|
127 | width: '10px',
|
---|
128 | margin: '1em',
|
---|
129 | };
|
---|
130 | ```
|
---|
131 |
|
---|
132 | In 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.
|
---|
133 |
|
---|
134 | ```ts
|
---|
135 | import type * as CSS from 'csstype';
|
---|
136 |
|
---|
137 | const style: CSS.PropertiesFallback = {
|
---|
138 | display: ['-webkit-flex', 'flex'],
|
---|
139 | color: 'white',
|
---|
140 | };
|
---|
141 | ```
|
---|
142 |
|
---|
143 | There's even string literals for pseudo selectors and elements.
|
---|
144 |
|
---|
145 | ```ts
|
---|
146 | import type * as CSS from 'csstype';
|
---|
147 |
|
---|
148 | const pseudos: { [P in CSS.SimplePseudos]?: CSS.Properties } = {
|
---|
149 | ':hover': {
|
---|
150 | display: 'flex',
|
---|
151 | },
|
---|
152 | };
|
---|
153 | ```
|
---|
154 |
|
---|
155 | Hyphen 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`.
|
---|
156 |
|
---|
157 | ```ts
|
---|
158 | import type * as CSS from 'csstype';
|
---|
159 |
|
---|
160 | interface Style extends CSS.Properties, CSS.PropertiesHyphen {}
|
---|
161 |
|
---|
162 | const style: Style = {
|
---|
163 | 'flex-grow': 1,
|
---|
164 | 'flex-shrink': 0,
|
---|
165 | 'font-weight': 'normal',
|
---|
166 | backgroundColor: 'white',
|
---|
167 | };
|
---|
168 | ```
|
---|
169 |
|
---|
170 | Adding type checked CSS properties to a `HTMLElement`.
|
---|
171 |
|
---|
172 | ```ts
|
---|
173 | import type * as CSS from 'csstype';
|
---|
174 |
|
---|
175 | const style: CSS.Properties = {
|
---|
176 | color: 'red',
|
---|
177 | margin: '1em',
|
---|
178 | };
|
---|
179 |
|
---|
180 | let button = document.createElement('button');
|
---|
181 |
|
---|
182 | Object.assign(button.style, style);
|
---|
183 | ```
|
---|
184 |
|
---|
185 | ## What should I do when I get type errors?
|
---|
186 |
|
---|
187 | The 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:
|
---|
188 |
|
---|
189 | _If you're using CSS Custom Properties you can step directly to step 3._
|
---|
190 |
|
---|
191 | 1. **First of all, make sure you're doing it right.** A type error could also indicate that you're not :wink:
|
---|
192 |
|
---|
193 | - 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
|
---|
194 | - 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
|
---|
195 |
|
---|
196 | 2. **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.
|
---|
197 | 3. Fix the issue locally with **TypeScript** (Flow further down):
|
---|
198 |
|
---|
199 | - The recommended way is to use **module augmentation**. Here's a few examples:
|
---|
200 |
|
---|
201 | ```ts
|
---|
202 | // My css.d.ts file
|
---|
203 | import type * as CSS from 'csstype';
|
---|
204 |
|
---|
205 | declare module 'csstype' {
|
---|
206 | interface Properties {
|
---|
207 | // Add a missing property
|
---|
208 | WebkitRocketLauncher?: string;
|
---|
209 |
|
---|
210 | // Add a CSS Custom Property
|
---|
211 | '--theme-color'?: 'black' | 'white';
|
---|
212 |
|
---|
213 | // Allow namespaced CSS Custom Properties
|
---|
214 | [index: `--theme-${string}`]: any;
|
---|
215 |
|
---|
216 | // Allow any CSS Custom Properties
|
---|
217 | [index: `--${string}`]: any;
|
---|
218 |
|
---|
219 | // ...or allow any other property
|
---|
220 | [index: string]: any;
|
---|
221 | }
|
---|
222 | }
|
---|
223 | ```
|
---|
224 |
|
---|
225 | - The alternative way is to use **type assertion**. Here's a few examples:
|
---|
226 |
|
---|
227 | ```ts
|
---|
228 | const style: CSS.Properties = {
|
---|
229 | // Add a missing property
|
---|
230 | ['WebkitRocketLauncher' as any]: 'launching',
|
---|
231 |
|
---|
232 | // Add a CSS Custom Property
|
---|
233 | ['--theme-color' as any]: 'black',
|
---|
234 | };
|
---|
235 | ```
|
---|
236 |
|
---|
237 | Fix the issue locally with **Flow**:
|
---|
238 |
|
---|
239 | - Use **type assertion**. Here's a few examples:
|
---|
240 |
|
---|
241 | ```js
|
---|
242 | const style: $Exact<CSS.Properties<*>> = {
|
---|
243 | // Add a missing property
|
---|
244 | [('WebkitRocketLauncher': any)]: 'launching',
|
---|
245 |
|
---|
246 | // Add a CSS Custom Property
|
---|
247 | [('--theme-color': any)]: 'black',
|
---|
248 | };
|
---|
249 | ```
|
---|
250 |
|
---|
251 | ## Version 3.0
|
---|
252 |
|
---|
253 | - **All property types are exposed with namespace**
|
---|
254 | TypeScript: `Property.AlignContent` (was `AlignContentProperty` before)
|
---|
255 | Flow: `Property$AlignContent`
|
---|
256 | - **All at-rules are exposed with namespace**
|
---|
257 | TypeScript: `AtRule.FontFace` (was `FontFace` before)
|
---|
258 | Flow: `AtRule$FontFace`
|
---|
259 | - **Data types are NOT exposed**
|
---|
260 | E.g. `Color` and `Box`. Because the generation of data types may suddenly be removed or renamed.
|
---|
261 | - **TypeScript hack for autocompletion**
|
---|
262 | 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`.
|
---|
263 | - **New generic for time**
|
---|
264 | Read more on the ["Generics"](#generics) section.
|
---|
265 | - **Flow types improvements**
|
---|
266 | Flow Strict enabled and exact types are used.
|
---|
267 |
|
---|
268 | ## Contributing
|
---|
269 |
|
---|
270 | **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.
|
---|
271 |
|
---|
272 | ### Commands
|
---|
273 |
|
---|
274 | - `npm run build` Generates typings and type checks them
|
---|
275 | - `npm run watch` Runs build on each save
|
---|
276 | - `npm run test` Runs the tests
|
---|
277 | - `npm run lazy` Type checks, lints and formats everything
|
---|