html-compo.js

HTML Components Create and Reuse

A lightweight JavaScript library for creating reusable HTML components without any build tools or complex setup.

🚀 Quick Start

<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>

📚 Documentation

🌐 Online Resources

📦 Installation

<script src="https://cdn.jsdelivr.net/npm/html-compo@latest/src/main.js"></script>

NPM

npm install html-compo

Then import in your project:

import 'html-compo';

✨ Key Features

🏃‍♂️ Basic Usage

1. Define Components

<html-components>
  <greeting-card>
    <div class="greeting">
      <h2>Hello, @{self.data}!</h2>
      <p>This is a reusable component.</p>
    </div>
  </greeting-card>
</html-components>

2. Use Components

<greeting-card>World</greeting-card>
<greeting-card>HTML-COMPO.js</greeting-card>

3. With Attributes

<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>

🎮 Interactive Examples

Check out our interactive examples page featuring:

🔗 API Reference

Global API

// Get component by reference
const component = htmlCompo.getComponent('my-ref');

// Manipulate component content
component.html('New content');
component.attr('class', 'new-class');

Template Variables

Component Definition

<html-components>
  <my-component compo-attrs="prop1,prop2=defaultValue" fragment="true">
    <!-- Component template with @{variables} -->
  </my-component>
</html-components>

🎯 Advanced Features

Shadow DOM Encapsulation

<html-components>
  <styled-component fragment="true">
    <style>
      .component-style { color: blue; }
    </style>
    <div class="component-style">@{self.data}</div>
  </styled-component>
</html-components>

Component References

<interactive-counter ref="counter1" value="0"></interactive-counter>

<script>
const counter = htmlCompo.getComponent('counter1');
counter.attr('value', '10');
</script>

💾 TypeScript Support

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.

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

📄 License

MIT License - see LICENSE file for details.

👨‍💻 Author

Sarthak Pokharel