diff --git a/app/javascript/controllers/color_badge_controller.js b/app/javascript/controllers/color_badge_controller.js new file mode 100644 index 000000000..e0c0459c2 --- /dev/null +++ b/app/javascript/controllers/color_badge_controller.js @@ -0,0 +1,18 @@ +import { Controller } from "@hotwired/stimulus"; + +// Connects to data-controller="color-badge" +// Used by the transaction merchant form to show a preview of what the avatar will look like +export default class extends Controller { + static targets = ["name", "badge"]; + + handleNameChange = (e) => { + this.nameTarget.textContent = e.currentTarget.value || "?"; + }; + + handleColorChange(e) { + const color = e.currentTarget.value; + this.badgeTarget.style.backgroundColor = `color-mix(in oklab, ${color} 10%, transparent)`; + this.badgeTarget.style.borderColor = `color-mix(in oklab, ${color} 10%, transparent)`; + this.badgeTarget.style.color = color; + } +} diff --git a/app/views/shared/_color_badge.html.erb b/app/views/shared/_color_badge.html.erb new file mode 100644 index 000000000..3a5419a2c --- /dev/null +++ b/app/views/shared/_color_badge.html.erb @@ -0,0 +1,10 @@ +<%# locals: (name: nil, color: "#000") %> + +<% background_color = "color-mix(in oklab, #{color} 10%, transparent)" %> +<% border_color = "color-mix(in oklab, #{color} 10%, transparent)" %> + + + <%= name || "?" %> + diff --git a/app/views/tags/_badge.html.erb b/app/views/tags/_badge.html.erb index 5db5451fc..2fcc5f692 100644 --- a/app/views/tags/_badge.html.erb +++ b/app/views/tags/_badge.html.erb @@ -1,10 +1,10 @@ <%# locals: (tag:) %> <% tag ||= null_category %> - - + <%= tag.name %> diff --git a/app/views/tags/_form.html.erb b/app/views/tags/_form.html.erb index 2c42c8bef..625f19679 100644 --- a/app/views/tags/_form.html.erb +++ b/app/views/tags/_form.html.erb @@ -1,19 +1,19 @@ -
+
<%= styled_form_with model: tag, class: "space-y-4", data: { turbo_frame: :_top } do |f| %>
- <%= render partial: "shared/color_avatar", locals: { name: tag.name, color: tag.color } %> + <%= render partial: "shared/color_badge", locals: { name: tag.name, color: tag.color } %>
<% Tag::COLORS.each do |color| %> <% end %>
- <%= f.text_field :name, placeholder: t(".placeholder"), autofocus: true, required: true, data: { color_avatar_target: "name" } %> + <%= f.text_field :name, placeholder: t(".placeholder"), autofocus: true, required: true, data: { action: "input->color-badge#handleNameChange" } %>
diff --git a/app/views/tags/_tag.html.erb b/app/views/tags/_tag.html.erb index 2dd897706..6c1a0a560 100644 --- a/app/views/tags/_tag.html.erb +++ b/app/views/tags/_tag.html.erb @@ -2,9 +2,8 @@
- <%= render partial: "shared/color_avatar", locals: { name: tag.name, color: tag.color } %>

- <%= tag.name %> + <%= render partial: "tags/badge", locals: { tag: tag } %>