komposable / komponent

An opinionated way of organizing front-end code in Ruby on Rails, based on components
http://komponent.io
MIT License
426 stars 31 forks source link

block_given? from component module #45

Closed Spone closed 6 years ago

Spone commented 6 years ago

When calling block_given? from a method in a component module, it returns false, even if the component is called with a block.

If I call block_given? from the partial, it works as expected.

Is there a way to improve this?

Module:

module TestComponent
  extend ComponentHelper

  def modifiers
    @modifiers ||= ""
    @modifiers += " has-block" if block_given? # this doesn't work
    @modifiers
  end

Partial:

.test(class=modifiers)
  - if block_given?
    .test-content= yield

Rendering the component with a block:

= c "test"
  p This is a test
sigmike commented 6 years ago

You're really not calling your helper with a block, so it's normal block_given? returns false.

We could add a new method that returns what you want though. We would just have to

Spone commented 6 years ago

Yes, it's the normal behavior. Nonetheless, it would be a good idea to be able to check if the component has been called with a block.

block_given_to_component? sounds like a good name.