element_component 0.5.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. checksums.yaml +4 -4
  2. data/AGENTS.md +126 -0
  3. data/README.md +440 -25
  4. data/examples/alert_example.rb +97 -0
  5. data/examples/badge_example.rb +63 -0
  6. data/examples/breadcrumb_example.rb +36 -0
  7. data/examples/button_example.rb +86 -0
  8. data/examples/button_group_example.rb +55 -0
  9. data/examples/card_example.rb +62 -0
  10. data/examples/carousel_example.rb +95 -0
  11. data/examples/close_button_example.rb +40 -0
  12. data/examples/dropdown_example.rb +123 -0
  13. data/examples/list_group_example.rb +60 -0
  14. data/examples/modal_example.rb +120 -0
  15. data/examples/nav_example.rb +75 -0
  16. data/examples/navbar_example.rb +96 -0
  17. data/examples/pagination_example.rb +47 -0
  18. data/examples/progress_example.rb +54 -0
  19. data/examples/spinner_example.rb +49 -0
  20. data/examples/table_example.rb +41 -0
  21. data/lib/element_component/components/alert/close_button.rb +17 -0
  22. data/lib/element_component/components/alert/heading.rb +14 -0
  23. data/lib/element_component/components/alert/link.rb +15 -0
  24. data/lib/element_component/components/alert.rb +25 -0
  25. data/lib/element_component/components/badge.rb +18 -0
  26. data/lib/element_component/components/breadcrumb/item.rb +29 -0
  27. data/lib/element_component/components/breadcrumb.rb +26 -0
  28. data/lib/element_component/components/button.rb +25 -0
  29. data/lib/element_component/components/button_group.rb +18 -0
  30. data/lib/element_component/components/card/body.rb +14 -0
  31. data/lib/element_component/components/card/footer.rb +14 -0
  32. data/lib/element_component/components/card/header.rb +14 -0
  33. data/lib/element_component/components/card/image.rb +17 -0
  34. data/lib/element_component/components/card/text.rb +14 -0
  35. data/lib/element_component/components/card/title.rb +14 -0
  36. data/lib/element_component/components/card.rb +21 -0
  37. data/lib/element_component/components/carousel/caption.rb +14 -0
  38. data/lib/element_component/components/carousel/item.rb +16 -0
  39. data/lib/element_component/components/carousel.rb +70 -0
  40. data/lib/element_component/components/close_button.rb +17 -0
  41. data/lib/element_component/components/dropdown/divider.rb +20 -0
  42. data/lib/element_component/components/dropdown/header.rb +22 -0
  43. data/lib/element_component/components/dropdown/item.rb +33 -0
  44. data/lib/element_component/components/dropdown/menu.rb +15 -0
  45. data/lib/element_component/components/dropdown.rb +43 -0
  46. data/lib/element_component/components/list_group/item.rb +23 -0
  47. data/lib/element_component/components/list_group.rb +18 -0
  48. data/lib/element_component/components/modal/body.rb +14 -0
  49. data/lib/element_component/components/modal/content.rb +14 -0
  50. data/lib/element_component/components/modal/dialog.rb +21 -0
  51. data/lib/element_component/components/modal/footer.rb +14 -0
  52. data/lib/element_component/components/modal/header.rb +16 -0
  53. data/lib/element_component/components/modal/title.rb +14 -0
  54. data/lib/element_component/components/modal.rb +42 -0
  55. data/lib/element_component/components/nav/item.rb +14 -0
  56. data/lib/element_component/components/nav/link.rb +18 -0
  57. data/lib/element_component/components/nav.rb +23 -0
  58. data/lib/element_component/components/navbar/brand.rb +15 -0
  59. data/lib/element_component/components/navbar/collapse.rb +16 -0
  60. data/lib/element_component/components/navbar/nav.rb +14 -0
  61. data/lib/element_component/components/navbar/toggler.rb +22 -0
  62. data/lib/element_component/components/navbar.rb +51 -0
  63. data/lib/element_component/components/pagination/item.rb +27 -0
  64. data/lib/element_component/components/pagination.rb +33 -0
  65. data/lib/element_component/components/progress/bar.rb +24 -0
  66. data/lib/element_component/components/progress.rb +17 -0
  67. data/lib/element_component/components/spinner.rb +19 -0
  68. data/lib/element_component/components/table.rb +21 -0
  69. data/lib/element_component/components.rb +24 -0
  70. data/lib/element_component/element.rb +21 -7
  71. data/lib/element_component/version.rb +3 -1
  72. data/lib/element_component.rb +3 -1
  73. metadata +68 -1
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ElementComponent
4
+ module Components
5
+ class NavLink < Element
6
+ def initialize(href: "#", active: false, disabled: false, **attributes, &block)
7
+ super("a", &block)
8
+
9
+ add_attribute(class: "nav-link")
10
+ add_attribute(class: "active") if active
11
+ add_attribute(class: "disabled") if disabled
12
+ add_attribute(href: href)
13
+ add_attribute("aria-current": "page") if active
14
+ add_attribute(attributes) unless attributes.empty?
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "nav/item"
4
+ require_relative "nav/link"
5
+
6
+ module ElementComponent
7
+ module Components
8
+ class Nav < Element
9
+ VALID_TYPES = %i[tabs pills underline].freeze
10
+
11
+ def initialize(type: nil, fill: false, justified: false, vertical: false, **attributes, &block)
12
+ super("ul", &block)
13
+
14
+ add_attribute(class: "nav")
15
+ add_attribute(class: "nav-#{type}") if type
16
+ add_attribute(class: "nav-fill") if fill
17
+ add_attribute(class: "nav-justified") if justified
18
+ add_attribute(class: "flex-column") if vertical
19
+ add_attribute(attributes) unless attributes.empty?
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ElementComponent
4
+ module Components
5
+ class NavbarBrand < Element
6
+ def initialize(href: "#", **attributes, &block)
7
+ super("a", &block)
8
+
9
+ add_attribute(class: "navbar-brand")
10
+ add_attribute(href: href)
11
+ add_attribute(attributes) unless attributes.empty?
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ElementComponent
4
+ module Components
5
+ class NavbarCollapse < Element
6
+ def initialize(id: nil, **attributes, &block)
7
+ super("div", &block)
8
+
9
+ add_attribute(class: "collapse")
10
+ add_attribute(class: "navbar-collapse")
11
+ add_attribute(id: id) if id
12
+ add_attribute(attributes) unless attributes.empty?
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ElementComponent
4
+ module Components
5
+ class NavbarNav < Element
6
+ def initialize(**attributes, &)
7
+ super("ul", &)
8
+
9
+ add_attribute(class: "navbar-nav")
10
+ add_attribute(attributes) unless attributes.empty?
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ElementComponent
4
+ module Components
5
+ class NavbarToggler < Element
6
+ def initialize(target: nil, **attributes)
7
+ super("button", closing_tag: false)
8
+
9
+ add_attribute(class: "navbar-toggler")
10
+ add_attribute(type: "button")
11
+ add_attribute("data-bs-toggle": "collapse")
12
+ add_attribute("data-bs-target": "##{target}") if target
13
+ add_attribute("aria-controls": target) if target
14
+ add_attribute("aria-expanded": "false")
15
+ add_attribute("aria-label": "Toggle navigation")
16
+ add_attribute(attributes) unless attributes.empty?
17
+
18
+ add_content(Element.new("span", class: "navbar-toggler-icon"))
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "navbar/brand"
4
+ require_relative "navbar/toggler"
5
+ require_relative "navbar/collapse"
6
+ require_relative "navbar/nav"
7
+
8
+ module ElementComponent
9
+ module Components
10
+ class Navbar < Element
11
+ VALID_EXPAND = %i[sm md lg xl xxl].freeze
12
+ VALID_THEMES = %i[light dark].freeze
13
+ VALID_FIXED = %i[top bottom].freeze
14
+ VALID_STICKY = %i[top bottom].freeze
15
+
16
+ def initialize(expand: :lg, theme: :light, background: nil, fixed: nil, sticky: nil, container: true, **attributes,
17
+ &block)
18
+ super("nav", &block)
19
+
20
+ add_attribute(class: "navbar")
21
+ add_attribute(class: "navbar-expand-#{expand}")
22
+ add_attribute(class: "navbar-#{theme}")
23
+ add_attribute(class: "bg-#{background}") if background
24
+ add_attribute(class: "fixed-#{fixed}") if fixed
25
+ add_attribute(class: "sticky-#{sticky}") if sticky
26
+ add_attribute(attributes) unless attributes.empty?
27
+
28
+ @use_container = container
29
+ end
30
+
31
+ private
32
+
33
+ def build
34
+ @html << "<#{@element}"
35
+ @html << (mount_attributes.empty? ? ">" : " #{mount_attributes}>")
36
+
37
+ if @use_container
38
+ container_class = @use_container == true ? "container-fluid" : "container"
39
+ @html << "<div class=\"#{container_class}\">"
40
+ end
41
+
42
+ @html << mount_content
43
+
44
+ @html << "</div>" if @use_container
45
+
46
+ @html << "</#{@element}>" if @closing_tag
47
+ @html
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ElementComponent
4
+ module Components
5
+ class PageItem < Element
6
+ def initialize(active: false, disabled: false, href: "#", **attributes, &block)
7
+ super("li", &block)
8
+
9
+ add_attribute(class: "page-item")
10
+ add_attribute(class: "active") if active
11
+ add_attribute(class: "disabled") if disabled
12
+ add_attribute("aria-current": "page") if active
13
+ add_attribute(attributes) unless attributes.empty?
14
+
15
+ @page_href = href
16
+ @page_disabled = disabled
17
+ end
18
+
19
+ private
20
+
21
+ def mount_content
22
+ inner = super
23
+ "<a class=\"page-link\" href=\"#{@page_href}\"#{' tabindex="-1"' if @page_disabled}>#{inner}</a>"
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "pagination/item"
4
+
5
+ module ElementComponent
6
+ module Components
7
+ class Pagination < Element
8
+ VALID_SIZES = %i[sm lg].freeze
9
+
10
+ def initialize(size: nil, **attributes, &block)
11
+ @pagination_size = size
12
+ super("nav", &block)
13
+
14
+ add_attribute("aria-label": "Pagination")
15
+ add_attribute(attributes) unless attributes.empty?
16
+ end
17
+
18
+ private
19
+
20
+ def build
21
+ @html << "<#{@element}"
22
+ @html << (mount_attributes.empty? ? ">" : " #{mount_attributes}>")
23
+ @html << "<ul class=\"pagination"
24
+ @html << " pagination-#{@pagination_size}" if @pagination_size
25
+ @html << "\">"
26
+ @html << mount_content
27
+ @html << "</ul>"
28
+ @html << "</#{@element}>" if @closing_tag
29
+ @html
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ElementComponent
4
+ module Components
5
+ class ProgressBar < Element
6
+ VALID_VARIANTS = %i[primary secondary success danger warning info light dark].freeze
7
+
8
+ def initialize(value: 0, variant: nil, striped: false, animated: false, **attributes, &block)
9
+ super("div", &block)
10
+
11
+ add_attribute(class: "progress-bar")
12
+ add_attribute(class: "bg-#{variant}") if variant
13
+ add_attribute(class: "progress-bar-striped") if striped
14
+ add_attribute(class: "progress-bar-animated") if animated
15
+ add_attribute(role: "progressbar")
16
+ add_attribute("aria-valuenow": value.to_s)
17
+ add_attribute("aria-valuemin": "0")
18
+ add_attribute("aria-valuemax": "100")
19
+ add_attribute(style: "width: #{value}%")
20
+ add_attribute(attributes) unless attributes.empty?
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "progress/bar"
4
+
5
+ module ElementComponent
6
+ module Components
7
+ class Progress < Element
8
+ def initialize(**attributes, &)
9
+ super("div", &)
10
+
11
+ add_attribute(class: "progress")
12
+ add_attribute(role: "progressbar")
13
+ add_attribute(attributes) unless attributes.empty?
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ElementComponent
4
+ module Components
5
+ class Spinner < Element
6
+ VALID_VARIANTS = %i[primary secondary success danger warning info light dark].freeze
7
+ VALID_TYPES = %i[border grow].freeze
8
+
9
+ def initialize(type: :border, variant: nil, **attributes, &block)
10
+ super("div", &block)
11
+
12
+ add_attribute(class: "spinner-#{type}")
13
+ add_attribute(class: "text-#{variant}") if variant
14
+ add_attribute(role: "status")
15
+ add_attribute(attributes) unless attributes.empty?
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ElementComponent
4
+ module Components
5
+ class Table < Element
6
+ VALID_VARIANTS = %i[primary secondary success danger warning info light dark].freeze
7
+
8
+ def initialize(striped: false, bordered: false, hover: false, small: false, variant: nil, **attributes, &block)
9
+ super("table", &block)
10
+
11
+ add_attribute(class: "table")
12
+ add_attribute(class: "table-striped") if striped
13
+ add_attribute(class: "table-bordered") if bordered
14
+ add_attribute(class: "table-hover") if hover
15
+ add_attribute(class: "table-sm") if small
16
+ add_attribute(class: "table-#{variant}") if variant
17
+ add_attribute(attributes) unless attributes.empty?
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "components/alert"
4
+ require_relative "components/badge"
5
+ require_relative "components/breadcrumb"
6
+ require_relative "components/button"
7
+ require_relative "components/button_group"
8
+ require_relative "components/card"
9
+ require_relative "components/carousel"
10
+ require_relative "components/close_button"
11
+ require_relative "components/dropdown"
12
+ require_relative "components/list_group"
13
+ require_relative "components/modal"
14
+ require_relative "components/nav"
15
+ require_relative "components/navbar"
16
+ require_relative "components/pagination"
17
+ require_relative "components/progress"
18
+ require_relative "components/spinner"
19
+ require_relative "components/table"
20
+
21
+ module ElementComponent
22
+ module Components
23
+ end
24
+ end
@@ -1,14 +1,17 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ElementComponent
2
4
  class Element
3
5
  attr_reader :element, :attributes, :contents, :html
4
6
 
5
- def initialize(element, closing_tag: true, **attribute)
7
+ def initialize(element, closing_tag: true, **attribute, &block)
6
8
  @element = element
7
9
  @closing_tag = closing_tag
8
- @html = ""
10
+ @html = String.new
9
11
 
10
12
  add_attribute!(attribute)
11
13
  reset_contents!
14
+ instance_eval(&block) if block
12
15
  end
13
16
 
14
17
  def add_content!(content)
@@ -64,18 +67,20 @@ module ElementComponent
64
67
  @attributes = {}
65
68
  end
66
69
 
67
- def new_element(*args, **kargs) = Element.new(*args, **kargs)
70
+ def new_element(...) = Element.new(...)
68
71
 
69
72
  def render
70
- before_render if respond_to? 'before_render'
73
+ @html = String.new
74
+
75
+ before_render if respond_to? "before_render"
71
76
 
72
- if respond_to? 'around_render'
77
+ if respond_to? "around_render"
73
78
  around_render { build }
74
79
  else
75
80
  build
76
81
  end
77
82
 
78
- after_render(@html) if respond_to? 'after_render'
83
+ after_render(@html) if respond_to? "after_render"
79
84
 
80
85
  @html
81
86
  end
@@ -95,10 +100,19 @@ module ElementComponent
95
100
  end
96
101
 
97
102
  def mount_content
98
- @contents.map do |content|
103
+ @contents.dup.map do |content|
99
104
  case content
100
105
  in Element
101
106
  content.render
107
+ in Proc
108
+ result = instance_eval(&content)
109
+ if result.equal?(self)
110
+ ""
111
+ elsif result.respond_to?(:render)
112
+ result.render
113
+ else
114
+ result.to_s
115
+ end
102
116
  else
103
117
  content.to_s
104
118
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ElementComponent
2
- VERSION = "0.5.0"
4
+ VERSION = "0.7.0"
3
5
  end
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative "element_component/version"
2
4
  require_relative "element_component/element"
5
+ require_relative "element_component/components"
3
6
 
4
7
  module ElementComponent
5
8
  class Error < StandardError; end
6
- # Your code goes here...
7
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: element_component
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - João Paulo Correia
@@ -18,10 +18,77 @@ extra_rdoc_files: []
18
18
  files:
19
19
  - ".rspec"
20
20
  - ".rubocop.yml"
21
+ - AGENTS.md
21
22
  - LICENSE.txt
22
23
  - README.md
23
24
  - Rakefile
25
+ - examples/alert_example.rb
26
+ - examples/badge_example.rb
27
+ - examples/breadcrumb_example.rb
28
+ - examples/button_example.rb
29
+ - examples/button_group_example.rb
30
+ - examples/card_example.rb
31
+ - examples/carousel_example.rb
32
+ - examples/close_button_example.rb
33
+ - examples/dropdown_example.rb
34
+ - examples/list_group_example.rb
35
+ - examples/modal_example.rb
36
+ - examples/nav_example.rb
37
+ - examples/navbar_example.rb
38
+ - examples/pagination_example.rb
39
+ - examples/progress_example.rb
40
+ - examples/spinner_example.rb
41
+ - examples/table_example.rb
24
42
  - lib/element_component.rb
43
+ - lib/element_component/components.rb
44
+ - lib/element_component/components/alert.rb
45
+ - lib/element_component/components/alert/close_button.rb
46
+ - lib/element_component/components/alert/heading.rb
47
+ - lib/element_component/components/alert/link.rb
48
+ - lib/element_component/components/badge.rb
49
+ - lib/element_component/components/breadcrumb.rb
50
+ - lib/element_component/components/breadcrumb/item.rb
51
+ - lib/element_component/components/button.rb
52
+ - lib/element_component/components/button_group.rb
53
+ - lib/element_component/components/card.rb
54
+ - lib/element_component/components/card/body.rb
55
+ - lib/element_component/components/card/footer.rb
56
+ - lib/element_component/components/card/header.rb
57
+ - lib/element_component/components/card/image.rb
58
+ - lib/element_component/components/card/text.rb
59
+ - lib/element_component/components/card/title.rb
60
+ - lib/element_component/components/carousel.rb
61
+ - lib/element_component/components/carousel/caption.rb
62
+ - lib/element_component/components/carousel/item.rb
63
+ - lib/element_component/components/close_button.rb
64
+ - lib/element_component/components/dropdown.rb
65
+ - lib/element_component/components/dropdown/divider.rb
66
+ - lib/element_component/components/dropdown/header.rb
67
+ - lib/element_component/components/dropdown/item.rb
68
+ - lib/element_component/components/dropdown/menu.rb
69
+ - lib/element_component/components/list_group.rb
70
+ - lib/element_component/components/list_group/item.rb
71
+ - lib/element_component/components/modal.rb
72
+ - lib/element_component/components/modal/body.rb
73
+ - lib/element_component/components/modal/content.rb
74
+ - lib/element_component/components/modal/dialog.rb
75
+ - lib/element_component/components/modal/footer.rb
76
+ - lib/element_component/components/modal/header.rb
77
+ - lib/element_component/components/modal/title.rb
78
+ - lib/element_component/components/nav.rb
79
+ - lib/element_component/components/nav/item.rb
80
+ - lib/element_component/components/nav/link.rb
81
+ - lib/element_component/components/navbar.rb
82
+ - lib/element_component/components/navbar/brand.rb
83
+ - lib/element_component/components/navbar/collapse.rb
84
+ - lib/element_component/components/navbar/nav.rb
85
+ - lib/element_component/components/navbar/toggler.rb
86
+ - lib/element_component/components/pagination.rb
87
+ - lib/element_component/components/pagination/item.rb
88
+ - lib/element_component/components/progress.rb
89
+ - lib/element_component/components/progress/bar.rb
90
+ - lib/element_component/components/spinner.rb
91
+ - lib/element_component/components/table.rb
25
92
  - lib/element_component/element.rb
26
93
  - lib/element_component/version.rb
27
94
  homepage: https://github.com/joaopaulocorreia/element_component