tramway 0.1.4 → 0.1.6

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: eedc7b84e9ad80ba5af66fc619af0aea5f24c17e4512e194a6069036ed00f646
4
- data.tar.gz: 3e4ac83ede95f1c90e4346f4723551f224efb8921ce1a8802d4977198623441a
3
+ metadata.gz: e5d6641dda4dedcf7e4d1a4e0638690e21b5f72fbfdccca6c40fa423eec9e3f8
4
+ data.tar.gz: c0e4e0955c58b253c5aac6198302b6c11f3393f42d5edfd4fc317dd7b1a7894b
5
5
  SHA512:
6
- metadata.gz: 43325b31d7a7ca934f39160066dd39653767efd04589e735b1cc2cfaf8ed09c087b9e846816bb97131c11d39e6309d0ef7f9925810159acdb05e3ad77f595aef
7
- data.tar.gz: b3da39f4acde834c8897e2ff2853a0c7cfe7cd83d04c0817c30017801010e16d03d5249bb87dd2f98b08b166f1ed594499c71b90fbf1f799b92979363706ab7f
6
+ metadata.gz: f2b1fd87395239845d1eeb4e901e44063e1487bdd83f226a247746dc1bc4fc555cfba4c8357345486dd31e5f5e8c0b5be59e2831e599a87d9c10632b221ad64e
7
+ data.tar.gz: eb6e88f325e65e1c49094a197a58419c11c71a5c24f78a5132167798ea4bff644c0a6d98320b073d16cea1b8baa258fa2300583474e95223e0f4ba09e9152327
data/README.md CHANGED
@@ -17,35 +17,66 @@ Tramway use [Tailwind](https://tailwindcss.com/) by default. All UI helpers impl
17
17
 
18
18
  ### Navbar
19
19
 
20
- #### tailwind_link_to
20
+ Tramway provides DSL for rendering Tailwind Navgiation bar.
21
21
 
22
- Tramway provides `tailwind_link_to` method, that uses `link_to`
23
-
24
- ```haml
25
- = tailwind_link_to "/users/sign_in" do
26
- Sign In
22
+ ```ruby
23
+ tramway_navbar title: 'Purple Magic', background: { color: :red, intensity: 500 } 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
- OR
35
+ will render [this](https://play.tailwindcss.com/UZPTCudFw5)
30
36
 
31
- ```haml
32
- = tailwind_link_to 'Sign In', "/users/sign_in"
33
- ```
37
+ #### tramway_navbar
34
38
 
35
- will render [this](https://play.tailwindcss.com/RT3Vvauu78)
39
+ This helper provides several options. Here is YAML view of `tramway_navbar` options structure
36
40
 
37
- ```haml
38
- = tailwind_link_to "/users/sign_out", method: :delete, confirm: 'Wanna quit?' do
39
- Sign Out
41
+ ```yaml
42
+ title: String that will be added to the left side of the navbar
43
+ background:
44
+ color: Css-color. Supports all named CSS colors and HEX colors
45
+ intensity: Color intensity. Range: **100..950**. Used by Tailwind. Not supported in case of using HEX color in the background.color
40
46
  ```
41
47
 
42
- OR
48
+ #### nav.left and nav.right
43
49
 
44
- ```
45
- = tailwind_link_to 'Sign Out', "/users/sign_out", method: :delete, confirm: 'Wanna quit?'
46
- ```
50
+ Tramway navbar provides `left` and `right` methods that puts items to left and right part of navbar.
47
51
 
48
- will render [this](https://play.tailwindcss.com/7qPmG4ltEU))
52
+ #### nav.item
53
+
54
+ 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.
55
+
56
+ ```ruby
57
+ tramway_navbar title: 'Purple Magic' do |nav|
58
+ nav.left do
59
+ nav.item 'Users', '/users'
60
+
61
+ # NOTE you can achieve the same with
62
+
63
+ nav.item '/users' do
64
+ 'Users'
65
+ end
66
+
67
+ # NOTE nav.item supports turbo-method and turbo-confirm attributes
68
+
69
+ nav.item 'Delete user', '/users/destroy', method: :delete, confirm: 'Are you sure?'
70
+
71
+ # will render this
72
+ # <li>
73
+ # <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">
74
+ # Sign out
75
+ # </a>
76
+ # </li>
77
+ end
78
+ end
79
+ ```
49
80
 
50
81
  ## Contributing
51
82
 
@@ -0,0 +1,3 @@
1
+ %li
2
+ = link_to @href, class: @style, **@options do
3
+ = content
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tailwinds
4
- module Navbar
4
+ module Nav
5
5
  # Render button styled with Tailwind using button_to or link_to methods
6
6
  #
7
- class ButtonComponent < TailwindComponent
7
+ class ItemComponent < TailwindComponent
8
8
  def initialize(**options)
9
9
  @href = options[:href]
10
10
  @style = 'text-white hover:bg-red-300 px-4 py-2 rounded'
@@ -0,0 +1,15 @@
1
+ %nav.py-4.px-8.flex.justify-between.items-center{ class: "bg-#{@color}" }
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,36 @@
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
+ @color = background(options)
12
+ end
13
+
14
+ private
15
+
16
+ CSS_COLORS = %i[aqua black blue fuchsia gray green lime maroon navy olive orange purple red
17
+ silver teal white yellow].freeze
18
+ MIN_INTENSITY = 100
19
+ MAX_INTENSITY = 950
20
+
21
+ def background(options)
22
+ color = options.dig(:background, :color) || :purple
23
+ intensity = options.dig(:background, :intensity) || 700
24
+
25
+ unless (MIN_INTENSITY..MAX_INTENSITY).cover?(intensity)
26
+ raise "Navigation Background Color intensity should be between #{MIN_INTENSITY} and #{MAX_INTENSITY}"
27
+ end
28
+
29
+ if color.in? CSS_COLORS
30
+ "#{color}-#{intensity}"
31
+ else
32
+ "[#{color}]"
33
+ end
34
+ end
35
+ end
36
+ end
@@ -8,9 +8,9 @@ module Tramway
8
8
 
9
9
  initializer 'tramway.load_helpers' do
10
10
  ActiveSupport.on_load(:action_view) do
11
- require 'tramway/helpers/tailwind_helpers'
11
+ require 'tramway/helpers/navbar_helper'
12
12
 
13
- ActionView::Base.include Tramway::Helpers::TailwindHelpers
13
+ ActionView::Base.include Tramway::Helpers::NavbarHelper
14
14
  end
15
15
  end
16
16
  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.4'
4
+ VERSION = '0.1.6'
5
5
  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.4
4
+ version: 0.1.6
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-15 00:00:00.000000000 Z
12
+ date: 2023-06-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: haml-rails
@@ -79,13 +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
88
- - lib/tramway/helpers/tailwind_helpers.rb
90
+ - lib/tramway/helpers/navbar_helper.rb
91
+ - lib/tramway/navbar.rb
89
92
  - lib/tramway/version.rb
90
93
  homepage: https://github.com/purple-magic/tramway
91
94
  licenses:
@@ -1,2 +0,0 @@
1
- = link_to @href, class: @style, **@options do
2
- = content
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Tramway
4
- module Helpers
5
- # ActionView helpers for tailwind
6
- #
7
- module TailwindHelpers
8
- def tailwind_link_to(text_or_url, url = nil, **options, &block)
9
- raise 'You can not provide argument and code block in the same time' if url.present? && block_given?
10
-
11
- if url.present?
12
- options.merge! href: url
13
- render(Tailwinds::Navbar::ButtonComponent.new(**options)) { text_or_url }
14
- else
15
- options.merge! href: text_or_url
16
- render(Tailwinds::Navbar::ButtonComponent.new(**options), &block)
17
- end
18
- end
19
- end
20
- end
21
- end