A lightweight JavaScript library for creating reusable HTML components without any build tools or complex setup.
<script src="https://cdn.jsdelivr.net/npm/html-compo@latest/src/main.js"></script>
<user-card>John Doe</user-card>
<user-card>Jane Smith</user-card>
<html-components>
<user-card>
<div class="card">
<h3>@{self.data}</h3>
<p>Welcome, @{self.data}!</p>
</div>
</user-card>
</html-components>
<script src="https://cdn.jsdelivr.net/npm/html-compo@latest/src/main.js"></script>
npm install html-compo
Then import in your project:
import 'html-compo';
@{variable}
templatingfragment="true"
ref
attribute<html-components>
<greeting-card>
<div class="greeting">
<h2>Hello, @{self.data}!</h2>
<p>This is a reusable component.</p>
</div>
</greeting-card>
</html-components>
<greeting-card>World</greeting-card>
<greeting-card>HTML-COMPO.js</greeting-card>
<user-profile name="John" role="Admin" email="[email protected]"></user-profile>
<html-components>
<user-profile compo-attrs="name,role,email">
<div class="profile">
<h3>@{name}</h3>
<p>Role: @{role}</p>
<p>Email: @{email}</p>
</div>
</user-profile>
</html-components>
Check out our interactive examples page featuring:
// Get component by reference
const component = htmlCompo.getComponent('my-ref');
// Manipulate component content
component.html('New content');
component.attr('class', 'new-class');
@{self.data}
- Content inside component tags@{self.componentName}
- Component tag name@{attributeName}
- Values from component attributes<html-components>
<my-component compo-attrs="prop1,prop2=defaultValue" fragment="true">
<!-- Component template with @{variables} -->
</my-component>
</html-components>
<html-components>
<styled-component fragment="true">
<style>
.component-style { color: blue; }
</style>
<div class="component-style">@{self.data}</div>
</styled-component>
</html-components>
<interactive-counter ref="counter1" value="0"></interactive-counter>
<script>
const counter = htmlCompo.getComponent('counter1');
counter.attr('value', '10');
</script>
HTML-COMPO.js includes full TypeScript support:
// Type-safe component access
const component = window.htmlCompo.getComponent('my-ref');
if (component) {
const content: string = component.html();
component.attr('class', 'new-class');
}
See TypeScript documentation for complete setup instructions.
Contributions are welcome! Please feel free to submit a Pull Request.
MIT License - see LICENSE file for details.
Sarthak Pokharel