# frozen_string_literal: true
module Ariadne
module Behaviors
module Tooltipable
def with_tooltip(text, **options)
@tooltip_id = Generate.id
html_attrs.merge!(
"data-ariadne-tooltip-target" => "activator",
"aria-describedby" => @tooltip_id,
)
prepend_action(html_attrs, tooltip_action)
# TODO: this should raise if component's tag isn't an ARIA-compatible tag
# ('a, button, input, select, textarea, [tabindex]:not([tabindex="-1"])')
Ariadne::Behaviors::Tooltip.new(id: @tooltip_id, text: text, component: self, **options)
end
def tooltip_action
"mouseover->ariadne-tooltip#showTooltip mouseout->ariadne-tooltip#hideTooltip"
end
end
class Tooltip < Ariadne::BaseComponent
option :id
option :text
option :component
option :size, default: -> { :sm }
erb_template <<~ERB
<%= render(component) { content } %>
ERB
style do
base do
[
"absolute",
"w-max",
"z-20",
"isolate",
"flex",
"flex-col",
"gap-0.5",
"p-2",
"text-center",
"rounded-lg",
"bg-zinc-950",
"dark:bg-zinc-100",
"shadow-lg",
"dark:shadow-zinc-100/50",
]
end
variants do
size do
# w-36
sm { "max-w-[min(144px,90vw)]" }
# w-52
md { "max-w-[min(208px,90vw)]" }
# w-96
lg { "max-w-[min(384px,90vw)]" }
end
end
end
style :tooltip_wrapper do
base do
"hidden"
end
end
style :arrow do
base do
[
"absolute",
"z-10",
"w-2",
"h-2",
"rounded-tl-sm",
"rotate-45",
"bg-zinc-950",
"dark:bg-zinc-50",
"shadow-lg",
"dark:shadow-zinc-50",
]
end
end
style :text do
base do
[
"text-sm",
"text-zinc-50",
"dark:text-zinc-900",
]
end
end
style :action_text do
base do
[
"ariadne-xs-md",
"text-zinc-400",
"dark:text-zinc-600",
]
end
end
end
end
end