
Research
Malicious npm Packages Impersonate Flashbots SDKs, Targeting Ethereum Wallet Credentials
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
@locomotivemtl/component-manager
Advanced tools
A powerful, lightweight Web Component management system based on nanostores with automatic lifecycle tracking, global access bridge, and performance optimizations.
A powerful, lightweight Web Component management system based on nanostores with automatic lifecycle tracking, global access bridge, and performance optimizations.
npm install @locomotivemtl/component-manager nanostores
import { ComponentElement } from '@locomotivemtl/component-manager';
// Extend any HTML element
class Accordion extends HTMLDetailsElement {
constructor() {
super();
}
toggle() {
this.toggleAttribute('open');
}
}
// Register with ComponentElement for automatic management
customElements.define('c-accordion', ComponentElement(Accordion, 'Accordion'), {
extends: 'details'
});
<details is="c-accordion">
<summary>Click me</summary>
<p>This is an accordion content!</p>
</details>
⚠️ Safari Compatibility Note
Theis
attribute for extending built-in elements is not supported in Safari. For better cross-browser compatibility, consider using autonomous custom elements instead:
<c-accordion> <summary>Click me</summary> <p>This is an accordion content!</p> </c-accordion>
import { getComponentById, getComponentsByPrototype, findComponents } from '@locomotivemtl/component-manager';
// Get specific component by ID with full type safety
const $accordion = getComponentById<Accordion>('accordion-1');
$accordion?.toggle(); // TypeScript knows about toggle() method
// Get all components of a type with type safety
const $allAccordions = getComponentsByPrototype<Accordion>('Accordion');
$allAccordions.forEach($accordion => $accordion.toggle()); // TypeScript knows about Accordion methods
// Advanced queries with generic support
const $openDialogs = findComponents<Dialog>($component =>
$component.prototypeType === 'Dialog' && $component.hasAttribute('open')
);
import { ComponentElement } from '@locomotivemtl/component-manager';
class Dialog extends HTMLElement {
constructor() {
super();
}
open() {
this.showModal();
}
close() {
this.close();
}
}
customElements.define('c-dialog', ComponentElement(Dialog, 'Dialog'));
You can extend any built-in HTML element to gain specific behaviors while adding component management:
class CustomButton extends HTMLButtonElement {
constructor() {
super();
}
activate() {
this.classList.add('active');
}
}
customElements.define('c-button', ComponentElement(CustomButton, 'Button'), {
extends: 'button'
});
<button is="c-button">Enhanced Button</button>
ComponentElement(BaseClass, className)
Enhances any custom element class with automatic management features.
Parameters:
BaseClass
- The base custom element class to extendclassName
- String identifier for the component typeReturns: Enhanced class with lifecycle management
⚠️ Important: The className
parameter is required as a string to prevent issues with JavaScript minifiers (Vite, Webpack, etc.) that rename class names during production builds. By providing an explicit string, your component types remain consistent across development and production environments.
// ✅ Correct - className as string prevents minification issues
customElements.define('c-accordion', ComponentElement(Accordion, 'Accordion'));
// ❌ Don't rely on class.name - gets minified in production
// ComponentElement(Accordion, Accordion.name) // Could become 'a' in production
getComponentById<T>(id: string): ComponentOfType<T> | undefined
Retrieves a component by its unique ID with optional type casting.
Generic Usage:
// Get component with full type safety
const $dialog = getComponentById<Dialog>('my-dialog');
if ($dialog) {
$dialog.open();
}
getComponentsByPrototype<T>(prototype: string, selectorsToExclude?: string[] | string | HTMLElement | ManagedComponent): ComponentOfType<T>[]
Gets all components of a specific prototype with optional exclusion filters and full type safety.
Generic Usage:
// Get all components with type safety
const $allDialogs = getComponentsByPrototype<Dialog>('Dialog');
$allDialogs.forEach($dialog => {
$dialog.open();
});
// With exclusions
const $otherDialogs = getComponentsByPrototype<Dialog>('Dialog', '#current-dialog');
findComponents<T>(predicate: (component: ManagedComponent) => boolean): ComponentOfType<T>[]
Query components using a custom predicate function with optional type casting.
Generic Usage:
// Find components with specific attributes and type safety
const $activeComponents = findComponents<MyComponent>($component =>
$component.hasAttribute('active')
);
// Find components by custom criteria with full typing
const $openDialogs = findComponents<Dialog>($component =>
$component.prototypeType === 'Dialog' && $component.hasAttribute('open')
);
$openDialogs.forEach($dialog => {
$dialog.close();
});
getRegisteredPrototypes(): string[]
Returns array of all registered component prototype names.
getComponentStats(): Record<string, number>
Returns object with component counts by prototype.
getComponentCount(): number
Returns total number of managed components.
Debug logging is controlled via a global window flag. Set window.COMPONENT_MANAGER_DEBUG = true
before or after component initialization to see lifecycle events in the console.
Access components from external libraries or inline HTML without importing the component manager:
<button onclick="window.ComponentManager.getById('modal-1')?.open()">
Open Modal
</button>
<div onclick="window.ComponentManager.getByPrototype('Dialog').forEach($component => $component.close())">
Close All Dialogs
</div>
window.ComponentManager = {
components: ManagedComponent[]; // All managed components
getById: (id: string) => ManagedComponent | undefined;
getByPrototype: (prototype: string) => ManagedComponent[];
find: (predicate: Function) => ManagedComponent[];
getRegisteredPrototypes: () => string[];
getComponentCount: () => number;
}
The component manager now provides full TypeScript support with automatic type inference:
class MyDialog extends HTMLElement {
public isOpen = false;
open() {
this.isOpen = true;
this.showModal();
}
close() {
this.isOpen = false;
this.close();
}
}
customElements.define('c-dialog', ComponentElement(MyDialog, 'Dialog'));
// ✅ Full type safety - no need for separate interfaces!
const $dialog = getComponentById<MyDialog>('dialog-1');
if ($dialog) {
$dialog.open(); // ✅ TypeScript knows about open() method
}
// ✅ Works with arrays too
const $allDialogs = getComponentsByPrototype<MyDialog>('Dialog');
$allDialogs.forEach($dialog => {
$dialog.open(); // ✅ Full type safety for each element
});
// Exclude by selector
const $otherAccordions = getComponentsByPrototype<Accordion>('Accordion', '#current-accordion');
// Exclude by element reference
const $otherAccordions = getComponentsByPrototype<Accordion>('Accordion', this);
// Exclude multiple selectors
const $filtered = getComponentsByPrototype<Dialog>('Dialog', ['.ignore', '#skip-me']);
The component manager includes automatic caching for getComponentsByPrototype()
calls:
Enable debug logging to track component lifecycle by setting a global flag:
<!-- In your HTML head, before importing component-manager -->
<script>
window.COMPONENT_MANAGER_DEBUG = true;
</script>
<!-- Then import your components -->
<script type="module">
import '@locomotivemtl/component-manager';
// Components will log from the very beginning!
</script>
// In browser console - enable debugging
window.COMPONENT_MANAGER_DEBUG = true;
// Disable debugging
window.COMPONENT_MANAGER_DEBUG = false;
When enabled, you'll see component lifecycle events in the console:
🔧 ComponentManager: "accordion-1" (Accordion) registered
✅ ComponentManager: "accordion-1" connected to DOM
❌ ComponentManager: "accordion-1" disconnected from DOM
Note: Debug state is checked in real-time, so you can toggle it on/off anytime and it will affect subsequent component lifecycle events.
Always call parent lifecycle methods in your components:
class MyComponent extends HTMLElement {
connectedCallback() {
// Component manager handles registration automatically
// Add your component-specific logic here
this.setupEventListeners();
}
disconnectedCallback() {
// Component manager handles cleanup automatically
// Add your component-specific cleanup here
this.removeEventListeners();
}
}
componentname-1
, componentname-2
, etc.<c-dialog id="main-modal">
ComponentOfType<T>
type automatically provides both your component properties and manager properties// ✅ Recommended - Direct class usage
const $search = getComponentById<Search>('search-1');
// ❌ Not needed - No need for separate interfaces
interface SearchComponent extends ManagedComponent {
// ...
}
const $search = getComponentById<SearchComponent>('search-1');
Built with ❤️ by Locomotive
FAQs
A powerful, lightweight Web Component management system based on nanostores with automatic lifecycle tracking, global access bridge, and performance optimizations.
The npm package @locomotivemtl/component-manager receives a total of 10 weekly downloads. As such, @locomotivemtl/component-manager popularity was classified as not popular.
We found that @locomotivemtl/component-manager demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
Security News
Ruby maintainers from Bundler and rbenv teams are building rv to bring Python uv's speed and unified tooling approach to Ruby development.
Security News
Following last week’s supply chain attack, Nx published findings on the GitHub Actions exploit and moved npm publishing to Trusted Publishers.