question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature: dynamic class adding with @apply directive (similar to compose of css module)

See original GitHub issue

I’ve been thinking about this feature for a while now, but with tailwind this can’t work. But maybe with windicss?

Sometimes it is cleaner to use the @apply directive. For example, to shorten the line length or to refer the same styling in a component without duplicating the same classes. My disadvantage with @apply is, that it generates a whole new class with the styling that is usually already there. In larger projects this will end up in a larger css file than if only the pure tailwind classes were used. But what would be, if @apply didn’t generate a new class but an array of classes that the object needs and add them dynamically? Similar to the compose method of css modules

I think this is an incredible improvement!

Here is an example of what my idea might look like: This is the Code I am writing:

<div class="btn" /> 
<style>
  .btn {
    @apply w-4 h-4 bg-blue-500;
  }
</style>

and this is what the compiled code looks like:

<div class="w-4 h-4 bg-blue-500" />

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
ahkucommented, Apr 4, 2021

This is a great idea, but the example syntax would not work as .btn might be used by other styles eyes due to the implicit nature of css.

Would it be possible to borrow the css modules syntax, ie create a component-scoped helper:

<template>
  <button :class="$style.button" />
</template>

<style module>
  .button {
    @apply text-red-500;
  }
</style>

Otherwise, it might be easier to just do some string concatenation.

1reaction
mgutzcommented, Jun 6, 2021

great minds think alike 😃 Thank you for this wonderful utility!

Read more comments on GitHub >

github_iconTop Results From Across the Web

A complete guide to CSS Modules in React (Part-2) - codeburst
A quick recap: we know that when CSS is applied as modular CSS, the class names are changed at the compile-time and are...
Read more >
Functions & Directives - Tailwind CSS
Directives. Directives are custom Tailwind-specific at-rules you can use in your CSS that offer special functionality for Tailwind CSS projects.
Read more >
Dynamic classname inside ngClass in angular 2
How can I write a css class inside .scss file dynamically. For ex: I have written a "overlay" class. This class being used...
Read more >
Angular NgClass Example – How to Add Conditional CSS ...
ngClass is a directive in Angular [https://angular.io/api/common/NgClass] that adds and removes CSS classes on an HTML element.
Read more >
A deep dive into CSS Module - LogRocket Blog
Composition is a feature in CSS Module that enables us to compose selectors. Consequently, we can compose a class by inheriting styles from ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found