Class: DaisyUI::Tooltip

Inherits:
Base
  • Object
show all
Defined in:
lib/daisy_ui/tooltip.rb

Constant Summary collapse

PLACEMENTS =
%i[top bottom left right].freeze
DEFAULT_STIMULUS_IDENTIFIER =
"daisy-tooltip"
POPOVER_STYLE =
"position:fixed;inset:auto;margin:0;border:0;opacity:1;transform:none;overflow:visible;" \
"pointer-events:none;visibility:hidden"
ARROW_STYLE =
"position:absolute;width:.625rem;height:.25rem;background:var(--tt-bg);" \
"clip-path:polygon(0 0,100% 0,50% 100%);pointer-events:none"

Constants inherited from Base

Base::BOOLS, Base::COLOR_MODIFIERS

Instance Method Summary collapse

Methods inherited from Base

inherited, register_modifiers

Constructor Details

#initialize(*modifiers, tip: nil, as: :div, popover_id: nil, stimulus: true, **options) ⇒ Tooltip

Returns a new instance of Tooltip.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/daisy_ui/tooltip.rb', line 16

def initialize(*modifiers, tip: nil, as: :div, popover_id: nil, stimulus: true, **options)
  @popover = modifiers.include?(:popover)
  @popover_id = popover_id if @popover
  @stimulus = stimulus
  @tip = tip
  if popover?
    validate_stimulus!
    boolean_placements = PLACEMENTS.select { |candidate| options[candidate] == true }
    wire_controller(options, modifiers + boolean_placements)
  end
  super(*modifiers, as:, **options)
end

Instance Method Details

#content(**options) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/daisy_ui/tooltip.rb', line 42

def content(**options, &)
  if popover?
    @content_rendered = true
    options[:id] ||= popover_id
    options[:popover] = "manual"
    options[:role] ||= "tooltip"
    options[:style] = merge_style(options[:style], POPOVER_STYLE)
    merge_stimulus_target(options, "content")

    div(class: component_classes("tooltip-content", options:), **options) do
      span(
        aria_hidden: "true",
        data: { stimulus_target_key => "arrow" },
        style: ARROW_STYLE
      )
      yield if block_given?
    end
  else
    div(class: component_classes("tooltip-content", options:), **options, &)
  end
end

#view_templateObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/daisy_ui/tooltip.rb', line 29

def view_template(&)
  if popover?
    public_send(as, class: merge_classes(classes, "after:hidden"), **attributes) do
      yield self if block_given?
      content { plain tip } if tip && !@content_rendered
    end
  else
    opts = { class: classes, **attributes }
    opts[:data_tip] = tip if tip
    public_send(as, **opts, &)
  end
end