Skip to content

Commit 53687e7

Browse files
committed
Add Progress component
1 parent 8be3d4f commit 53687e7

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

lib/ruby_ui/progress/progress.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
module RubyUI
4+
class Progress < Base
5+
def initialize(value: 0, min: 0, max: 100, **attrs)
6+
@value = value
7+
@min = min
8+
@max = max
9+
10+
super(**attrs)
11+
end
12+
13+
def view_template
14+
div(**attrs) do
15+
div(**indicator_attrs)
16+
end
17+
end
18+
19+
private
20+
21+
def default_attrs
22+
{
23+
role: "progressbar",
24+
aria_valuenow: @value,
25+
aria_valuemin: @min,
26+
aria_valuemax: @max,
27+
aria_valuetext: "#{@value}%",
28+
class: "relative h-2 overflow-hidden rounded-full bg-primary/20 [&>*]:bg-primary"
29+
}
30+
end
31+
32+
def indicator_attrs
33+
{
34+
class: "h-full w-full flex-1",
35+
style: "transform: translateX(-#{@max - @value}%)"
36+
}
37+
end
38+
end
39+
end

test/ruby_ui/progress_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
class RubyUI::ProgressTest < ComponentTest
6+
def test_render
7+
output = phlex do
8+
RubyUI::Progress(value: 50, min: 25, max: 100)
9+
end
10+
11+
assert_match(/aria-valuemin="25"/, output)
12+
assert_match(/aria-valuemax="100"/, output)
13+
assert_match(/aria-valuenow="50"/, output)
14+
assert_match(/translateX\(-50%\)/, output)
15+
end
16+
end

0 commit comments

Comments
 (0)