custom_elements-rails 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b77ebf93580a7eb3cb83365dc83ba086f6d3bb1f20e173020039c08c5234669c
4
- data.tar.gz: fc62a709561eeeefdae11cac69983062664d21065e058b376932258d3d1a522a
3
+ metadata.gz: 00a594ba7a841b984e65721f217af0248ba150fe032038db7d6501606900fc14
4
+ data.tar.gz: 9db69aa3b4a6f8e621631dc22dad1640af90aafc2b6ec2fbc090089cb28d69e4
5
5
  SHA512:
6
- metadata.gz: bb09a978e032b95385a14b5a2e7ef060edd995981466eb941cef16c423bf27ba00ab012f9169e9502ea40c60308014b19fcdc8e3d1601b3f57c847d830cd276b
7
- data.tar.gz: b7dc6b1f79854aa70ae148222eb8093d772cb647f927dc7d73f4d8a7b0202389c4f5f80db1f2645bb21c8982e4785aa1097365fd521521d12a9670b3100a8ebf
6
+ metadata.gz: 47d8050c6e7e08715d63169ed4040f3d422652ba69140d84d2d269941184d0270281115b77f5f1413e4de62b7ba1efffcc0f78da3004fa95095c1a83e4efd907
7
+ data.tar.gz: 6b52ed941852c7543bbf0f9f82d5f6631bf06ea956248ac828d8660511894a7e189a9f0a459439053f7fd461d228c097db6d5ad16481c50fca7ea59013d0053f
data/README.md CHANGED
@@ -1,10 +1,11 @@
1
- # Custom Elements with Rails
1
+ # Custom Elements Rails
2
2
 
3
- This gem provides a small js-helper, installation and generators to use custom elements in conjunction with `importmap-rails`.
3
+ [![Tests](https://github.com/codergeek121/custom_elements-rails/actions/workflows/ruby.yml/badge.svg?branch=main)](https://github.com/codergeek121/custom_elements-rails/actions/workflows/ruby.yml)
4
4
 
5
- ## Usage
5
+ This gem adds a simple way to automatically register custom elements in your `importmap-rails` app. No build step required!
6
6
 
7
7
  ## Installation
8
+
8
9
  Add this line to your application's Gemfile:
9
10
 
10
11
  ```ruby
@@ -23,7 +24,7 @@ Run the initial setup:
23
24
  $ rails custom_elements:install
24
25
  ```
25
26
 
26
- This will setup and add the following files:
27
+ This will setup and edit add the following files:
27
28
 
28
29
  ```
29
30
  app/javascript
@@ -33,25 +34,27 @@ app/javascript
33
34
  └── index.js
34
35
  ```
35
36
 
36
- You can now add the `<app-hello>` custom element in your HTML. No build step needed.
37
+ The `<app-hello>` custom element can be used in the views now.
38
+
39
+ You can generate a new custom element with `rails g custom_element abc`.
37
40
 
38
41
  ## How it works
39
42
 
40
- `eagerDefineCustomElementsFrom("custom_elements", { prefix: "app" })` will parse the JSON-importmap rendered by the `importmap-rails` gem and register custom elements with `customElements.define(...)` to the browser's custom element registry.
43
+ The setup will add a JS function call `eagerDefineCustomElementsFrom` that parses the importmap rendered by the `importmap-rails` gem.
44
+ It registers custom elements with `customElements.define(...)` in the browser's registry based on the filenames in the `custom_elements` folder automatically.
41
45
 
42
46
  ```
43
- custom_elements/hello_element.js // will register <app-hello>
47
+ custom_elements/hello_element.js // will register <app-hello> automatically
44
48
  ```
45
49
 
46
- Your `_element.js` files have to `export default` custom elements for this to work.
50
+ Your `*_element.js` files have to `export default` custom elements for this to work properly.
47
51
 
48
52
  > [!WARNING]
49
- > Only single word elements are support currently.
50
-
53
+ > Only single word elements are supported currently. See https://github.com/codergeek121/custom_elements-rails/issues/1
51
54
 
52
- ## Add a custom element
55
+ ## Add a custom element with the built-in generator
53
56
 
54
- Generate a new custom element with:
57
+ This gem adds a generator to generate new custom elements with:
55
58
 
56
59
  ```bash
57
60
  $ rails generate custom_element test
@@ -59,13 +62,58 @@ $ rails generate custom_element test
59
62
 
60
63
  This will generate
61
64
 
65
+ ```javascript
66
+ // app/javascript/custom_elements/test_element.js
67
+ export default class extends HTMLElement {
68
+ constructor() {
69
+ super()
70
+ }
71
+
72
+ connectedCallback() {
73
+ console.log("test_element.js")
74
+ }
75
+ }
62
76
  ```
63
- app/javascript
64
- └── custom_elements
65
- └── test_element.js
77
+
78
+ which in turn will register a `<app-test></app-test>` custom element automatically in your app.
79
+
80
+ ```bash
81
+ $ rails generate custom_element test
82
+ ```
83
+
84
+ To observe changes in your custom element's attributes, you need to set a static array of attribute names. The generator also supports setting those automatically:
85
+
86
+ ```bash
87
+ $ rails generate custom_element test attribute1
66
88
  ```
67
89
 
68
- which in turn will register a `<app-test></app-test>` custom element.
90
+ will generate
91
+
92
+ ```javascript
93
+ export default class extends HTMLElement {
94
+ static observedAttributes = ["attribute1"]
95
+
96
+ constructor() {
97
+ super()
98
+ }
99
+
100
+ connectedCallback() {
101
+ console.log("test_element.js")
102
+ }
103
+
104
+ get attribute1() {
105
+ return this.getAttribute("attribute1")
106
+ }
107
+ }
108
+ ```
109
+
110
+ ## Documentation
111
+
112
+ `eagerDefineCustomElementsFrom(under, options)`
113
+
114
+ Currently supported `options`:
115
+
116
+ * `prefix`: The custom elements namespace/prefix.
69
117
 
70
118
  ## Contributing
71
119
 
data/Rakefile CHANGED
@@ -1,3 +1,6 @@
1
- require "bundler/setup"
1
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
2
+ load "rails/tasks/engine.rake"
3
+
4
+ load "rails/tasks/statistics.rake"
2
5
 
3
6
  require "bundler/gem_tasks"
@@ -2,7 +2,9 @@ module CustomElements
2
2
  module Rails
3
3
  class Railtie < ::Rails::Engine
4
4
  initializer "custom_elements-rails.assets.precompile" do |app|
5
- app.config.assets.precompile += %w( custom_elements-rails.js )
5
+ if app.config.respond_to? :assets
6
+ app.config.assets.precompile += %w( custom_elements-rails.js )
7
+ end
6
8
  end
7
9
  end
8
10
  end
@@ -1,5 +1,5 @@
1
1
  module CustomElements
2
2
  module Rails
3
- VERSION = "0.1.2"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -1,6 +1,8 @@
1
1
  class CustomElementGenerator < Rails::Generators::NamedBase
2
2
  source_root File.expand_path("templates", __dir__)
3
3
 
4
+ argument :attributes, type: :array, default: [], banner: "Observed attributes"
5
+
4
6
  def copy_custom_element
5
7
  template "custom_element.js", "app/javascript/custom_elements/#{file_name}_element.js"
6
8
  end
@@ -1,4 +1,7 @@
1
1
  export default class extends HTMLElement {
2
+ <% if attributes.any? %>
3
+ static observedAttributes = <%= attributes.map { _1.name } %>
4
+ <% end %>
2
5
  constructor() {
3
6
  super()
4
7
  }
@@ -6,4 +9,10 @@ export default class extends HTMLElement {
6
9
  connectedCallback() {
7
10
  console.log("<%= file_name %>")
8
11
  }
12
+ <% attributes.each do |attribute| -%>
13
+
14
+ get <%= attribute.name %>() {
15
+ return this.getAttribute("<%= attribute.name %>")
16
+ }
17
+ <% end -%>
9
18
  }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: custom_elements-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Niklas Häusele
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-04-22 00:00:00.000000000 Z
11
+ date: 2024-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,14 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '7'
19
+ version: 7.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '7'
26
+ version: 7.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: appraisal
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  description: 'A simple way to use custom elements to your Rails app. #nobuild'
28
42
  email:
29
43
  - niklas.haeusele@hey.com