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. Learn more →
Top 8 JavaScript Emotion Projects
-
Project mention: Why is does modern HTML/CSS seem so complex and convoluted? (details in comment) | reddit.com/r/webdev | 2023-05-23
-
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!
-
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/
-
-
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.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 modeNow 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 modeBut the tags inserted by
v-html
won't receive thedata-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 modeNow 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 modeThe
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 modeOr this
.alert /deep/ h1 { … }
Enter fullscreen mode Exit fullscreen modeOr even this
.alert >>> h1 { … }
Enter fullscreen mode Exit fullscreen modeIn 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 modeThis 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 modeThis 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 modeNow 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 modeYou'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 modeh1[data-v-3f4a8ec2-s] { // styles for the tag coming from the slot }
Enter fullscreen mode Exit fullscreen modeThe 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 maintag, 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 modeAlso 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 modeJS-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 modeI hope you've found this post useful and it will help you write CSS more effectively in your next Vue.js project!
-
-
-
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.
-
JavaScript Emotion related posts
- The Skinny on CSS in Vue Single File Components
- Dynamic styling in Vue.js
- Rapid UI development with styled-system
- React Apollo SSR Boilerplate
- Ideas for a Weather App with React
-
A note from our sponsor - CodiumAI
codium.ai | 31 May 2023
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 |