phlex 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of phlex might be problematic. Click here for more details.

Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/.editorconfig +8 -0
  3. data/.rubocop.yml +3 -7
  4. data/Gemfile +2 -1
  5. data/Procfile.dev +3 -0
  6. data/Rakefile +3 -5
  7. data/docs/build.rb +15 -10
  8. data/docs/components/code_span.rb +9 -0
  9. data/docs/components/example.rb +1 -1
  10. data/docs/components/heading.rb +1 -1
  11. data/docs/components/layout.rb +38 -16
  12. data/docs/components/markdown.rb +17 -3
  13. data/docs/components/nav/item.rb +33 -0
  14. data/docs/components/nav.rb +6 -0
  15. data/docs/components/tabs/tab.rb +3 -1
  16. data/docs/components/title.rb +1 -1
  17. data/docs/page_builder.rb +3 -0
  18. data/docs/pages/helpers.rb +97 -0
  19. data/docs/pages/index.rb +6 -17
  20. data/docs/pages/library/collections.rb +101 -0
  21. data/docs/pages/rails/getting_started.rb +53 -0
  22. data/docs/pages/rails/helpers.rb +53 -0
  23. data/docs/pages/rails/layouts.rb +61 -0
  24. data/docs/pages/rails/migrating.rb +37 -0
  25. data/docs/pages/rails/rendering_views.rb +35 -0
  26. data/docs/pages/templates.rb +51 -149
  27. data/docs/pages/views.rb +55 -94
  28. data/fixtures/dummy/app/components/comment_component.html.erb +14 -0
  29. data/fixtures/dummy/app/components/comment_component.rb +8 -0
  30. data/fixtures/dummy/app/components/reaction_component.html.erb +3 -0
  31. data/fixtures/dummy/app/components/reaction_component.rb +7 -0
  32. data/fixtures/dummy/app/controllers/comments_controller.rb +4 -0
  33. data/fixtures/dummy/app/views/articles/form.rb +2 -0
  34. data/fixtures/dummy/app/views/card.rb +3 -1
  35. data/fixtures/dummy/app/views/comments/comment.rb +25 -0
  36. data/fixtures/dummy/app/views/comments/index.html.erb +3 -0
  37. data/fixtures/dummy/app/views/comments/reaction.rb +17 -0
  38. data/fixtures/dummy/app/views/comments/show.html.erb +3 -0
  39. data/fixtures/test_helper.rb +3 -0
  40. data/lib/generators/phlex/collection/USAGE +8 -0
  41. data/lib/generators/phlex/collection/collection_generator.rb +13 -0
  42. data/lib/generators/phlex/collection/templates/collection.rb.erb +15 -0
  43. data/lib/generators/phlex/layout/USAGE +8 -0
  44. data/lib/generators/phlex/layout/layout_generator.rb +13 -0
  45. data/lib/generators/phlex/layout/templates/layout.rb.erb +30 -0
  46. data/lib/generators/phlex/page/USAGE +8 -0
  47. data/lib/generators/phlex/page/page_generator.rb +13 -0
  48. data/lib/generators/phlex/page/templates/page.rb.erb +11 -0
  49. data/lib/generators/phlex/table/USAGE +8 -0
  50. data/lib/generators/phlex/table/table_generator.rb +14 -0
  51. data/lib/generators/phlex/table/templates/table.rb.erb +9 -0
  52. data/lib/phlex/collection.rb +58 -0
  53. data/lib/phlex/engine.rb +0 -3
  54. data/lib/phlex/html.rb +7 -13
  55. data/lib/phlex/rails/helpers.rb +81 -0
  56. data/lib/phlex/rails/layout.rb +15 -0
  57. data/lib/phlex/table.rb +104 -0
  58. data/lib/phlex/version.rb +1 -1
  59. data/lib/phlex/view.rb +10 -4
  60. data/lib/phlex.rb +1 -0
  61. metadata +52 -3
  62. data/lib/phlex/rails/tag_helpers.rb +0 -29
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pages
4
+ module Rails
5
+ class Helpers < ApplicationPage
6
+ def template
7
+ render Layout.new(title: "Using Rails helpers in Phlex views") do
8
+ render Markdown.new <<~MD
9
+ # Using Rails helpers in Phlex views
10
+
11
+ ## The `helpers` proxy
12
+
13
+ You can use the `helpers` proxy to access any Rails helper from a Phlex view. For example, you can use the `#t` helper for translations:
14
+
15
+ ```ruby
16
+ module Views
17
+ class Hello < ApplicationView
18
+ delegate :t, to: :helpers
19
+
20
+ def template
21
+ h1 do
22
+ t "hello"
23
+ end
24
+ end
25
+ end
26
+ end
27
+ ```
28
+
29
+ ## Layout helpers
30
+
31
+ Rails tag helpers return strings which makes them less than ideal to use from Phlex. Including `Phlex::Rails::Layout` gives you access to the following Rails helper proxies which immediately output to the buffer:
32
+
33
+ #{(Phlex::Rails::Layout.instance_methods - Module.methods).sort.map { "1. `#{_1}`" }.join("\n")}
34
+
35
+ Using these is equvalent to passing the output of the original Rails helpers to `raw`, e.g:
36
+
37
+ ```ruby
38
+ raw helpers.javascript_include_tag
39
+ ```
40
+
41
+ ## Including proxies
42
+
43
+ The following modules can be included for direct access to these helpers:
44
+
45
+ #{Phlex::Rails::Helpers.constants.sort.map { "1. `Phlex::Rails::Helpers::#{_1}`" }.join("\n")}
46
+
47
+ Note, helpers that produce HTML are adapted to output to the buffer directly.
48
+ MD
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pages
4
+ module Rails
5
+ class Layouts < ApplicationPage
6
+ def template
7
+ render Layout.new(title: "Getting started with Rails") do
8
+ render Markdown.new <<~MD
9
+ # Layouts in Rails
10
+
11
+ Rails has an implicit layouts feature that automatically wraps views in a layout template, usually `views/layouts/application.html.erb`. When using Phlex, you'll probably want to by-pass this feature completely. Here's why:
12
+
13
+ Sometimes you need to pass some argument to the layout, such as a page title that needs to be rendered in a `<title>` tag in the HTML `<head>`. Rails lets you do this from another part of the view using the `content_for` feature, but this pattern precludes defining an explicit interface for your layout through its initializer.
14
+
15
+ If layouts are Phlex views, they can be rendered just like any other view and can require that `title` argument from their initializer. The trick is the page view renders its content into the layout view.
16
+ MD
17
+
18
+ render Example.new do |e|
19
+ e.tab "layout.rb", <<~RUBY
20
+ module Views
21
+ class Layout < Phlex::View
22
+ def initialize(title:)
23
+ @title = title
24
+ end
25
+
26
+ def template(&)
27
+ html do
28
+ head do
29
+ title { @title }
30
+ end
31
+
32
+ body(&)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ RUBY
38
+
39
+ e.tab "index.rb", <<~RUBY
40
+ module Views
41
+ class Index < Phlex::View
42
+ def template
43
+ render Layout.new(title: "Hello") do
44
+ h1 { "👋" }
45
+ end
46
+ end
47
+ end
48
+ end
49
+ RUBY
50
+
51
+ e.execute "Views::Index.new.call"
52
+ end
53
+
54
+ render Markdown.new <<~MD
55
+ If you're using a Phlex view as a layout, you'll want to disable layouts from your Rails controller. You can do this by adding `layout false` to your controller. In a new app, you'll probably want to add this to the `ApplicationController`.
56
+ MD
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pages
4
+ class Rails::Migrating < ApplicationPage
5
+ def template
6
+ render Layout.new(title: "Migrating to Phlex in Rails") do
7
+ render Markdown.new <<~MD
8
+ # Migrating an existing Rails app to Phlex
9
+
10
+ Whether you currently use ActionView or ViewComponent with ERB, HAML or Slim, you can start using Phlex in your Rails app today without a big rewrite.
11
+
12
+ ## You can render Phlex views into existing templates
13
+
14
+ Phlex views implement the _renderable_ interface for Rails, which means they can be rendered from a controller or another view template — even ViewComponent templates. This means you can gradually migrate specific pages and components to Phlex without having to change anything else.
15
+
16
+ ## You can render ActionView partials and ViewComponent components in Phlex views
17
+
18
+ That's right, the `render` method in Phlex doesn't only work with Phlex views. You can use it to render ActionView partials and ViewComponent components too.
19
+
20
+ Say you have an `articles/index.html.erb` template that renders a list of articles using the `articles/_article.html.erb` partial. You can convert the index template to an `Articles::Index` Phlex view while continuing to render the same ActionView partial inside it.
21
+
22
+ ## Use an ERB → Phlex converter
23
+
24
+ The ERB → Phlex converter, [Phlexing](https://www.phlexing.fun), can do the heavy-lifting but it won't help you architect your components / design system. Take your time, converting things piece-by-piece.
25
+
26
+ If you're using ViewComponent, you might find you can convert components to Phlex views without even changing any call-sites.
27
+
28
+ ## Save the layout 'til last
29
+
30
+ After everything I said about layouts in the previous section, I'll let you in on a little secret: Phlex actually does support `content_for` in one direction. You can't yield a `content_for` block in Phlex, but you can assign one.
31
+
32
+ If you're working on an established Rails app, the layout is probably the last thing you should convert. Just include `Phlex::Rails::Helpers::ContentFor` in your `ApplicationView` and you'll be able to render Phlex views into existing ActionView layouts and assign `content_for` blocks too.
33
+ MD
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pages
4
+ class Rails::RenderingViews < ApplicationPage
5
+ def template
6
+ render Layout.new(title: "Getting started with Rails") do
7
+ render Markdown.new <<~MD
8
+ # Rendering Phlex views in Rails
9
+
10
+ You can render a `Phlex::View` from your Rails controller actions as well as other views, and even from ActionView / ViewComponent templates.
11
+
12
+ Instead of implicitly rendering an ERB template with automatic access to all your controller instance variables, you need to explicitly render Phlex views from your controller action methods.
13
+
14
+ Doing this allows you to design views without implicit dependencies on controller instance variables, making them much easier to test and reuse and reason about.
15
+
16
+ ```ruby
17
+ class ArticlesController < ApplicationController
18
+ def index
19
+ render Views::Articles::Index.new(
20
+ articles: Article.all.load_async
21
+ )
22
+ end
23
+
24
+ def show
25
+ render Views::Articles::Show.new(
26
+ article: Article.find(params[:id])
27
+ )
28
+ end
29
+ end
30
+ ```
31
+ MD
32
+ end
33
+ end
34
+ end
35
+ end
@@ -5,101 +5,111 @@ module Pages
5
5
  def template
6
6
  render Layout.new(title: "Templates in Phlex") do
7
7
  render Markdown.new(<<~MD)
8
- # Templates
8
+ # Working with templates
9
9
 
10
- Rather than use another language like ERB, HAML or Slim, Phlex provides a Ruby DSL for defining HTML templates.
10
+ In Phlex, templates are just methods that call other methods that add things to the output buffer. When you call the method `h1`, an `<h1>` tag is buffered for output.
11
11
 
12
- You can create a view class by subclassing `Phlex::View` and defining a method called `template`. Within the `template` method, you can compose HTML markup by calling the name of any [HTML element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element).
12
+ ## Tag attributes
13
13
 
14
- The first argument to an HTML element method is the _text content_ for that element. For example, here’s a view with an `<h1>` element that says “Hello World!”
14
+ You can add attributes to HTML tags by passing keyword arguments to the tag methods.
15
15
  MD
16
16
 
17
17
  render Example.new do |e|
18
- e.tab "heading.rb", <<~RUBY
19
- class Heading < Phlex::View
18
+ e.tab "hello.rb", <<~RUBY
19
+ class Hello < Phlex::View
20
20
  def template
21
- h1 "Hello World!"
21
+ h1(class: "text-xl font-bold") { "👋 Hello World!" }
22
22
  end
23
23
  end
24
24
  RUBY
25
25
 
26
- e.execute "Heading.new.call"
26
+ e.execute "Hello.new.call"
27
27
  end
28
28
 
29
29
  render Markdown.new(<<~MD)
30
- The text content is always HTML-escaped, so it’s safe to use with user input.
30
+ ## Hash attributes
31
31
 
32
- ## Attributes
33
-
34
- You can add attributes to HTML elements by passing keyword arguments to the tag method. Underscores (`_`) in attribute names are automatically converted to dashes (`-`).
32
+ If you pass a `Hash` as an attribute value, the hash will be flattened with a dash between each section.
35
33
  MD
36
34
 
37
35
  render Example.new do |e|
38
- e.tab "heading.rb", <<~RUBY
39
- class Heading < Phlex::View
36
+ e.tab "hello.rb", <<~RUBY
37
+ class Hello < Phlex::View
40
38
  def template
41
- h1 "Hello World!",
42
- class: "text-xl font-bold",
43
- aria_details: "details"
39
+ div(data: { controller: "hello" }) do
40
+ # ...
41
+ end
44
42
  end
45
43
  end
46
44
  RUBY
47
45
 
48
- e.execute "Heading.new.call"
46
+ e.execute "Hello.new.call"
49
47
  end
50
48
 
51
49
  render Markdown.new(<<~MD)
52
- You can also use *boolean* attributes. When set to `true`, the attribute will be rendered without a value, when _falsy_, the attribute isn’t rendered at all.
50
+ ## Boolean attributes
51
+
52
+ When `true`, the attribute will be rendered without a value; when _falsy_, the attribute isn’t rendered at all. You can still use the strings `"true"` and `"false"` as values for non-boolean attributes.
53
53
  MD
54
54
 
55
55
  render Example.new do |e|
56
- e.tab "example.rb", <<~RUBY
57
- class Example < Phlex::View
56
+ e.tab "channel_controls.rb", <<~RUBY
57
+ class ChannelControls < Phlex::View
58
58
  def template
59
- input type: "radio", name: "channel", id: "1", checked: true
60
- input type: "radio", name: "channel", id: "2", checked: false
59
+ input(
60
+ value: "1",
61
+ name: "channel",
62
+ type: "radio",
63
+ checked: true
64
+ )
65
+
66
+ input(
67
+ value: "2",
68
+ name: "channel",
69
+ type: "radio",
70
+ checked: false
71
+ )
61
72
  end
62
73
  end
63
74
  RUBY
64
75
 
65
- e.execute "Example.new.call"
76
+ e.execute "ChannelControls.new.call"
66
77
  end
67
78
 
68
79
  render Markdown.new(<<~MD)
69
- ## Nesting
80
+ ## The template tag
70
81
 
71
- Pass a block to an element method to nest other elements inside it.
82
+ Because the `template` method is used to define the view template itself, you'll need to use the method `template_tag` if you want to to render an HTML `<template>` tag.
72
83
  MD
73
84
 
74
85
  render Example.new do |e|
75
- e.tab "nav.rb", <<~RUBY
76
- class Nav < Phlex::View
86
+ e.tab "example.rb", <<~RUBY
87
+ class Example < Phlex::View
77
88
  def template
78
- nav do
79
- ul do
80
- li { a "Home", href: "/" }
81
- li { a "About", href: "/about" }
82
- li { a "Contact", href: "/contact" }
83
- end
89
+ template_tag do
90
+ img src: "hidden.jpg", alt: "A hidden image."
84
91
  end
85
92
  end
86
93
  end
87
94
  RUBY
88
95
 
89
- e.execute "Nav.new.call"
96
+ e.execute "Example.new.call"
90
97
  end
91
98
 
92
99
  render Markdown.new(<<~MD)
93
100
  ## Stand-alone text
94
101
 
95
- You can also output text without wrapping it in an element by using the `text` method. All text content is HTML-escaped, so it’s safe to use with user input.
102
+ You can output text content without wrapping it in an element by using the `text` helper method.
96
103
  MD
97
104
 
98
105
  render Example.new do |e|
99
106
  e.tab "heading.rb", <<~RUBY
100
107
  class Heading < Phlex::View
101
108
  def template
102
- h1 { strong "Hello "; text "World!" }
109
+ h1 do
110
+ strong { "Hello " }
111
+ text "World!"
112
+ end
103
113
  end
104
114
  end
105
115
  RUBY
@@ -110,132 +120,24 @@ module Pages
110
120
  render Markdown.new(<<~MD)
111
121
  ## Whitespace
112
122
 
113
- While the examples on this page have been formatted for readability, there is usually no whitespace between HTML tags. If you need to add some whitespace, you can use the `whitespace` method. This is useful for adding space between _inline_ elements to allow them to wrap.
123
+ The example output on this site is formatted for readability, but there is usually no whitespace between HTML tags in the output. If you need to add some whitespace, you can use the `whitespace` helper. This is useful for adding space between _inline_ elements to allow them to wrap.
114
124
  MD
115
125
 
116
126
  render Example.new do |e|
117
127
  e.tab "links.rb", <<~RUBY
118
128
  class Links < Phlex::View
119
129
  def template
120
- a "Home", href: "/"
130
+ a(href: "/") { "Home" }
121
131
  whitespace
122
- a "About", href: "/about"
132
+ a(href: "/about") { "About" }
123
133
  whitespace
124
- a "Contact", href: "/contact"
134
+ a(href: "/contact") { "Contact" }
125
135
  end
126
136
  end
127
137
  RUBY
128
138
 
129
139
  e.execute "Links.new.call"
130
140
  end
131
-
132
- render Markdown.new(<<~MD)
133
- ## Tokens and classes
134
-
135
- The `tokens` method helps you define conditional HTML attribute tokens (such as CSS classes).
136
-
137
- The `tokens` method accepts a splat of tokens that should always be output, and accepts keyword arguments for conditional tokens.
138
-
139
- The keyword arguments allow you to specify under which conditions certain tokens are applicable. The keyword argument keys are the conditions and the values are the tokens. Conditions can be Procs or Symbols that map to a relevant method. The `:active?` Symbol, for example, maps to the `active?` instance method.
140
-
141
- Here we have a `Link` view that produces an `<a>` tag with the CSS class `nav-item`. And if the link is _active_, we also apply the CSS class `nav-item-active`.
142
- MD
143
-
144
- render Example.new do |e|
145
- e.tab "link.rb", <<~RUBY
146
- class Link < Phlex::View
147
- def initialize(text, to:, active:)
148
- @text = text
149
- @to = to
150
- @active = active
151
- end
152
-
153
- def template
154
- a @text, href: @to, class: tokens("nav-item",
155
- active?: "nav-item-active")
156
- end
157
-
158
- private
159
-
160
- def active? = @active
161
- end
162
- RUBY
163
-
164
- e.tab "example.rb", <<~RUBY
165
- class Example < Phlex::View
166
- def template
167
- nav do
168
- ul do
169
- li { render Link.new("Home", to: "/", active: true) }
170
- li { render Link.new("About", to: "/about", active: false) }
171
- end
172
- end
173
- end
174
- end
175
- RUBY
176
-
177
- e.execute "Example.new.call"
178
- end
179
-
180
- render Markdown.new(<<~MD)
181
- You can also use the `classes` helper method to create a token list of classes. Since this method returns a hash (e.g. `{ class: "your CSS classes here" }`), you can destructure it into a `class:` keyword argument using the `**` prefix operator.
182
- MD
183
-
184
- render Example.new do |e|
185
- e.tab "link.rb", <<~RUBY
186
- class Link < Phlex::View
187
- def initialize(text, to:, active:)
188
- @text = text
189
- @to = to
190
- @active = active
191
- end
192
-
193
- def template
194
- a @text, href: @to, **classes("nav-item",
195
- active?: "nav-item-active")
196
- end
197
-
198
- private
199
-
200
- def active? = @active
201
- end
202
- RUBY
203
-
204
- e.tab "example.rb", <<~RUBY
205
- class Example < Phlex::View
206
- def template
207
- nav do
208
- ul do
209
- li { render Link.new("Home", to: "/", active: true) }
210
- li { render Link.new("About", to: "/about", active: false) }
211
- end
212
- end
213
- end
214
- end
215
- RUBY
216
-
217
- e.execute "Example.new.call"
218
- end
219
-
220
- render Markdown.new(<<~MD)
221
- ## The template element
222
-
223
- Because the `template` method is used to define the view template itself, you need to use the method `template_tag` if you want to to render an HTML `<template>` tag.
224
- MD
225
-
226
- render Example.new do |e|
227
- e.tab "example.rb", <<~RUBY
228
- class Example < Phlex::View
229
- def template
230
- template_tag do
231
- img src: "hidden.jpg", alt: "A hidden image."
232
- end
233
- end
234
- end
235
- RUBY
236
-
237
- e.execute "Example.new.call"
238
- end
239
141
  end
240
142
  end
241
143
  end