Class: Keystone::Ui::StatCardComponent
- Inherits:
-
ViewComponent::Base
- Object
- ViewComponent::Base
- Keystone::Ui::StatCardComponent
- 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
-
#calculation ⇒ Object
readonly
Returns the value of attribute calculation.
-
#change ⇒ Object
readonly
Returns the value of attribute change.
-
#definition ⇒ Object
readonly
Returns the value of attribute definition.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#suffix ⇒ Object
readonly
Returns the value of attribute suffix.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #change? ⇒ Boolean
- #change_classes ⇒ Object
- #change_label ⇒ Object
- #classes ⇒ Object
- #disclosure_classes ⇒ Object
- #info? ⇒ Boolean
- #info_button_classes ⇒ Object
- #info_icon ⇒ Object
-
#initialize(label:, value:, variant: :neutral, suffix: nil, definition: nil, calculation: nil, change: nil) ⇒ StatCardComponent
constructor
A new instance of StatCardComponent.
- #label_classes ⇒ Object
- #suffix? ⇒ Boolean
- #suffix_classes ⇒ Object
- #value_classes ⇒ Object
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
#calculation ⇒ Object (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 |
#change ⇒ Object (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 |
#definition ⇒ Object (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 |
#label ⇒ Object (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 |
#suffix ⇒ Object (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 |
#value ⇒ Object (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
61 62 63 |
# File 'app/components/keystone/ui/stat_card_component.rb', line 61 def change? !@change.nil? end |
#change_classes ⇒ Object
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_label ⇒ Object
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 |
#classes ⇒ Object
37 38 39 |
# File 'app/components/keystone/ui/stat_card_component.rb', line 37 def classes CARD_CLASSES end |
#disclosure_classes ⇒ Object
79 80 81 |
# File 'app/components/keystone/ui/stat_card_component.rb', line 79 def disclosure_classes DISCLOSURE_CLASSES end |
#info? ⇒ Boolean
57 58 59 |
# File 'app/components/keystone/ui/stat_card_component.rb', line 57 def info? !@definition.nil? || !@calculation.nil? end |
#info_button_classes ⇒ Object
83 84 85 |
# File 'app/components/keystone/ui/stat_card_component.rb', line 83 def INFO_BUTTON_CLASSES end |
#info_icon ⇒ Object
87 88 89 |
# File 'app/components/keystone/ui/stat_card_component.rb', line 87 def info_icon INFO_ICON end |
#label_classes ⇒ Object
41 42 43 |
# File 'app/components/keystone/ui/stat_card_component.rb', line 41 def label_classes LABEL_CLASSES end |
#suffix? ⇒ Boolean
53 54 55 |
# File 'app/components/keystone/ui/stat_card_component.rb', line 53 def suffix? !@suffix.nil? end |
#suffix_classes ⇒ Object
49 50 51 |
# File 'app/components/keystone/ui/stat_card_component.rb', line 49 def suffix_classes SUFFIX_CLASSES end |
#value_classes ⇒ Object
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 |