Skip to content

Commit

Permalink
Organize src/ file structure
Browse files Browse the repository at this point in the history
  • Loading branch information
raghav-misra committed Aug 17, 2020
1 parent 7e82cb1 commit cf95143
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 18 deletions.
26 changes: 24 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# `porpoise-router`

A simple client-side routing solution for custom elements.
A framework-agnostic, standalone router, designed for use with Web Components

## Initialize JS:
```js
/* Initialize the router */
import { createRouter } from "porpoise-router";

const router = createRouter({
const router = createRouter("root", {
// path "/" renders
// <index-view></index-view>
"/": "index-view",
Expand All @@ -22,3 +23,24 @@ const router = createRouter({
});
```

## Initialize HTML:
```html
<p-router name="root"></p-router>
```

## Porpoise Integration (Optional):
```js
import * as Porpoise from "porpoise";

// Allow access to the router:
Porpoise.globalize("router", () => router);

// Access the router in porpoise elements via this.$globals.router:
Porpoise.construct({
/* your component... */
events: {
click() {
this.$globals.router.push("/about");
}
}
})
Empty file added src/components/p-link.ts
Empty file.
5 changes: 1 addition & 4 deletions src/porpoise-router.ts → src/components/p-router.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
export interface IRouteDescriptor {
element: string,
props?: Record<string, string>
}
import { IRouteDescriptor } from "../internals/api.js";

export class PorpoiseRouter extends HTMLElement {
routes: Record<string, string | IRouteDescriptor> = Object.create(null);
Expand Down
11 changes: 11 additions & 0 deletions src/functions/create-router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { PorpoiseRouter } from "../components/p-router.js";
import { IRouteDescriptor } from "../internals/api.js";

export function createRouter(target: string, routes: Record<string, string | IRouteDescriptor>) {
const routerNode = document.querySelector(`p-router[name="${target}"]`) as PorpoiseRouter;

if (routerNode) {
routerNode.configure(routes);
}
else console.error(`Could not find <porpoise-router name="${target}"></porpoise-router> in the DOM.`);
}
24 changes: 12 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// ES5 interop with custom elements:
import "./es5-adapter.js";
import { PorpoiseRouter, IRouteDescriptor } from "./porpoise-router.js";
import { pathToRegexp } from "path-to-regexp";

export default function createRouter(target: string, routes: Record<string, string | IRouteDescriptor>) {
const routerNode = document.querySelector(`porpoise-router[name="${target}"]`) as PorpoiseRouter;

if (routerNode) {
routerNode.configure(routes);
}
else console.error(`Could not find <porpoise-router name="${target}"></porpoise-router> in the DOM.`);
}
import "./internals/es5-adapter.js";

// Register components:
import "./components/p-link.js";
import "./components/p-router.js";

// Setup function:
export { createRouter } from "./functions/create-router.js";

// Export typings:
export * from "./internals/api.js";

4 changes: 4 additions & 0 deletions src/internals/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface IRouteDescriptor {
element: string,
props?: Record<string, string>
}
File renamed without changes.

0 comments on commit cf95143

Please sign in to comment.