fix(demotools): update DeesDemoWrapper to handle multiple slotted elements in runAfterRender callback
This commit is contained in:
		| @@ -5,7 +5,7 @@ | ||||
|   "description": "A set of web component tools for creating element catalogues, enabling the structured development and documentation of custom elements and pages.", | ||||
|   "exports": { | ||||
|     ".": "./dist_ts_web/index.js", | ||||
|     "./demoTools": "./dist_ts_demotools" | ||||
|     "./demotools": "./dist_ts_demotools" | ||||
|   }, | ||||
|   "type": "module", | ||||
|   "scripts": { | ||||
|   | ||||
| @@ -30,22 +30,30 @@ A utility component for wrapping demo elements with post-render functionality. | ||||
|  | ||||
| **Usage:** | ||||
| ```typescript | ||||
| import { DeesDemoWrapper } from '@design.estate/dees-wcctools/demoTools'; | ||||
| import * as demoTools from '@design.estate/dees-wcctools/demotools'; | ||||
|  | ||||
| // In your demo function: | ||||
| demo: () => html` | ||||
|   <dees-demowrapper .runAfterRender=${(element) => { | ||||
|     // Do something with the rendered element | ||||
|     element.setAttribute('data-demo', 'true'); | ||||
|     console.log('Element rendered:', element); | ||||
|   <dees-demowrapper .runAfterRender=${(children: HTMLCollection) => { | ||||
|     // Access all slotted elements | ||||
|     const firstElement = children[0] as HTMLElement; | ||||
|     firstElement.setAttribute('data-demo', 'true'); | ||||
|     console.log('All slotted elements:', children); | ||||
|      | ||||
|     // Work with multiple elements | ||||
|     Array.from(children).forEach(child => { | ||||
|       console.log('Element rendered:', child); | ||||
|     }); | ||||
|   }}> | ||||
|     <my-custom-element></my-custom-element> | ||||
|     <div>Additional content</div> | ||||
|   </dees-demowrapper> | ||||
| ` | ||||
| ``` | ||||
|  | ||||
| **Features:** | ||||
| - Wraps demo elements without affecting layout (uses `display: contents`) | ||||
| - Provides access to the rendered element instance after mounting | ||||
| - Provides access to ALL slotted elements via HTMLCollection after mounting | ||||
| - Supports async operations in runAfterRender callback | ||||
| - Automatically handles timing to ensure element is fully rendered | ||||
| - Automatically handles timing to ensure elements are fully rendered | ||||
| - Can handle multiple slotted elements, not just the first one | ||||
							
								
								
									
										42
									
								
								readme.md
									
									
									
									
									
								
							
							
						
						
									
										42
									
								
								readme.md
									
									
									
									
									
								
							| @@ -178,28 +178,33 @@ public static styles = [ | ||||
| The demo tools provide enhanced testing capabilities: | ||||
|  | ||||
| ```typescript | ||||
| import * as demoTools from '@design.estate/dees-wcctools/demoTools'; | ||||
| import * as demoTools from '@design.estate/dees-wcctools/demotools'; | ||||
|  | ||||
| @customElement('my-component') | ||||
| export class MyComponent extends DeesElement { | ||||
|   public static demo = () => html` | ||||
|     <dees-demowrapper .runAfterRender=${async (element: MyComponent) => { | ||||
|       // Access the rendered component instance | ||||
|       console.log('Component mounted:', element); | ||||
|     <dees-demowrapper .runAfterRender=${async (children: HTMLCollection) => { | ||||
|       // Access all slotted elements | ||||
|       console.log('Slotted elements:', children.length); | ||||
|        | ||||
|       // Access specific element (e.g., first child) | ||||
|       const myComponent = children[0] as MyComponent; | ||||
|       console.log('Component mounted:', myComponent); | ||||
|        | ||||
|       // Simulate user interactions | ||||
|       element.value = 'Test value'; | ||||
|       await element.updateComplete; | ||||
|       myComponent.value = 'Test value'; | ||||
|       await myComponent.updateComplete; | ||||
|        | ||||
|       // Trigger methods | ||||
|       element.handleClick(); | ||||
|       myComponent.handleClick(); | ||||
|        | ||||
|       // Assert component state | ||||
|       if (element.isValid) { | ||||
|         console.log('Component validation passed'); | ||||
|       } | ||||
|       // Work with multiple elements if present | ||||
|       Array.from(children).forEach((child, index) => { | ||||
|         console.log(`Child ${index}:`, child.tagName); | ||||
|       }); | ||||
|     }}> | ||||
|       <my-component></my-component> | ||||
|       <div>Additional content</div> | ||||
|     </dees-demowrapper> | ||||
|   `; | ||||
| } | ||||
| @@ -304,10 +309,12 @@ For complex property types, implement custom logic in your demo: | ||||
|  | ||||
| ```typescript | ||||
| public static demo = () => html` | ||||
|   <dees-demowrapper .runAfterRender=${(el) => { | ||||
|     // Set up complex property handling | ||||
|     el.addEventListener('property-change', (e) => { | ||||
|       console.log('Property changed:', e.detail); | ||||
|   <dees-demowrapper .runAfterRender=${(children) => { | ||||
|     // Set up complex property handling for all children | ||||
|     Array.from(children).forEach(child => { | ||||
|       child.addEventListener('property-change', (e) => { | ||||
|         console.log('Property changed:', e.detail); | ||||
|       }); | ||||
|     }); | ||||
|   }}> | ||||
|     <my-component></my-component> | ||||
| @@ -346,9 +353,10 @@ Initialize the WCC Tools dashboard. | ||||
| ### DeesDemoWrapper | ||||
| Component for wrapping demos with post-render logic. | ||||
|  | ||||
| - `runAfterRender`: Function called after the wrapped element renders | ||||
| - Receives the first child element as parameter | ||||
| - `runAfterRender`: Function called after the wrapped elements render | ||||
| - Receives the complete HTMLCollection of all slotted elements | ||||
| - Supports async operations | ||||
| - Access individual elements via array index or Array.from() | ||||
|  | ||||
| ## Browser Support | ||||
| - Chrome/Edge (latest) | ||||
|   | ||||
| @@ -34,11 +34,11 @@ The properties panel has timing issues detecting rendered elements because: | ||||
| ## Created DeesDemoWrapper Component | ||||
| - Location: ts_demotools/demotools.ts | ||||
| - Allows wrapping demo elements with post-render functionality | ||||
| - Provides runAfterRender callback that receives the rendered element | ||||
| - Provides runAfterRender callback that receives ALL slotted elements as HTMLCollection | ||||
| - Uses display: contents to not affect layout | ||||
| - Handles timing automatically with 50ms delay after firstUpdated | ||||
| - Supports both sync and async callbacks | ||||
| - Exports available at @design.estate/dees-wcctools/demoTools | ||||
| - Exports available at @design.estate/dees-wcctools/demotools (lowercase) | ||||
|  | ||||
| # Documentation Update (COMPLETED) | ||||
|  | ||||
| @@ -50,4 +50,13 @@ The properties panel has timing issues detecting rendered elements because: | ||||
| - Best practices and guidelines | ||||
| - API reference | ||||
| - Browser support information | ||||
| - Complete examples for all major features | ||||
| - Complete examples for all major features | ||||
|  | ||||
| # Enhanced DemoWrapper (COMPLETED) | ||||
|  | ||||
| ## Modified runAfterRender callback: | ||||
| - Now receives HTMLCollection of ALL slotted elements instead of just the first one | ||||
| - Allows access to complete slotted DOM structure | ||||
| - Updated documentation with correct import path (lowercase 'demotools') | ||||
| - Examples show how to work with multiple slotted elements | ||||
| - Maintains backward compatibility by accessing elements via index | ||||
| @@ -3,7 +3,7 @@ import { DeesElement, customElement, html, css, property, type TemplateResult } | ||||
| @customElement('dees-demowrapper') | ||||
| export class DeesDemoWrapper extends DeesElement { | ||||
|   @property({ attribute: false }) | ||||
|   public runAfterRender: (element: HTMLElement) => void | Promise<void>; | ||||
|   public runAfterRender: (children: HTMLCollection) => void | Promise<void>; | ||||
|  | ||||
|   public static styles = [ | ||||
|     css` | ||||
| @@ -25,14 +25,12 @@ export class DeesDemoWrapper extends DeesElement { | ||||
|     // Wait a bit for slotted content to render | ||||
|     await new Promise(resolve => setTimeout(resolve, 50)); | ||||
|      | ||||
|     // Find the first element child (excluding text nodes) | ||||
|     // Get all slotted elements | ||||
|     const slottedElements = this.children; | ||||
|     if (slottedElements.length > 0 && this.runAfterRender) { | ||||
|       const firstElement = slottedElements[0] as HTMLElement; | ||||
|        | ||||
|       // Call the runAfterRender function with the element | ||||
|       // Call the runAfterRender function with all slotted elements | ||||
|       try { | ||||
|         await this.runAfterRender(firstElement); | ||||
|         await this.runAfterRender(slottedElements); | ||||
|       } catch (error) { | ||||
|         console.error('Error in runAfterRender:', error); | ||||
|       } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user