Skip to content

Commit 26d5427

Browse files
Merge pull request #24 from NxtLvLSoftware/dev-to-dist
Merge dev changes to dist
2 parents 369c399 + 05ee7c2 commit 26d5427

16 files changed

Lines changed: 121 additions & 64 deletions

File tree

README.md

Lines changed: 65 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
## About
4949
This package provides support for writing reusable Alpine.js components in Typescript.
5050
Supporting Alpine's existing model of registering any generic object/type and user-defined
51-
classes inheriting from the [AlpineComponent class](./src/Component.ts) which provides type
51+
classes inheriting from the [AlpineComponent class](https://github.com/NxtLvLSoftware/alpine-typescript/blob/dev/src/Component.ts) which provides type
5252
definitions for Alpine's magics. This enables your IDE to give useful auto-completion without
5353
any extra plugins/extensions and gives Typescript a better understanding of your component
5454
code, producing useful compilation/transpilation errors before testing.
@@ -65,10 +65,27 @@ import Alpine from 'alpinejs';
6565

6666
window.Alpine = Alpine;
6767

68-
import { componentPlugin } from '@nxtlvlsoftware/alpine-typescript';
68+
import {componentPlugin} from '@nxtlvlsoftware/alpine-typescript';
6969

70-
Alpine.plugin(componentPlugin);
71-
Alpine.start();
70+
window.addEventListener('alpine-components:init', () => {
71+
// window.Alpine.Components.register();
72+
});
73+
74+
window.Alpine.plugin(componentPlugin);
75+
window.Alpine.start();
76+
```
77+
Using the default export:
78+
```typescript
79+
import Alpine from 'alpinejs';
80+
81+
window.Alpine = Alpine;
82+
83+
window.addEventListener('alpine-components:init', () => {
84+
// window.Alpine.Components.register();
85+
});
86+
87+
window.Alpine.plugin(require('@nxtlvlsoftware/alpine-typescript'));
88+
window.Alpine.start();
7289
```
7390
This is the easiest way to get started in existing projects but doesn't offer a way to modify the
7491
default options provided the package.
@@ -79,16 +96,22 @@ import Alpine from 'alpinejs';
7996

8097
window.Alpine = Alpine;
8198

82-
import { AlpineComponents } from '@nxtlvlsoftware/alpine-typescript';
99+
import {AlpineComponents} from '@nxtlvlsoftware/alpine-typescript';
83100

84-
AlpineComponents.bootstrap();
101+
AlpineComponents.bootstrap({
102+
components: {
103+
//
104+
}});
85105
```
86106
The default options are best suited to new projects and automatically starts Alpine for you.
87107
To integrate into existing projects seamlessly, just pass in an options object:
88108
```typescript
89-
import { AlpineComponents } from '@nxtlvlsoftware/alpine-typescript';
109+
import {AlpineComponents} from '@nxtlvlsoftware/alpine-typescript';
90110

91111
AlpineComponents.bootstrap({
112+
components: {
113+
//
114+
},
92115
startAlpine: false,
93116
logErrors: true // should only enable this in dev enviroments for debugging
94117
}); // pass the Alpine object explicity if you aren't following the default convention
@@ -102,10 +125,13 @@ import Alpine from 'alpinejs';
102125

103126
let myAlpine = Alpine;
104127

105-
import { AlpineComponents } from '@nxtlvlsoftware/alpine-typescript';
128+
import {AlpineComponents} from '@nxtlvlsoftware/alpine-typescript';
106129

107130
const isProduction = () => false; // equivelent would be injected/definied server-side by your framework
108131
AlpineComponents.bootstrap({
132+
components: {
133+
//
134+
},
109135
logErrors: !isProduction()
110136
}, myAlpine);
111137

@@ -122,13 +148,13 @@ This package is designed to be easily integrated into existing projects. You can
122148
migrating complex, logic heavy components into classes without worrying about the simple stuff
123149
that makes Alpine so powerful.
124150

125-
Take a look at the provided example project [here.](./examples/project)
151+
Take a look at the provided example project [here.](https://github.com/NxtLvLSoftware/alpine-typescript/blob/dev/examples/project)
126152

127153
##### Project Setup
128-
You'll want to start by [installing](#installation) the package then add these lines to your
154+
You'll want to start by [installing](https://github.com/NxtLvLSoftware/alpine-typescript/blob/dev/README.md#installation) the package then add these lines to your
129155
client script:
130156
```typescript
131-
import { AlpineComponents } from '@nxtlvlsoftware/alpine-typescript';
157+
import {AlpineComponents} from '@nxtlvlsoftware/alpine-typescript';
132158

133159
AlpineComponents.bootstrap({
134160
components: {
@@ -142,9 +168,9 @@ AlpineComponents.bootstrap({
142168
Now create a directory for your components, something like `components` and create a new
143169
Typescript file `components/MyComponent.ts` containing:
144170
```typescript
145-
import { AlpineComponent } from "@nxtlvlsoftware/alpine-typescript";
171+
import {AlpineComponent} from "@nxtlvlsoftware/alpine-typescript";
146172

147-
export class MyComponent extends AlpineComponent<MyComponent> {
173+
export class MyComponent extends AlpineComponent {
148174

149175
constructor(
150176
public required: string,
@@ -161,7 +187,7 @@ export class MyComponent extends AlpineComponent<MyComponent> {
161187
```
162188
Register your new component in the `AlpineComponents.bootstrap()` method call:
163189
```typescript
164-
import { MyComponent } from './components/MyComponent';
190+
import {MyComponent} from './components/MyComponent';
165191

166192
AlpineComponents.bootstrap({
167193
components: {
@@ -174,10 +200,10 @@ You can now use the component in your HTML with `x-data="myComponent('Hello Worl
174200
#### In Packages
175201
The main purpose of this package is to easily distribute components in their own package.
176202

177-
Take a look at the provided example components package [here.](./examples/package)
203+
Take a look at the provided example components package [here.](https://github.com/NxtLvLSoftware/alpine-typescript/blob/dev/examples/package)
178204

179205
##### Package Setup
180-
The easiest way to start distributing your components is to copy everything from the [package example](./examples/package)
206+
The easiest way to start distributing your components is to copy everything from the [package example](https://github.com/NxtLvLSoftware/alpine-typescript/blob/dev/examples/package)
181207
directory and start writing your components.
182208

183209
Manual setup requires a project with `typescript` installed to compile the code to javascript:
@@ -288,13 +314,12 @@ export namespace MyComponents {
288314

289315
// Support loading our components with a standardized call to Alpine.plugin().
290316
export function myPlugin(alpine: Globals.Alpine): void {
291-
MyComponents.bootstrap(
292-
{
293-
// can't assume we're the only ones using the component library
294-
bootstrapComponents: false,
295-
// definitely not the only ones using alpine if we're being used as a plugin here
296-
startAlpine: false
297-
}, alpine);
317+
MyComponents.bootstrap({
318+
// can't assume we're the only ones using the component library
319+
bootstrapComponents: false,
320+
// definitely not the only ones using alpine if we're being used as a plugin here
321+
startAlpine: false
322+
}, alpine);
298323
}
299324
```
300325
Export the `MyComponent` namespace and `myPlugin()` function as default from `index.ts`:
@@ -307,7 +332,7 @@ export {
307332
/**
308333
* Alpine plugin as default export.
309334
*/
310-
import { myPlugin } from './src/Plugin';
335+
import {myPlugin} from './src/Plugin';
311336
export default myPlugin;
312337
```
313338
Now you can start writing your components. Remember to create the `src/components/MyComponent.ts`
@@ -325,7 +350,7 @@ Any function that satisfies this type requirement:
325350
```typescript
326351
type KnownGenericConstructor<T> = (...args: any[]) => T;
327352
```
328-
Will be accepted as a component constructor. The [signature](./src/Store.ts#L101) for registering components of this type is:
353+
Will be accepted as a component constructor. The [signature](https://github.com/NxtLvLSoftware/alpine-typescript/blob/dev/src/Store.ts#L101) for registering components of this type is:
329354
```typescript
330355
/**
331356
* Register a generic object (alpine data) as a component.
@@ -364,16 +389,21 @@ Or by listening for the `alpine-components:init` on the `document` global:
364389
document.addEventListener('alpine-components:init', () => {
365390
window.Alpine.Components.register('noArgs', noArgsComponent);
366391
window.Alpine.Components.register('singleValue', singleValueComponent);
392+
// or registerAll() when component names are hardcoded
393+
window.Alpine.Components.registerAll({
394+
noArgs: noArgsComponent,
395+
singleValue: singleValueComponent
396+
});
367397
});
368398
```
369399

370400
##### Typescript Classes
371401
The main purpose of this package is to use class definitions as Alpine components. Any class
372-
inheriting from [AlpineComponent](./src/Component#L45) is accepted.
402+
inheriting from [AlpineComponent](https://github.com/NxtLvLSoftware/alpine-typescript/blob/dev/src/Component#L45) is accepted.
373403

374404
Both register function definitions accept constructor functions for these classes.
375405

376-
Explicit register class constructor [signature](src/Store.ts#L109):
406+
Explicit register class constructor [signature](https://github.com/NxtLvLSoftware/alpine-typescript/blob/dev/src/Store.ts#L109):
377407
```typescript
378408
/**
379409
* Register a class inheriting from {@link Impl.AlpineComponent} as a component.
@@ -385,7 +415,7 @@ register<T extends Impl.AlpineComponent>(component: Impl.KnownClassConstructor<T
385415
```
386416
If no name is provided it will fall back to the name of the class (prototype name.)
387417
388-
You can also register classes with the generic [signature](./src/Store.ts#L101):
418+
You can also register classes with the generic [signature](https://github.com/NxtLvLSoftware/alpine-typescript/blob/dev/src/Store.ts#L101):
389419
```typescript
390420
/**
391421
* Register a generic object (alpine data) as a component.
@@ -395,7 +425,7 @@ You can also register classes with the generic [signature](./src/Store.ts#L101):
395425
*/
396426
register<T>(name: string, component: Impl.KnownConstructor<T>): void;
397427
```
398-
The component parameters prototype is checked for inheritance from the [AlpineComponent](./src/Component#L45)
428+
The component parameters prototype is checked for inheritance from the [AlpineComponent](https://github.com/NxtLvLSoftware/alpine-typescript/blob/dev/src/Component#L45)
399429
class and handled accordingly.
400430
401431
#### Using Components
@@ -417,21 +447,20 @@ Here's the contrived `dropdown` example re-written to use a class:
417447

418448
window.Alpine = Alpine;
419449

420-
import { AlpineComponents, AlpineComponent } from '@nxtlvlsoftware/alpine-typescript';
450+
import {AlpineComponents, AlpineComponent} from '@nxtlvlsoftware/alpine-typescript';
421451

422452
class ToggleComponent extends AlpineComponent {
423453
constructor(
424-
open: boolean = false
454+
public open: boolean = false
425455
) { super(); }
426456

427-
toggle() { this.open = !open; }
457+
toggle(): void { this.open = !this.open; }
428458
}
429459

430-
window.addEventListener('alpine-components:init', () => {
431-
window.Alpine.Components.register(ToggleComponent, 'toggle');
432-
});
433-
434460
AlpineComponents.bootstrap({
461+
components: {
462+
toggle: ToggleComponent
463+
},
435464
logErrors: true
436465
});
437466
</script>

examples/package/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* Export functions and types.
33
*/
44

5-
export { AlertComponent } from './src/components/AlertComponent';
6-
export { ToggleComponent } from './src/components/ToggleComponent';
5+
export {AlertComponent} from './src/components/AlertComponent';
6+
export {ToggleComponent} from './src/components/ToggleComponent';
77

88
export {
99
MyComponents,
@@ -14,5 +14,5 @@ export {
1414
/**
1515
* Alpine plugin as default export.
1616
*/
17-
import { myPlugin } from './src/Plugin';
17+
import {myPlugin} from './src/Plugin';
1818
export default myPlugin;

examples/package/src/Plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export namespace MyComponents {
9292
if (opts.bootstrapComponents) {
9393
AlpineComponents.bootstrap(opts, alpine);
9494
}
95-
};
95+
}
9696

9797
}
9898

examples/package/src/components/AlertComponent.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AlpineComponent } from "@nxtlvlsoftware/alpine-typescript";
1+
import {AlpineComponent} from "@nxtlvlsoftware/alpine-typescript";
22

33
export const DefaultAlertComponentName = 'alert';
44

@@ -8,7 +8,7 @@ export const DefaultAlertComponentName = 'alert';
88
* All properties and events are prefixed with 'alert' to make
99
* it clear which variables belong to the component in the HTML.
1010
*/
11-
export class AlertComponent extends AlpineComponent<AlertComponent> {
11+
export class AlertComponent extends AlpineComponent {
1212

1313
constructor(
1414
public alertState: boolean = false

examples/package/src/components/ToggleComponent.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AlpineComponent } from '@nxtlvlsoftware/alpine-typescript';
1+
import {AlpineComponent} from '@nxtlvlsoftware/alpine-typescript';
22

33
export const DefaultToggleComponentName: string = 'toggle';
44

@@ -8,7 +8,7 @@ export const DefaultToggleComponentName: string = 'toggle';
88
* All properties and events are prefixed with 'switch' to make
99
* it clear which variables belong to the component in the HTML.
1010
*/
11-
export class ToggleComponent extends AlpineComponent<ToggleComponent> {
11+
export class ToggleComponent extends AlpineComponent {
1212

1313
constructor(
1414
public toggleState: boolean = false

examples/project/src/components/AlertComponent.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { AlpineComponent } from "@nxtlvlsoftware/alpine-typescript";
1+
import {AlpineComponent} from "@nxtlvlsoftware/alpine-typescript";
22

33
/**
44
* Simple alert component for closing the default alert.
55
*
66
* All properties and events are prefixed with 'alert' to make
77
* it clear which variables belong to the component in the HTML.
88
*/
9-
export class AlertComponent extends AlpineComponent<AlertComponent> {
9+
export class AlertComponent extends AlpineComponent {
1010

1111
constructor(
1212
public alertState: boolean = false

examples/project/src/components/ToggleComponent.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { AlpineComponent } from "@nxtlvlsoftware/alpine-typescript";
1+
import {AlpineComponent} from "@nxtlvlsoftware/alpine-typescript";
22

33
/**
44
* Simple toggle that switches between on/off values.
55
*
66
* All properties and events are prefixed with 'toggle' to make
77
* it clear which variables belong to the component in the HTML.
88
*/
9-
export class ToggleComponent extends AlpineComponent<ToggleComponent> {
9+
export class ToggleComponent extends AlpineComponent {
1010

1111
constructor(
1212
public toggleState: boolean = false

examples/project/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import Alpine from 'alpinejs';
44

55
window.Alpine = Alpine;
66

7-
import { AlpineComponents } from '@nxtlvlsoftware/alpine-typescript';
7+
import {AlpineComponents} from '@nxtlvlsoftware/alpine-typescript';
88

99
import {AlertComponent} from "./components/AlertComponent";
10-
import { ToggleComponent } from "./components/ToggleComponent";
10+
import {ToggleComponent} from "./components/ToggleComponent";
1111

1212
AlpineComponents.bootstrap({
1313
components: {

index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ export {
2929
/**
3030
* Alpine plugin as default export.
3131
*/
32-
import { componentsPlugin } from './src/Plugin';
32+
import {componentsPlugin} from './src/Plugin';
3333
export default componentsPlugin;

0 commit comments

Comments
 (0)