Class: PhlexKit::Heading

Inherits:
BaseComponent show all
Defined in:
app/components/phlex_kit/typography/heading.rb

Overview

Heading, ported from ruby_ui's RubyUI::Heading. level: (1–6) picks the element AND a default size; as: overrides the element; size: (1–9) overrides the size on ruby_ui's scale (text-xs … text-5xl). Presentational; attrs pass through via mix. Tailwind → vanilla .pk-heading* (typography.css).

Constant Summary collapse

SIZES =
%w[1 2 3 4 5 6 7 8 9].freeze
LEVEL_SIZE =

ruby_ui's level → default size mapping (h1 is big, h4 smaller); 5/6 extend the scale downward — HTML has h1–h6, so all six must map.

{ "1" => "7", "2" => "6", "3" => "5", "4" => "4", "5" => "3", "6" => "2" }.freeze
AS_TAGS =

as: is dispatched dynamically (send) — whitelist so it can't reach arbitrary (including private) methods. Mirrors Separator/Marker.

%i[h1 h2 h3 h4 h5 h6 p span div].freeze

Instance Method Summary collapse

Methods inherited from BaseComponent

#on

Constructor Details

#initialize(level: 1, as: nil, size: nil, **attrs) ⇒ Heading

level: defaults to 1 so Heading.new is identical to Heading.new(level: 1) (a nil default used to render h1 with the level-6 size — self-disagreeing).



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/components/phlex_kit/typography/heading.rb', line 17

def initialize(level: 1, as: nil, size: nil, **attrs)
  # Fail loud at construction: level 7 previously exploded at render time
  # with NoMethodError (no h7 element), and 5/6 fell through the size map.
  unless LEVEL_SIZE.key?(level.to_s)
    raise ArgumentError, "Invalid heading level: #{level.inspect} — valid: 1–6"
  end
  @level = level
  @as = as&.to_sym
  if @as && !AS_TAGS.include?(@as)
    raise ArgumentError, "unknown Heading as: #{@as.inspect} (use one of #{AS_TAGS.join(", ")})"
  end
  @size = size
  @attrs = attrs
end

Instance Method Details

#view_template(&block) ⇒ Object



32
33
34
# File 'app/components/phlex_kit/typography/heading.rb', line 32

def view_template(&block)
  send(element, **mix({ class: classes }, @attrs), &block)
end