Class: Polaris::TextComponent

Inherits:
Component
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
app/components/polaris/text_component.rb

Constant Summary collapse

AS_DEFAULT =
:p
AS_OPTIONS =
i[
  h1
  h2
  h3
  h4
  h5
  h6
  p
  span
  legend
]
VARIANT_DEFAULT =
:bodyMd
VARIANT_MAPPINGS =
{
  headingXs: "Polaris-Text--headingXs",
  headingSm: "Polaris-Text--headingSm",
  headingMd: "Polaris-Text--headingMd",
  headingLg: "Polaris-Text--headingLg",
  headingXl: "Polaris-Text--headingXl",
  heading2xl: "Polaris-Text--heading2xl",
  heading3xl: "Polaris-Text--heading3xl",
  heading4xl: "Polaris-Text--heading4xl",
  bodySm: "Polaris-Text--bodySm",
  bodyMd: "Polaris-Text--bodyMd",
  bodyLg: "Polaris-Text--bodyLg"
}
VARIANT_OPTIONS =
VARIANT_MAPPINGS.keys
ALIGNMENT_MAPPINGS =
{
  start: "Polaris-Text--start",
  center: "Polaris-Text--center",
  end: "Polaris-Text--end",
  justify: "Polaris-Text--justify"
}
ALIGNMENT_OPTIONS =
ALIGNMENT_MAPPINGS.keys
FONT_WEIGHT_MAPPINGS =
{
  regular: "Polaris-Text--regular",
  medium: "Polaris-Text--medium",
  semibold: "Polaris-Text--semibold",
  bold: "Polaris-Text--bold"
}
FONT_WEIGHT_VARIANT_MAPPINGS =
{
  headingXs: :semibold,
  headingSm: :semibold,
  headingMd: :semibold,
  headingLg: :semibold,
  headingXl: :semibold,
  heading2xl: :semibold,
  heading3xl: :semibold,
  heading4xl: :bold,
  bodySm: :regular,
  bodyMd: :regular,
  bodyLg: :regular
}
FONT_WEIGHT_OPTIONS =
FONT_WEIGHT_MAPPINGS.keys
COLOR_MAPPINGS =
{
  success: "Polaris-Text--success",
  critical: "Polaris-Text--critical",
  warning: "Polaris-Text--warning",
  subdued: "Polaris-Text--subdued",
  text_inverse: "Polaris-Text__text--inverse"
}
COLOR_OPTIONS =
COLOR_MAPPINGS.keys

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(alignment: nil, as: AS_DEFAULT, break_word: false, color: nil, font_weight: nil, truncate: false, variant: VARIANT_DEFAULT, visually_hidden: false, **system_arguments) ⇒ TextComponent

Returns a new instance of TextComponent.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/components/polaris/text_component.rb', line 81

def initialize(
  alignment: nil,
  as: AS_DEFAULT,
  break_word: false,
  color: nil,
  font_weight: nil,
  truncate: false,
  variant: VARIANT_DEFAULT,
  visually_hidden: false,
  **system_arguments
)
  @system_arguments = system_arguments

  @alignment = alignment
  @color = color
  @font_weight = font_weight
  @variant = variant

  @system_arguments[:tag] = as || (visually_hidden ? :span : :p)
  @system_arguments[:classes] = class_names(
    @system_arguments[:classes],
    "Polaris-Text--root",
    VARIANT_MAPPINGS[fetch_or_fallback(VARIANT_OPTIONS, variant, VARIANT_DEFAULT)],
    alignment_class,
    color_class,
    font_weight_class,
    "Polaris-Text--block": alignment || truncate,
    "Polaris-Text--break": break_word,
    "Polaris-Text--truncate": truncate,
    "Polaris-Text--visuallyHidden": visually_hidden
  )
end

Instance Attribute Details

#alignment ⇒ Object (readonly)

Returns the value of attribute alignment.



7
8
9
# File 'app/components/polaris/text_component.rb', line 7

def alignment
  @alignment
end

#as ⇒ Object (readonly)

Returns the value of attribute as.



7
8
9
# File 'app/components/polaris/text_component.rb', line 7

def as
  @as
end

#color ⇒ Object (readonly)

Returns the value of attribute color.



7
8
9
# File 'app/components/polaris/text_component.rb', line 7

def color
  @color
end

#font_weight ⇒ Object (readonly)

Returns the value of attribute font_weight.



7
8
9
# File 'app/components/polaris/text_component.rb', line 7

def font_weight
  @font_weight
end

#variant ⇒ Object (readonly)

Returns the value of attribute variant.



7
8
9
# File 'app/components/polaris/text_component.rb', line 7

def variant
  @variant
end

Instance Method Details

#alignment_class ⇒ Object



114
115
116
117
118
# File 'app/components/polaris/text_component.rb', line 114

def alignment_class
  return nil if @alignment.nil?

  ALIGNMENT_MAPPINGS[fetch_or_fallback(ALIGNMENT_OPTIONS, @alignment, ALIGNMENT_OPTIONS.first)]
end

#color_class ⇒ Object



120
121
122
123
124
# File 'app/components/polaris/text_component.rb', line 120

def color_class
  return nil if @color.nil?

  COLOR_MAPPINGS[fetch_or_fallback(COLOR_OPTIONS, @color, COLOR_OPTIONS.first)]
end

#font_weight_class ⇒ Object



126
127
128
129
130
# File 'app/components/polaris/text_component.rb', line 126

def font_weight_class
  return FONT_WEIGHT_MAPPINGS[FONT_WEIGHT_VARIANT_MAPPINGS[@variant]] if @font_weight.nil?

  FONT_WEIGHT_MAPPINGS[fetch_or_fallback(FONT_WEIGHT_OPTIONS, @font_weight, FONT_WEIGHT_OPTIONS.first)]
end