# spa-vanilla

> a library to create spa's using only vanilla js

Latest version **1.0.0** (published 2022-12-16) · 0 weekly downloads

## Install

```sh
npm install spa-vanilla
pnpm add spa-vanilla
yarn add spa-vanilla
bun add spa-vanilla
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2022-12-16 |
| First published | 2022-12-16 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 31.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Pedro henrique |
| Maintainers | pedrodev |
| Keywords | spa, vanilla, vanilla js, router, component, state |

## Links

- npm: https://www.npmjs.com/package/spa-vanilla
- Repository: https://github.com/PedroHenrique-git/spa-vanilla
- Homepage: https://github.com/PedroHenrique-git/spa-vanilla#readme
- Issues: https://github.com/PedroHenrique-git/spa-vanilla/issues
- npm.io page: https://npm.io/package/spa-vanilla

## Dependencies (1)

- [path-to-regexp](https://npm.io/package/path-to-regexp.md) ^6.2.1

## Alternatives

- [express-promise-router](https://npm.io/package/express-promise-router.md) — 736.1K weekly downloads
- [next-usequerystate](https://npm.io/package/next-usequerystate.md) — 29.8K weekly downloads
- [@bitkyc08/opencodex](https://npm.io/package/@bitkyc08/opencodex.md) — 4.6K weekly downloads
- [lynkr](https://npm.io/package/lynkr.md) — 575 weekly downloads
- [baremetal.js](https://npm.io/package/baremetal.js.md) — 42 weekly downloads

## Recent versions

- 1.0.0 (latest) — 2022-12-16

## README

## Spa vanilla

spa vanilla it's a library to create spa's (single page application) using only vanilla js

## List of main components

- [Component](#component)
- [Router](#router)
- [State](#state)

## Utility functions

- [changeRoute](#changeRoute)
- [createApp](#createApp)
- [createContainer](#createContainer)
- [createFragment](#createFragment)
- [dispatchNavigationEvent](#dispatchNavigationEvent)
- [getMatchRoute](#getMatchRoute)
- [getRegexRoute](#getRegexRoute)
- [removeAllNodes](#removeAllNodes)

## Main components

### Component

The abstract class Component is the basis for any lib component, a component must 
have a render method that returns an html string and 
also a single parent element.

~~~typescript
    import { Component } from '../main';

    export class MockComponent extends Component<{ count: number }, { title: string }> {
        private buttonIncrease: HTMLButtonElement | null = null;
        private buttonDecrease: HTMLButtonElement | null = null;

        selectors(): void {
            this.buttonIncrease = this.root.querySelector(
            `#button-increase-${this.key}`,
            );
            this.buttonDecrease = this.root.querySelector(
            `#button-decrease-${this.key}`,
            );
        }

        events(): void {
            this.buttonIncrease?.addEventListener('click', () => {
            this.setState({ count: (this.getState()?.count ?? 0) + 1 });
            });

            this.buttonDecrease?.addEventListener('click', () => {
            this.setState({ count: (this.getState()?.count ?? 0) - 1 });
            });
        }

        render(): string {
            return String.raw`
                    <div data-testid='${this.key}'>
                        <div>
                            <h1 data-testid='mock-title-${this.key}'>${this.getProps()?.title ?? 'Mock component'}</h1>
                            <div>
                                <div data-testid='mock-visor-${this.key}'>${this.getState() ? this.getState()?.count : 'without state'}</div>
                            </div>
                            <div>
                                <button id='button-increase-${this.key}' data-testid='button-increase-${this.key}'>increase</button>
                                <button id='button-decrease-${this.key}' data-testid='button-decrease-${this.key}'>decrease</button>
                            </div>
                        </div>
                    </div>
                `;
        }
    }    
~~~

### Router

the Router class represents the routes that the application will take, 
or each route it is necessary to define a page (a function) 
that receives the route data as a parameter.

~~~typescript
    new Router(
        {
            '/': (routeData) => {},
            '/page/:id': (routeData) => {},
        },
        app,
    );
~~~

### State

the State class represents a dataset that can have subscribers 
(elements that react to changes in state), 
every element passed as a subscriber must implement the Observer interface

~~~typescript
    export type Context = {
        tasks: Task[];
        selectedTask: { task: Task | null | undefined; id: number } | null;
    };

    const initialState: Context = {
        tasks: [
            {
                name: 'buy rice',
                status: 'In progress',
            },
            {
                name: 'study english',
                status: 'done',
            },
            {
                name: 'go to the gym',
                status: 'In progress',
            },
            {
                name: 'walk the dog',
                status: 'done',
            },
        ],
        selectedTask: null,
    };

    export const tasksState = new State<Context>(initialState);
~~~

## Utility functions

### changeRoute

function responsible for changing the application's route

~~~typescript
    changeRoute(path: string, data: any = {}, unused = '')
~~~

### createApp

function responsible for creating the application root

~~~typescript
    const app = createApp()
~~~

### createContainer

function responsible for creating an html container for your components

~~~typescript
    const container = createContainer('main', 'content', app, (container) => {
      new MockComponent(container);
      new MockComponent(container);
    });
~~~

### createFragment

function responsible for receiving an html string and returning a DomFragment

~~~typescript
    const template = createFragment(`
        <div>
            <h1>Fragment</h1>
        </div>
    `);
~~~

### dispatchNavigationEvent

function responsible for triggering the page break event

~~~typescript
    dispatchNavigationEvent()
~~~

### getMatchRoute

function that receives a route and checks if a path follows the route pattern

~~~typescript
    getMatchRoute('/page/:id', '/page/1')
~~~

### getRegexRoute

function that takes a route(string) and returns its corresponding regex

~~~typescript
    expect(String(getRegexRoute('/path/:id')))
~~~

### removeAllNodes

function that takes an html element and removes all of its children

~~~typescript
    removeAllNodes(app);
~~~

## License

This project is licensed under the MIT License - see the LICENSE file for details

---
_Source: https://npm.io/package/spa-vanilla · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
