The personal finance app for everyone (by everyone)
Ruby
fix(design-system): DS::Alert alignment, accessibility, and hierarchy polish (#1734)
Guillem Arias Faustef50c151e21d1
diff --git a/app/components/DS/alert.html.erb b/app/components/DS/alert.html.erbindex 419a4607c9..efda082218 100644--- a/app/components/DS/alert.html.erb+++ b/app/components/DS/alert.html.erb@@ -1,15 +1,37 @@-<div class="<%= container_classes %>">- <%= helpers.icon icon_name, size: "sm", color: icon_color, class: "shrink-0 mt-0.5" %>+<%= tag.div(class: container_classes, role: aria_role, "aria-labelledby": (aria_role && title.present?) ? title_id : nil) do %>+ <% if title.present? %>+ <div class="flex items-center gap-3">+ <%= helpers.icon icon_name, size: "sm", color: icon_color, class: "shrink-0 -mt-0.5" %>+ <p id="<%= title_id %>" class="text-sm font-semibold text-primary flex-1 leading-5">+ <span class="sr-only"><%= variant_label %>:</span>+ <%= title %>+ </p>+ </div>- <div class="flex-1 text-sm text-primary space-y-1">- <% if title.present? %>- <p class="font-medium text-primary"><%= title %></p>+ <% if content.present? || message.present? %>+ <div class="text-sm text-primary mt-2 pl-7 space-y-1">+ <% if content.present? %>+ <%= content %>+ <% else %>+ <%= message %>+ <% end %>+ </div><% end %>-- <% if content.present? %>- <%= content %>- <% elsif message.present? %>- <%= message %>- <% end %>- </div>-</div>+ <% elsif content.present? %>+ <div class="flex items-start gap-3">+ <%= helpers.icon icon_name, size: "sm", color: icon_color, class: "shrink-0" %>+ <div class="flex-1 text-sm text-primary space-y-1">+ <span class="sr-only"><%= variant_label %>:</span>+ <%= content %>+ </div>+ </div>+ <% elsif message.present? %>+ <div class="flex items-center gap-3">+ <%= helpers.icon icon_name, size: "sm", color: icon_color, class: "shrink-0 -mt-0.5" %>+ <p class="text-sm text-primary flex-1 leading-5">+ <span class="sr-only"><%= variant_label %>:</span>+ <%= message %>+ </p>+ </div>+ <% end %>+<% end %>diff --git a/app/components/DS/alert.rb b/app/components/DS/alert.rbindex dc7025eb72..050beaf66d 100644--- a/app/components/DS/alert.rb+++ b/app/components/DS/alert.rb@@ -1,22 +1,34 @@class DS::Alert < DesignSystemComponentVARIANTS = %i[info success warning error destructive].freeze+ LIVE_MODES = %i[none status alert].freeze- def initialize(message: nil, title: nil, variant: :info)+ def initialize(message: nil, title: nil, variant: :info, live: :none)@message = message@title = title@variant = normalize_variant(variant)+ @live = normalize_live(live)endprivate- attr_reader :message, :title, :variant+ attr_reader :message, :title, :variant, :livedef normalize_variant(raw)sym = raw.respond_to?(:to_sym) ? raw.to_sym : nilVARIANTS.include?(sym) ? sym : :infoend+ def normalize_live(raw)+ sym = raw.respond_to?(:to_sym) ? raw.to_sym : nil+ case sym+ when :polite then :status+ when :assertive then :alert+ when *LIVE_MODES then sym+ else :none+ end+ end+def container_classes- base_classes = "flex items-start gap-3 p-4 rounded-lg border"+ base_classes = "p-4 rounded-lg border"variant_classes = case variantwhen :info@@ -57,4 +69,19 @@ class DS::Alert < DesignSystemComponent"info"endend++ def aria_role+ case live+ when :status then "status"+ when :alert then "alert"+ end+ end++ def variant_label+ I18n.t("ds.alert.variants.#{variant}")+ end++ def title_id+ @title_id ||= "DS-alert-title-#{SecureRandom.hex(4)}"+ endenddiff --git a/app/views/settings/providers/_binance_panel.html.erb b/app/views/settings/providers/_binance_panel.html.erbindex f93eeea477..49b8cd0d44 100644--- a/app/views/settings/providers/_binance_panel.html.erb+++ b/app/views/settings/providers/_binance_panel.html.erb@@ -3,10 +3,8 @@<%= render DS::Alert.new(variant: :warning,- message: safe_join([- content_tag(:p, t("settings.providers.binance_panel.no_withdraw_title"), class: "font-medium"),- content_tag(:p, t("settings.providers.binance_panel.no_withdraw_body"), class: "mt-1")- ])+ title: t("settings.providers.binance_panel.no_withdraw_title"),+ message: t("settings.providers.binance_panel.no_withdraw_body")) %><div class="space-y-2">diff --git a/app/views/settings/providers/_enable_banking_panel.html.erb b/app/views/settings/providers/_enable_banking_panel.html.erbindex 4085520efc..45344ea557 100644--- a/app/views/settings/providers/_enable_banking_panel.html.erb+++ b/app/views/settings/providers/_enable_banking_panel.html.erb@@ -71,10 +71,8 @@<% if has_authenticated_connections && !is_new_record %><%= render DS::Alert.new(variant: :warning,- message: safe_join([- content_tag(:p, "Configuration locked", class: "font-medium"),- content_tag(:p, "Disconnect all linked banks before changing these credentials.", class: "mt-1")- ])+ title: "Configuration locked",+ message: "Disconnect all linked banks before changing these credentials.") %><% end %>diff --git a/app/views/settings/providers/show.html.erb b/app/views/settings/providers/show.html.erbindex b78a19a962..9108eabcd2 100644--- a/app/views/settings/providers/show.html.erb+++ b/app/views/settings/providers/show.html.erb@@ -4,10 +4,8 @@<% if @encryption_error %><%= render DS::Alert.new(variant: :error,- message: safe_join([- content_tag(:h2, t("settings.providers.encryption_error.title"), class: "font-medium"),- content_tag(:p, t("settings.providers.encryption_error.message"), class: "text-sm mt-1")- ])+ title: t("settings.providers.encryption_error.title"),+ message: t("settings.providers.encryption_error.message")) %><% else %><div class="flex items-center justify-between gap-4">diff --git a/config/locales/views/components/en.yml b/config/locales/views/components/en.ymlindex 9230a79ec7..b17124e370 100644--- a/config/locales/views/components/en.yml+++ b/config/locales/views/components/en.yml@@ -1,5 +1,13 @@---en:+ ds:+ alert:+ variants:+ info: Info+ success: Success+ warning: Warning+ error: Error+ destructive: Errorprovider_sync_summary:title: Sync summarylast_sync: "Last sync: %{time_ago} ago"