A modern web component library built with TypeScript and Rollup, designed to be lightweight, reusable, and easy to integrate into any web project. Published to GitHub Packages for easy distribution and version management.
- Modern Web Components: Built using native Custom Elements API
- TypeScript Support: Full TypeScript definitions included
- Multiple Build Formats: UMD, ES modules, and minified versions
- GitHub Packages: Published to GitHub Packages for easy distribution
- Development Server: Built-in development server for testing
First, configure npm to use GitHub Packages for this scope. Create or update your .npmrc file:
@colmkenna:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=YOUR_GITHUB_TOKEN
Then install the package:
npm install @colmkenna/ck-webcomponents<script src="https://unpkg.com/@colmkenna/ck-webcomponents@latest/dist/ck-editable-array/ck-editable-array.min.js"></script>Then import in your JavaScript:
import '@colmkenna/ck-webcomponents';Or import specific components:
import { CkEditableArray } from '@colmkenna/ck-webcomponents';A simple greeting component with customizable name and color.
<!-- Basic usage -->
<ck-editable-array></ck-editable-array>
<!-- With custom name -->
<ck-editable-array name="Developer"></ck-editable-array>
<!-- With custom name and color -->
<ck-editable-array name="Developer" color="#ff6b6b"></ck-editable-array>| Attribute | Type | Default | Description |
|---|---|---|---|
name |
string | "World" | The name to display in the greeting |
color |
string | "#333" | Text color for the message |
The component also supports JavaScript property access:
const ckEditableArray = document.querySelector('ck-editable-array');
ckEditableArray.name = 'New Name';
ckEditableArray.color = '#blue';- Node.js 16+
- npm or yarn
- Clone the repository:
git clone https://github.com/ColmKenna/ckWebComponents.git
cd ckWebComponents- Install dependencies:
npm install- Start development mode:
npm run dev- Serve the demo page:
npm run servenpm run build- Build the library for productionnpm run dev- Build in watch mode for developmentnpm run serve- Serve the dist folder on localhost:8080npm run clean- Clean the dist folder
webcomponent-library/
βββ src/
β βββ components/
β β βββ ck-editable-array/
β β βββ ck-editable-array.ts
β βββ index.ts
βββ dist/
β βββ index.html (demo page)
β βββ ck-editable-array/
β β βββ ck-editable-array.js (UMD build)
β β βββ ck-editable-array.esm.js (ES module build)
β β βββ ck-editable-array.min.js (minified UMD build)
β βββ index.d.ts (TypeScript definitions)
βββ package.json
βββ rollup.config.js
βββ tsconfig.json
βββ README.md
- Create a new component file in
src/components/:
export class MyComponent extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
}
connectedCallback() {
this.render();
}
private render() {
this.shadowRoot!.innerHTML = `
<style>
/* Component styles */
</style>
<div>
<!-- Component template -->
</div>
`;
}
}
// Register the component
if (!customElements.get('my-component')) {
customElements.define('my-component', MyComponent);
}- Export the component in
src/index.ts:
export { MyComponent } from './components/my-component/my-component.component';
import './components/my-component/my-component.component';This project includes comprehensive testing, linting, and formatting setup.
npm run build- Build the project for productionnpm run dev- Start development mode with watchnpm run serve- Serve the built files for testingnpm test- Run testsnpm run test:watch- Run tests in watch modenpm run test:coverage- Run tests with coverage reportnpm run lint- Run ESLintnpm run lint:fix- Run ESLint with auto-fixnpm run format- Format code with Prettiernpm run format:check- Check code formatting
Tests are written using Jest with jsdom environment for DOM testing. Test files are located in __tests__ directories next to the components they test.
# Run all tests
npm test
# Run tests with coverage
npm run test:coverage
# Run tests in watch mode during development
npm run test:watchThe project uses ESLint for code linting and Prettier for code formatting:
# Check for linting issues
npm run lint
# Auto-fix linting issues
npm run lint:fix
# Format code
npm run format
# Check if code is properly formatted
npm run format:check- Create a new directory under
src/components/ - Add your component TypeScript file
- Create a corresponding test directory under
tests/(e.g.,tests/my-component/) - Add test files in the test directory
- Export your component from
src/index.ts - Run tests to ensure everything works
Example component structure:
src/components/my-component/
βββ my-component.ts
tests/my-component/
βββ my-component.test.ts
to GitHub Packages
The project is configured to automatically publish to GitHub Packages when you create a new release:
- Update the version in
package.json:
npm version patch # or minor, major- Push the tag to GitHub:
git push origin --tags- The GitHub Action will automatically build and publish the package.
You can also publish manually:
- Build the project:
npm run build- Make sure you're authenticated with GitHub Packages:
npm login --scope=@colmkenna --registry=https://npm.pkg.github.com- Publish:
npm publishAfter installing, you can use the components in your HTML:
<!DOCTYPE html>
<html>
<head>
<script type="module">
import '@colmkenna/ck-webcomponents';
</script>
</head>
<body>
<ck-editable-array name="GitHub Packages"></ck-editable-array>
</body>
</html>MIT License - see LICENSE file for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
For questions and support, please open an issue on GitHub.