Class: Primer::BaseComponent
- Includes:
- TestSelectorHelper
- 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/app/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> “`
## HTML attributes
System arguments include most HTML attributes. For example:
| Name | Type | Description | | :- | :- | :- | | ‘width` | `Integer` | Width. | | `height` | `Integer` | Height. | | `data` | `Hash` | Data attributes: `data: { foo: :bar }` renders `data-foo=’bar’‘. | | `aria` | `Hash` | Aria attributes: `aria: { label: “foo” }` renders `aria-label=’foo’‘. | | `title` | `String` | The `title` attribute. | | `style` | `String` | Inline styles. | | `hidden` | `Boolean` | Whether to assign the `hidden` attribute. |
Constant Summary
Constants included from TestSelectorHelper
TestSelectorHelper::TEST_SELECTOR_TAG
Constants included from Status::Dsl
Constants included from ViewHelper
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 TestSelectorHelper
Methods included from JoinStyleArgumentsHelper
Methods included from FetchOrFallbackHelper
#fetch_or_fallback, #fetch_or_fallback_boolean, #silence_deprecations?
Methods included from ClassNameHelper
Constructor Details
#initialize(tag:, classes: nil, **system_arguments) ⇒ BaseComponent
Returns a new instance of BaseComponent.
113 114 115 116 117 118 119 |
# File 'app/components/primer/base_component.rb', line 113 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
121 122 123 |
# File 'app/components/primer/base_component.rb', line 121 def call content_tag(@tag, content, @content_tag_args.merge(@result)) end |