phlex 0.1.0 → 0.2.1

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 (70) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +1 -0
  3. data/.rubocop.yml +4 -0
  4. data/Gemfile +15 -3
  5. data/README.md +14 -30
  6. data/Rakefile +4 -6
  7. data/bench.rb +14 -0
  8. data/config.ru +9 -0
  9. data/docs/assets/application.css +24 -0
  10. data/docs/assets/logo.png +0 -0
  11. data/docs/build.rb +22 -0
  12. data/docs/components/callout.rb +9 -0
  13. data/docs/components/code_block.rb +26 -0
  14. data/docs/components/example.rb +32 -0
  15. data/docs/components/heading.rb +9 -0
  16. data/docs/components/layout.rb +45 -0
  17. data/docs/components/markdown.rb +26 -0
  18. data/docs/components/tabs/tab.rb +26 -0
  19. data/docs/components/tabs.rb +30 -0
  20. data/docs/components/title.rb +9 -0
  21. data/docs/page_builder.rb +36 -0
  22. data/docs/pages/application_page.rb +7 -0
  23. data/docs/pages/components.rb +175 -0
  24. data/docs/pages/index.rb +40 -0
  25. data/docs/pages/templates.rb +242 -0
  26. data/fixtures/component_helper.rb +16 -0
  27. data/fixtures/dummy/app/assets/config/manifest.js +0 -0
  28. data/fixtures/dummy/app/controllers/articles_controller.rb +4 -0
  29. data/fixtures/dummy/app/views/articles/form.rb +13 -0
  30. data/fixtures/dummy/app/views/articles/index.html.erb +11 -0
  31. data/fixtures/dummy/app/views/articles/new.html.erb +1 -0
  32. data/fixtures/dummy/app/views/card.rb +13 -0
  33. data/fixtures/dummy/config/database.yml +3 -0
  34. data/fixtures/dummy/config/routes.rb +5 -0
  35. data/fixtures/dummy/config/storage.yml +3 -0
  36. data/fixtures/dummy/db/schema.rb +6 -0
  37. data/fixtures/dummy/log/.gitignore +1 -0
  38. data/fixtures/dummy/public/favicon.ico +0 -0
  39. data/fixtures/layout.rb +31 -0
  40. data/fixtures/page.rb +41 -0
  41. data/fixtures/test_helper.rb +13 -0
  42. data/lib/generators/phlex/component/USAGE +8 -0
  43. data/lib/generators/phlex/component/component_generator.rb +13 -0
  44. data/lib/generators/phlex/component/templates/component.rb.erb +8 -0
  45. data/lib/overrides/symbol/name.rb +5 -0
  46. data/lib/phlex/block.rb +18 -0
  47. data/lib/phlex/buffered.rb +19 -0
  48. data/lib/phlex/component.rb +169 -23
  49. data/lib/phlex/configuration.rb +7 -0
  50. data/lib/phlex/html.rb +65 -0
  51. data/lib/phlex/rails/tag_helpers.rb +29 -0
  52. data/lib/phlex/rails.rb +8 -0
  53. data/lib/phlex/renderable.rb +35 -0
  54. data/lib/phlex/version.rb +1 -1
  55. data/lib/phlex.rb +25 -15
  56. data/package-lock.json +1195 -0
  57. data/package.json +5 -0
  58. data/phlex_logo.png +0 -0
  59. data/tailwind.config.js +7 -0
  60. metadata +64 -21
  61. data/Gemfile.lock +0 -32
  62. data/lib/phlex/callable.rb +0 -11
  63. data/lib/phlex/context.rb +0 -39
  64. data/lib/phlex/node.rb +0 -13
  65. data/lib/phlex/page.rb +0 -3
  66. data/lib/phlex/tag/standard_element.rb +0 -15
  67. data/lib/phlex/tag/void_element.rb +0 -9
  68. data/lib/phlex/tag.rb +0 -43
  69. data/lib/phlex/tags.rb +0 -108
  70. data/lib/phlex/text.rb +0 -13
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pages
4
+ class Index < ApplicationPage
5
+ def template
6
+ render Layout.new(title: "Introduction to Phlex") do
7
+ render Markdown.new(<<~MD)
8
+ # Introduction
9
+
10
+ Phlex is a framework for building fast, reusable, testable view components in pure Ruby.
11
+
12
+ Think of a component as anything on a web page that you could draw a circle around and name. Starting on the outside, you have the _“page”_ itself, inside the page is a _“header”_, in the header is a _“nav bar”_ and in the nav bar is a _“nav bar item.”_ These objects together make up an entire page and we call the objects components.
13
+
14
+ Each component object is an instance of a specific class of component. The nav-bar, for example, might contain three different nav-bar-items, but they’re all instances of the nav-bar-item class. This class, then, manifests everything there is to know about nav bar items in general. It models:
15
+
16
+ 1. the **data** attributes being represented — perhaps url and label;
17
+ 2. a **template**, which dictates how the data should be represented with HTML markup and CSS classes; and
18
+ 3. **logic**, conditions, or calculations on the data — perhaps a predicate method to determine whether the link is active or not.
19
+
20
+ # Why use Phlex?
21
+
22
+ ## Better developer experience 🎉
23
+
24
+ You don’t need to introduce a new language like Slim, HAML, or ERB. Phlex components are plain old Ruby objects: component classes are just Ruby classes, templates are just methods, and HTML tags are just method calls. If you know how to define a method that calls another method, you pretty much already know how to use Phlex.
25
+
26
+ ## Better safety 🥽
27
+ Rails partials can implicitly depend on instance variables from a controller or view. This happens more often than you might think when copying code into a new partial extraction. If the partial is then rendered in a different context or the instance variable’s meaning changes, things can break quite severely without warning.
28
+
29
+ Conversely, Phlex component templates render in an isolated execution context where only the instance variables and methods for the specific component are exposed.
30
+
31
+ ## Better performance 🚀
32
+
33
+ Phlex is ~4.35× faster than ActionView and ~2× faster than ViewComponent. Phlex components are also streamable.
34
+
35
+ Rails apps typically spend 40-80% of the response time rendering views, so this could be a significant factor in overall app performance.
36
+ MD
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,242 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pages
4
+ class Templates < ApplicationPage
5
+ def template
6
+ render Layout.new(title: "Templates in Phlex") do
7
+ render Markdown.new(<<~MD)
8
+ # Templates
9
+
10
+ Rather than use another langauge like ERB, HAML or Slim, Phlex provides a Ruby DSL for defining HTML templates.
11
+
12
+ You can create a component class by subclassing `Phlex::Component` 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).
13
+
14
+ The first argument to an HTML element method is the _text content_ for that element. For example, here’s a component with an `<h1>` element that says “Hello World!”
15
+ MD
16
+
17
+ render Example.new do |e|
18
+ e.tab "heading.rb", <<~RUBY
19
+ class Heading < Phlex::Component
20
+ def template
21
+ h1 "Hello World!"
22
+ end
23
+ end
24
+ RUBY
25
+
26
+ e.execute "Heading.new.call"
27
+ end
28
+
29
+ render Markdown.new(<<~MD)
30
+ The text content is always HTML-escaped, so it’s safe to use with user input.
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 (`-`).
35
+ MD
36
+
37
+ render Example.new do |e|
38
+ e.tab "heading.rb", <<~RUBY
39
+ class Heading < Phlex::Component
40
+ def template
41
+ h1 "Hello World!",
42
+ class: "text-xl font-bold",
43
+ aria_details: "details"
44
+ end
45
+ end
46
+ RUBY
47
+
48
+ e.execute "Heading.new.call"
49
+ end
50
+
51
+ 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.
53
+ MD
54
+
55
+ render Example.new do |e|
56
+ e.tab "example.rb", <<~RUBY
57
+ class Example < Phlex::Component
58
+ def template
59
+ input type: "radio", name: "channel", id: "1", checked: true
60
+ input type: "radio", name: "channel", id: "2", checked: false
61
+ end
62
+ end
63
+ RUBY
64
+
65
+ e.execute "Example.new.call"
66
+ end
67
+
68
+ render Markdown.new(<<~MD)
69
+ ## Nesting
70
+
71
+ Pass a block to an element method to nest other elements inside it.
72
+ MD
73
+
74
+ render Example.new do |e|
75
+ e.tab "nav.rb", <<~RUBY
76
+ class Nav < Phlex::Component
77
+ 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
84
+ end
85
+ end
86
+ end
87
+ RUBY
88
+
89
+ e.execute "Nav.new.call"
90
+ end
91
+
92
+ render Markdown.new(<<~MD)
93
+ ## Stand-alone text
94
+
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.
96
+ MD
97
+
98
+ render Example.new do |e|
99
+ e.tab "heading.rb", <<~RUBY
100
+ class Heading < Phlex::Component
101
+ def template
102
+ h1 { strong "Hello "; text "World!" }
103
+ end
104
+ end
105
+ RUBY
106
+
107
+ e.execute "Heading.new.call"
108
+ end
109
+
110
+ render Markdown.new(<<~MD)
111
+ ## Whitespace
112
+
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.
114
+ MD
115
+
116
+ render Example.new do |e|
117
+ e.tab "links.rb", <<~RUBY
118
+ class Links < Phlex::Component
119
+ def template
120
+ a "Home", href: "/"
121
+ whitespace
122
+ a "About", href: "/about"
123
+ whitespace
124
+ a "Contact", href: "/contact"
125
+ end
126
+ end
127
+ RUBY
128
+
129
+ e.execute "Links.new.call"
130
+ 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` component 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::Component
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::Component
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::Component
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::Component
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 component 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::Component
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
+ end
240
+ end
241
+ end
242
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ComponentHelper
4
+ def self.extended(parent)
5
+ parent.instance_exec do
6
+ let(:output) { example.call }
7
+ let(:example) { component.new }
8
+ end
9
+ end
10
+
11
+ def component(&block)
12
+ let :component do
13
+ Class.new(Phlex::Component, &block)
14
+ end
15
+ end
16
+ end
File without changes
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ArticlesController < ActionController::Base
4
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Views
4
+ module Articles
5
+ class Form < Phlex::Component
6
+ def template
7
+ form_with url: "test" do |f|
8
+ f.text_field :name
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ <p>Before</p>
2
+ <%= render Views::Card.new do |a| %>
3
+ <p>Start Card A</p>
4
+ <%= a.title "Hello from A" %>
5
+ <%= render Views::Card.new do |b| %>
6
+ <p>Start Card B</p>
7
+ <%= b.title "Hello from B" %>
8
+ <p>End Card B</p>
9
+ <% end %>
10
+ <p>End Card A</p>
11
+ <% end %>
@@ -0,0 +1 @@
1
+ <%= render Views::Articles::Form.new %>
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Views
4
+ class Card < Phlex::Component
5
+ def template(&block)
6
+ article class: "drop-shadow p-5 rounded", &block
7
+ end
8
+
9
+ def title(text)
10
+ h3 text, class: "font-bold"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: db/combustion_test.sqlite
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ Rails.application.routes.draw do
4
+ # Rails routes here
5
+ end
@@ -0,0 +1,3 @@
1
+ test:
2
+ service: Disk
3
+ root: /Users/joeldrapper/src/joeldrapper/phlex/tmp/storage
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ ActiveRecord::Schema.define do
4
+ # Set up any tables you need to exist for your test suite that don't belong
5
+ # in migrations.
6
+ end
@@ -0,0 +1 @@
1
+ *.log
File without changes
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Example
4
+ class LayoutComponent < Phlex::Component
5
+ def initialize(title: "Example")
6
+ @title = title
7
+ end
8
+
9
+ def template(&block)
10
+ html do
11
+ head do
12
+ title @title
13
+ meta name: "viewport", content: "width=device-width,initial-scale=1"
14
+ link href: "/assets/tailwind.css", rel: "stylesheet"
15
+ end
16
+
17
+ body class: "bg-zinc-100" do
18
+ nav class: "p-5", id: "main_nav" do
19
+ ul do
20
+ li(class: "p-5") { a "Home", href: "/" }
21
+ li(class: "p-5") { a "About", href: "/about" }
22
+ li(class: "p-5") { a "Contact", href: "/contact" }
23
+ end
24
+ end
25
+
26
+ div class: "container mx-auto p-5", &block
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
data/fixtures/page.rb ADDED
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Example
4
+ class Page < Phlex::Component
5
+ def template
6
+ render LayoutComponent.new do
7
+ h1 "Hi"
8
+
9
+ 5.times do
10
+ div do
11
+ 10.times do
12
+ a "Test", href: "something", unique: SecureRandom.uuid, data: { value: 1 }
13
+ end
14
+ end
15
+ end
16
+
17
+ table do
18
+ thead do
19
+ 10.times do
20
+ tr do
21
+ th "Hi"
22
+ end
23
+ end
24
+ end
25
+
26
+ tbody do
27
+ 100.times do
28
+ tr class: "a b c d e f g", id: "something" do
29
+ 10.times do
30
+ td class: "f g h i j k l" do
31
+ span "Test"
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "phlex"
4
+ require "bundler"
5
+
6
+ Bundler.require :test
7
+
8
+ Combustion.path = "fixtures/dummy"
9
+ Combustion.initialize! :action_controller do
10
+ config.autoload_paths << "#{root}/app"
11
+ end
12
+
13
+ require "component_helper"
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Generates a phlex component with the given name
3
+
4
+ Example:
5
+ rails generate phlex:component Sidebar
6
+
7
+ This will create:
8
+ app/views/components/sidebar_component.rb
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phlex
4
+ module Generators
5
+ class ComponentGenerator < ::Rails::Generators::NamedBase
6
+ source_root File.expand_path("templates", __dir__)
7
+
8
+ def create_component
9
+ template "component.rb", File.join("app/views", class_path, "#{file_name}.rb")
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,8 @@
1
+ <% module_namespacing do -%>
2
+ module Views
3
+ class <%= class_name %> < Phlex::Component
4
+ def template
5
+ end
6
+ end
7
+ end
8
+ <% end %>
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Overrides::Symbol::Name
4
+ refine(Symbol) { alias_method :name, :to_s }
5
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phlex
4
+ class Block
5
+ def initialize(context, &block)
6
+ @context = context
7
+ @block = block
8
+ end
9
+
10
+ def to_proc
11
+ method(:call).to_proc
12
+ end
13
+
14
+ def call(*args, **kwargs)
15
+ @context.instance_exec(*args, **kwargs, &@block)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phlex
4
+ class Buffered
5
+ def initialize(object, buffer:)
6
+ @object, @buffer = object, buffer
7
+ end
8
+
9
+ def method_missing(name, *args, **kwargs, &block)
10
+ output = @object.public_send(name, *args, **kwargs, &block)
11
+ @buffer << output if output.is_a? String
12
+ nil
13
+ end
14
+
15
+ def respond_to_missing?(name)
16
+ @object.respond_to?(name)
17
+ end
18
+ end
19
+ end