|
1 | | -# Web Component Starter Template |
| 1 | +# table-sortable Web Component |
2 | 2 |
|
3 | | -A comprehensive, production-ready starter template for creating Web Components. This template is based on the architecture and best practices from my web components work. |
| 3 | +[](https://www.npmjs.com/package/@aarongustafson/table-sortable) [](https://github.com/aarongustafson/table-sortable/actions) |
4 | 4 |
|
5 | | -## ✨ Features |
| 5 | +A web component to enable users to sort the data in a table based on table cell values. |
6 | 6 |
|
7 | | -- **Modern Tooling**: Vitest, ESLint, Prettier, Happy DOM |
8 | | -- **Best Practices**: Shadow DOM, Custom Elements v1, proper encapsulation |
9 | | -- **Multiple Import Options**: Auto-define, manual definition, or both |
10 | | -- **Testing**: Comprehensive test setup with coverage reporting |
11 | | -- **CI/CD**: GitHub Actions workflows included |
12 | | -- **Developer Experience**: Demo page, interactive setup, extensive documentation |
13 | | -- **Publishing Ready**: npm package configuration and automated publishing workflow |
| 7 | +## Demo |
14 | 8 |
|
15 | | -## 🚀 Quick Start |
| 9 | +[Live Demo](https://aarongustafson.github.io/table-sortable/demo/) ([Source](./demo/index.html)) |
16 | 10 |
|
17 | | -### Use This Template |
18 | | - |
19 | | -1. Click "Use this template" on GitHub, or: |
20 | | - |
21 | | -```bash |
22 | | -git clone https://github.com/aarongustafson/web-component-starter.git my-component |
23 | | -cd my-component |
24 | | -``` |
25 | | - |
26 | | -2. Run the interactive setup: |
| 11 | +## Installation |
27 | 12 |
|
28 | 13 | ```bash |
29 | | -npm install |
30 | | -npm run setup |
| 14 | +npm install @aarongustafson/table-sortable |
31 | 15 | ``` |
32 | 16 |
|
33 | | -The setup wizard will: |
34 | | -- Ask for your component name (e.g., `my-awesome-component`) |
35 | | -- Ask for a description |
36 | | -- Rename all files automatically |
37 | | -- Replace all placeholders in code and configuration |
38 | | -- **Generate your component's README from template** |
39 | | -- **Clean up template setup files** (SETUP.md, README.tpl, scripts/) |
40 | | -- Install dependencies |
41 | | -- Initialize git repository |
| 17 | +## Usage |
42 | 18 |
|
43 | | -### Manual Setup |
| 19 | +### Option 1: Auto-define the custom element (easiest) |
44 | 20 |
|
45 | | -If you prefer manual setup, see [SETUP.md](SETUP.md) for detailed instructions. |
| 21 | +Import the package to automatically define the `<table-sortable>` custom element: |
46 | 22 |
|
47 | | -## 📁 What's Included |
48 | | - |
49 | | -``` |
50 | | -web-component-starter/ |
51 | | -├── COMPONENT-NAME.js # Component implementation |
52 | | -├── index.js # Main entry (class + auto-define) |
53 | | -├── define.js # Auto-define only |
54 | | -├── custom-elements.json # Custom Elements Manifest |
55 | | -├── package.json # Package config with scripts |
56 | | -├── LICENSE # MIT License |
57 | | -├── README.md # This file (replaced after setup) |
58 | | -├── README.tpl # Template for your component's README |
59 | | -├── .gitignore # Git ignore |
60 | | -├── .npmignore # npm ignore |
61 | | -├── .prettierrc # Prettier config |
62 | | -├── .editorconfig # Editor config |
63 | | -├── eslint.config.js # ESLint config |
64 | | -├── vitest.config.js # Vitest config |
65 | | -├── .github/ |
66 | | -│ ├── workflows/ |
67 | | -│ │ ├── ci.yml # Continuous integration |
68 | | -│ │ └── publish.yml # Auto-publish to npm |
69 | | -│ └── ISSUE_TEMPLATE/ # Bug & feature templates |
70 | | -├── scripts/ |
71 | | -│ └── setup.js # Interactive setup wizard (removed after setup) |
72 | | -├── test/ |
73 | | -│ ├── setup.js # Test configuration |
74 | | -│ └── COMPONENT-NAME.test.js # Test suite |
75 | | -├── demo/ |
76 | | -│ └── index.html # Live demo page |
77 | | -├── SETUP.md # Manual setup guide (removed after setup) |
78 | | -└── CONTRIBUTING.md # Contribution guidelines |
| 23 | +```javascript |
| 24 | +import '@aarongustafson/table-sortable'; |
79 | 25 | ``` |
80 | 26 |
|
81 | | -## 🛠️ Development |
82 | | - |
83 | | -### Available Scripts |
| 27 | +Or use the define-only script in HTML: |
84 | 28 |
|
85 | | -```bash |
86 | | -npm run setup # Interactive setup wizard |
87 | | -npm test # Run tests in watch mode |
88 | | -npm run test:run # Run tests once |
89 | | -npm run test:ui # Open Vitest UI |
90 | | -npm run test:coverage # Generate coverage report |
91 | | -npm run lint # Lint with ESLint + Prettier |
92 | | -npm run format # Auto-fix linting issues |
| 29 | +```html |
| 30 | +<script src="./node_modules/@aarongustafson/table-sortable/define.js" type="module"></script> |
93 | 31 | ``` |
94 | 32 |
|
95 | | -### Component Architecture |
96 | | - |
97 | | -This template provides three flexible import options: |
| 33 | +### Option 2: Import the class and define manually |
98 | 34 |
|
99 | | -**Option 1: Auto-define (easiest)** |
100 | | -```javascript |
101 | | -import '@yourscope/component-name'; |
102 | | -// Element is automatically registered |
103 | | -``` |
| 35 | +Import the class and define the custom element with your preferred tag name: |
104 | 36 |
|
105 | | -**Option 2: Manual registration** |
106 | 37 | ```javascript |
107 | | -import { ComponentNameElement } from '@yourscope/component-name/component-name.js'; |
108 | | -customElements.define('my-custom-name', ComponentNameElement); |
109 | | -``` |
| 38 | +import { TableSortableElement } from '@aarongustafson/table-sortable/table-sortable.js'; |
110 | 39 |
|
111 | | -**Option 3: Both** |
112 | | -```javascript |
113 | | -import { ComponentNameElement } from '@yourscope/component-name'; |
114 | | -// Element is registered AND class is available for extension |
| 40 | +customElements.define('my-custom-name', TableSortableElement); |
115 | 41 | ``` |
116 | 42 |
|
117 | | -## 🧪 Testing |
| 43 | +### Basic Example |
118 | 44 |
|
119 | | -Includes: |
120 | | -- **Vitest**: Fast, modern test runner |
121 | | -- **Happy DOM**: Lightweight browser environment |
122 | | -- **Testing Library**: DOM testing utilities |
123 | | -- **Coverage**: V8 coverage reporting |
124 | | -- **UI**: Interactive test debugging |
125 | | - |
126 | | -Example: |
127 | | -```javascript |
128 | | -import { describe, it, expect } from 'vitest'; |
129 | | - |
130 | | -describe('MyComponent', () => { |
131 | | - it('should render', () => { |
132 | | - const el = document.createElement('my-component'); |
133 | | - expect(el).toBeInstanceOf(HTMLElement); |
134 | | - }); |
135 | | -}); |
| 45 | +```html |
| 46 | +<table-sortable> |
| 47 | + <!-- Your content here --> |
| 48 | +</table-sortable> |
136 | 49 | ``` |
137 | 50 |
|
138 | | -## 📦 Publishing |
| 51 | +## Attributes |
139 | 52 |
|
140 | | -### Setup Automated Publishing with OIDC (Recommended) |
| 53 | +| Attribute | Type | Default | Description | |
| 54 | +|-----------|------|---------|-------------| |
| 55 | +| `example-attribute` | `string` | `""` | Description of the attribute | |
141 | 56 |
|
142 | | -This template is configured to publish to npm using OpenID Connect (OIDC), which is more secure than using long-lived NPM tokens. |
| 57 | +## Events |
143 | 58 |
|
144 | | -**Initial Setup:** |
| 59 | +The component fires custom events that you can listen to: |
145 | 60 |
|
146 | | -1. Publish your package to npm manually the first time: |
147 | | - ```bash |
148 | | - npm run test:run # Ensure tests pass |
149 | | - npm run lint # Ensure code is clean |
150 | | - npm publish # First publish must be manual |
151 | | - ``` |
| 61 | +| Event | Description | Detail | |
| 62 | +|-------|-------------|--------| |
| 63 | +| `table-sortable:event` | Fired when something happens | `{ data }` | |
152 | 64 |
|
153 | | -2. Configure OIDC on npm: |
154 | | - - Visit your package's access page: `https://www.npmjs.com/package/@yourscope/your-component-name/access` |
155 | | - - Under "Publishing Access", click "Configure OIDC" |
156 | | - - Add GitHub Actions as a trusted publisher with these settings: |
157 | | - - **Provider**: GitHub |
158 | | - - **Organization/Username**: Your GitHub username or organization |
159 | | - - **Repository**: Your repository name |
160 | | - - **Workflow**: `.github/workflows/publish.yml` |
161 | | - - **Environment**: Leave blank (unless you use GitHub environments) |
| 65 | +### Example Event Handling |
162 | 66 |
|
163 | | -3. Create a GitHub release to trigger automated publishing: |
164 | | - - Use `npm version` to update version and create a tag: `npm version patch` (or `minor`/`major`) |
165 | | - - Push with tags: `git push --follow-tags` |
166 | | - - Or create a release through GitHub's UI |
| 67 | +```javascript |
| 68 | +const element = document.querySelector('table-sortable'); |
167 | 69 |
|
168 | | -The GitHub Actions workflow (`.github/workflows/publish.yml`) will automatically publish to npm when you create a new version tag. |
| 70 | +element.addEventListener('table-sortable:event', (event) => { |
| 71 | + console.log('Event fired:', event.detail); |
| 72 | +}); |
| 73 | +``` |
169 | 74 |
|
170 | | -### Manual Publishing |
| 75 | +## CSS Custom Properties |
171 | 76 |
|
172 | | -If you prefer to publish manually without automation: |
| 77 | +| Property | Default | Description | |
| 78 | +|----------|---------|-------------| |
| 79 | +| `--example-color` | `#000` | Example color property | |
173 | 80 |
|
174 | | -```bash |
175 | | -npm run test:run # Ensure tests pass |
176 | | -npm run lint # Ensure code is clean |
177 | | -npm publish # Publish to npm |
| 81 | +### Example Styling |
| 82 | + |
| 83 | +```css |
| 84 | +table-sortable { |
| 85 | + --example-color: #ff0000; |
| 86 | +} |
178 | 87 | ``` |
179 | 88 |
|
180 | | -## 🌐 Browser Support |
| 89 | +## Browser Support |
181 | 90 |
|
182 | | -Works in all modern browsers supporting: |
| 91 | +This component uses modern web standards: |
183 | 92 | - Custom Elements v1 |
184 | 93 | - Shadow DOM v1 |
185 | 94 | - ES Modules |
186 | 95 |
|
187 | | -For legacy browsers, use polyfills. |
188 | | - |
189 | | -## 📚 Documentation |
190 | | - |
191 | | -- [SETUP.md](SETUP.md) - Detailed setup instructions (removed after setup) |
192 | | -- [CONTRIBUTING.md](CONTRIBUTING.md) - Contribution guidelines |
193 | | -- [LICENSE](LICENSE) - MIT License |
194 | | - |
195 | | -## 🎯 Use Cases |
| 96 | +For older browsers, you may need polyfills. |
196 | 97 |
|
197 | | -Perfect for: |
198 | | -- Reusable UI components |
199 | | -- Design system elements |
200 | | -- Form controls and widgets |
201 | | -- Interactive content blocks |
202 | | -- Accessibility-enhanced components |
| 98 | +## Development |
203 | 99 |
|
204 | | -## 🙏 Credits |
| 100 | +```bash |
| 101 | +# Install dependencies |
| 102 | +npm install |
205 | 103 |
|
206 | | -Based on best practices from: |
207 | | -- [form-obfuscator](https://github.com/aarongustafson/form-obfuscator) by Aaron Gustafson |
208 | | -- [Open Web Components](https://open-wc.org/) |
| 104 | +# Run tests |
| 105 | +npm test |
209 | 106 |
|
210 | | -## 📄 License |
| 107 | +# Run tests with coverage |
| 108 | +npm run test:coverage |
211 | 109 |
|
212 | | -MIT - See [LICENSE](LICENSE) |
| 110 | +# Lint code |
| 111 | +npm run lint |
213 | 112 |
|
214 | | -## 🤝 Contributing |
| 113 | +# Format code |
| 114 | +npm run format |
215 | 115 |
|
216 | | -Contributions welcome! See [CONTRIBUTING.md](CONTRIBUTING.md) |
| 116 | +# View demo |
| 117 | +open demo/index.html |
| 118 | +``` |
217 | 119 |
|
218 | | ---- |
| 120 | +## License |
219 | 121 |
|
220 | | -**Ready to build your web component?** Run `npm run setup` to get started! 🚀 |
| 122 | +MIT © [Aaron Gustafson](https://www.aaron-gustafson.com/) |
0 commit comments