All Projects → frenic → Csstype

frenic / Csstype

Licence: mit
Strict TypeScript and Flow types for style based on MDN data

Programming Languages

typescript
32286 projects
flow
126 projects
types
53 projects

Labels

Projects that are alternatives of or similar to Csstype

Native Css
Convert pure CSS to React Style or javascript literal objects.
Stars: ✭ 322 (-57.63%)
Mutual labels:  style
Snazzy
Format JavaScript Standard Style as Stylish (i.e. snazzy) output
Stars: ✭ 381 (-49.87%)
Mutual labels:  style
Pastel
Terminal output styling with intuitive and clean API.
Stars: ✭ 569 (-25.13%)
Mutual labels:  style
Handycontrols
Contains some simple and commonly used WPF controls based on HandyControl
Stars: ✭ 347 (-54.34%)
Mutual labels:  style
Django Api Domains
A pragmatic styleguide for Django API Projects
Stars: ✭ 365 (-51.97%)
Mutual labels:  style
Styled widget
Simplifying widget style in Flutter.
Stars: ✭ 424 (-44.21%)
Mutual labels:  style
Uber go guide cn
Uber Go 语言编码规范中文版. The Uber Go Style Guide .
Stars: ✭ 4,277 (+462.76%)
Mutual labels:  style
Remark Lint
Markdown code style linter
Stars: ✭ 718 (-5.53%)
Mutual labels:  style
Proselint
A linter for prose.
Stars: ✭ 3,836 (+404.74%)
Mutual labels:  style
Git Style Guide
A Git Style Guide
Stars: ✭ 4,851 (+538.29%)
Mutual labels:  style
Ruby Style Guide
📘 Russian Version: A community-driven Ruby coding style guide.
Stars: ✭ 358 (-52.89%)
Mutual labels:  style
Pokemon Terminal
Pokemon terminal themes.
Stars: ✭ 3,874 (+409.74%)
Mutual labels:  style
Colorful
Terminal string styling done right, in Python 🐍 🎉
Stars: ✭ 456 (-40%)
Mutual labels:  style
Easydropdown
A lightweight library for building beautiful styleable <select> elements
Stars: ✭ 337 (-55.66%)
Mutual labels:  style
Secureuxtheme
🎨 A secure boot compatible in-memory UxTheme patcher
Stars: ✭ 586 (-22.89%)
Mutual labels:  style
Rmq
RMQ - RubyMotionQuery
Stars: ✭ 311 (-59.08%)
Mutual labels:  style
Phpinsights
🔰 Instant PHP quality checks from your console
Stars: ✭ 4,442 (+484.47%)
Mutual labels:  style
Rspec Style Guide
Best practices for writing your specs!
Stars: ✭ 735 (-3.29%)
Mutual labels:  style
Weui
A UI library by WeChat official design team, includes the most useful widgets/modules in mobile web applications.
Stars: ✭ 26,030 (+3325%)
Mutual labels:  style
Macro Components
Create flexible layout and composite UI components without the need to define arbitrary custom props
Stars: ✭ 485 (-36.18%)
Mutual labels:  style

CSSType

npm

TypeScript and Flow definitions for CSS, generated by data from MDN. It provides autocompletion and type checking for CSS properties and values.

TypeScript

import * as CSS from 'csstype';

const style: CSS.Properties = {
  colour: 'white', // Type error on property
  textAlign: 'middle', // Type error on value
};

Flow

// @flow strict
import * as CSS from 'csstype';

const style: CSS.Properties<> = {
  colour: 'white', // Type error on property
  textAlign: 'middle', // Type error on value
};

Further examples below will be in TypeScript!

Getting started

$ npm install csstype
$ # or
$ yarn add csstype

Table of content

Style types

Properties are categorized in different uses and in several technical variations to provide typings that suits as many as possible.

Default Hyphen Fallback HyphenFallback
All Properties PropertiesHyphen PropertiesFallback PropertiesHyphenFallback
Standard StandardProperties StandardPropertiesHyphen StandardPropertiesFallback StandardPropertiesHyphenFallback
Vendor VendorProperties VendorPropertiesHyphen VendorPropertiesFallback VendorPropertiesHyphenFallback
Obsolete ObsoleteProperties ObsoletePropertiesHyphen ObsoletePropertiesFallback ObsoletePropertiesHyphenFallback
Svg SvgProperties SvgPropertiesHyphen SvgPropertiesFallback SvgPropertiesHyphenFallback

Categories:

  • All - Includes Standard, Vendor, Obsolete and Svg
  • Standard - Current properties and extends subcategories StandardLonghand and StandardShorthand (e.g. StandardShorthandProperties)
  • Vendor - Vendor prefixed properties and extends subcategories VendorLonghand and VendorShorthand (e.g. VendorShorthandProperties)
  • Obsolete - Removed or deprecated properties
  • Svg - SVG-specific properties

Variations:

  • Default - JavaScript (camel) cased property names
  • Hyphen - CSS (kebab) cased property names
  • Fallback - Also accepts array of values e.g. string | string[]

At-rule types

At-rule interfaces with descriptors.

TypeScript: These will be found in the AtRule namespace, e.g. AtRule.Viewport.
Flow: These will be prefixed with AtRule$, e.g. AtRule$Viewport.

Default Hyphen Fallback HyphenFallback
@counter-style CounterStyle CounterStyleHyphen CounterStyleFallback CounterStyleHyphenFallback
@font-face FontFace FontFaceHyphen FontFaceFallback FontFaceHyphenFallback
@viewport Viewport ViewportHyphen ViewportFallback ViewportHyphenFallback

Pseudo types

String literals of pseudo classes and pseudo elements

  • Pseudos

    Extends:

    • AdvancedPseudos

      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.

    • SimplePseudos

      Plain pseudos e.g. :hover that can only be one variation.

Generics

All interfaces has two optional generic argument to define length and time: CSS.Properties<TLength = string | 0, TTime = string>

  • Length is the first generic parameter and defaults to string | 0 because 0 is the only length where the unit identifier is optional. You can specify this, e.g. string | number, for platforms and libraries that accepts any numeric value as length with a specific unit.
    const style: CSS.Properties<string | number> = {
      width: 100,
    };
    
  • 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.
    const style: CSS.Properties<string | number, number> = {
      transitionDuration: 1000,
    };
    

Usage

import * as CSS from 'csstype';

const style: CSS.Properties = {
  width: '10px',
  margin: '1em',
};

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.

import * as CSS from 'csstype';

const style: CSS.PropertiesFallback = {
  display: ['-webkit-flex', 'flex'],
  color: 'white',
};

There's even string literals for pseudo selectors and elements.

import * as CSS from 'csstype';

const pseudos: { [P in CSS.SimplePseudos]?: CSS.Properties } = {
  ':hover': {
    display: 'flex',
  },
};

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.

import * as CSS from 'csstype';

interface Style extends CSS.Properties, CSS.PropertiesHyphen {}

const style: Style = {
  'flex-grow': 1,
  'flex-shrink': 0,
  'font-weight': 'normal',
  backgroundColor: 'white',
};

Adding type checked CSS properties to a HTMLElement.

import * as CSS from 'csstype';

const style: CSS.Properties = {
  color: 'red',
  margin: '1em',
};

let button = document.createElement('button');

Object.assign(button.style, style);

What should I do when I get type errors?

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:

If you're using CSS Custom Properties you can step directly to step 3.

  1. First of all, make sure you're doing it right. A type error could also indicate that you're not 😉

    • 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
    • If you're using TypeScript, type widening could be the reason you get Type 'string' is not assignable to... errors
  2. Have a look in 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.

  3. Fix the issue locally with TypeScript (Flow further down):

    • The recommended way is to use module augmentation. Here's a few examples:

      // My css.d.ts file
      import * as CSS from 'csstype';
      
      declare module 'csstype' {
        interface Properties {
          // Add a missing property
          WebkitRocketLauncher?: string;
      
          // Add a CSS Custom Property
          '--theme-color'?: 'black' | 'white';
      
          // ...or allow any other property
          [index: string]: any;
        }
      }
      
    • The alternative way is to use type assertion. Here's a few examples:

      const style: CSS.Properties = {
        // Add a missing property
        ['WebkitRocketLauncher' as any]: 'launching',
      
        // Add a CSS Custom Property
        ['--theme-color' as any]: 'black',
      };
      

    Fix the issue locally with Flow:

    • Use type assertion. Here's a few examples:

      const style: $Exact<CSS.Properties<*>> = {
        // Add a missing property
        [('WebkitRocketLauncher': any)]: 'launching',
      
        // Add a CSS Custom Property
        [('--theme-color': any)]: 'black',
      };
      

Version 3.0

  • All property types are exposed with namespace
    TypeScript: Property.AlignContent (was AlignContentProperty before)
    Flow: Property$AlignContent
  • All at-rules are exposed with namespace
    TypeScript: AtRule.FontFace (was FontFace before)
    Flow: AtRule$FontFace
  • Data types are NOT exposed
    E.g. Color and Box. Because the generation of data types may suddenly be removed or renamed.
  • TypeScript hack for autocompletion
    Uses (string & {}) for literal string unions and (number & {}) for literal number unions (related issue). Utilize PropertyValue<T> to unpack types from e.g. (string & {}) to string.
  • New generic for time
    Read more on the "Generics" section.
  • Flow types improvements
    Flow Strict enabled and exact types are used.

Contributing

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.

Commands

  • yarn build Generates typings and type checks them
  • yarn watch Runs build on each save
  • yarn test Runs the tests
  • yarn lazy Type checks, lints and formats everything
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].