@humanspeak/svelte-render
Version:
Manage complex Svelte behaviors outside of templates with full type safety
70 lines (69 loc) • 2.24 kB
JavaScript
/**
* Configuration class for rendering Svelte components with props and slots
* @template TComponent - The Svelte component type
*/
/* trunk-ignore(eslint/@typescript-eslint/no-explicit-any) */
export class ComponentRenderConfig {
component;
props;
/**
* Creates a new component render configuration
* @param component - The Svelte component to render
* @param props - Optional props to pass to the component
*/
constructor(
/**
* The Svelte component to render
*/
// trunk-ignore(eslint/no-unused-vars)
component,
/**
* Optional props to pass to the component
*/
// trunk-ignore(eslint/no-unused-vars)
props) {
this.component = component;
this.props = props;
}
/**
* @deprecated This method will be removed in the next major release. Please use svelte-5 event syntax instead.
* List of event handlers to attach to the component
*/
/* trunk-ignore(eslint/@typescript-eslint/no-explicit-any) */
// trunk-ignore(eslint/no-unused-vars)
eventHandlers = [];
/**
* @deprecated This method will be removed in the next major release. Please use svelte-5 event syntax instead.
*
* Attaches an event handler to the component
* @param type - The event type to listen for
* @param handler - The event handler function
* @returns this - For method chaining
*/
/* trunk-ignore(eslint/@typescript-eslint/no-explicit-any) */
on(type,
// trunk-ignore(eslint/no-unused-vars)
handler) {
this.eventHandlers.push([type, handler]);
this.props ??= {};
this.props[`on${String(type)}`] = handler;
return this;
}
/**
* List of child components to render in the default slot
*/
children = [];
/**
* Sets the children to render in the default slot
* @param children - The child components to render
* @returns this - For method chaining
*/
slot(...children) {
this.children = children;
return this;
}
}
/* trunk-ignore(eslint/@typescript-eslint/no-explicit-any) */
export function createRender(component, props) {
return new ComponentRenderConfig(component, props);
}