Class: Primer::BaseComponent
- Defined in:
- app/components/primer/base_component.rb
Overview
All Primer ViewComponents accept a standard set of options called System Arguments, mimicking the [styled-system API](styled-system.com/table) [used by Primer React](primer.style/components/system-props).
Under the hood, System Arguments are [mapped](github.com/primer/view_components/blob/main/lib/primer/classify.rb) to Primer CSS classes, with any remaining options passed to Rails’ [‘content_tag`](api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-content_tag).
## Responsive values
To apply different values across responsive breakpoints, pass an array with up to five values in the order ‘[default, small, medium, large, xlarge]`. To skip a breakpoint, pass `nil`.
For example:
“‘erb <%= render Primer::HeadingComponent.new(mt: [0, nil, nil, 4, 2]) do %>
Hello world
<% end %> “‘
Renders:
“‘html <h1 class=“mt-0 mt-lg-4 mt-xl-2”>Hello world</h1> “`
Constant Summary collapse
- TEST_SELECTOR_TAG =
:test_selector
Constants included from FetchOrFallbackHelper
FetchOrFallbackHelper::InvalidValueError
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(tag:, classes: nil, **system_arguments) ⇒ BaseComponent
constructor
A new instance of BaseComponent.
Methods included from FetchOrFallbackHelper
Methods included from ClassNameHelper
Constructor Details
#initialize(tag:, classes: nil, **system_arguments) ⇒ BaseComponent
Returns a new instance of BaseComponent.
62 63 64 65 66 67 68 |
# File 'app/components/primer/base_component.rb', line 62 def initialize(tag:, classes: nil, **system_arguments) @tag = tag @result = Primer::Classify.call(**system_arguments.merge(classes: classes)) # Filter out Primer keys so they don't get assigned as HTML attributes @content_tag_args = add_test_selector(system_arguments).except(*Primer::Classify::VALID_KEYS) end |
Instance Method Details
#call ⇒ Object
70 71 72 |
# File 'app/components/primer/base_component.rb', line 70 def call content_tag(@tag, content, **@content_tag_args.merge(@result)) end |