Skip to content

Commit dedad31

Browse files
authored
Merge pull request #720 from tinycarol/namespacing-guidelines
docs(guidelines): add namespacing guidelines for DXP
2 parents f35a9b3 + c7d4bcb commit dedad31

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

guidelines/dxp/namespacing.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Namespacing guidelines
2+
3+
To promote consistency, we have established the following rule of thumb for namespacing in DXP:
4+
5+
- Unique attributes, such as `id`s, should _not_ be namespaced
6+
- Non-unique attributes, such as `name`, should be namespaced
7+
- Fallback: if `id` is not passed, default to using `name` as `id`
8+
9+
## Reasoning
10+
11+
The purpose of the `id` attribute is to define something unique to the whole page. By following this approach, we ensure that these attributes can be used in methods such as `document.getElementById` as is, without juggling namespaces.
12+
13+
Other attributes, such as `name`, only need to be unique for some internal mechanism, such as in a `form` element. By namespacing them internally we ensure that, in case they end up being used under the hood as `id`s, they won't bleed into other components and cause duplicate issues.
14+
15+
## Examples
16+
17+
### Unique attributes
18+
19+
Take the following snippet:
20+
21+
```html
22+
<aui:form id="fm" namespace="myNamespace_">
23+
<clay:headless-data-set-display formId="fm" namespace="myNamespace_" />
24+
</aui:form>
25+
```
26+
27+
The `clay:headless-data-set-display` element receives the `id` of its wrapping form as a `formId` attribute. This `id` **doesn't include the namespace**, and should not be namespaced inside the form taglib. The `clay:headless-data-set-display` component can now internally use `document.getElementById(formId)` to interact with the form when necessary, without adding the `namespace` before it, because the resulting HTML for the `form` would be as follows:
28+
29+
```html
30+
<form id="fm">...</form>
31+
```
32+
33+
### Non-unique attributes
34+
35+
Take this other example:
36+
37+
```html
38+
<aui:form name="fm" namespace="myNamespace_">
39+
<clay:headless-data-set-display formName="fm" namespace="myNamespace_" />
40+
</aui:form>
41+
```
42+
43+
In this case, the `form` tag has a `name` attribute, which **will be namespaced under the hood**. Since we aren't passing an `id` prop, the component must use the fallback case and use the `name` as an `id`, resulting in the following HTML:
44+
45+
```html
46+
<form id="myNamespace_fm" name="myNamespace_fm">...</form>
47+
```
48+
49+
In this case, for the `clay:headless-data-set-display`, we are passing the `formName` prop instead of `id`. The component must namespace the `formName` attribute to find the element on the page, as such:
50+
51+
```js
52+
const formElement = document.getElementById(`${namespace}${formName}`);
53+
```

0 commit comments

Comments
 (0)