Created
December 9, 2021 19:23
-
-
Save pbstriker38/296058345e136662ee2b7878bbf7734a to your computer and use it in GitHub Desktop.
Circular progress bar ViewComponent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Circle --> | |
<div> | |
<!-- Building a Progress Ring: https://css-tricks.com/building-progress-ring-quickly/ --> | |
<svg height="<%= @diameter %>" width="<%= @diameter %>"> | |
<circle | |
class="text-gray-300" | |
stroke-width="<%= @stroke_width %>" | |
stroke="currentColor" | |
fill="transparent" | |
r="<%= normalized_radius %>" | |
cx="<%= radius %>" | |
cy="<%= radius %>" | |
/> | |
<circle | |
class="text-blue transform -rotate-180 origin-center" | |
stroke-width="<%= @stroke_width %>" | |
stroke-linecap="round" | |
stroke="currentColor" | |
fill="transparent" | |
r="<%= normalized_radius %>" | |
cx="<%= radius %>" | |
cy="<%= radius %>" | |
stroke-dasharray="<%= circumference %> <%= circumference %>" | |
stroke-dashoffset="<%= dash_offset %>" | |
/> | |
</svg> | |
<span class="text-blue-light"><%= @complete_count %></span> | |
<span class="text-blue-light"><%= @total_count %></span> | |
<span class="text-blue-light"><%= progress_percent %></span> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
class ProgressCircleComponent < ViewComponent::Base | |
def initialize(diameter:, stroke_width:, total_count:, complete_count:) | |
@diameter = diameter | |
@stroke_width = stroke_width | |
@total_count = total_count | |
@complete_count = complete_count | |
end | |
def radius | |
@diameter / 2 | |
end | |
def normalized_radius | |
radius - (@stroke_width * 2) | |
end | |
def circumference | |
normalized_radius * 2 * Math::PI | |
end | |
def progress | |
(@complete_count.fdiv(@total_count) * 100) | |
end | |
def progress_percent | |
number_to_percentage(progress.floor.clamp(0, 100), precision: 0) | |
end | |
def dash_offset | |
circumference - (progress / 100.0 * circumference) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment