1.1.2 • Published 10 months ago

@octamap/alpine-router v1.1.2

Weekly downloads
-
License
ISC
Repository
github
Last release
10 months ago

šŸ“š @octamap/alpine-router

A lightweight and powerful router for Alpine.js applications. Define routes directly in your HTML with ease, supporting both static file-based routing, dynamic programmatic navigation, and query parameters.


šŸš€ Features

āœ… Declarative Routing: Define routes seamlessly with the router attribute.
āœ… Static Routing: Map routes to files in your /public folder automatically.
āœ… Dynamic API: $router for navigation (push, replace, path, query).
āœ… Query Parameter Support: Manage and access URL query strings effortlessly.
āœ… TypeScript Support: Full type support, even with the CDN version.
āœ… Lightweight: Just 1.2 KB (gzipped).
āœ… Flexible Installation: Use via npm or jsDelivr CDN.
āœ… Transition Support: Easy to add smooth transitions during route changes.


šŸ“¦ Installation

Using npm

npm install @octamap/alpine-router

Then import it into your code (ensure Alpine.js is loaded beforehand):

import "@octamap/alpine-router";

Using jsDelivr CDN

Add the script before Alpine.js:

<!-- Alpine Router -->
<script src="https://cdn.jsdelivr.net/npm/@octamap/alpine-router@1.x.x"></script>
<!-- Alpine.js -->
<script src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js" defer></script>

TypeScript Support

Create a TypeScript declaration file (alpine-router.d.ts):

import "@octamap/alpine-router/types";

šŸ› ļø Usage

1. Define Your Router

Add the router attribute to your HTML element:

<body>
  <div router="my-router">
    <h2>Default Page (shown at `/`)</h2>
  </div>
</body>

2. Structure Routes in /public

Organize your route pages in the /public folder:

/src
/public
    /my-router
        login.html       // Accessible at `/login`
        check-inbox.html // Accessible at `/check-inbox`

When users visit /login, login.html will be loaded.


🌐 Dynamic Routing API

Within Alpine Components

Navigate dynamically using $router:

<div x-show="$router.path === `/`">
  <button @click="$router.push('/login')">Go to Login</button>
</div>

API Methods:

  • $router.push(path) → Navigate to a new route.
  • $router.replace(path) → Replace the current route.
  • $router.path → Get the current path.
  • $router.query → Access query parameters.

Global Access via window.router

Use global routing methods:

window.router.push('/login');  // Navigate to `/login`
window.router.replace('/home'); // Replace current path with `/home`

🌟 Using Query Parameters

1. Access Query Parameters

You can access query parameters directly using $router.query.

Example:

<div x-data>
    <p>Current User: <span x-text="$router.query.user"></span></p>
    <p>Theme: <span x-text="$router.query.theme"></span></p>
</div>

Given URL:

/profile?user=123&theme=dark

Result:

  • $router.query.user → "123"
  • $router.query.theme → "dark"

2. Navigate with Query Parameters

Using $router.push

Adds an entry to the browser history while including query parameters.

$router.push('/dashboard', { user: '456', theme: 'light' });

Resulting URL:

/dashboard?user=456&theme=light

Using $router.replace

Replaces the current history entry with a new path and query parameters.

$router.replace('/settings', { mode: 'edit', debug: 'true' });

Resulting URL:

/settings?mode=edit&debug=true

3. Dynamically Update Query Parameters

If you want to update only the query parameters while keeping the current path:

$router.replace($router.path, { sort: 'asc', filter: 'active' });

Resulting URL (assuming current path is /tasks):

/tasks?sort=asc&filter=active

4. Reactive Query in Alpine.js

Query parameters in Alpine.js are reactive:

<div x-data>
    <p>Current Page: <span x-text="$router.query.page || 'No page specified'"></span></p>
</div>

Navigate dynamically:

$router.push('/items', { page: '2' });

Dynamic Update Result:

2

šŸŽ­ Transitions on Navigation

Add smooth transitions during route changes:

<body router="main" router-transition="fade" style="transition: opacity 0.15s;">
  <h2>Welcome Page</h2>
  <button @click="$router.push('/another-path')">Navigate</button>
</body>

Key Attributes:

  • router="main" → Sets the main router container.
  • router-transition="fade" → Enables fade transitions.
  • transition: opacity 0.15s; → Adds smooth fading.

When navigating, content will fade out before the new content fades in.


šŸ“˜ API Reference

MethodDescriptionExample
$router.pushNavigate to a path$router.push('/home')
$router.replaceReplace current path$router.replace('/about')
$router.pathGet the current pathconsole.log($router.path)
$router.queryGet current query parametersconsole.log($router.query.user)

šŸ”— CDN Example

Example setup using Alpine.js and Alpine Router via CDN:

<!-- Alpine.js -->
<script src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x"></script>
<!-- Alpine Router -->
<script src="https://cdn.jsdelivr.net/npm/@octamap/alpine-router"></script>

<body>
  <div router="my-router">
    <h2 x-show="$router.path === '/'">Home Page</h2>
    <p>User: <span x-text="$router.query.user"></span></p>
    <button @click="$router.push('/login', { user: '123' })">Go to Login</button>
  </div>
</body>

šŸ“‘ TypeScript Support

To enable TypeScript support: 1. Create a file alpine-router.d.ts. 2. Add the following content:

import "@octamap/alpine-router/types";

Enjoy full TypeScript support across your app.


šŸ›”ļø License

This project is licensed under the MIT License.


šŸ¤ Contributing

We welcome contributions!

  • Report issues
  • Suggest improvements
  • Open pull requests

Find us on GitHub.


šŸ§‘ā€šŸ’» Author

Developed with ā¤ļø by Octamap Team.


Happy Routing! 🚦✨

1.1.2

10 months ago

1.1.1

10 months ago

1.1.0

10 months ago

1.0.8

10 months ago

1.0.7

10 months ago

1.0.6

10 months ago

1.0.5

10 months ago

1.0.4

10 months ago

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago