Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: Allow Tailwind @apply syntax within Vue <style lang="postcss"> tags #3362

Merged
merged 1 commit into from
Oct 26, 2021

Conversation

ryan-ludwig
Copy link
Contributor

Allows writing CSS in .Vue filles like:

<template>
  <div class="my-special-snowflake">
    I'm special
  </div>
</template>
...
<style lang="postcss" scoped>
.my-special-snowflake {
	@apply tw-bg-primary-inverse md:tw-bg-secondary md:tw-w-[1234px];
}
</style>

The CSS this outputs is

<style type="text/css">
.my-special-snowflake {
  --tw-bg-opacity:1;
  background-color:rgba(var(--bg-primary-inverse), var(--tw-bg-opacity));
}

@media (min-width:45.875rem) {
  .my-special-snowflake {
    --tw-bg-opacity:1;
    background-color:rgba(var(--bg-secondary),var(--tw-bg-opacity));
    width:1234px
  }
}
</style>

https://tailwindcss.com/docs/extracting-components#extracting-component-classes-with-apply

The majority of the time, I'd recommend against using this technique. Using the utility classes and breaking your re-usable element into it's own Vue component gives us more advantages (like re-using the bundled tailwind .css file the user has already downloaded.

However, I ran into an issue in a typography task where making a reusable Vue component didn't really make sense, but regular CSS classes did.

This gives us the ability to utilize the design system that Tailwind gives us (colors, breakpoints, spacing, etc), while still writing regular CSS classes. I think this might be a good way to handle unusual one-off widths and other values, especially when you need breakpoints too.

The CSS generated like this does not get added to the main Tailwind CSS file. It goes into a <style> tag loaded only on that page.

@ryan-ludwig ryan-ludwig requested review from emuvente, mcstover, eddieferrer and BoulderBrains and removed request for emuvente and eddieferrer October 26, 2021 17:02
Copy link
Collaborator

@mcstover mcstover left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely a useful tool to couple with the primary class based application of tailwind.

Along with sizing, this should help us convert more complex nested style structures as well (for example: hover parent element and alter child element)

@ryan-ludwig
Copy link
Contributor Author

Along with sizing, this should help us convert more complex nested style structures as well (for example: hover parent element and alter child element)

Yeah for sure! Just a note, nesting like Sass doesn't work out of the gate with PostCSS. I think we'd need to add a plugin for that. But you can still write it in one line

.my-special-snowflake:hover .icicle > p { 
  @apply tw-bg-primary;
}

Copy link
Collaborator

@emuvente emuvente left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A nice tool to have in the toolbox! I agree that we should discourage devs from using this as the primary way to apply repeated styles.

Copy link
Contributor

@BoulderBrains BoulderBrains left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@mcstover
Copy link
Collaborator

Along with sizing, this should help us convert more complex nested style structures as well (for example: hover parent element and alter child element)

Yeah for sure! Just a note, nesting like Sass doesn't work out of the gate with PostCSS. I think we'd need to add a plugin for that. But you can still write it in one line

.my-special-snowflake:hover .icicle > p { 
  @apply tw-bg-primary;
}

Indeed. back to basic css syntax when needed.

@ryan-ludwig ryan-ludwig merged commit 865f3eb into master Oct 26, 2021
@ryan-ludwig ryan-ludwig deleted the postcss_tailwind branch October 26, 2021 20:08
@ryan-ludwig ryan-ludwig mentioned this pull request Dec 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants