Skip to content

Commit fe085c6

Browse files
committed
fix(avatar): preserve image visibility without JS
Image was hidden by default and only revealed by the Stimulus controller's load handler, which meant clients with JS disabled never saw the avatar image. Make the image visible by default and let the controller hide it on connect when it isn't yet loaded; load/error events restore the proper image/fallback toggle.
1 parent da55705 commit fe085c6

3 files changed

Lines changed: 8 additions & 3 deletions

File tree

gem/lib/ruby_ui/avatar/avatar_controller.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ export default class extends Controller {
44
static targets = ["image", "fallback"];
55

66
connect() {
7-
if (!this.hasImageTarget) return;
7+
if (!this.hasImageTarget) {
8+
return;
9+
}
810

911
if (this.imageTarget.complete && this.imageTarget.naturalWidth > 0) {
1012
this.showImage();
1113
} else {
14+
// Image not yet loaded (or failed): hide it so the fallback shows.
15+
// Image visibility is restored by the load/error handlers.
1216
this.showFallback();
1317
}
1418
}

gem/lib/ruby_ui/avatar/avatar_image.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def default_attrs
2121
ruby_ui__avatar_target: "image",
2222
action: "load->ruby-ui--avatar#showImage error->ruby-ui--avatar#showFallback"
2323
},
24-
class: "hidden aspect-square h-full w-full",
24+
class: "aspect-square h-full w-full",
2525
alt: @alt,
2626
src: @src
2727
}

gem/test/ruby_ui/avatar_test.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def test_render_with_all_items
1616
assert_match(/data-ruby-ui--avatar-target="image"/, output)
1717
assert_match(/load->ruby-ui--avatar#showImage error->ruby-ui--avatar#showFallback/, output)
1818
assert_match(/data-ruby-ui--avatar-target="fallback"/, output)
19-
assert_match(/hidden aspect-square/, output)
19+
assert_match(/class="aspect-square h-full w-full"/, output)
20+
refute_match(/class="[^"]*\bhidden\b[^"]*aspect-square/, output)
2021
end
2122
end

0 commit comments

Comments
 (0)