Skip to main content
Question

Can we create a loop in figma's new prototyping with variables?


KARTIK_MALVIYA

I wanted to build a in app rating screen that pops up after a successful event. I want the interaction to work like this

  • If user clicks the 4th star, stars from index 1 to 4 should change its state from disabled to active (grey to yellow), same interaction for all the stars.

9 replies

Pavel_Kiselev

Actually you can do this with variables. Create a local one name “#rating=0”

In your rating component have variant prop that has values from 0…5 and bind that variable to that property.

And then use prototype mode to set rating value when clicking each star. Like when I click star number five set #rating=5


KARTIK_MALVIYA

I tried that earlier, created a component set but I had to apply same interaction for let’s say 3 star rating, I had to apply the “on tap” interaction on the third star of all the variants, similarly for each star, otherwise the interaction won’t work.

This method is high effort work (25 interactions), and using variables won’t be beneficial.
If there was a way to create a loop that loops from Index Z to 1.


Pavel_Kiselev

I’d guess that you can apply tap interaction on the instance so it is only x5, not 25, it will propagate to other variants as an override. And then if you wrap it with another component it could work


Peter_Uithoven

Would be cool if there was a variable for the index an element is at. Something like #index. For the first star it would be 1, the second 2 etc.
That all the stars can have the same interaction. You’d just need to connect the stars to the same local variable as the rating component.
Would still require a local variable per rating element.


Ramshid_K1

I made this with some color variables and a single icon. Not sure whether this is the correct method.

figma file | prototype link


Andre_Constancio

I did something similar back when Figma didn’t have variables. There’s definitely an easier way to do it now. However, if you need it asap, I’ve just published the file.:

File


Alexander_Kamenchuk

I found a way.
We need to create a pseudo-component, for example, an indicator with three colors. The transition from 1 to the second and to the third state with a delay of several milliseconds (set refresh rate to your case) then we close the loop creating a transition from state 3 to state 1.

After creating an indicator, for each of the three conditions, you can add another one that will do.


Crysker
  • New Member
  • 1 reply
  • January 30, 2025

Is it also possible to write a loop where i have a timer? So far I created this component where i use after delay to make it look like the timer is working, but I couldnt manage to make it loop with variables sadly. Anyone got any idea?

 


tank666
  • 4858 replies
  • January 30, 2025

@Crysker, check out the example of a countdown timer I shared here:

You can create a count up timer using the same principle.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings