Class: Keystone::Ui::StatCardComponent

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/keystone/ui/stat_card_component.rb

Constant Summary collapse

CARD_CLASSES =
"rounded-xl border border-gray-200 bg-white p-6 dark:border-zinc-700 dark:bg-zinc-800"
LABEL_CLASSES =
"text-sm font-medium text-gray-500 dark:text-gray-400"
VALUE_BASE_CLASSES =
"mt-1 text-3xl font-bold"
SUFFIX_CLASSES =
"text-lg text-gray-500 dark:text-gray-400"
DISCLOSURE_CLASSES =
"hidden mt-4 space-y-1 border-t border-gray-200 pt-3 text-sm text-gray-600 dark:border-zinc-700 dark:text-gray-400"
INFO_BUTTON_CLASSES =
"shrink-0 text-gray-400 transition hover:text-accent-600 dark:hover:text-accent-400"
INFO_ICON =
<<~SVG.freeze
  <svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path stroke-linecap="round" stroke-linejoin="round" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" /></svg>
SVG
VARIANT_CLASSES =
{
  neutral: "text-gray-900 dark:text-white",
  success: "text-green-600 dark:text-green-400",
  danger: "text-red-600 dark:text-red-400",
  warning: "text-yellow-600 dark:text-yellow-400",
  info: "text-accent-600 dark:text-accent-400"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label:, value:, variant: :neutral, suffix: nil, definition: nil, calculation: nil, change: nil) ⇒ StatCardComponent

Returns a new instance of StatCardComponent.



27
28
29
30
31
32
33
34
35
# File 'app/components/keystone/ui/stat_card_component.rb', line 27

def initialize(label:, value:, variant: :neutral, suffix: nil, definition: nil, calculation: nil, change: nil)
  @label = label
  @value = value
  @variant = variant
  @suffix = suffix
  @definition = definition
  @calculation = calculation
  @change = change
end

Instance Attribute Details

#calculationObject (readonly)

Returns the value of attribute calculation.



25
26
27
# File 'app/components/keystone/ui/stat_card_component.rb', line 25

def calculation
  @calculation
end

#changeObject (readonly)

Returns the value of attribute change.



25
26
27
# File 'app/components/keystone/ui/stat_card_component.rb', line 25

def change
  @change
end

#definitionObject (readonly)

Returns the value of attribute definition.



25
26
27
# File 'app/components/keystone/ui/stat_card_component.rb', line 25

def definition
  @definition
end

#labelObject (readonly)

Returns the value of attribute label.



25
26
27
# File 'app/components/keystone/ui/stat_card_component.rb', line 25

def label
  @label
end

#suffixObject (readonly)

Returns the value of attribute suffix.



25
26
27
# File 'app/components/keystone/ui/stat_card_component.rb', line 25

def suffix
  @suffix
end

#valueObject (readonly)

Returns the value of attribute value.



25
26
27
# File 'app/components/keystone/ui/stat_card_component.rb', line 25

def value
  @value
end

Instance Method Details

#change?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'app/components/keystone/ui/stat_card_component.rb', line 61

def change?
  !@change.nil?
end

#change_classesObject



72
73
74
75
76
77
# File 'app/components/keystone/ui/stat_card_component.rb', line 72

def change_classes
  return "text-gray-500 dark:text-gray-400" if @change.zero?
  return "text-red-600 dark:text-red-400" if @change.negative?

  "text-green-600 dark:text-green-400"
end

#change_labelObject



65
66
67
68
69
70
# File 'app/components/keystone/ui/stat_card_component.rb', line 65

def change_label
  formatted = "#{format("%.1f", @change.abs)}%"
  return formatted if @change.zero?

  "#{@change.negative? ? "" : ""} #{formatted}"
end

#classesObject



37
38
39
# File 'app/components/keystone/ui/stat_card_component.rb', line 37

def classes
  CARD_CLASSES
end

#disclosure_classesObject



79
80
81
# File 'app/components/keystone/ui/stat_card_component.rb', line 79

def disclosure_classes
  DISCLOSURE_CLASSES
end

#info?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'app/components/keystone/ui/stat_card_component.rb', line 57

def info?
  !@definition.nil? || !@calculation.nil?
end

#info_button_classesObject



83
84
85
# File 'app/components/keystone/ui/stat_card_component.rb', line 83

def info_button_classes
  INFO_BUTTON_CLASSES
end

#info_iconObject



87
88
89
# File 'app/components/keystone/ui/stat_card_component.rb', line 87

def info_icon
  INFO_ICON
end

#label_classesObject



41
42
43
# File 'app/components/keystone/ui/stat_card_component.rb', line 41

def label_classes
  LABEL_CLASSES
end

#suffix?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'app/components/keystone/ui/stat_card_component.rb', line 53

def suffix?
  !@suffix.nil?
end

#suffix_classesObject



49
50
51
# File 'app/components/keystone/ui/stat_card_component.rb', line 49

def suffix_classes
  SUFFIX_CLASSES
end

#value_classesObject



45
46
47
# File 'app/components/keystone/ui/stat_card_component.rb', line 45

def value_classes
  "#{VALUE_BASE_CLASSES} #{VARIANT_CLASSES.fetch(@variant)}"
end