Class: Shadcn::Chart::Tooltip::Component

Inherits:
ApplicationViewComponent show all
Defined in:
app/components/shadcn/chart/tooltip/component.rb

Overview

ChartTooltipContent, 1:1 in markup.

Upstream's is rendered by recharts each time the pointer moves, with the hovered item's payload. Here it is rendered once, empty, and the controller writes the label, the name, the value and the indicator's colour into it — which is the same DOM, arrived at from the other side.

indicator: is upstream's dot | line | dashed, and the classes for all three come from chart.tsx.

Constant Summary collapse

INDICATORS =
{
  dot: "h-2.5 w-2.5",
  line: "w-1",
  dashed: "w-0 border-[1.5px] border-dashed bg-transparent"
}.freeze

Constants inherited from ApplicationViewComponent

ApplicationViewComponent::CONTENTS_STYLE, ApplicationViewComponent::VOID_TAGS

Instance Attribute Summary collapse

Attributes inherited from ApplicationViewComponent

#attributes

Instance Method Summary collapse

Methods inherited from ApplicationViewComponent

#css_classes, default_tag, #merged_style, #render_element, #shadcn_t, slot_name, #style_variants, #tag_name

Constructor Details

#initialize(indicator: :dot, hide_label: false, hide_indicator: false, **attributes) ⇒ Component

Returns a new instance of Component.



34
35
36
37
38
39
# File 'app/components/shadcn/chart/tooltip/component.rb', line 34

def initialize(indicator: :dot, hide_label: false, hide_indicator: false, **attributes)
  @indicator = INDICATORS.key?(indicator&.to_sym) ? indicator.to_sym : :dot
  @hide_label = hide_label
  @hide_indicator = hide_indicator
  super(**attributes)
end

Instance Attribute Details

#hide_indicatorObject (readonly)

Returns the value of attribute hide_indicator.



32
33
34
# File 'app/components/shadcn/chart/tooltip/component.rb', line 32

def hide_indicator
  @hide_indicator
end

#hide_labelObject (readonly)

Returns the value of attribute hide_label.



32
33
34
# File 'app/components/shadcn/chart/tooltip/component.rb', line 32

def hide_label
  @hide_label
end

#indicatorObject (readonly)

Returns the value of attribute indicator.



32
33
34
# File 'app/components/shadcn/chart/tooltip/component.rb', line 32

def indicator
  @indicator
end

Instance Method Details

#callObject



51
52
53
# File 'app/components/shadcn/chart/tooltip/component.rb', line 51

def call
  render_element(body: safe_join([ (heading unless nest_label?), items ].compact))
end

#element_attributes(**defaults) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'app/components/shadcn/chart/tooltip/component.rb', line 41

def element_attributes(**defaults)
  super(**{
    "data-shadcn--chart-target" => "tooltip",
    hidden: true,
    # Positioned against the container rather than by the wrapper
    # recharts renders and this port does not.
    style: merged_style("position:absolute;pointer-events:none")
  }.merge(defaults))
end