Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/markmead/alpinejs-component

Reusable HTML components powered by Alpine JS reactivity 🛸
https://github.com/markmead/alpinejs-component

alpinejs alpinejs-data alpinejs-plugin javascript-components

Last synced: about 2 months ago
JSON representation

Reusable HTML components powered by Alpine JS reactivity 🛸

Awesome Lists containing this project

README

        

# Alpine JS Component

Reusable HTML components powered by Alpine JS reactivity 🛸

## Install

### With a CDN

```html

```

### With a Package Manager

```shell
npm install -D alpinejs-component

yarn add -D alpinejs-component
```

```js
import Alpine from 'alpinejs'
import component from 'alpinejs-component'

Alpine.plugin(component)

Alpine.start()
```

## Example

### On Page Components

You can render on page components by using a `` with an `id` that
matches the `template` attribute on the component.

Here we are rendering the component HTML found in ``
element.

```html













  • ```

    ### Global Components

    If you don't want on page components you can use the `url` attribute which
    accepts a path to the HTML component.

    Here we are telling Alpine JS to fetch the HTML from `/public/person.html`
    within the codebase.

    ```html








    ```

    Then we'd have a file `/public/person.html` which could look like this.

    ```html








  • ```

    ## Dynamic Templates

    You can pass `template` or `url` as a dynamic value, here's an example.

    ```html




      // Or





    ```

    ## Styling Components

    ### Including Stylesheets

    You can use `styles` attribute to specify which stylesheets to include.

    ```html

    /* ... */

    ```

    You can also include multiple stylesheets by separating them with a comma.

    ```html

    /* ... */

    /* ... */

    ```

    Or, if you want to include all stylesheets you can use `styles="global"`

    ### Inline Stylesheet

    You can add a `` element with the components CSS to the component itself.

    ```html
    <div>
    <style>
    .example {
    background: #00f;
    }

    ```

    ## Renaming Component

    If you need to change the name `x-component`, you can do so by setting the
    global `xComponent` object. This is necessary because blade components start
    with `x-`, which can cause conflicts.

    ```js
    window.xComponent = {
    name: 'a-component',
    }
    ```

    You will then call components like this:

    ```html








    ```

    ### Stats

    ![](https://img.shields.io/bundlephobia/min/alpinejs-component)
    ![](https://img.shields.io/npm/v/alpinejs-component)
    ![](https://img.shields.io/npm/dt/alpinejs-component)
    ![](https://img.shields.io/github/license/markmead/alpinejs-component)