tramway 0.1.3.1 → 0.1.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0c47949a76aef2c3d5dc7e663bb34012b3450fbbe6f699e4ca7e9d0d98a9613f
4
- data.tar.gz: 1bbed346affab3a710234b4848af549200ae94cb1f3d05f97b3c7bd3acd63dcf
3
+ metadata.gz: 711582391b4cec7b6f8c8808a9d20a1c9695825e94e292073fe85e15d066f11c
4
+ data.tar.gz: 178c09a9c02feaf8f02094849cbe9fd8756a6f1806419eee369bb1ae0df023b2
5
5
  SHA512:
6
- metadata.gz: 7119b80a25ff0a302a3b8fd36b10877ce9f256952faf784d88097e7c0862e770f88eb948e909efc6a431ce273817f3847e17296211946bdfd95113984770658b
7
- data.tar.gz: 8fc6359c32257287ee3ae2383dfb502d6ed8415a15c1b6e977bbfed087cf4f1352316a2afb2db4657930a3ff4f75cc114a973b1b00bf5ef8c82db18dea833b14
6
+ metadata.gz: 63d37dcea2ec474996c0a261b0b9d7e3619337f0fc3443f75608633c4c4c1ed08be7a367d79ce8b4c7db33695cfaa18e4052458f78ebbff3d13bc89e9e319418
7
+ data.tar.gz: 0a10282ff924f4adfd5c892852ed0943a23f3374c1ded44c59344854cdd43034549cd39a2eafd2b8e4af6d5dd60abd06c2cb247ab1a2e70f01b8bc8aba3076f0
data/README.md CHANGED
@@ -17,23 +17,55 @@ Tramway use [Tailwind](https://tailwindcss.com/) by default. All UI helpers impl
17
17
 
18
18
  ### Navbar
19
19
 
20
- #### Button
20
+ Tramway provides DSL for rendering Tailwind Navgiation bar.
21
21
 
22
- Tramway provides `Tailwinds::Navbar::ButtonComponent`, that uses `button_to` or `link_to`
23
-
24
- ```haml
25
- = render(Tailwinds::Navbar::ButtonComponent.new(href: "/users/sign_in")) do
26
- Sign In
22
+ ```ruby
23
+ tramway_navbar title: 'Purple Magic' do |nav|
24
+ nav.left do
25
+ nav.item 'Users', '/users'
26
+ nav.item 'Podcasts', '/podcasts'
27
+ end
28
+
29
+ nav.right do
30
+ nav.item 'Sign out', '/users/sessions', method: :delete, confirm: 'Wanna quit?'
31
+ end
32
+ end
27
33
  ```
28
34
 
29
- will render [this](https://play.tailwindcss.com/RT3Vvauu78)
35
+ will render [this](https://play.tailwindcss.com/UZPTCudFw5)
30
36
 
31
- ```haml
32
- = render(Tailwinds::Navbar::ButtonComponent.new(action: "/users/sign_out", method: :delete)) do
33
- Sign Out
34
- ```
37
+ #### nav.left and nav.right
38
+
39
+ Tramway navbar provides `left` and `right` methods that puts items to left and right part of navbar.
35
40
 
36
- will render [this](https://play.tailwindcss.com/pJ8450tV21)
41
+ #### nav.item
42
+
43
+ Item in navigation is rendered `li a` inside navbar `ul` tag on the left or right sides. `nav.item` uses the same approach as `link_to` method with syntax sugar.
44
+
45
+ ```ruby
46
+ tramway_navbar title: 'Purple Magic' do |nav|
47
+ nav.left do
48
+ nav.item 'Users', '/users'
49
+
50
+ # NOTE you can achieve the same with
51
+
52
+ nav.item '/users' do
53
+ 'Users'
54
+ end
55
+
56
+ # NOTE nav.item supports turbo-method and turbo-confirm attributes
57
+
58
+ nav.item 'Delete user', '/users/destroy', method: :delete, confirm: 'Are you sure?'
59
+
60
+ # will render this
61
+ # <li>
62
+ # <a data-turbo-method="delete" data-turbo-confirm="Are you sure?" href="/users/sign_out" class="text-white hover:bg-red-300 px-4 py-2 rounded">
63
+ # Sign out
64
+ # </a>
65
+ # </li>
66
+ end
67
+ end
68
+ ```
37
69
 
38
70
  ## Contributing
39
71
 
@@ -0,0 +1,3 @@
1
+ %li
2
+ = link_to @href, class: @style, **@options do
3
+ = content
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tailwinds
4
+ module Nav
5
+ # Render button styled with Tailwind using button_to or link_to methods
6
+ #
7
+ class ItemComponent < TailwindComponent
8
+ def initialize(**options)
9
+ @href = options[:href]
10
+ @style = 'text-white hover:bg-red-300 px-4 py-2 rounded'
11
+ @options = prepare(options:)
12
+ end
13
+
14
+ private
15
+
16
+ def prepare(options:)
17
+ options.reduce({}) do |hash, (key, value)|
18
+ case key
19
+ when :method, :confirm
20
+ hash.deep_merge data: { "turbo_#{key}".to_sym => value }
21
+ else
22
+ hash.merge key => value
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,15 @@
1
+ %nav.bg-red-500.py-4.px-8.flex.justify-between.items-center
2
+ - if @title.present? || @left_items.present?
3
+ .flex
4
+ - if @title.present?
5
+ .text-xl.text-white.font-bold
6
+ = @title
7
+ - if @left_items.present?
8
+ %ul.flex.items-center.space-x-4
9
+ - @left_items.each do |item|
10
+ = item
11
+
12
+ - if @right_items.present?
13
+ %ul.flex.items-center.space-x-4
14
+ - @right_items.each do |item|
15
+ = item
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tailwinds
4
+ # Navbar class main class
5
+ #
6
+ class NavbarComponent < TailwindComponent
7
+ def initialize(**options)
8
+ @title = options[:title]
9
+ @left_items = options[:left_items]
10
+ @right_items = options[:right_items]
11
+ end
12
+ end
13
+ end
@@ -5,5 +5,13 @@ module Tramway
5
5
  #
6
6
  class Engine < ::Rails::Engine
7
7
  isolate_namespace Tramway
8
+
9
+ initializer 'tramway.load_helpers' do
10
+ ActiveSupport.on_load(:action_view) do
11
+ require 'tramway/helpers/navbar_helper'
12
+
13
+ ActionView::Base.include Tramway::Helpers::NavbarHelper
14
+ end
15
+ end
8
16
  end
9
17
  end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tramway/navbar'
4
+
5
+ module Tramway
6
+ module Helpers
7
+ # Providing navbar helpers for ActionView
8
+ #
9
+ module NavbarHelper
10
+ def tramway_navbar(**options)
11
+ if block_given?
12
+ @navbar = Tramway::Navbar.new self
13
+
14
+ yield @navbar
15
+
16
+ options[:left_items] = @navbar.items[:left]
17
+ options[:right_items] = @navbar.items[:right]
18
+ end
19
+
20
+ render(Tailwinds::NavbarComponent.new(**options))
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tramway
4
+ # Navbar class for Tramway navbars
5
+ #
6
+ class Navbar
7
+ attr_reader :items
8
+
9
+ def initialize(context)
10
+ @items = {}
11
+ @context = context
12
+ end
13
+
14
+ def left
15
+ return unless block_given?
16
+
17
+ @items[:left] = []
18
+
19
+ @filling = :left
20
+
21
+ yield self
22
+ end
23
+
24
+ def right
25
+ return unless block_given?
26
+
27
+ @items[:right] = []
28
+
29
+ @filling = :right
30
+
31
+ yield self
32
+ end
33
+
34
+ def item(text_or_url, url = nil, **options, &block)
35
+ raise 'You can not provide argument and code block in the same time' if url.present? && block_given?
36
+
37
+ rendered_item = if url.present?
38
+ options.merge! href: url
39
+ @context.render(Tailwinds::Nav::ItemComponent.new(**options)) { text_or_url }
40
+ else
41
+ options.merge! href: text_or_url
42
+ @context.render(Tailwinds::Nav::ItemComponent.new(**options), &block)
43
+ end
44
+
45
+ @items[@filling] << rendered_item
46
+ end
47
+ end
48
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tramway
4
- VERSION = '0.1.3.1'
4
+ VERSION = '0.1.5'
5
5
  end
data/lib/tramway.rb CHANGED
@@ -7,5 +7,4 @@ require 'view_component/engine'
7
7
 
8
8
  # Core module for the whole gem
9
9
  module Tramway
10
- # Your code goes here...
11
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tramway
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3.1
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - kalashnikovisme
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2023-06-04 00:00:00.000000000 Z
12
+ date: 2023-06-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: haml-rails
@@ -79,12 +79,16 @@ files:
79
79
  - Rakefile
80
80
  - app/components/tailwind_component.html.haml
81
81
  - app/components/tailwind_component.rb
82
- - app/components/tailwinds/navbar/button_component.html.haml
83
- - app/components/tailwinds/navbar/button_component.rb
82
+ - app/components/tailwinds/nav/item_component.html.haml
83
+ - app/components/tailwinds/nav/item_component.rb
84
+ - app/components/tailwinds/navbar_component.html.haml
85
+ - app/components/tailwinds/navbar_component.rb
84
86
  - config/routes.rb
85
87
  - lib/tasks/tramway_tasks.rake
86
88
  - lib/tramway.rb
87
89
  - lib/tramway/engine.rb
90
+ - lib/tramway/helpers/navbar_helper.rb
91
+ - lib/tramway/navbar.rb
88
92
  - lib/tramway/version.rb
89
93
  homepage: https://github.com/purple-magic/tramway
90
94
  licenses:
@@ -1,6 +0,0 @@
1
- - if @action.present?
2
- = button_to @action, class: @style, **@options do
3
- = content
4
- - else
5
- = link_to @href, class: @style, **@options do
6
- = content
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Tailwinds
4
- module Navbar
5
- # Render button styled with Tailwind using button_to or link_to methods
6
- #
7
- class ButtonComponent < TailwindComponent
8
- def initialize(**options)
9
- if options[:action].present?
10
- @action = options[:action]
11
- else
12
- @href = options[:href]
13
- end
14
-
15
- @style = 'text-white hover:bg-red-300 px-4 py-2 rounded'
16
- @options = options.except(:action, :href)
17
- end
18
- end
19
- end
20
- end