Skip to content

Commit d82f798

Browse files
committed
Add sharing components docs
Signed-off-by: macdonst <simon.macdonald@gmail.com>
1 parent f0f63d8 commit d82f798

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

app/docs/md/elements/sharing.md

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
---
2+
title: Sharing
3+
links: # Further Reading
4+
- Contributing packages to the npm registry: https://docs.npmjs.com/packages-and-modules/contributing-packages-to-the-registry
5+
---
6+
7+
As you build Enhance applications, you may notice a pattern where you create components for forms, buttons, cards, etc., that are often very similar across each application. You can combine these components into a single library to make their sharing between applications easier.
8+
9+
A pattern that works quite well with Enhance applications follows.
10+
11+
## Publishing
12+
13+
Most folks will create a npm package to collect their shared components. The structure of this package is up to you, but we recommend that the [main](https://docs.npmjs.com/cli/v10/configuring-npm/package-json#main) field of your `package.json` file points to a file structured like the following example.
14+
15+
In the file that represents the main entry point to your module, start by first importing the components you wish to publish.
16+
17+
```javascript
18+
import CheckBox from './packages/check-box/CheckBox.js'
19+
import TextInput from './packages/text-input/TextInput.js'
20+
```
21+
22+
Next, create an `elements` object that will map the custom element tag name to the [pure function](https://en.wikipedia.org/wiki/Pure_function) that generates your component.
23+
24+
```javascript
25+
const elements = {
26+
"enhance-checkbox": CheckBox,
27+
"enhance-text-input": TextInput,
28+
}
29+
```
30+
31+
Then export this `elements` object so it may be imported in your Enhance applications.
32+
33+
```javascript
34+
export default elements
35+
```
36+
37+
Finally, export each component by name to add more flexibility to your components package. This step is optional but beneficial if you have projects that don’t use all components in your component suite.
38+
39+
```javascript
40+
export {
41+
CheckBox,
42+
TextInput
43+
}
44+
```
45+
46+
Here's the complete code for our example entry point for the module.
47+
48+
<doc-code filename="index.js">
49+
50+
```javascript
51+
import CheckBox from './packages/check-box/CheckBox.js'
52+
import TextInput from './packages/text-input/TextInput.js'
53+
54+
const elements = {
55+
"enhance-checkbox": CheckBox,
56+
"enhance-text-input": TextInput,
57+
}
58+
59+
export default elements
60+
61+
export {
62+
CheckBox,
63+
TextInput
64+
}
65+
```
66+
67+
</doc-code>
68+
69+
## Importing
70+
71+
To use your component library in your Enhance application first install it as an Enhance dependency. For the rest of this document, we will use `@enhance/form-elements` as our example package name.
72+
73+
```bash
74+
npm install @enhance/form-elements
75+
```
76+
77+
Now that we've included one (or more) component libraries in our app we need to tell Enhance where to find them. To do that, we will edit, or create if it doesn't exist, the `app/enhance.mjs` file. It is in this file where we can import components from external packages.
78+
79+
<doc-code filename="app/elements.mjs">
80+
81+
```javascript
82+
import formElements from '@enhance/form-elements'
83+
let elements = { ...formElements }
84+
export default elements
85+
```
86+
87+
</doc-code>
88+
89+
### Multiple Component Libraries
90+
91+
If you combine multiple component libraries, you will use the same approach in your `app/elements.mjs` file.
92+
93+
<doc-code filename="app/elements.mjs">
94+
95+
```javascript
96+
import formElements from '@enhance/form-elements'
97+
import exampleComponents from '@example/components'
98+
let elements = { ...formElements, ...exampleComponents }
99+
export default elements
100+
```
101+
102+
</doc-code>
103+
104+
### Renaming Components
105+
106+
Occasionally, you will find that you need to rename an imported component because of a conflict in tag names. Let's pretend we already have a component called `enhance-checkbox` and we also want to use the `enhance-checkbox` from `@enhance/form-elements`. To do that we'll give the `enhance-checkbox` in `@enhance/form-elements` a new tag name of `my-checkbox`.
107+
108+
<doc-code filename="app/elements.mjs">
109+
110+
```javascript
111+
import formElements from '@enhance/form-elements'
112+
delete Object.assign(
113+
formElements,
114+
{['my-checkbox']: formElements['enhance-checkbox'] }
115+
)['enhance-checkbox'];
116+
let elements = { ...formElements }
117+
export default elements
118+
```
119+
120+
</doc-code>
121+
122+
### Selective Imports
123+
124+
You will often find that you don’t need all of the components in the component library you are importing. If the component library you are using follows our recommendation to provide name exports, this is easy to accomplish. For example, our `@enhance/form-elements` example exports a `CheckBox` and a `TextInput`, but our application only needs the Che`ckBox. We can then write our `app/elements.mjs` file like this:
125+
126+
<doc-code filename="app/elements.mjs">
127+
128+
```javascript
129+
import { CheckBox } from '@enhance/form-elements'
130+
let elements = {
131+
"enhance-checkbox": CheckBox
132+
}
133+
export default elements
134+
```
135+
136+
</doc-code>

app/docs/nav-data.mjs

+4
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ export const data = [
9393
'context',
9494
],
9595
},
96+
{
97+
slug: 'sharing',
98+
label: 'Sharing',
99+
},
96100
],
97101
},
98102
{

0 commit comments

Comments
 (0)