npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jetbrains/kotlin-styled

v1.0.0-pre.91

Published

Kotlin wrappers for styled-components and inline-style-prefixer

Downloads

110

Readme

kotlin-styled

Kotlin wrappers for styled-components and inline-style-prefixer.

Installation

  1. npm i @jetbrains/kotlin-css @jetbrains/kotlin-css-js @jetbrains/kotlin-styled inline-style-prefixer styled-components

  2. npm run gen-idea-libs

See the Bintray page for Maven and Gradle installation instructions.

Getting Started

kotlin-styled is a great fit for applications built using kotlin-react. It gives you not only a type-safe way of authoring stylesheets, but it also takes care of adding vendor prefixes for your CSS rules, assembling stylesheets, and injecting them into the DOM.

If you are not familiar with styled-components or CSS-in-JS in general, now would be a good time to inform yourself about the concept, because kotlin-styled implements this exact idea... in Kotlin.

When using just kotlin-react you would create a regular CSS file and then you would reference CSS classes from Kotlin like this:

fun RBuilder.div() {
    div("some-class") {
        +"Hello world!"
    }
}

With kotlin-styled you never have to leave Kotlin:

fun RBuilder.div() {
    styledDiv {
        css {
            padding(vertical = 16.px)
	        
            backgroundColor = Color.green
        }    
        +"Hello world!"
    }
}

While you can mix markup and styles in one-off scenarios like the example above, most times you would probably want to have them separated to enable code reuse:

object ComponentStyles : StyleSheet("ComponentStyles", isStatic = true) {
    val wrapper by css {
        padding(vertical = 16.px)
        
        backgroundColor = Color.green
    }
}

fun RBuilder.div() {
    styledDiv {
        css {
            +ComponentStyles.wrapper
        }
        +"Hello world!"
    }
}

The latter is much easier to debug in the browser as well: when inspecting the element you'll see readable class names, e.g. class="ComponentStyles-wrapper" rather than generated ones.

CSS Properties

The DSL supports most common CSS properties and values, including animations, transforms, shadows, flexbox, and grids. SVG properties are not supported yet, contributions are welcome. However, you can use put("property", "value") syntax for any unsupported property:

fun RBuilder.div() {
    styledDiv {
        css {
            put("will-change", "transform")
        }
    }
}

CSS Selectors

The DSL allows you to use most CSS selectors. See CSSBuilder for more details. Contributions are welcome.

After creating a StyleSheet just go ahead and start using it in a component, it will be injected automatically.

object ComponentStyles : StyleSheet("ComponentStyles") {
    // Example of an ".element:hover" selector
    val element by css {        
        backgroundColor = Color.green
        
        hover {
            backgroundColor = Color.red
        }
    }
    
    // Example of a ".wrapper > *" selector
    val wrapper by css {
        children {
            // CSS properties
        }
    }
    
    // Example of a ".wrapper > div" selector
    val wrapper by css {
        children("div") {
            // CSS properties
        }
    }
    
    // Example of a ".wrapper:hover .inner" selector 
    val wrapper by css {
        // CSS properties
    }
    
    val inner by css {
        backgroundColor = Color.green
        
        // Use reflection to refer to other elements, it's longer but safer than using hard-coded class names
        ancestorHover("${ComponentStyles.name}-${ComponentStyles::wrapper.name}") {
            backgroundColor = Color.red
        }
    }        
}

fun RBuilder.div() {
    styledDiv {
        css {
            +ComponentStyles.element
        }
        +"An element"
    }

    styledDiv {
        css {
            +ComponentStyles.wrapper
        }
        styledDiv {
            css {
                +ComponentStyles.inner
            }
            +"Inner element"
        }
    }
}

Global Styles

To create a global stylesheet use the CSSBuilder class and the StyledComponents.injectGlobal() function:

val styles = CSSBuilder().apply {
    body {
        margin = 0.px
        padding = 0.px
    }
}

StyledComponents.injectGlobal(styles.toString())