Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/archtechx/alpine-typescript
TypeScript support for Alpine.js
https://github.com/archtechx/alpine-typescript
Last synced: about 12 hours ago
JSON representation
TypeScript support for Alpine.js
- Host: GitHub
- URL: https://github.com/archtechx/alpine-typescript
- Owner: archtechx
- License: mit
- Created: 2021-02-26T13:44:03.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-10-13T09:15:43.000Z (about 2 years ago)
- Last Synced: 2024-11-06T17:45:33.954Z (7 days ago)
- Language: TypeScript
- Size: 43 KB
- Stars: 53
- Watchers: 2
- Forks: 3
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# TypeScript support for Alpine.js v2
This package provides full support for class components in Alpine.js using a thin TypeScript layer.
It's used like this:
**Register a component**
```ts
import DarkModeToggle from './darkModeToggle';Alpine.component('darkModeToggle', DarkModeToggle);
```**Use it in a template**
```html
Switch theme
```## Installation
```
npm install --save-dev @leanadmin/alpine-typescript
```The package will automatically initialize itself when needed, i.e. when one of its components is used in the currently executed JS bundle.
If you'd like to initialize it manually, you can use:
```ts
import { bootstrap } from '@leanadmin/alpine-typescript';bootstrap();
```Additionally, you can use the `addTitles()` function to add `x-title` attributes to all components. The class name of each component will be used as its title. This is useful if you're using tools like the [Alpine.js devtools](https://github.com/alpine-collective/alpinejs-devtools).
```ts
import { addTitles } from '@leanadmin/alpine-typescript';addTitles();
```## Usage
You can use a component by calling `Alpine.component('componentName')(arg1, arg2)`. If your component has no arguments, still append `()` at the end of the call.
The `component()` call itself returns a function for creating instances of the component. Invoking the function ensures that the component has a unique instance each time.
```html
Switch theme
``````html
...
```## Creating components
To create a component, you need to create the component object and register it using one of the provided helpers.
Component objects can be:
- classes
- functions returning plain objectsIn the context of plain objects, the wrapper function acts as a constructor that can pass initial data to the object.
## Registering components
A component can be registered like this:
```ts
import ExampleComponent from './ExampleComponent';
import { component } from '@leanadmin/alpine-typescript';component('example', ExampleComponent);
```Which will make it accessible using `Alpine.component('example')('foo', 'bar')`.
**Note: You may notice that `Alpine.component()` can also be used to register components. However, it's better to avoid using it.** The reason for this is that `window.Alpine` might not yet be accessible when you're registering components, and if it is, it's possible that it's already evaluated some of the `x-data` attributes. `component()` is guaranteed to work. And of course, you can alias the import if you wish to use a different name.
To register multiple components, you can use the `registerComponents()` helper.
This can pair well with scripts that crawl your e.g. `alpine/` directory to register all components using their file names.
```ts
// alpine/index.jsimport { registerComponents } from '@leanadmin/alpine-typescript';
const files = require.context('./', true, /.*.ts/)
.keys()
.map(file => file.substr(2, file.length - 5)) // Remove ./ and .ts
.filter(file => file !== 'index')
.reduce((files: { [name: string]: Function }, file: string) => {
files[file] = require(`./${file}.ts`).default;return files;
}, {});registerComponents(files);
```## Class components
You can create class components by extending `AlpineComponent` and exporting the class as `default`.
`AlpineComponent` provides full IDE support for Alpine's magic properties. This means that you can use `this.$el`, `this.$nextTick(() => this.foo = this.bar)`, and more with perfect type enforcement.
```ts
import { AlpineComponent } from '@leanadmin/alpine-typescript';export default class DarkModeToggle extends AlpineComponent {
public theme: string|null = null;/** Used for determining the transition direction. */
public previousTheme: string|null = null;public browserTheme(): string {
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
}public switchTheme(theme: string): void {
this.$nextTick(() => this.previousTheme = this.theme);this.theme = theme;
window.localStorage.setItem('leanTheme', theme);
this.updateDocumentClass(theme);
}// ...
public init(): void {
this.loadStoredTheme();
this.registerListener();
}
}
```## Plain object components
To register a plain object as an Alpine component, export a function that wraps the object like this:
```ts
export default (foo: string, bar: number) => ({
foo,
bar,someFunction() {
console.log(this.foo);
}
})
```The function will serve as a "constructor" for the object, setting default values and anything else that's needed.
Note that the `=> ({` part is just syntactic sugar, you're free to use `return` if it's useful in your case:
```ts
export default (foo: string, bar: number) => {
return {
foo,
bar,// ...
}
}
```# Real-world example
Here's a practical example that uses constructors, `init()`, refs, events, and includes dependencies the right way.
This example uses the Alpine component that we use for search on the [Lean documentation site](https://lean-admin.dev).
resources/js/app.ts
```ts
import { Alpine, component } from '@leanadmin/alpine-typescript';declare global {
interface Window {
Alpine: Alpine;
}
}import Search from './search';
component('search', Search);
import 'alpinejs';
```**`app.ts` highlights:**
- It's a good idea to declare the `Alpine` property on `Window` in case you need to use `window.Alpine`. The library provides an interface for this.
- We initialize each component by calling `component()`
- We import Alpine *after* this packageresources/js/search.ts
```ts
import { AlpineComponent } from '@leanadmin/alpine-typescript';type AlgoliaIndex = {
search: Function,
};type Result = any;
export default class Search extends AlpineComponent {
search: string = '';
results: Result[] = [];constructor(
public index: AlgoliaIndex,
) {
super();
}previousResult(): void {
let result = this.currentResult();if (! result) {
if (this.results.length) {
// First result
this.getResult(0).focus();
} else if (this.search.length) {
// Re-fetch results
this.queryAlgolia();
}return;
}if (result.previousElementSibling instanceof HTMLElement && result.previousElementSibling.tagName === 'A') {
(result.previousElementSibling).focus();
} else {
// Last result
this.getResult(this.results.length - 1).focus();
}
};nextResult(): void {
let result = this.currentResult();if (! result) {
if (this.results.length) {
// First result
this.getResult(0).focus();
} else if (this.search.length) {
// Re-fetch results
this.queryAlgolia();
}return;
}if (result.nextElementSibling instanceof HTMLElement) {
result.nextElementSibling.focus();
} else {
// First result
this.getResult(0).focus();
}
};getResult(index: number): HTMLElement {
return this.$refs.results.children[index + 1] as HTMLElement;
};currentResult(): HTMLElement|null {
if (! this.$refs.results.contains(document.activeElement)) {
return null;
}return document.activeElement as HTMLElement;
};queryAlgolia(): void {
if (this.search) {
this.index.search(this.search, {
hitsPerPage: 3,
}).then(({ hits }) => {
this.results = hits.filter((hit: Result) => {
// Remove duplicate results
const occurances: any[] = hits.filter((h: Result) => h.hierarchy.lvl1 === hit.hierarchy.lvl1);return occurances.length === 1;
});this.results.forEach((result: Result) => {
// Clean displayed text
if (result._highlightResult && result._highlightResult.content) {
return result._highlightResult.content.value.replace(' ', '');
}
});if (this.results.length) {
this.$nextTick(() => this.getResult(0).focus());
}
})
} else {
this.results = [];this.$refs.search.focus();
}
};init(): void {
this.$watch('search', () => this.queryAlgolia());
}
}
```**`search.ts` highlights:**
- We `export default` the class
- We have to call `super()` if we define a constructor
- Sometimes, we have to use `as HTMLElement` because the DOM API can return `Element` which doesn't have methods like `focus()`
- We define an `init()` method and can access magic properties there
- We create helper types for consistency among parameters and return types, even if the type is `any` because we don't know much about the structure. Especially useful for API calls.page.blade.php
```html
...
```**`page.blade.php` highlights:**
- We call the component in `x-data`
- We use both the constructor and `init`. The constructor cannot access magic properties, `init` can.
- We can still use Alpine syntax in the template with no issues