Skip to content

Instantly share code, notes, and snippets.

@jaynetics
Created August 15, 2025 07:27
Show Gist options
  • Save jaynetics/bacdf6bafc5ac4e7d5fa3dd2f76c008a to your computer and use it in GitHub Desktop.
Save jaynetics/bacdf6bafc5ac4e7d5fa3dd2f76c008a to your computer and use it in GitHub Desktop.
# This is a copy of the tabs component from AA 4.0.0.beta15. It might break in the future.
# Maybe an alternative will emerge in https://github.com/activeadmin/activeadmin/pull/8762
# The class name does not matter. Put this in an initializer, or in app/lib & require manually.
class ActiveAdminTabs < ActiveAdmin::Component
builder_method :tabs
def tab(title, options = {}, &block)
title = title.to_s.titleize if title.is_a? Symbol
@menu << build_menu_item(title, options, &block)
@tabs_content << build_content_item(title, options, &block)
end
def build(attributes = {})
super(attributes)
add_class 'tabs'
@menu = nav(
class: 'tabs-nav flex flex-wrap mb-2 text-sm font-medium text-center border-b border-gray-200 dark:border-gray-700',
role: 'tablist', 'data-tabs-toggle': "#tabs-container-#{object_id}"
)
@tabs_content = div(class: 'tabs-content p-4 mb-6', id: "tabs-container-#{object_id}")
end
def build_menu_item(title, options)
fragment = options.fetch(:id, fragmentize(title))
html_options = options.fetch(:html_options, {}).merge(
class: 'block p-4 border-b-2 border-transparent rounded-t-md hover:text-gray-600 dark:hover:text-gray-300 no-underline',
'data-tabs-target': "##{fragment}", role: 'tab', 'aria-controls': fragment, href: "#"
)
a html_options do
title
end
end
def build_content_item(title, options, &block)
options = options.reverse_merge(id: fragmentize(title), class: 'hidden', role: 'tabpanel', 'aria-labelledby': "#{title}-tab")
div(options, &block)
end
private
def fragmentize(string)
"tabs-#{string.parameterize}-#{object_id}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment