Class: DocsKit::TopbarLink

Inherits:
Data
  • Object
show all
Defined in:
lib/docs_kit/topbar_link.rb

Overview

A single external link rendered in the topbar next to the theme switcher — a repo link, a chat invite, a social profile. Sites declare these in config as plain Hashes; #topbar_links normalizes each into a TopbarLink so the Shell stays value-object-driven (like DocsKit::NavItem for the sidebar):

c.topbar_links = [
{ href: "https://github.com/me/repo", label: "GitHub",  icon: :github },
{ href: "https://discord.gg/abc",     label: "Discord", icon: :discord },
]

The Shell renders each as an icon-only ghost button; #label is the accessible name (aria-label + title). #icon is a token DocsUI::BrandMark resolves — a shipped brand mark (:github, :discord, …) or, failing that, a lucide icon name (any string DocsUI::Icon knows). nil #icon renders the label as text.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(href:, label:, icon: nil) ⇒ TopbarLink

Returns a new instance of TopbarLink.



19
20
21
# File 'lib/docs_kit/topbar_link.rb', line 19

def initialize(href:, label:, icon: nil)
  super(href:, label:, icon: icon&.to_sym)
end

Instance Attribute Details

#hrefObject (readonly)

Returns the value of attribute href

Returns:

  • (Object)

    the current value of href



18
19
20
# File 'lib/docs_kit/topbar_link.rb', line 18

def href
  @href
end

#iconObject (readonly)

Returns the value of attribute icon

Returns:

  • (Object)

    the current value of icon



18
19
20
# File 'lib/docs_kit/topbar_link.rb', line 18

def icon
  @icon
end

#labelObject (readonly)

Returns the value of attribute label

Returns:

  • (Object)

    the current value of label



18
19
20
# File 'lib/docs_kit/topbar_link.rb', line 18

def label
  @label
end

Class Method Details

.from(link) ⇒ Object

Build a TopbarLink from a Hash (symbol- OR string-keyed, so a YAML/JSON config loads cleanly) or pass an existing TopbarLink through unchanged.



25
26
27
28
29
30
# File 'lib/docs_kit/topbar_link.rb', line 25

def self.from(link)
  return link if link.is_a?(self)

  attrs = link.to_h.transform_keys(&:to_sym)
  new(href: attrs[:href], label: attrs[:label], icon: attrs[:icon])
end

Instance Method Details

#external?Boolean

Whether the href points off-site (absolute http/https). The Shell adds target=_blank + rel=noopener only for external links; a site-relative link (e.g. "/changelog") opens in place like any nav link.

Returns:

  • (Boolean)


35
36
37
# File 'lib/docs_kit/topbar_link.rb', line 35

def external?
  href.to_s.match?(%r{\Ahttps?://}i)
end