Class: NitroKit::Button

Inherits:
Component
  • Object
show all
Defined in:
app/components/nitro_kit/button.rb

Constant Summary collapse

BASE =
[
  "inline-flex items-center cursor-pointer shrink-0 justify-center rounded-md border gap-2 font-medium",
  # Disabled
  "disabled:opacity-70 disabled:pointer-events-none",
  # Focus
  "focus:outline-none focus:ring-[3px] focus:ring-offset-2 focus:ring-ring ring-offset-background",
  # Icon
  "[&_svg]:size-4 [&_svg]:pointer-events-none [&_svg]:shrink-0",
  # If icon only, make square
  "[&_svg:first-child:last-child]:-mx-2"
].freeze
VARIANTS =
{
  default: [
    "bg-background text-foreground",
    "hover:bg-zinc-50 dark:hover:bg-zinc-900"
  ],
  primary: [
    "bg-primary text-white dark:text-zinc-950 border-primary",
    "hover:bg-primary/90 dark:hover:bg-primary/90"
  ],
  destructive: [
    "bg-destructive text-white border-destructive",
    "hover:bg-destructive/90 dark:hover:bg-destructive/90",
    "disabled:text-white/80"
  ],
  ghost: [
    "bg-transparent text-foreground border-transparent",
    "hover:bg-zinc-100 dark:hover:bg-zinc-900",
    "disabled:text-muted-foreground"
  ]
}.freeze
SIZES =
{
  base: "px-4 h-9",
  sm: "px-2.5 h-7 text-sm",
  xs: "px-1.5 h-6 text-xs"
}.freeze

Instance Attribute Summary collapse

Attributes inherited from Component

#attrs

Instance Method Summary collapse

Methods inherited from Component

#data_merge, merge, #merge

Constructor Details

#initialize(href: nil, icon: nil, icon_right: nil, size: :base, type: :button, variant: :default, **attrs) ⇒ Button

Returns a new instance of Button.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/components/nitro_kit/button.rb', line 42

def initialize(
  href: nil,
  icon: nil,
  icon_right: nil,
  size: :base,
  type: :button,
  variant: :default,
  **attrs
)
  super(**attrs)

  @href = href
  @icon = icon
  @icon_right = icon_right
  @size = size
  @type = type
  @variant = variant
end

Instance Attribute Details

#hrefObject (readonly)

Returns the value of attribute href.



61
62
63
# File 'app/components/nitro_kit/button.rb', line 61

def href
  @href
end

#iconObject (readonly)

Returns the value of attribute icon.



61
62
63
# File 'app/components/nitro_kit/button.rb', line 61

def icon
  @icon
end

#icon_rightObject (readonly)

Returns the value of attribute icon_right.



61
62
63
# File 'app/components/nitro_kit/button.rb', line 61

def icon_right
  @icon_right
end

#sizeObject (readonly)

Returns the value of attribute size.



61
62
63
# File 'app/components/nitro_kit/button.rb', line 61

def size
  @size
end

#typeObject (readonly)

Returns the value of attribute type.



61
62
63
# File 'app/components/nitro_kit/button.rb', line 61

def type
  @type
end

#variantObject (readonly)

Returns the value of attribute variant.



61
62
63
# File 'app/components/nitro_kit/button.rb', line 61

def variant
  @variant
end

Instance Method Details

#view_template(&block) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/components/nitro_kit/button.rb', line 70

def view_template(&block)
  class_list = merge(
    [
      BASE,
      VARIANTS[variant],
      SIZES[size],
      attrs[:class]
    ]
  )

  if href
    a(href:, **attrs, class: class_list) do
      contents(&block)
    end
  else
    button(type:, **attrs, class: class_list) do
      contents(&block)
    end
  end
end