@adobe-commerce/elsie 1.4.1-alpha101 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe-commerce/elsie",
3
- "version": "1.4.1-alpha101",
3
+ "version": "1.4.1",
4
4
  "license": "SEE LICENSE IN LICENSE.md",
5
5
  "description": "Domain Package SDK",
6
6
  "engines": {
@@ -121,5 +121,21 @@ button.addEventListener('click', () => {
121
121
  });
122
122
  ```
123
123
 
124
+ ### Unmounting components without instance access
125
+
126
+ The `Render.unmount` static method provides a way to unmount components from the DOM when you don't have direct access to the component instance.
127
+ This is particularly useful in scenarios where components are rendered inside modals, dialogs, or other temporary containers that need to be cleaned up.
128
+
129
+ #### Example
130
+
131
+ ```js
132
+ // Close the dialog
133
+ dialog.close();
134
+
135
+ // Unmount any dropin containers rendered in the modal
136
+ dialog.querySelectorAll('[data-dropin-container]').forEach(Render.unmount);
137
+ ```
138
+
139
+ This approach ensures that all dropin components are properly cleaned up when their container elements are removed from the DOM, preventing memory leaks and maintaining application performance.
124
140
  </Unstyled>
125
141
 
@@ -71,17 +71,21 @@ export class Render {
71
71
  rootElement.innerHTML = '';
72
72
 
73
73
  // clone the root element to initialize rendering on the background
74
- const tmp = document.createElement('div');
74
+ const root = document.createElement('div');
75
75
 
76
76
  // apply base design tokens and global styles to the root element
77
77
  rootElement.classList.add('dropin-design');
78
+ rootElement.setAttribute('data-dropin-container', Component.name);
78
79
 
79
- render(<Root next={state} />, tmp);
80
+ // store the virtual root element
81
+ (rootElement as any).__rootElement = root;
82
+
83
+ render(<Root next={state} />, root);
80
84
 
81
85
  // API object to control the rendered component
82
86
  const API: RenderAPI = {
83
87
  remove: () => {
84
- render(null, tmp);
88
+ render(null, root);
85
89
  },
86
90
  setProps: (cb: (prev: T) => T) => {
87
91
  const next = cb(state.peek());
@@ -97,7 +101,7 @@ export class Render {
97
101
  rootElement.classList.add('dropin-design');
98
102
 
99
103
  // append the rendered component to the DOM only when all slots are resolved
100
- rootElement.appendChild(tmp.firstChild ?? tmp);
104
+ rootElement.appendChild(root.firstChild ?? root);
101
105
 
102
106
  return resolve(API);
103
107
  }
@@ -106,14 +110,24 @@ export class Render {
106
110
  };
107
111
  }
108
112
 
113
+ /**
114
+ * Unmounts a container from a root element.
115
+ * @param rootElement - The root element to unmount the container from.
116
+ */
117
+ static unmount(rootElement: HTMLElement) {
118
+ const root = (rootElement as any)?.__rootElement;
119
+ if (!root) throw new Error('Root element is not defined');
120
+ render(null, root);
121
+ }
122
+
109
123
  /**
110
124
  * UnRenders a component from a root element.
111
125
  * @param rootElement - The root element to unmount the component from.
112
- * @deprecated Use `remove` method from the returned object of the `mount` method instead.
126
+ * @deprecated Use `remove` method from the returned object of the `mount` method instead or `unmount` method from the `Render` class.
113
127
  */
114
128
  unmount(rootElement: HTMLElement) {
115
129
  if (!rootElement) throw new Error('Root element is not defined');
116
- rootElement.firstChild?.remove();
130
+ rootElement.firstChild?.remove();
117
131
  }
118
132
 
119
133
  /**
@@ -135,4 +149,4 @@ export class Render {
135
149
  { ...options }
136
150
  );
137
151
  }
138
- }
152
+ }