Class: Ariadne::BaseComponent

Inherits:
Component
  • Object
show all
Defined in:
app/components/ariadne/base_component.rb

Overview

All Ariadne ViewComponents accept a standard set of options: classes, which match Tailwind CSS classes, and attributes, which match HTML attributes..

Under the hood, classes are [mapped](github.com/ariadne/view_components/blob/main/lib/ariadne/classify.rb) to Tailwind CSS classes, with any unconsumed attributes passed to Rails’ [‘content_tag`](api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-content_tag).

Constant Summary collapse

SELF_CLOSING_TAGS =
[:area, :base, :br, :col, :embed, :hr, :img, :input, :link, :meta, :param, :source, :track, :wbr].freeze

Constants inherited from Component

Component::INVALID_ARIA_LABEL_TAGS

Constants included from Status::Dsl

Status::Dsl::STATUSES

Constants included from ViewHelper

ViewHelper::HELPERS

Constants included from TestSelectorHelper

TestSelectorHelper::TEST_SELECTOR_TAG

Constants included from FetchOrFallbackHelper

FetchOrFallbackHelper::InvalidValueError, FetchOrFallbackHelper::TRUE_OR_FALSE

Instance Method Summary collapse

Methods included from LoggerHelper

#logger, #silence_deprecations?, #silence_warnings?

Methods included from JoinStyleArgumentsHelper

#join_style_arguments

Methods included from TestSelectorHelper

#add_test_selector

Methods included from FetchOrFallbackHelper

#check_incoming_attribute, #check_incoming_tag, #check_incoming_value, #fetch_or_raise, #fetch_or_raise_boolean

Methods included from ClassNameHelper

#class_names

Constructor Details

#initialize(tag:, classes: "", attributes: {}) ⇒ BaseComponent

## HTML attributes

System arguments include most HTML attributes. For example:

| Name | Type | Description | | :- | :- | :- | | ‘aria` | `Hash` | Aria attributes: `aria: { label: “foo” }` renders `aria-label=’foo’‘. | | `data` | `Hash` | Data attributes: `data: { foo: :bar }` renders `data-foo=’bar’‘. | | `height` | `Integer` | The height. | | `hidden` | `Boolean` | Whether to assign the `hidden` attribute. | | `style` | `String` | Inline styles. | | `title` | `String` | The `title` attribute. | | `width` | `Integer` | The width. |

## Animation

| Name | Type | Description | | :- | :- | :- | | ‘animation` | Symbol | <%= one_of(Ariadne::Classify::Utilities.mappings(:animation)) %> |

## Border

| Name | Type | Description | | :- | :- | :- | | ‘border_bottom` | Integer | Set to `0` to remove the bottom border. | | `border_left` | Integer | Set to `0` to remove the left border. | | `border_radius` | Integer | <%= one_of([0, 1, 2, 3]) %> | | `border_right` | Integer | Set to `0` to remove the right border. | | `border_top` | Integer | Set to `0` to remove the top border. | | `border` | Symbol | <%= one_of([:left, :top, :bottom, :right, :y, :x, true]) %> | | `box_shadow` | Boolean, Symbol | Box shadow. <%= one_of([true, :medium, :large, :extra_large, :none]) %> |

## Color

| Name | Type | Description | | :- | :- | :- | | ‘bg` | Symbol | Background color. <%= one_of(Ariadne::Classify::Utilities.mappings(:bg)) %> | | `border_color` | Symbol | Border color. <%= one_of(Ariadne::Classify::Utilities.mappings(:border_color)) %> | | `color` | Symbol | Text color. <%= one_of(Ariadne::Classify::Utilities.mappings(:color)) %> |

## Flex

| Name | Type | Description | | :- | :- | :- | | ‘align_items` | Symbol | <%= one_of(Ariadne::Classify::FLEX_ALIGN_ITEMS_VALUES) %> | | `align_self` | Symbol | <%= one_of(Ariadne::Classify::FLEX_ALIGN_SELF_VALUES) %> | | `direction` | Symbol | <%= one_of(Ariadne::Classify::FLEX_DIRECTION_VALUES) %> | | `flex` | Integer, Symbol | <%= one_of(Ariadne::Classify::FLEX_VALUES) %> | | `flex_grow` | Integer | To enable, set to `0`. | | `flex_shrink` | Integer | To enable, set to `0`. | | `flex_wrap` | Symbol | <%= one_of(Ariadne::Classify::FLEX_WRAP_MAPPINGS.keys) %> | | `justify_content` | Symbol | <%= one_of(Ariadne::Classify::FLEX_JUSTIFY_CONTENT_VALUES) %> |

## Grid

| Name | Type | Description | | :- | :- | :- | | ‘clearfix` | Boolean | Whether to assign the `clearfix` class. | | `col` | Integer | Number of columns. <%= one_of(Ariadne::Classify::Utilities.mappings(:col)) %> | | `container` | Symbol | Size of the container. <%= one_of(Ariadne::Classify::Utilities.mappings(:container)) %> |

## Layout

| Name | Type | Description | | :- | :- | :- | | ‘display` | Symbol | <%= one_of(Ariadne::Classify::Utilities.mappings(:display)) %> | | `w` | Symbol | <%= one_of(Ariadne::Classify::Utilities.mappings(:w)) %> | | `h` | Symbol | <%= one_of(Ariadne::Classify::Utilities.mappings(:h)) %> | | `hide` | Symbol | Hide the element at a specific breakpoint. <%= one_of(Ariadne::Classify::Utilities.mappings(:hide)) %> | | `visibility` | Symbol | Visibility. <%= one_of(Ariadne::Classify::Utilities.mappings(:visibility)) %> | | `vertical_align` | Symbol | <%= one_of(Ariadne::Classify::Utilities.mappings(:vertical_align)) %> |

## Position

| Name | Type | Description | | :- | :- | :- | | ‘bottom` | Boolean | If `false`, sets `bottom: 0`. | | `float` | Symbol | <%= one_of(Ariadne::Classify::Utilities.mappings(:float)) %> | | `left` | Boolean | If `false`, sets `left: 0`. | | `position` | Symbol | <%= one_of(Ariadne::Classify::Utilities.mappings(:position)) %> | | `right` | Boolean | If `false`, sets `right: 0`. | | `top` | Boolean | If `false`, sets `top: 0`. |

## Spacing

| Name | Type | Description | | :- | :- | :- | | ‘m` | Integer | Margin. <%= one_of(Ariadne::Classify::Utilities.mappings(:m)) %> | | `mb` | Integer | Margin bottom. <%= one_of(Ariadne::Classify::Utilities.mappings(:mb)) %> | | `ml` | Integer | Margin left. <%= one_of(Ariadne::Classify::Utilities.mappings(:ml)) %> | | `mr` | Integer | Margin right. <%= one_of(Ariadne::Classify::Utilities.mappings(:mr)) %> | | `mt` | Integer | Margin top. <%= one_of(Ariadne::Classify::Utilities.mappings(:mt)) %> | | `mx` | Integer | Horizontal margins. <%= one_of(Ariadne::Classify::Utilities.mappings(:mx)) %> | | `my` | Integer | Vertical margins. <%= one_of(Ariadne::Classify::Utilities.mappings(:my)) %> | | `p` | Integer | Padding. <%= one_of(Ariadne::Classify::Utilities.mappings(:p)) %> | | `pb` | Integer | Padding bottom. <%= one_of(Ariadne::Classify::Utilities.mappings(:pb)) %> | | `pl` | Integer | Padding left. <%= one_of(Ariadne::Classify::Utilities.mappings(:pl)) %> | | `pr` | Integer | Padding right. <%= one_of(Ariadne::Classify::Utilities.mappings(:pr)) %> | | `pt` | Integer | Padding left. <%= one_of(Ariadne::Classify::Utilities.mappings(:pt)) %> | | `px` | Integer | Horizontal padding. <%= one_of(Ariadne::Classify::Utilities.mappings(:px)) %> | | `py` | Integer | Vertical padding. <%= one_of(Ariadne::Classify::Utilities.mappings(:py)) %> |

## Typography

| Name | Type | Description | | :- | :- | :- | | ‘font_family` | Symbol | Font weight. <%= one_of() %> | | `font_size` | String, Integer, Symbol | <%= one_of([“00”, 0, 1, 2, 3, 4, 5, 6, :small, :normal]) %> | | `font_style` | Symbol | Font weight. <%= one_of() %> | | `font_weight` | Symbol | Font weight. <%= one_of([:light, :normal, :bold, :emphasized]) %> | | `text_align` | Symbol | Text alignment. <%= one_of([:left, :right, :center]) %> | | `text_transform` | Symbol | Text alignment. <%= one_of() %> | | `underline` | Boolean | Whether text should be underlined. | | `word_break` | Symbol | Whether to break words on line breaks. <%= one_of(Ariadne::Classify::Utilities.mappings(:word_break)) %> |

## Other

| Name | Type | Description | | :- | :- | :- | | classes | String | CSS class name value to be concatenated with generated Tailwind CSS classes. | | test_selector | String | Adds ‘data-test-selector=’given value’‘ in non-Production environments for testing purposes. |



133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/components/ariadne/base_component.rb', line 133

def initialize(tag:, classes: "", attributes: {})
  @tag = tag

  @attributes = validate_attributes(tag: tag, attributes: attributes)
  @attributes[:"data-ariadne-view-component"] = true

  classes = classes.present? ? @attributes.merge(classes: classes) : @attributes.fetch(:classes, "")
  @classes = Ariadne::Classify.call(classes)

  # Filter out Ariadne keys so they don't get assigned as HTML attributes
  @content_tag_args = add_test_selector(@attributes).except(*Ariadne::Classify::Utilities::UTILITIES.keys)
end

Instance Method Details

#callObject



146
147
148
149
150
151
152
153
# File 'app/components/ariadne/base_component.rb', line 146

def call
  options = @content_tag_args.merge(@classes)
  if SELF_CLOSING_TAGS.include?(@tag)
    tag(@tag, options)
  else
    (@tag, content, options)
  end
end