# frozen_string_literal: true module Ariadne # Arranges items as a grid. class GridComponent < Ariadne::Component DEFAULT_CLASSES = "grid grid-cols-1 gap-6 sm:grid-cols-2 lg:grid-cols-3" DEFAULT_LINK_COLOR_CLASSES = "text-button-text-color bg-button-bg-color focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-purple-500" # The items to show in the grid. renders_many :items, "Item" # @example Default # # <%= render(Ariadne::GridComponent.new) { "Example" } %> # # @param classes [String] <%= link_to_classes_docs %> # @param attributes [Hash] <%= link_to_attributes_docs %> def initialize(classes: "", attributes: {}) @classes = class_names( DEFAULT_CLASSES, classes ) default_attributes = { role: "list" } @attributes = default_attributes.merge(attributes) end # This component is part of `GridComponent` and should not be # used as a standalone component. class Item < Ariadne::Component DEFAULT_ITEM_CLASSES = "flex flex-col text-center rounded-lg shadow my-4 text-black-700 border-black" DEFAULT_ENTRY_CLASSES = "group flex-1 flex flex-col p-8 hover:bg-button-hover-color hover:text-gray-500 rounded-lg" renders_one :entry, lambda { |classes: "", &block| view_context.capture do render(Ariadne::BaseComponent.new(tag: :div, classes: classes)) { block&.call } end } DEFAULT_ACTION_LINK_CLASSES = "text-button-text-color relative -mr-px w-0 flex-1 inline-flex items-center justify-center py-4 text-sm font-medium border border-transparent rounded-bl-lg rounded-lg" renders_many :actions, lambda { |href:, icon:, label:, size: Ariadne::HeroiconComponent::SIZE_DEFAULT, variant: HeroiconsHelper::Icon::VARIANT_SOLID, classes: "", attributes: {}, text_classes: ""| actual_classes = class_names(DEFAULT_ACTION_LINK_CLASSES, classes) render(Ariadne::LinkComponent.new(href: href, classes: actual_classes, attributes: attributes)) do render(Ariadne::HeroiconComponent.new(icon: icon, size: size, variant: variant, text_classes: text_classes)) { label } end } attr_reader :href, :classes, :attributes def initialize(href: nil, classes: "", attributes: {}) @href = href @classes = class_names(DEFAULT_ITEM_CLASSES, classes) @attributes = attributes end def has_href? @href.present? end def call render(Ariadne::BaseComponent.new(tag: :div, classes: @classes, attributes: @attributes)) end end end end