Skip to content

Commit fc2e44a

Browse files
committed
Add optional prefix, improve readme
1 parent 28dbfd2 commit fc2e44a

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

README.md

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
This gem adds a simple way to automatically register custom elements in your `importmap-rails` app. No build step required!
66

7+
- Supports `importmap-rails` v1 and v2.
8+
- Supports `rails` 7.0, 7.1 & 8.0.
9+
710
## Installation
811

912
Add this line to your application's Gemfile:
@@ -34,49 +37,48 @@ app/javascript
3437
└── index.js
3538
```
3639

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`.
3843

39-
You can generate a new custom element with `rails g custom_element abc`.
44+
### How It Works
4045

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(...)`.
4247

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:
4549

4650
```
47-
custom_elements/hello_element.js // will register <app-hello> automatically
51+
custom_elements/hello_element.js // Automatically registers <app-hello>
4852
```
4953

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.
5156
5257
### Naming Convention for Custom Elements
5358

5459
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:
5560

5661
#### Usage
5762

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:
5964

6065
```js
6166
eagerDefineCustomElementsFrom("custom_elements", { prefix: "app" });
6267
```
6368

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-
7369
| Filepath | Generated Custom Element Name |
7470
|-------------------------------------|--------------------------------|
7571
| `custom_elements/demo_element.js` | `<app-demo>` |
7672
| `custom_elements/demo-element.js` | `<app-demo>` |
7773
| `custom_elements/foo_bar_element.js`| `<app-foo-bar>` |
7874
| `custom_elements/folder/foo_bar_element.js` | `<app-folder--foo-bar>` |
7975

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+
8082
## Add a custom element with the built-in generator
8183

8284
This gem adds a generator to generate new custom elements with:
@@ -136,9 +138,9 @@ export default class extends HTMLElement {
136138

137139
`eagerDefineCustomElementsFrom(under, options)`
138140

139-
Currently supported `options`:
141+
Currently supported optional `options`:
140142

141-
* `prefix`: The custom elements namespace/prefix.
143+
* `prefix`: The custom elements namespace. (default: "app")
142144

143145
## Contributing
144146

app/assets/javascript/custom_elements-rails.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
export function eagerDefineCustomElementsFrom(namespace, options = {}) {
2+
const defaultOptions = {
3+
prefix: 'app'
4+
}
5+
6+
options = { ...defaultOptions, ...options }
27
const pathToElementName = (path) => {
38
const parts = path.split('/').map(p => p.replace(/_/g, '-'));
49
return `${options.prefix}-${parts.slice(0, -1).join('--')}${parts.length > 1 ? '--' : ''}${parts.at(-1)}`;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import { eagerDefineCustomElementsFrom } from "custom_elements-rails"
22

3-
eagerDefineCustomElementsFrom("custom_elements", { prefix: "app" })
3+
eagerDefineCustomElementsFrom("custom_elements")

0 commit comments

Comments
 (0)