Class: Flowbite::Progress
- Inherits:
-
ViewComponent::Base
- Object
- ViewComponent::Base
- Flowbite::Progress
- Defined in:
- app/components/flowbite/progress.rb
Overview
Renders a progress bar indicating the completion of a task.
The progress value is clamped to the range 0 to 100 and used as the width of the bar.
The track is rendered with role="progressbar" and the matching ARIA
value attributes. The label, if given, is used as its accessible name.
Pass aria: {label: "..."} to name a progress bar without a visible
label.
Constant Summary collapse
- LABEL_POSITIONS =
[:inside, :none, :outside].freeze
- SIZES =
{ sm: ["h-1.5"], default: ["h-2"], lg: ["h-2.5"] }.freeze
- INSIDE_LABEL_CLASSES =
Only vertical padding, so the bar can shrink to zero width at 0%.
[ "text-xs", "font-medium", "text-white", "text-center", "py-0.5", "leading-none", "rounded-full", "h-4", "flex", "items-center", "justify-center" ].freeze
Instance Attribute Summary collapse
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#label_position ⇒ Object
readonly
Returns the value of attribute label_position.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
-
.bar_classes(label_position: :none, size: :default, style: :brand) ⇒ Array<String>
The CSS classes for the bar indicating the progress.
-
.classes(label_position: :none, size: :default) ⇒ Array<String>
The CSS classes for the track containing the bar.
- .sizes ⇒ Object
- .styles ⇒ Object
Instance Method Summary collapse
-
#bar_classes ⇒ Array<String>
The CSS classes for the bar.
-
#classes ⇒ Array<String>
The CSS classes for the track.
-
#initialize(progress:, class: nil, label: nil, label_position: :none, size: :default, style: :brand, **options) ⇒ Progress
constructor
A new instance of Progress.
- #label_inside? ⇒ Boolean
- #label_outside? ⇒ Boolean
-
#percentage ⇒ String
The progress formatted as a percentage, ie "45%".
-
#progress ⇒ Numeric
The progress clamped to the range 0 to 100.
-
#rounded_progress ⇒ Numeric
The clamped progress rounded to one decimal place.
-
#track_options ⇒ Hash
The HTML attributes for the track element, including the progressbar role and ARIA values.
-
#width_style ⇒ String
The inline style setting the width of the bar.
Constructor Details
#initialize(progress:, class: nil, label: nil, label_position: :none, size: :default, style: :brand, **options) ⇒ Progress
Returns a new instance of Progress.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'app/components/flowbite/progress.rb', line 100 def initialize(progress:, class: nil, label: nil, label_position: :none, size: :default, style: :brand, **) unless LABEL_POSITIONS.include?(label_position) raise ArgumentError, "Unknown label_position: #{label_position}. " \ "Available label positions: #{LABEL_POSITIONS.join(", ")}" end @class = Array.wrap(binding.local_variable_get(:class)) @label = label @label_position = label_position @options = @progress = progress @size = size @style = style end |
Instance Attribute Details
#label ⇒ Object (readonly)
Returns the value of attribute label.
84 85 86 |
# File 'app/components/flowbite/progress.rb', line 84 def label @label end |
#label_position ⇒ Object (readonly)
Returns the value of attribute label_position.
84 85 86 |
# File 'app/components/flowbite/progress.rb', line 84 def label_position @label_position end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
84 85 86 |
# File 'app/components/flowbite/progress.rb', line 84 def @options end |
Class Method Details
.bar_classes(label_position: :none, size: :default, style: :brand) ⇒ Array<String>
Returns The CSS classes for the bar indicating the progress.
50 51 52 53 54 55 56 57 |
# File 'app/components/flowbite/progress.rb', line 50 def (label_position: :none, size: :default, style: :brand) style = styles.fetch(style) if label_position == :inside style.fetch(:default) + INSIDE_LABEL_CLASSES else style.fetch(:default) + sizes.fetch(size) + ["rounded-full"] end end |
.classes(label_position: :none, size: :default) ⇒ Array<String>
Returns The CSS classes for the track containing the bar.
61 62 63 64 65 |
# File 'app/components/flowbite/progress.rb', line 61 def classes(label_position: :none, size: :default) result = ["w-full", "bg-neutral-quaternary", "rounded-full"] result += sizes.fetch(size) unless label_position == :inside result end |
.sizes ⇒ Object
67 68 69 |
# File 'app/components/flowbite/progress.rb', line 67 def sizes SIZES end |
.styles ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'app/components/flowbite/progress.rb', line 71 def styles Flowbite::Styles.from_hash( { brand: {default: ["bg-brand"]}, danger: {default: ["bg-danger"]}, dark: {default: ["bg-dark"]}, success: {default: ["bg-success"]}, warning: {default: ["bg-warning"]} }.freeze ) end |
Instance Method Details
#bar_classes ⇒ Array<String>
Returns The CSS classes for the bar.
117 118 119 |
# File 'app/components/flowbite/progress.rb', line 117 def self.class.(label_position: label_position, size: @size, style: @style) end |
#classes ⇒ Array<String>
Returns The CSS classes for the track.
122 123 124 |
# File 'app/components/flowbite/progress.rb', line 122 def classes self.class.classes(label_position: label_position, size: @size) + @class end |
#label_inside? ⇒ Boolean
126 127 128 |
# File 'app/components/flowbite/progress.rb', line 126 def label_inside? label_position == :inside end |
#label_outside? ⇒ Boolean
130 131 132 |
# File 'app/components/flowbite/progress.rb', line 130 def label_outside? label_position == :outside end |
#percentage ⇒ String
Returns The progress formatted as a percentage, ie "45%".
135 136 137 |
# File 'app/components/flowbite/progress.rb', line 135 def percentage "#{rounded_progress}%" end |
#progress ⇒ Numeric
Returns The progress clamped to the range 0 to 100.
140 141 142 |
# File 'app/components/flowbite/progress.rb', line 140 def progress @progress.clamp(0, 100) end |
#rounded_progress ⇒ Numeric
Returns The clamped progress rounded to one decimal place. Whole numbers are returned as Integers, ie 45 rather than 45.0.
146 147 148 149 |
# File 'app/components/flowbite/progress.rb', line 146 def rounded_progress value = progress.to_f.round(1) (value == value.to_i) ? value.to_i : value end |
#track_options ⇒ Hash
Returns The HTML attributes for the track element, including the progressbar role and ARIA values. Merged with the given options.
153 154 155 156 |
# File 'app/components/flowbite/progress.rb', line 153 def aria = {label: label.presence, valuemax: 100, valuemin: 0, valuenow: rounded_progress}.compact {aria: aria, role: "progressbar"}.deep_merge() end |
#width_style ⇒ String
Returns The inline style setting the width of the bar.
159 160 161 |
# File 'app/components/flowbite/progress.rb', line 159 def width_style "width: #{percentage}" end |