Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow recursively nested tokens in attributes #807

Merged
merged 1 commit into from
Oct 4, 2024

Conversation

willcosgrove
Copy link
Contributor

If you have a component that you want to take in an attribute of a token list, like class, and you want your interface to be polymorphic like an element method's attributes would be, you currently need to type check the provided attribute and handle it manually.

class Card < Phlex::HTML
  def initialize(class:)
    @class = grab(class:)
  end

  def view_template
    div(class: ["component-classes", @class])
  end
end

Before this PR, the above example will break if the person using the Card component tries to supply an array of classes, because Array is not a valid token type for __nested_tokens__.

render Card.new(class: "fine") # => <div class="component-classes fine"></div>
render Card.new(class: ["not-fine"]) # => 💥 

Now if an array is passed, it will also have __nested_tokens__ called on it.

render Card.new(class: ["now-fine"]) # => <div class="component-classes now-fine"></div>

@willcosgrove willcosgrove merged commit 86e565d into main Oct 4, 2024
5 checks passed
@willcosgrove willcosgrove deleted the recursive-nested-tokens branch October 4, 2024 02:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants