Class: Flowbite::Progress

Inherits:
ViewComponent::Base
  • Object
show all
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.

Examples:

Basic usage

<%= render(Flowbite::Progress.new(progress: 45)) %>

With a custom style and size

<%= render(Flowbite::Progress.new(progress: 45, size: :lg, style: :success)) %>

With the label inside the bar

<%= render(Flowbite::Progress.new(label_position: :inside, progress: 45)) %>

With a label outside the bar

<%= render(Flowbite::Progress.new(label: "Flowbite", label_position: :outside, progress: 45)) %>

With an accessible name and no visible label

<%= render(Flowbite::Progress.new(aria: {label: "Upload progress"}, progress: 45)) %>

See Also:

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(progress:, class: nil, label: nil, label_position: :none, size: :default, style: :brand, **options) ⇒ Progress

Returns a new instance of Progress.

Parameters:

  • class (String, Array<String>) (defaults to: nil) —

    Additional CSS classes for the track element.

  • label (String) (defaults to: nil) —

    Text to show next to the percentage when label_position is :outside. Also used as the accessible name of the progress bar.

  • label_position (Symbol) (defaults to: :none) —

    Where to show the percentage (:inside, :none, :outside).

  • options (Hash) —

    Additional HTML options for the track element.

  • progress (Numeric) —

    The progress in percent. Values outside the range 0 to 100 are clamped.

  • size (Symbol) (defaults to: :default) —

    The height of the progress bar (:sm, :default, :lg). Ignored when label_position is :inside.

  • style (Symbol) (defaults to: :brand) —

    The color style of the bar (:brand, :danger, :dark, :success, :warning).



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, **options)
  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 = 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
  @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.

Returns:

  • (Array<String>) —

    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 bar_classes(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.

Returns:

  • (Array<String>) —

    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.

Returns:

  • (Array<String>) —

    The CSS classes for the bar.



117
118
119
# File 'app/components/flowbite/progress.rb', line 117

def bar_classes
  self.class.bar_classes(label_position: label_position, size: @size, style: @style)
end

#classes ⇒ Array<String>

Returns The CSS classes for the track.

Returns:

  • (Array<String>) —

    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

Returns:

  • (Boolean)


126
127
128
# File 'app/components/flowbite/progress.rb', line 126

def label_inside?
  label_position == :inside
end

#label_outside? ⇒ Boolean

Returns:

  • (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%".

Returns:

  • (String) —

    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.

Returns:

  • (Numeric) —

    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.

Returns:

  • (Numeric) —

    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.

Returns:

  • (Hash) —

    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 track_options
  aria = {label: label.presence, valuemax: 100, valuemin: 0, valuenow: rounded_progress}.compact
  {aria: aria, role: "progressbar"}.deep_merge(options)
end

#width_style ⇒ String

Returns The inline style setting the width of the bar.

Returns:

  • (String) —

    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