preact-hashish-router 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,6 +1 @@
1
- export { A } from "./A";
2
- export { useParams, useRouter, useSearchParams } from "./context";
3
- export { NotFound } from "./NotFound";
4
- export { Route } from "./Route";
5
- export { Router } from "./Router";
6
- export { RouterErrorBoundary } from "./RouterErrorBoundary";
1
+ import{forwardRef as k}from"preact/compat";import{createContext as V}from"preact";import{useContext as h}from"preact/hooks";var o=V({active_path:"",active_route_data:null,params:void 0,searchParams:new URLSearchParams,go(){}}),p=()=>{let e=h(o);if(!e)throw new Error("useHashisherContext should be inside a HashisherContext provider");return e};function S(){let e=h(o);if(!e)throw new Error("useParams should be inside a HashisherContext provider");return e.params}function M(){let e=h(o);if(!e)throw new Error("useSearchParams should be inside a HashisherContext provider");return e.searchParams}var N=()=>{let e=h(o);if(!e)throw new Error("useRouter should be inside a HashisherContext provider");return{path:e.active_path,params:e.params,go:e.go,searchParams:e.searchParams}};import{jsx as E}from"preact/jsx-runtime";var L=k(({href:e,...t},s)=>{let{go:m}=p();if(!e)throw new Error("A: href must be defined");return E("a",{ref:s,href:e,onClick:i=>{i.preventDefault(),m(e)},...t})});import{Fragment as z,jsx as w}from"preact/jsx-runtime";var U=e=>(A(e.element),w(z,{})),R=w("div",{children:"404 Not Found"}),A=e=>{R=e},d=()=>R;import{addRoute as W,createRouter as F}from"rou3";var l=F(),y=(e,t)=>{W(l,void 0,e,{component:t.element,fallback:t.fallback||null,lazy:!!t.lazy})};import{Fragment as D,jsx as O}from"preact/jsx-runtime";function T(e){return y(e.path,e),O(D,{})}import{useCallback as G,useLayoutEffect as I,useState as u}from"preact/hooks";import{findRoute as J}from"rou3";import{parseURL as v}from"ufo";import{Suspense as B}from"preact/compat";import{jsx as q}from"preact/jsx-runtime";var C=()=>{let{active_route_data:e}=p();return e?e.component===null?d():e.lazy?q(B,{fallback:e.fallback,children:e.component}):e.component:d()};import{jsx as Q,jsxs as X}from"preact/jsx-runtime";var K=e=>{let[t,s]=u(()=>typeof window<"u"?window.location.pathname:null),[m,i]=u(void 0),[g,x]=u(new URLSearchParams),[b,P]=u(null),_=G(r=>{let a=v(window.location.href),n=r===null?a.pathname:r,c=J(l,void 0,n);if(!c){s(n),x(new URLSearchParams(a.search)),P(null),i(void 0),e.type==="browser"&&window.history.pushState(null,"",n);return}s(n),x(new URLSearchParams(a.search)),i({...c.params}),P({...c.data}),e.type==="browser"&&window.history.pushState(null,"",n)},[]);I(()=>{if(e.type!=="browser")return;let r=()=>{_(null)};return window.addEventListener("popstate",r),r(),()=>{window.removeEventListener("popstate",r)}},[]);let H=r=>{let a=v(r).pathname;_(a)};return X(o.Provider,{value:{active_path:t,searchParams:g,params:m,active_route_data:b,go:H},children:[e.children,Q(C,{})]})};import{Component as Y}from"preact";import{jsxs as Z}from"preact/jsx-runtime";var f=class extends Y{state={error:null};static getDerivedStateFromError(t){return{error:t.message}}componentDidCatch(t){this.setState({error:t.message})}render(){return this.state.error?this.props.fallback?this.props.fallback:Z("p",{children:["Oh no! We ran into an error: ",this.state.error]}):this.props.children}};export{L as A,U as NotFound,T as Route,K as Router,f as RouterErrorBoundary,S as useParams,N as useRouter,M as useSearchParams};
package/package.json CHANGED
@@ -1,14 +1,16 @@
1
1
  {
2
2
  "name": "preact-hashish-router",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "type": "module",
5
5
  "module": "dist/index.js",
6
6
  "description": "A simple router for preact",
7
7
  "scripts": {
8
- "build": "tsc -p ./tsconfig.lib.json",
8
+ "types": "tsc -p ./tsconfig.lib.json",
9
+ "bundle": "esbuild ./src/index.ts --bundle --minify --platform=browser --packages=external --outfile=./dist/index.js --format=esm",
9
10
  "format": "prettier --write src app --ignore-unknown --cache",
10
11
  "app:dev": "vite",
11
- "prepublishOnly": "npm run build",
12
+ "build": "rm -rf ./dist && bun run types && bun run bundle",
13
+ "prepublishOnly": "bun run types && bun run bundle",
12
14
  "push": "npm version patch && git push",
13
15
  "push-minor": "npm version minor && git push",
14
16
  "push-major": "npm version major && git push"
@@ -36,6 +38,7 @@
36
38
  "devDependencies": {
37
39
  "@preact/preset-vite": "^2.10.2",
38
40
  "@types/node": "^22.17.1",
41
+ "esbuild": "^0.25.9",
39
42
  "preact-render-to-string": "^6.5.13",
40
43
  "prettier": "^3.6.2",
41
44
  "prettier-plugin-organize-imports": "^4.2.0",
package/dist/A.js DELETED
@@ -1,13 +0,0 @@
1
- import { jsx as _jsx } from "preact/jsx-runtime";
2
- import { forwardRef } from "preact/compat";
3
- import { useHashisherContext } from "./context";
4
- export const A = forwardRef(({ href, ...props }, forwardedRef) => {
5
- const { go } = useHashisherContext();
6
- if (!href) {
7
- throw new Error("A: href must be defined");
8
- }
9
- return (_jsx("a", { ref: forwardedRef, href: href, onClick: (event) => {
10
- event.preventDefault();
11
- go(href);
12
- }, ...props }));
13
- });
package/dist/NotFound.js DELETED
@@ -1,10 +0,0 @@
1
- import { Fragment as _Fragment, jsx as _jsx } from "preact/jsx-runtime";
2
- export const NotFound = (props) => {
3
- set_not_found_element(props.element);
4
- return _jsx(_Fragment, {});
5
- };
6
- let not_found_element = _jsx("div", { children: "404 Not Found" });
7
- export const set_not_found_element = (el) => {
8
- not_found_element = el;
9
- };
10
- export const get_not_found_element = () => not_found_element;
@@ -1,16 +0,0 @@
1
- import { jsx as _jsx } from "preact/jsx-runtime";
2
- import { Suspense } from "preact/compat";
3
- import { useHashisherContext } from "./context";
4
- import { get_not_found_element } from "./NotFound";
5
- export const RenderMatchedRoute = () => {
6
- const { active_route_data } = useHashisherContext();
7
- if (!active_route_data)
8
- return get_not_found_element();
9
- if (active_route_data.component === null) {
10
- return get_not_found_element();
11
- }
12
- if (active_route_data.lazy) {
13
- return _jsx(Suspense, { fallback: active_route_data.fallback, children: active_route_data.component });
14
- }
15
- return active_route_data.component;
16
- };
package/dist/Route.js DELETED
@@ -1,6 +0,0 @@
1
- import { Fragment as _Fragment, jsx as _jsx } from "preact/jsx-runtime";
2
- import { add_route_to_matcher } from "./router/matcher";
3
- export function Route(props) {
4
- add_route_to_matcher(props.path, props);
5
- return _jsx(_Fragment, {});
6
- }
package/dist/Router.js DELETED
@@ -1,62 +0,0 @@
1
- import { jsx as _jsx, jsxs as _jsxs } from "preact/jsx-runtime";
2
- import { useCallback, useLayoutEffect, useState } from "preact/hooks";
3
- import { findRoute } from "rou3";
4
- import { parseURL } from "ufo";
5
- import { HashisherContext } from "./context";
6
- import { RenderMatchedRoute } from "./RenderMatchedRoute";
7
- import { Matcher } from "./router/matcher";
8
- export const Router = (props) => {
9
- const [active_path, set_active_path] = useState(() => {
10
- if (typeof window !== "undefined")
11
- return window.location.pathname;
12
- return null;
13
- });
14
- const [params, setParams] = useState(undefined);
15
- const [searchParams, setSearchParams] = useState(new URLSearchParams());
16
- const [active_route_data, set_active_route_data] = useState(null);
17
- const execute_path_change = useCallback((raw_path) => {
18
- const url = parseURL(window.location.href);
19
- const newPath = raw_path === null ? url.pathname : raw_path;
20
- const route_data = findRoute(Matcher, undefined, newPath);
21
- if (!route_data) {
22
- set_active_path(newPath);
23
- setSearchParams(new URLSearchParams(url.search));
24
- set_active_route_data(null);
25
- setParams(undefined);
26
- if (props.type === "browser") {
27
- window.history.pushState(null, "", newPath);
28
- }
29
- return;
30
- }
31
- set_active_path(newPath);
32
- setSearchParams(new URLSearchParams(url.search));
33
- setParams({ ...route_data.params });
34
- set_active_route_data({ ...route_data.data });
35
- if (props.type === "browser") {
36
- window.history.pushState(null, "", newPath);
37
- }
38
- }, []);
39
- useLayoutEffect(() => {
40
- if (props.type !== "browser")
41
- return;
42
- const listener = () => {
43
- execute_path_change(null);
44
- };
45
- window.addEventListener("popstate", listener);
46
- listener();
47
- return () => {
48
- window.removeEventListener("popstate", listener);
49
- };
50
- }, []);
51
- const go_imperative = (newPath) => {
52
- const pathname = parseURL(newPath).pathname;
53
- execute_path_change(pathname);
54
- };
55
- return (_jsxs(HashisherContext.Provider, { value: {
56
- active_path,
57
- searchParams,
58
- params,
59
- active_route_data,
60
- go: go_imperative,
61
- }, children: [props.children, _jsx(RenderMatchedRoute, {})] }));
62
- };
@@ -1,19 +0,0 @@
1
- import { jsxs as _jsxs } from "preact/jsx-runtime";
2
- import { Component } from "preact";
3
- export class RouterErrorBoundary extends Component {
4
- state = { error: null };
5
- static getDerivedStateFromError(error) {
6
- return { error: error.message };
7
- }
8
- componentDidCatch(error) {
9
- this.setState({ error: error.message });
10
- }
11
- render() {
12
- if (this.state.error) {
13
- if (this.props.fallback)
14
- return this.props.fallback;
15
- return _jsxs("p", { children: ["Oh no! We ran into an error: ", this.state.error] });
16
- }
17
- return this.props.children;
18
- }
19
- }
package/dist/context.js DELETED
@@ -1,38 +0,0 @@
1
- import { createContext } from "preact";
2
- import { useContext } from "preact/hooks";
3
- export const HashisherContext = createContext({
4
- active_path: "",
5
- active_route_data: null,
6
- params: undefined,
7
- searchParams: new URLSearchParams(),
8
- go() { },
9
- });
10
- export const useHashisherContext = () => {
11
- const c = useContext(HashisherContext);
12
- if (!c)
13
- throw new Error("useHashisherContext should be inside a HashisherContext provider");
14
- return c;
15
- };
16
- export function useParams() {
17
- const c = useContext(HashisherContext);
18
- if (!c)
19
- throw new Error("useParams should be inside a HashisherContext provider");
20
- return c.params;
21
- }
22
- export function useSearchParams() {
23
- const c = useContext(HashisherContext);
24
- if (!c)
25
- throw new Error("useSearchParams should be inside a HashisherContext provider");
26
- return c.searchParams;
27
- }
28
- export const useRouter = () => {
29
- const c = useContext(HashisherContext);
30
- if (!c)
31
- throw new Error("useRouter should be inside a HashisherContext provider");
32
- return {
33
- path: c.active_path,
34
- params: c.params,
35
- go: c.go,
36
- searchParams: c.searchParams,
37
- };
38
- };
package/dist/matcher.js DELETED
@@ -1,6 +0,0 @@
1
- "use strict";
2
- import { createRouter as a, addRoute as t } from "rou3";
3
- export const Matcher = a(),
4
- add_route_to_matcher = (e, o) => {
5
- t(Matcher, void 0, e, { component: o.element, fallback: o.fallback || null, lazy: !!o.lazy });
6
- };
@@ -1,9 +0,0 @@
1
- import { addRoute, createRouter } from "rou3";
2
- export const Matcher = createRouter();
3
- export const add_route_to_matcher = (path, data) => {
4
- addRoute(Matcher, undefined, path, {
5
- component: data.element,
6
- fallback: data.fallback || null,
7
- lazy: Boolean(data.lazy),
8
- });
9
- };