|
4 | 4 |
|
5 | 5 | This gem adds a simple way to automatically register custom elements in your `importmap-rails` app. No build step required!
|
6 | 6 |
|
| 7 | +- Supports `importmap-rails` v1 and v2. |
| 8 | +- Supports `rails` 7.0, 7.1 & 8.0. |
| 9 | + |
7 | 10 | ## Installation
|
8 | 11 |
|
9 | 12 | Add this line to your application's Gemfile:
|
@@ -34,49 +37,48 @@ app/javascript
|
34 | 37 | └── index.js
|
35 | 38 | ```
|
36 | 39 |
|
37 |
| -The `<app-hello>` custom element can be used in the views now. |
| 40 | +The `<app-hello>` custom element can be used in views now. |
| 41 | + |
| 42 | +You can generate a new custom element `<app-demo>` with `rails generate custom_element demo`. |
38 | 43 |
|
39 |
| -You can generate a new custom element with `rails g custom_element abc`. |
| 44 | +### How It Works |
40 | 45 |
|
41 |
| -## How it works |
| 46 | +The `custom_elements-rails` gem uses `eagerDefineCustomElementsFrom` to automatically register [custom elements](https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_custom_elements) from the `custom_elements` folder. It reads the importmap from `importmap-rails` and registers elements using `customElements.define(...)`. |
42 | 47 |
|
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. |
| 48 | +For example: |
45 | 49 |
|
46 | 50 | ```
|
47 |
| -custom_elements/hello_element.js // will register <app-hello> automatically |
| 51 | +custom_elements/hello_element.js // Automatically registers <app-hello> |
48 | 52 | ```
|
49 | 53 |
|
50 |
| -Your `*_element.js` files have to `export default` custom elements for this to work properly. |
| 54 | +> [!IMPORTANT] |
| 55 | +> Make sure your `*_element.js` files use `export default` to define the custom elements. |
51 | 56 |
|
52 | 57 | ### Naming Convention for Custom Elements
|
53 | 58 |
|
54 | 59 | When defining custom elements from files, their filenames are used to generate the element names automatically. The following rules and examples clarify how file paths are converted to custom element names:
|
55 | 60 |
|
56 | 61 | #### Usage
|
57 | 62 |
|
58 |
| -Register all files in the `custom_elements` folder as custom elements using a prefix (e.g., `app`): |
| 63 | +Register all files in the `custom_elements` folder as custom elements: |
59 | 64 |
|
60 | 65 | ```js
|
61 | 66 | eagerDefineCustomElementsFrom("custom_elements", { prefix: "app" });
|
62 | 67 | ```
|
63 | 68 |
|
64 |
| -#### Conversion Rules |
65 |
| - |
66 |
| -- Filenames are transformed into kebab-case (lowercase with hyphens). |
67 |
| -- Words are separated by underscores (`_`) or hyphens (`-`) in the filename. |
68 |
| -- The folder structure is reflected in the name using double hyphens (`--`) to separate folder names from the file name. |
69 |
| -- A prefix (e.g., `app`) is added to the beginning of each custom element name. |
70 |
| - |
71 |
| -#### Examples |
72 |
| - |
73 | 69 | | Filepath | Generated Custom Element Name |
|
74 | 70 | |-------------------------------------|--------------------------------|
|
75 | 71 | | `custom_elements/demo_element.js` | `<app-demo>` |
|
76 | 72 | | `custom_elements/demo-element.js` | `<app-demo>` |
|
77 | 73 | | `custom_elements/foo_bar_element.js`| `<app-foo-bar>` |
|
78 | 74 | | `custom_elements/folder/foo_bar_element.js` | `<app-folder--foo-bar>` |
|
79 | 75 |
|
| 76 | +#### Conversion Rules |
| 77 | + |
| 78 | +- Filenames are transformed into kebab-case (lowercase with hyphens). |
| 79 | +- The folder structure is reflected in the name using double hyphens (`--`) to separate folder names from the file name. |
| 80 | +- A [configurable prefix](#documentation) is added to the beginning of each custom element name. |
| 81 | + |
80 | 82 | ## Add a custom element with the built-in generator
|
81 | 83 |
|
82 | 84 | This gem adds a generator to generate new custom elements with:
|
@@ -136,9 +138,9 @@ export default class extends HTMLElement {
|
136 | 138 |
|
137 | 139 | `eagerDefineCustomElementsFrom(under, options)`
|
138 | 140 |
|
139 |
| -Currently supported `options`: |
| 141 | +Currently supported optional `options`: |
140 | 142 |
|
141 |
| -* `prefix`: The custom elements namespace/prefix. |
| 143 | +* `prefix`: The custom elements namespace. (default: "app") |
142 | 144 |
|
143 | 145 | ## Contributing
|
144 | 146 |
|
|
0 commit comments