TypeStyle

repository·master·Indexed 25 days ago

https://github.com/typestyle/typestyle

A library for writing type-safe CSS using JavaScript or TypeScript. It provides autocomplete and requires zero configuration or custom AST transforms, making it compatible with any framework. Features include the `style` function for creating CSS classes, `createTypeStyle` for custom instances, `stylesheet` for generating class name maps, and utility functions like `extend`, `classes`, and `media` for advanced styling, nesting, and media queries.

Tokens
1.5K
Snippets
2
Records
18
Agent score
83%

What's inside typestyle

  1. Use the style function to create CSS classes

    master

    Import the style function from typestyle. Pass a style object to style() to convert it into a CSS class name. You can then apply this class name to elements in any framework (e.g., React or Angular).

    /** Import */
    import {style} from "typestyle";
    
    /** convert a style object to a CSS class name */
    const className = style({color: 'red'});
    
    /** Use the class name in a framework of choice */
    //  e.g. React
    const MyButton =
      ({onClick,children}) => <button className={className} onClick={onClick}>
            {children}
          </button>
  2. Use the default TypeStyle instance for styling

    master
    TypeStyle provides a zero-configuration default instance that can be used to create styles, keyframes, and font faces. Most common tasks can be performed by importing the exported utility functions directly from the package entry point.
  3. Generate a stylesheet of class names with `stylesheet`

    master
    The stylesheet function takes an object where keys are intended class names and values are CSSProperties. It returns an object with the same keys, but the values are the actual generated class names produced by TypeStyle.
  4. Create media queries with `media()`

    master

    The media function helps customize styles using media queries. It takes a MediaQuery object and one or more style objects, returning a nested style object that can be used within TypeStyle's style() function.

    Supported MediaQuery properties:

    • type (e.g., 'screen')
    • orientation (e.g., 'landscape')
    • minWidth, maxWidth, minHeight, maxHeight (automatically appends px if a number is provided)
    • prefersColorScheme (e.g., 'dark')
  5. Configure CSSProperties options

    master

    The CSSProperties interface extends standard CSS properties (via csstype) and includes Typestyle-specific configuration options:

    • $unique: A boolean flag. When set to true, the generated CSS selector gets its own unique location in the generated CSS, disabling deduplication. Instead of combining selectors (e.g., .classA, .classB { ... }), it generates separate rules (e.g., .classA { ... } .classB { ... }). This is useful for certain browser edge cases like placeholder styling.
  6. Merge style objects with `extend()`

    master

    The extend function merges multiple NestedCSSProperties objects into one. If multiple objects contain the same property, the last one provided wins.

    Key behaviors:

    • Falsy values (except 0) are ignored.
    • Supports recursive merging for $nest properties.
    • Supports merging for keys containing & (pseudo-selectors) or starting with @media.
  7. Define KeyFrames for animations

    master
    The KeyFrames interface is used to define CSS animation keyframes. It allows you to specify steps using strings like from, to, or percentage values (e.g., 10%). Each step maps to a set of CSSProperties or a string value.