JavaScript Emotion

Open-source JavaScript projects categorized as Emotion

Top 8 JavaScript Emotion Projects

  • emotion

    👩‍🎤 CSS-in-JS library designed for high performance style composition

    Project mention: Why is does modern HTML/CSS seem so complex and convoluted? (details in comment) | reddit.com/r/webdev | 2023-05-23
  • rebass

    :atom_symbol: React primitive UI components built with styled-system.

    Project mention: Comparing React Component Libraries | dev.to | 2022-09-16

    If you are a fan of Styled System, you are sure to love this library, as it was built on top of Styled System. I consider Rebass to be the most unopinionated library on the list, as unlike Material Ui, Semantic UI, and Bootstrap, its components do not come with a default theme, giving you the freedom to add your preferred theme to your application. Its styles are also much easier to override. Rebass is minimalist and was designed with style extension in mind.

  • Appwrite

    Appwrite - The Open Source Firebase alternative introduces iOS support . Appwrite is an open source backend server that helps you build native iOS applications much faster with realtime APIs for authentication, databases, files storage, cloud functions and much more!

  • styled-system

    ⬢ Style props for rapid UI development

    Project mention: Why Chakra? | reddit.com/r/reactjs | 2023-02-22

    But yeah, Tailwind fanboys do hate it, because Chakra needs some initial time to research and understand how it works under the hood. And then it gets super easy and intuitive to use. It's an implementation of a general concept abstraction called styled-system, I advise anyone to start their learning about Chakra from there: https://styled-system.com/

  • goober

    🥜 goober, a less than 1KB 🎉 css-in-js alternative with a familiar API

  • vue-emotion

    Seamlessly use emotion (CSS-in-JS) with Vue.js

    Project mention: The Skinny on CSS in Vue Single File Components | dev.to | 2022-12-13

    Notice that in development mode those styles will go into a in the and Vite's latest version will even add a data-vite-dev-id attribute with the url and generated hash of the CSS file it extracts from the SFC. Of course when building your app for production, your bundler of choice will put this code into a separate .css file.

    IDs in Vue.js generated style tags

    So it's that easy to create scoped styles in Vue.js. Fun fact the scoped attribute on the style tag actually comes from a W3C draft for implementing native scoping for CSS, which was unfortunately abandoned.

    Working around child selectors

    Now there is one small problem with this method of scoping CSS.

    Let's say you have an API that delivers raw HTML text, that content editors add using the WYSIWYG editor of some CMS.

    Now inserting raw HTML to Vue templates is trivial with the v-html directive, but you also want to style these tags independently, so you create a component to encapsulate their CSS.

    This would all look like this:

    <template>
        
    class="rich-text" v-html="props.text" /> template> <script setup> const props = defineProps({ text: String }) script> <style lang="scss" scoped> .rich-text { p { margin: 0 0 1rem 0; } ol { list-style-type: lower-alpha; } ul { list-style-type: circle; } // some more styles for all possible tags... } style>
    Enter fullscreen mode Exit fullscreen mode

    Now your component will render the following CSS.

    .rich-text p[data-v-60170f23] {
      margin: 0 0 1rem 0;
    }
    .rich-text ol[data-v-60170f23] {
      list-style-type: lower-alpha;
    }
    .rich-text ul[data-v-60170f23] {
      list-style-type: circle;
    }
    
    Enter fullscreen mode Exit fullscreen mode

    But the tags inserted by v-html won't receive the data-v- attributes as the other tags in your component.

    You can change the CSS output so that the scoping will also work for nested elements.

    <style lang="scss" scoped>
    .rich-text {
        :deep(p) {
            margin: 0 0 1rem 0;
        }
    
        :deep(ol) {
            list-style-type: lower-alpha;
        }
    
        :deep(ul) {
            list-style-type: circle;
        }
    
        // some more styles for all possible tags...
    }
    style>
    
    Enter fullscreen mode Exit fullscreen mode

    Now the styles generated will look like this:

    .rich-text[data-v-60170f23] p {
      margin: 0 0 1rem 0;
    }
    .rich-text[data-v-60170f23] ol {
      list-style-type: lower-alpha;
    }
    .rich-text[data-v-60170f23] ul {
      list-style-type: circle;
    }
    
    Enter fullscreen mode Exit fullscreen mode

    The data-v- goes in front of the parent selector so child selectors will get applied and scoping will be maintained.

    Syntax variations

    If you've googled this problem before you may have found some alternate syntaxes to do the same thing, so let's clarify things a bit.

    You've may seen this:

    .alert ::v-deep h1 {
    
    }
    
    Enter fullscreen mode Exit fullscreen mode

    Or this

    .alert /deep/ h1 {
    
    }
    
    Enter fullscreen mode Exit fullscreen mode

    Or even this

    .alert >>> h1 {
    
    }
    
    Enter fullscreen mode Exit fullscreen mode

    In Vue 3 and Vue 2.7 all of these have been deprecated, so while your code may work, the compiler will show a warning and they will stop working in some future release. So it's best to use :deep(), unless you are on Vue >2.7.

    Also note that if you are using SCSS, the >>> and /deep/ syntax will throw an error in Vue 3, while the ::v-deep will still work but with a warning.

    Bottom line is, just use :deep() on all newer versions of Vue.

    New CSS features in Vue 3

    With Vue 3 we've got two new combinators next to :deep().

    Scoped styles for slots

    First we have :slotted() which lets you target any HTML that's inserted to one of the slots of your component.

    Let's say you want to add another slot in the component and you'd want whatever element that goes in that slot to have a specific styling defined by it.

    <template>
        
    class="alert"> name="header" /> />
    template> <style scoped> .alert { --base-gutter: 0.4rem; padding: calc(var(--base-gutter) * 2); border: 1px solid #ffea2a; background-color: #fff48d; border-radius: var(--base-gutter); } h1 { font-size: 1.8rem; border-bottom: 1px solid rgba(0, 0, 0, 0.2); margin-bottom: calc(var(--base-gutter) * 2); margin-top: 0; padding-bottom: calc(var(--base-gutter) * 2) } style>
    Enter fullscreen mode Exit fullscreen mode
    
        <template #header>
          

    Hi there! template> This is some warning for the user from the system.
    Enter fullscreen mode Exit fullscreen mode

    This of course won't work. If you look at what CSS was generated, you'll find this selector.

    h1[data-v-3f4a8ec2] {
       // our h1 styles
    }
    
    Enter fullscreen mode Exit fullscreen mode

    This would work if we would have the

    tag in our component and only its text content coming in via a props, but for whatever reason we don't want to do that.

    To make this work we can change our component's CSS like so:

    :slotted(h1) {
        font-size: 1.8rem;
        border-bottom: 1px solid rgba(0, 0, 0, 0.2);
        margin-bottom: calc(var(--base-gutter) * 2);
        margin-top: 0;
        padding-bottom: calc(var(--base-gutter) * 2)
    }
    
    Enter fullscreen mode Exit fullscreen mode

    Now it works! And what's even cooler is, if that you add another style block to the ...

    h1 {
        color: red
    }
    
    Enter fullscreen mode Exit fullscreen mode

    ...and a this line of code to the template.

    I'm hard coded in the component
    Enter fullscreen mode Exit fullscreen mode

    You'll see that the

    we pass through the #header slot doesn't get red, while it's rules won't affect the "hard-coded"

    .

    If you are wondering how Vue.js does this, it's very easy. The difference in the rendered output is literally one character.

    h1[data-v-3f4a8ec2] {
      // styles for the tag inside the component
    }
    
    Enter fullscreen mode Exit fullscreen mode
    h1[data-v-3f4a8ec2-s] {
      // styles for the tag coming from the slot
    }
    
    Enter fullscreen mode Exit fullscreen mode

    The generated :scoped() selector gets an extra -s appended to the file name hash that's used to scope elements inside the components.

    Global styles

    The :global() combinator provides an escape hatch from the scoped styles. A good use case for this would be if you have some page identifier class on the main tag, which is normally out of scope of your Vue application, but you want to keep your styles that hook into these classes inside your page components.

    <style scoped>
    .App {
      color: #000;
    }
    
    :global(body.home-page) {
      background-color: antiquewhite;
    }
    style>
    
    Enter fullscreen mode Exit fullscreen mode

    Also note that this could be achieved by adding two tags in your SFC, one with the scoped attribute and one without it.

    <style>
    body.home-page {
      background-color: antiquewhite;
    }
    style>
    
    <style scoped>
    .App {
      color: #000;
    }
    style>
    
    Enter fullscreen mode Exit fullscreen mode

    JS-in-CSS the Vue way

    This last one is the most exciting and solves a lot of the use cases for which you would have had to turn to CSS-in-JS libraries like vue-emotion in the past.

    You can use the v-bind directive as a CSS value and extrapolate any JS value inside the tag of the SFC just as you would in your .

    This is great for every use case where you want your styles to react directly to some user input or state change without having to write a bunch of predefined classes.

    To demonstrate how powerful this feature is, I've added a small demo with a color picker.

    If you look at the rendered code you'll see that Vue.js is generating CSS custom properties that are inserted into inline styles and then they cascade down the component.

    data-v-45e5ffe2 style="--45e5ffe2-color:rgb(230, 74, 25);">
    data-v-45e5ffe2>
    class="vc-color-wrap transparent" data-v-11bd4fe5="">
    class="current-color" data-v-11bd4fe5 style="background: rgb(230, 74, 25);">

    class="example" data-v-45e5ffe2>Click on the square to select a color!

    Enter fullscreen mode Exit fullscreen mode
    .example[data-v-45e5ffe2] {
        color: var(--45e5ffe2-color);
    }
    
    Enter fullscreen mode Exit fullscreen mode

    I hope you've found this post useful and it will help you write CSS more effectively in your next Vue.js project!

  • gatsby-emotion-tailwind-starter

    Gatsby starter template, complete with Emotion & Tailwind

  • react-ssr-apollo-boilerplate

    A boilerplate for React with SSR using GraphQL and Apollo

  • InfluxDB

    Access the most powerful time series database as a service. Ingest, store, & analyze all types of time series data in a fully-managed, purpose-built database. Keep data forever with low-cost storage and superior data compression.

  • weather_app

    Weather from Open Weather API, displayed with Pic-Animation

NOTE: The open source projects on this list are ordered by number of github stars. The number of mentions indicates repo mentiontions in the last 12 Months or since we started tracking (Dec 2020). The latest post mention was on 2023-05-23.

JavaScript Emotion related posts

Index

What are some of the best open-source Emotion projects in JavaScript? This list will help you:

Project Stars
1 emotion 16,429
2 rebass 7,899
3 styled-system 7,598
4 goober 2,897
5 vue-emotion 214
6 gatsby-emotion-tailwind-starter 49
7 react-ssr-apollo-boilerplate 22
8 weather_app 1
TestGPT | Generating meaningful tests for busy devs
Get non-trivial tests (and trivial, too!) suggested right inside your IDE, so you can code smart, create more value, and stay confident when you push.
codium.ai