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

Class states defaults #4

Closed
danieldocki opened this issue Dec 24, 2021 · 3 comments · Fixed by #9
Closed

Class states defaults #4

danieldocki opened this issue Dec 24, 2021 · 3 comments · Fixed by #9

Comments

@danieldocki
Copy link

danieldocki commented Dec 24, 2021

Component:

defmodule MyAppWeb.Components.Button do
  use MyAppWeb, :component
  import PhxComponentHelpers

  @button_class "leading-6 rounded-full px-6 py-3 border border-orange-500 bg-orange-500 focus:outline focus:outline-2 focus:outline-orange-500/50"
  @label_class "text-medium text-white"

  def button(assigns) do
    assigns
    |> set_attributes([:label, :router], required: [:label, :router])
    |> extend_class(@button_class, attribute: :button_class)
    |> extend_class(@label_class, attribute: :label_class)
    |> render()
  end

  defp render(assigns) do
    ~H"""
    <%= live_patch [{:to, @router} | @heex_button_class] do %>
      <span {@heex_label_class}><%= @label %></span>
    <% end %>
    """
  end
end

Call component

<.button
  label="Button text"
  router="#"
  button_class="bg-indigo-500 border-indigo-100 focus:outline-indigo-500/50"
/>

Render

<a class="py-3 px-6 rounded-full leading-6 bg-indigo-500 border-indigo-100 focus:outline-indigo-500/50" data-phx-link="patch" data-phx-link-state="push" href="#">
  <span class="text-white text-medium">Button text</span>
</a>

Expected

<a class="py-3 px-6 rounded-full leading-6 bg-indigo-500 border-indigo-100 focus:outline focus:outline-2 focus:outline-indigo-500/50" data-phx-link="patch" data-phx-link-state="push" href="#">
  <span class="text-white text-medium">Button text</span>
</a>

I don't know why removes the classes focus:outline focus:outline-2

@cblavier
Copy link
Owner

cblavier commented Jan 3, 2022

Sorry for the delay, I'm just back from vacations!
This is indeed the current behaviour: extend_class will replace all classes starting with the same prefix (the prefix being everything before the first hyphen).
You're not the first one complaining about this, I will think of a smarter way to extend classes

@danieldocki
Copy link
Author

I've also been thinking about how to solve this for a few days, because this behavior happens here https://github.com/cblavier/phx_component_helpers/blob/main/lib/phx_component_helpers/css.ex#L31. I haven't come up with any solution to this problem yet.

@cblavier
Copy link
Owner

@danieldocki @omginbd let me know if 1.1.0 with new extend_class/3 is working better for you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants