Skip to content

Commit 52b58ce

Browse files
committed
Add a simple generator and expand the README
1 parent d9b8045 commit 52b58ce

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed

README.md

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Custom Elements with Rails
22

3-
This gem provides a small helper and installation scripts to use custom elements in conjunction with `importmap-rails`.
3+
This gem provides a small js-helper, installation and generators to use custom elements in conjunction with `importmap-rails`.
44

55
## Usage
66

@@ -33,14 +33,44 @@ app/javascript
3333
└── index.js
3434
```
3535

36-
You can now add the `<app-hello>` custom element in your app. No build step needed.
36+
You can now add the `<app-hello>` custom element in your HTML. No build step needed.
37+
38+
## How it works
39+
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.
41+
42+
```
43+
custom_elements/hello_element.js // will register <app-hello>
44+
```
45+
46+
Your `_element.js` files have to `export default` custom elements for this to work.
47+
48+
> [!WARNING]
49+
> Only single word elements are support currently.
50+
3751

3852
## Add a custom element
3953

40-
TODO
54+
Generate a new custom element with:
55+
56+
```bash
57+
$ rails generate custom_element test
58+
```
59+
60+
This will generate
61+
62+
```
63+
app/javascript
64+
└── custom_elements
65+
└── test_element.js
66+
```
67+
68+
which in turn will register a `<app-test></app-test>` custom element.
4169

4270
## Contributing
43-
Contribution directions go here.
71+
72+
TODO
4473

4574
## License
75+
4676
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

lib/custom_elements/rails/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module CustomElements
22
module Rails
3-
VERSION = "0.1.1"
3+
VERSION = "0.1.2"
44
end
55
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class CustomElementGenerator < Rails::Generators::NamedBase
2+
source_root File.expand_path("templates", __dir__)
3+
4+
def copy_custom_element
5+
template "custom_element.js", "app/javascript/custom_elements/#{file_name}_element.js"
6+
end
7+
end
8+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export default class extends HTMLElement {
2+
constructor() {
3+
super()
4+
}
5+
6+
connectedCallback() {
7+
console.log("<%= file_name %>")
8+
}
9+
}

0 commit comments

Comments
 (0)