kt.js 0.1.2 → 0.2.1
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/README.md +34 -9
- package/dist/index.d.ts +33 -1
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,6 +18,11 @@ As a web framework, repeatedly creating a large number of variables and objects
|
|
|
18
18
|
|
|
19
19
|
KT.js follows one rule: full controll of dom and avoid unless repainting.
|
|
20
20
|
|
|
21
|
+
## Current Core Features
|
|
22
|
+
|
|
23
|
+
- `h` function and its aliases
|
|
24
|
+
- Router
|
|
25
|
+
|
|
21
26
|
## Getting started
|
|
22
27
|
|
|
23
28
|
```bash
|
|
@@ -69,14 +74,12 @@ h('div', { class: className }, 'Styled text');
|
|
|
69
74
|
|
|
70
75
|
## Router
|
|
71
76
|
|
|
72
|
-
KT.js includes a lightweight client-side router:
|
|
77
|
+
KT.js includes a lightweight client-side router (hash-based):
|
|
73
78
|
|
|
74
79
|
```ts
|
|
75
80
|
import { createRouter, div, h1 } from 'kt.js';
|
|
76
81
|
|
|
77
82
|
const router = createRouter({
|
|
78
|
-
mode: 'hash', // or 'history'
|
|
79
|
-
container: document.getElementById('app'),
|
|
80
83
|
routes: [
|
|
81
84
|
{
|
|
82
85
|
path: '/',
|
|
@@ -87,19 +90,41 @@ const router = createRouter({
|
|
|
87
90
|
handler: (ctx) => div({}, [h1({}, `User ${ctx.params.id}`)]),
|
|
88
91
|
},
|
|
89
92
|
],
|
|
93
|
+
container: document.getElementById('app'),
|
|
94
|
+
beforeEach: async (to, from) => {
|
|
95
|
+
// Navigation guard - return false to block navigation
|
|
96
|
+
console.log('navigating to:', to.path);
|
|
97
|
+
return true;
|
|
98
|
+
},
|
|
99
|
+
afterEach: (to) => {
|
|
100
|
+
// Called after successful navigation
|
|
101
|
+
document.title = to.path;
|
|
102
|
+
},
|
|
103
|
+
onError: (error) => {
|
|
104
|
+
console.error('Router error:', error);
|
|
105
|
+
},
|
|
90
106
|
});
|
|
91
107
|
|
|
92
108
|
router.start();
|
|
109
|
+
|
|
110
|
+
// Navigate programmatically
|
|
111
|
+
router.push('/user/123');
|
|
112
|
+
router.push('/user/456?page=2');
|
|
113
|
+
|
|
114
|
+
// Get current route
|
|
115
|
+
const current = router.current();
|
|
116
|
+
console.log(current?.path, current?.params, current?.query);
|
|
93
117
|
```
|
|
94
118
|
|
|
95
119
|
**Features:**
|
|
96
120
|
|
|
97
|
-
- Hash
|
|
98
|
-
- Dynamic route parameters
|
|
99
|
-
- Query string parsing
|
|
100
|
-
-
|
|
101
|
-
-
|
|
102
|
-
-
|
|
121
|
+
- Hash-based routing (`#/path`)
|
|
122
|
+
- Dynamic route parameters (`/user/:id`)
|
|
123
|
+
- Query string parsing (`?key=value`)
|
|
124
|
+
- Async navigation guards (`beforeEach`)
|
|
125
|
+
- Lifecycle hooks (`afterEach`)
|
|
126
|
+
- Error handling (`onError`)
|
|
127
|
+
- Minimal footprint
|
|
103
128
|
|
|
104
129
|
## Notes
|
|
105
130
|
|
package/dist/index.d.ts
CHANGED
|
@@ -70,4 +70,36 @@ declare const img: (attr?: RawAttr, content?: RawContent) => HTMLImageElement;
|
|
|
70
70
|
*/
|
|
71
71
|
declare function h<T extends HTMLTag>(tag: T, attr?: RawAttr, content?: RawContent): HTMLElementTagNameMap[T];
|
|
72
72
|
|
|
73
|
-
|
|
73
|
+
// Minimal router types
|
|
74
|
+
|
|
75
|
+
interface RouteContext {
|
|
76
|
+
params: Record<string, string>;
|
|
77
|
+
query: Record<string, string>;
|
|
78
|
+
path: string;
|
|
79
|
+
meta?: Record<string, any>;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
type RouteHandler = (ctx: RouteContext) => HTMLElement | void | Promise<HTMLElement | void>;
|
|
83
|
+
|
|
84
|
+
interface RouteConfig {
|
|
85
|
+
path: string;
|
|
86
|
+
handler: RouteHandler;
|
|
87
|
+
meta?: Record<string, any>;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
interface RouterConfig {
|
|
91
|
+
routes: RouteConfig[];
|
|
92
|
+
container?: HTMLElement;
|
|
93
|
+
beforeEach?: (to: RouteContext, from: RouteContext | null) => boolean | Promise<boolean>;
|
|
94
|
+
afterEach?: (to: RouteContext) => void;
|
|
95
|
+
onError?: (error: Error) => void;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
declare const createRouter: (config: RouterConfig) => {
|
|
99
|
+
start: () => void;
|
|
100
|
+
stop: () => void;
|
|
101
|
+
push: (path: string) => Promise<void>;
|
|
102
|
+
current: () => RouteContext | null;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
export { a, btn, createRouter, div, form, h, h1, h2, h3, h4, h5, h6, img, input, label, li, ol, option, p, select, span, table, tbody, td, th, thead, tr, ul };
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const t=document.createElement,e=HTMLElement.prototype.append,n=HTMLElement.prototype.addEventListener,o=HTMLElement.prototype.setAttribute,
|
|
1
|
+
const t=document.createElement,e=HTMLElement.prototype.append,n=HTMLElement.prototype.addEventListener,o=HTMLElement.prototype.setAttribute,r=Array.isArray,a=Object.keys,c=Object.assign,s=t=>{throw new Error("Kt.js:"+t)},u=(t,e,n)=>{e in t?t[e]=!!n:o.call(t,e,n)},l=(t,e,n)=>{e in t?t[e]=n:o.call(t,e,n)},i={checked:u,selected:u,value:l,valueAsDate:l,valueAsNumber:l,defaultValue:l,defaultChecked:u,defaultSelected:u,disabled:u,readOnly:u,multiple:u,required:u,autofocus:u,open:u,controls:u,autoplay:u,loop:u,muted:u,defer:u,async:u,hidden:(t,e,n)=>t.hidden=!!n},d=(t,e,n)=>o.call(t,e,n);function p(t,e){"string"==typeof e?t.className=e:"object"==typeof e&&null!==e?function(t,e){const r=e.class,s=e.style;void 0!==r&&(t.className=r,delete e.class),void 0!==s&&("string"==typeof s?o.call(t,"style",s):c(t.style,s),delete e.style);const u=a(e);for(let o=u.length-1;o>=0;o--){const r=u[o],a=e[r];"function"!=typeof a?(i[r]||d)(t,r,a):n.call(t,r,a)}void 0!==r&&(e.style=r),void 0!==s&&(e.style=s)}(t,e):s("applyAttr attr must be an object.")}function f(n,o="",a=""){"string"!=typeof n&&s("h tagName must be a string.");const c=(u=n,t.call(document,u));var u;return p(c,o),function(t,n){"string"==typeof n?t.textContent=n:r(n)?e.apply(t,n):n instanceof HTMLElement?e.call(t,n):s("applyContent invalid content.")}(c,a),c}const h=t=>(e,n)=>f(t,e,n),m=h("div"),y=h("span"),w=h("label"),b=h("p"),g=h("h1"),v=h("h2"),E=h("h3"),j=h("h4"),$=h("h5"),A=h("h6"),C=h("ul"),H=h("ol"),L=h("li"),M=h("button"),R=h("form"),T=h("input"),O=h("select"),k=h("option"),q=h("table"),x=h("thead"),I=h("tbody"),N=h("tr"),U=h("th"),D=h("td"),K=h("a"),S=h("img"),V=t=>{const{routes:e,container:n,beforeEach:o,afterEach:r,onError:a}=t;let c=null;const s=e.map(t=>{const e=[],n=t.path.replace(/\/:([^/]+)/g,(t,n)=>(e.push(n),"/([^/]+)"));return{route:t,pattern:new RegExp(`^${n}$`),names:e}}),u=async t=>{try{const[e,a]=t.split("?"),u=(t=>{const e={};return t?((t.startsWith("?")?t.slice(1):t).split("&").forEach(t=>{const[n,o]=t.split("=");n&&(e[decodeURIComponent(n)]=decodeURIComponent(o||""))}),e):e})(a||""),l=(t=>{for(const{route:e,pattern:n,names:o}of s){const r=t.match(n);if(r){const t={};return o.forEach((e,n)=>t[e]=r[n+1]),{route:e,params:t}}}return null})(e);if(!l)throw new Error(`Route not found: ${e}`);const i={params:l.params,query:u,path:e,meta:l.route.meta};if(o){if(!await o(i,c))return}window.location.hash=a?`${e}?${a}`:e;const d=await l.route.handler(i);n&&d&&(n.innerHTML="",n.appendChild(d)),c=i,r&&r(i)}catch(t){a&&a(t)}},l=()=>{const t=window.location.hash.slice(1)||"/";u(t)};return{start:()=>{window.addEventListener("hashchange",l),l()},stop:()=>window.removeEventListener("hashchange",l),push:t=>u(t),current:()=>c}};export{K as a,M as btn,V as createRouter,m as div,R as form,f as h,g as h1,v as h2,E as h3,j as h4,$ as h5,A as h6,S as img,T as input,w as label,L as li,H as ol,k as option,b as p,O as select,y as span,q as table,I as tbody,D as td,U as th,x as thead,N as tr,C as ul};
|