lookbook icon indicating copy to clipboard operation
lookbook copied to clipboard

Dynamic param values based on component properties

Open strzibny opened this issue 2 years ago • 3 comments

Lookbook sounds like a great project!

I am currently evaluating if we can move on from storybook-viewcomponent and one thing we use a lot is passing param values as a Ruby constant, e.g.:

story :button do
   constructor(
      ..
      variant: select(ButtonComponent::VARIANTS, :secondary),
      ..
   )
end

The same has to be done with copying & pasting in Lookbook, e.g.:

# @param variant [Symbol] select [primary, secondary]

And I was thinking if it would be possible to evaluate Ruby for the values, e.g.:

# @param variant [Symbol] select `ButtonComponent::VARIANTS`

Is this possible?

strzibny avatar Jun 30 '22 13:06 strzibny

Okay, I went ahead and tried it. It's possible. lookbook_patch

So there should be a way to write a nicer patch and include it.

strzibny avatar Jul 01 '22 09:07 strzibny

Hey @strzibny - yes the current static nature of the @params tag arguments can be a bit limiting at times unfortunately. Addressing that by some sort of string evaluation is definitely possible - this issue is about potentially using ERB in comments which would just be just a slightly different syntax to achieve basically the same end result.

Up until now I've tried to steer away from any form of evaling of Ruby code just to try to avoid any obvious security gotchas. But it's something I'm actively looking into as I realise that it would be very useful to people.

allmarkedup avatar Jul 01 '22 20:07 allmarkedup

I think ERB sounds good too.

strzibny avatar Jul 04 '22 07:07 strzibny

Hey @strzibny, I've just shipped v1.1.0 that includes support for dynamically generating options (either via a method in the preview class itself or through arbitrary code evaluation).

Full details are in the docs - I'm going to close this issue down now but let me know if you run into any issues if you get a chance to try it out :-)

allmarkedup avatar Oct 01 '22 15:10 allmarkedup