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

ViewComponents only supports one variant #2154

Open
sfnelson opened this issue Nov 4, 2024 · 0 comments
Open

ViewComponents only supports one variant #2154

sfnelson opened this issue Nov 4, 2024 · 0 comments

Comments

@sfnelson
Copy link

sfnelson commented Nov 4, 2024

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.

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

No branches or pull requests

1 participant