Dynamic class in Stylus

Asked

Viewed 58 times

0

Is it possible to retrieve a part of a class name in an element and pass as value to the CSS property using Stylus? Example

.font-word-spacing-light--1 {
  word-spacing: -0.1px !important
}
.font-letter-spacing-light--1 {
  letter-spacing: -0.1px !important
}
.align-left--16px {
  position relative
  left -16px
}

In this case, use in the element as follows

<h2 class="font-word-spacing-light--1 font-letter-spacing-light--1 align-left--16px">Elemento</h2>

In this case, I wondered if this is possible, as vuetify does something like this, I wondered if it is possible, there are for example the color classes

cor-vermelha /* cor de fundo vermelha */
cor-vermelha--text /* cor da fonte vermelha */

I read in the manual that there are Mixins, Variables and Functions I just did not understand right how to use and it makes possible what I mentioned


Let’s say something practical would look like this (I’m just exemplifying with some syntax)

<h2 class="cor-do-texto--ff00ff tamanho-do-texto--60">
  Título com cor #ff00ff e tamanho 60PX
</h2>

<style lang="stylus">
  .cor-do-texto--{%s}
    color #{%s} /* imprime color: #ff00ff */

  .tamanho-do-texto--{%s}
    text-size {%s}PX /* imprime text-size: 60px */
</style>

resolution if someone comes here with the same doubt

maxkw = 7680

width(n)
  width n !important

min-width(n)
  min-width n !important

max-width(n)
  max-width n !important

.width
  for r in 0..maxkw
    &-{r}
      width(1px * r)
  for r in 0..maxkw
    &-v-{r}
      width(1vw * r)
  for r in 0..maxkw
    &-per-{r}
      width(1% * r)

.min-width
  for r in 0..maxkw
    &-{r}
      min-width(1px * r)
  for r in 0..maxkw
    &-v-{r}
      min-width(1vw * r)
  for r in 0..maxkw
    &-per-{r}
      min-width(1% * r)

.max-width
  for r in 0..maxkw
    &-{r}
      max-width(1px * r)
  for r in 0..maxk
    &-v-{r}
      max-width(1vw * r)
  for r in 0..maxkw
    &-per-{r}
      max-width(1% * r)


maxkh = 4320

height(n)
  height n !important

min-height(n)
  min-height n !important

max-height(n)
  max-height n !important

.height
  for r in 0..maxkh
    &-{r}
      height(1px * r)
  for r in 0..maxkh
    &-v-{r}
      height(1vh * r)
  for r in 0..maxkh
    &-per-{r}
      height(1% * r)

.min-height
  for r in 0..maxkh
    &-{r}
      min-height(1px * r)
  for r in 0..maxkh
    &-v-{r}
      min-height(1vh * r)
  for r in 0..maxkh
    &-per-{r}
      min-height(1% * r)

.max-height
  for r in 0..maxkh
    &-{r}
      max-height(1px * r)
  for r in 0..maxkh
    &-v-{r}
      max-height(1vh * r)
  for r in 0..maxkh
    &-per-{r}
      max-height(1% * r)
  • It’s not possible, that’s not the purpose of Stylus. You would have to have a class for each item you want to style, as the code needs to be processed to turn CSS. In that case, I think it is more advantageous to switch the properties of the element directly with JS.

  • @Renan understood, I imagined, what a pity that functions do not work like this

2 answers

0

I don’t know if I got it right, but I think that’s what you need. If you have a variável containing a cor, you can include this color in the property, as below.

If you have this Stylus

vermelha = red

.cor-vermelha 
    background vermelha

.cor-vermelha--text
    color vermelha

CSS output

.cor-vermelha {
    background: red;
}

.cor-vermelha--text {
    color: red;
}

OBS: the detail is that with the above code you will have the text color equal to the background and you would not see the text because the two are the same color...

HTML

<h2 class="cor-vermelha cor-vermelha--text">Elemento</h2>
  • hello, in this case, it’s almost this but the color red is just example, what I wanted is not to have to precede the color red, example . red-color then Stylus puts red on var $red or let’s assume I send color-text-ff0000 then it puts ff0000 on the variable and the class obtains this variable as something . text-color ff0000

  • @flourigh you want to use a class name that does not exist, for example color-fff and want the Browser to somehow identify this -fff and put the color of the white text? That is not possible! I think the closest you’ll get is this https://answall.com/questions/330390/existe-alguma-forma-de-usar-um-custom-attributomo-valor-de-property

  • it would basically be that, it indentifies the class and takes what is after -- as if it were a variable like vuetify does when we use color-that-I-want-text

  • @flourigh so the answer at the moment I believe it is not possible! Neither sound Stylus, or SASS, because from HTML you can not change a class that is in your . CSS, except as in the options of the link I gave you, but as these preprocessors you can not change the value of the variable from the style in HTML.

  • I understood, I thought it was possible because of the manual to leave to understand the variables but I did not understand perfectly how to use them, I will read a little more, I thank

  • @flourigh has how to pass me the link from where you saw this possibility?

  • I did not see this possibility explicitly but I saw some classes in vuetify that seemed to give this possibility and searching through the manual of Stylus, I saw some things like these http://stylus-lang.com/docs/bifs.html#lookupname & http://stylus-lang.com/docs/vargs.html & http://Stylus-lang.com/Docs/hashes.html#getters that seemed to give the possibility

  • 1

    @flourigh all these examples are built in Stylus and then compiled for CSS, there’s nothing to say that you can change the class name in HTML and it changes the property value in CSS. What can happen and you configure some Webpack of life to treat these kinds of things, ai at the time of climbing the project it takes the last string of the class name and creates a variable in Stylus with that value and arrow in the element, something very complex, but that would happen in the compilation and not "natively" writing HTML/CSS. That margin(n) or opacity(n) should be done in Stylus/CSS, not HTML

  • really, I’m using Vue.js & vuex & vuetify & Stylus, from to do with Vue but wanted to see if it’s possible with Stylus, thank you

Show 4 more comments

0


I had already left that aside, until today I discovered only by accident to have understood another factor of Stylus, ointerpolation with parameters, well, soon I discovered that I can do this way

  width(n)
    width n !important

  .width
    for row in 0..999999
      &-{row}
        width(1px * row)
    &-half
      width 50%
    &-full
      width 100%

and when creating an element with the class

<div class="width-100"/>

this will be 100 pixels or any amount up to at most 999999, may be more, but I see no reason for width

http://stylus-lang.com/docs/interpolation.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.