Skip to content

ViewComponents only supports one variantΒ #2154

@sfnelson

Description

@sfnelson

Steps to reproduce

Render an action that includes multiple variants. For example: :app and :admin.

request.variant << :app if request.user_agent&.include?("Hotwire Native")
request.variant << :admin if Current.admin.present?

Set up a situation where a view component uses the second variant (:admin).

Expected behavior

The template for the admin variant should render for admin requests from web and from turbo native.

Actual behavior

The default template renders for turbo native admin requests, while the admin template correctly renders for web requests.

System configuration

Rails version:
7.1.4

Ruby version:
3.3.5

Gem version:
3.18

Notes

This issue is caused by an assumption in ViewComponents::base#render_in that there will only be one variant. This is not a correct assumption.

# For template variants (+phone, +desktop, etc.)
@__vc_variant ||= @lookup_context.variants.first

I'm planning to add a test to the work I'm doing for #2128 and resolve the issue by using Rails' logic to pick the preferred variant.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions