@ktjs/shortcuts 0.5.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 kasukabe tsumugi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,138 @@
1
+ # @ktjs/shortcuts
2
+
3
+ <img src="https://raw.githubusercontent.com/baendlorel/kt.js/dev/.assets/ktjs-0.0.1.svg" alt="KT.js Logo" width="150"/>
4
+
5
+ > 📦 Part of [KT.js](https://raw.githubusercontent.com/baendlorel/kt.js/dev/README.md) - A simple and easy-to-use web framework that never re-renders.
6
+
7
+ Convenient shortcut functions for common DOM operations in KT.js.
8
+
9
+ ## Overview
10
+
11
+ `@ktjs/shortcuts` provides convenient shorthand functions and utilities to make working with KT.js even more productive. It includes shortcuts for all HTML elements, form helpers, layout utilities, and the powerful `withDefaults` function for creating element factories with predefined properties.
12
+
13
+ ## Features
14
+
15
+ - **Element Shortcuts**: Quick creation functions for all HTML elements
16
+ - `div`, `span`, `button`, `input`, `a`, etc.
17
+ - Same signature as `h` function but without the tag name
18
+ - Full TypeScript support with proper return types
19
+ - **Form Helpers**: Shortcuts for form elements with better ergonomics
20
+ - Pre-configured form controls
21
+ - Input type helpers
22
+ - **Layout Helpers**: Common layout patterns and containers
23
+ - **`withDefaults`**: Create element factories with default properties
24
+ - Works with both `h` function and shortcut functions
25
+ - Supports nested defaults (defaults on top of defaults)
26
+ - Full type inference for all properties
27
+
28
+ ## Installation
29
+
30
+ ```bash
31
+ pnpm add @ktjs/shortcuts @ktjs/core
32
+ ```
33
+
34
+ ## Basic Usage
35
+
36
+ ### Element Shortcuts
37
+
38
+ ```typescript
39
+ import { div, span, button, input, h1, p } from '@ktjs/shortcuts';
40
+
41
+ // Instead of h('div', ...)
42
+ const container = div({ class: 'container' }, [
43
+ h1({}, 'Title'),
44
+ p({}, 'Description'),
45
+ button({ '@click': () => alert('Hi') }, 'Click me'),
46
+ ]);
47
+
48
+ // Form elements
49
+ const form = div('form-wrapper', [
50
+ input({ type: 'text', placeholder: 'Name' }),
51
+ input({ type: 'email', placeholder: 'Email' }),
52
+ button({ type: 'submit' }, 'Submit'),
53
+ ]);
54
+
55
+ // Quick elements with just content
56
+ const title = h1('Welcome');
57
+ const text = span('Hello World');
58
+ ```
59
+
60
+ ### Available Element Shortcuts
61
+
62
+ The following shortcuts are exported from `common.ts`:
63
+
64
+ **Basic**: `div`, `span`, `label`, `a`, `img`
65
+
66
+ **Text**: `h1`, `h2`, `h3`, `h4`, `h5`, `h6`, `p`
67
+
68
+ **Forms**: `form`, `input`, `button`, `select`, `option`
69
+
70
+ **Lists**: `ul`, `ol`, `li`
71
+
72
+ **Tables**: `table`, `thead`, `tbody`, `tr`, `th`, `td`
73
+
74
+ Each function has the signature:
75
+
76
+ ```typescript
77
+ function element(attributes?: KAttribute, content?: KContent): HTMLElement;
78
+ function element(className: string, content?: KContent): HTMLElement;
79
+ function element(content: KContent): HTMLElement;
80
+ ```
81
+
82
+ ## API Reference
83
+
84
+ ## Common Patterns
85
+
86
+ ### Component Composition
87
+
88
+ ```typescript
89
+ import { div, button, input } from '@ktjs/shortcuts';
90
+
91
+ // Create reusable components
92
+ const createCard = (title: string, content: string) => {
93
+ return div('card', [div('card-title', title), div('card-content', content)]);
94
+ };
95
+
96
+ // Use throughout your app
97
+ const ui = div('app', [createCard('Profile', 'User profile'), createCard('Settings', 'Settings')]);
98
+ ```
99
+
100
+ ### Form Builders
101
+
102
+ ```typescript
103
+ import { input, button, div } from '@ktjs/shortcuts';
104
+
105
+ const createTextInput = (placeholder: string, name: string) => {
106
+ return input({ type: 'text', class: 'form-control', placeholder, name });
107
+ };
108
+
109
+ const createSubmitButton = (text: string) => {
110
+ return button({ type: 'submit', class: 'btn-primary' }, text);
111
+ };
112
+
113
+ const loginForm = div('login-form', [
114
+ createTextInput('Username', 'username'),
115
+ createTextInput('Password', 'password'),
116
+ createSubmitButton('Login'),
117
+ ]);
118
+ ```
119
+
120
+ ## Type System
121
+
122
+ The package includes comprehensive TypeScript definitions with:
123
+
124
+ - Proper element type inference for all shortcuts
125
+ - Attribute and content type checking
126
+ - Event handler type hints
127
+ - Support for conditional types in `withDefaults`
128
+
129
+ ## Performance
130
+
131
+ - All shortcut functions are thin wrappers around the `h` function
132
+ - Zero runtime overhead after bundling with tree-shaking
133
+ - `withDefaults` creates minimal closure overhead
134
+ - Native method caching inherited from `@ktjs/core`
135
+
136
+ ## License
137
+
138
+ MIT
@@ -0,0 +1,109 @@
1
+ import * as _ktjs_core from '@ktjs/core';
2
+
3
+ declare const div: (attr?: _ktjs_core.KTRawAttr, content?: _ktjs_core.KTRawContent) => HTMLDivElement;
4
+ declare const span: (attr?: _ktjs_core.KTRawAttr, content?: _ktjs_core.KTRawContent) => HTMLSpanElement;
5
+ declare const label: (attr?: _ktjs_core.KTRawAttr, content?: _ktjs_core.KTRawContent) => HTMLLabelElement;
6
+ declare const p: (attr?: _ktjs_core.KTRawAttr, content?: _ktjs_core.KTRawContent) => HTMLParagraphElement;
7
+ declare const h1: (attr?: _ktjs_core.KTRawAttr, content?: _ktjs_core.KTRawContent) => HTMLHeadingElement;
8
+ declare const h2: (attr?: _ktjs_core.KTRawAttr, content?: _ktjs_core.KTRawContent) => HTMLHeadingElement;
9
+ declare const h3: (attr?: _ktjs_core.KTRawAttr, content?: _ktjs_core.KTRawContent) => HTMLHeadingElement;
10
+ declare const h4: (attr?: _ktjs_core.KTRawAttr, content?: _ktjs_core.KTRawContent) => HTMLHeadingElement;
11
+ declare const h5: (attr?: _ktjs_core.KTRawAttr, content?: _ktjs_core.KTRawContent) => HTMLHeadingElement;
12
+ declare const h6: (attr?: _ktjs_core.KTRawAttr, content?: _ktjs_core.KTRawContent) => HTMLHeadingElement;
13
+ declare const ul: (attr?: _ktjs_core.KTRawAttr, content?: _ktjs_core.KTRawContent) => HTMLUListElement;
14
+ declare const ol: (attr?: _ktjs_core.KTRawAttr, content?: _ktjs_core.KTRawContent) => HTMLOListElement;
15
+ declare const li: (attr?: _ktjs_core.KTRawAttr, content?: _ktjs_core.KTRawContent) => HTMLLIElement;
16
+ declare const btn: (attr?: _ktjs_core.KTRawAttr, content?: _ktjs_core.KTRawContent) => HTMLButtonElement;
17
+ declare const form: (attr?: _ktjs_core.KTRawAttr, content?: _ktjs_core.KTRawContent) => HTMLFormElement;
18
+ declare const input: (attr?: _ktjs_core.KTRawAttr, content?: _ktjs_core.KTRawContent) => HTMLInputElement;
19
+ declare const select: (attr?: _ktjs_core.KTRawAttr, content?: _ktjs_core.KTRawContent) => HTMLSelectElement;
20
+ declare const option: (attr?: _ktjs_core.KTRawAttr, content?: _ktjs_core.KTRawContent) => HTMLOptionElement;
21
+ declare const table: (attr?: _ktjs_core.KTRawAttr, content?: _ktjs_core.KTRawContent) => HTMLTableElement;
22
+ declare const thead: (attr?: _ktjs_core.KTRawAttr, content?: _ktjs_core.KTRawContent) => HTMLTableSectionElement;
23
+ declare const tbody: (attr?: _ktjs_core.KTRawAttr, content?: _ktjs_core.KTRawContent) => HTMLTableSectionElement;
24
+ declare const tr: (attr?: _ktjs_core.KTRawAttr, content?: _ktjs_core.KTRawContent) => HTMLTableRowElement;
25
+ declare const th: (attr?: _ktjs_core.KTRawAttr, content?: _ktjs_core.KTRawContent) => HTMLTableCellElement;
26
+ declare const td: (attr?: _ktjs_core.KTRawAttr, content?: _ktjs_core.KTRawContent) => HTMLTableCellElement;
27
+ declare const a: (attr?: _ktjs_core.KTRawAttr, content?: _ktjs_core.KTRawContent) => HTMLAnchorElement;
28
+ declare const img: (attr?: _ktjs_core.KTRawAttr, content?: _ktjs_core.KTRawContent) => HTMLImageElement;
29
+
30
+ type otherstring = string & {};
31
+
32
+ /**
33
+ * Used to create enhanced HTML elements
34
+ */
35
+ interface KTBaseAttribute {
36
+ [k: string]: any;
37
+
38
+ id?: string;
39
+ class?: string;
40
+ style?: string | Partial<CSSStyleDeclaration>;
41
+
42
+ type?:
43
+ | 'text'
44
+ | 'password'
45
+ | 'email'
46
+ | 'number'
47
+ | 'tel'
48
+ | 'url'
49
+ | 'search'
50
+ | 'date'
51
+ | 'datetime-local'
52
+ | 'time'
53
+ | 'month'
54
+ | 'week'
55
+ | 'color'
56
+ | 'range'
57
+ | 'file'
58
+ | 'checkbox'
59
+ | 'radio'
60
+ | 'hidden'
61
+ | 'submit'
62
+ | 'reset'
63
+ | 'button'
64
+ | 'image'
65
+ | otherstring;
66
+ for?: string;
67
+
68
+ name?: string;
69
+ value?: string;
70
+ valueAsDate?: Date;
71
+ valueAsNumber?: number;
72
+ label?: string;
73
+ disabled?: string;
74
+
75
+ min?: string | number;
76
+ max?: string | number;
77
+ step?: string | number;
78
+
79
+ selected?: boolean;
80
+ checked?: boolean;
81
+
82
+ action?: string;
83
+ method?: 'POST' | 'GET' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS' | 'CONNECT' | 'TRACE' | otherstring;
84
+ }
85
+
86
+ type KTEventHandlersOrAttribute = {
87
+ [EventName in keyof HTMLElementEventMap]?: ((ev: HTMLElementEventMap[EventName]) => void) | string;
88
+ };
89
+
90
+ /**
91
+ * Event handlers with @ prefix (e.g., @click, @input)
92
+ * When key starts with @, it's always registered as an event handler
93
+ */
94
+ type KTPrefixedEventHandlers = {
95
+ [EventName in keyof HTMLElementEventMap as `@${EventName}`]?: (ev: HTMLElementEventMap[EventName]) => void;
96
+ };
97
+
98
+ type KTAttribute = KTBaseAttribute & KTEventHandlersOrAttribute & KTPrefixedEventHandlers;
99
+
100
+ /**
101
+ * Wrap `h`(or its shortcuts) with default attributes.
102
+ * - `defaultAttr.class` will be merged with the provided `class` attribute.
103
+ * @param func `h` or its shortcuts.
104
+ * @param defaultAttr default KTAttribute, must be an object.
105
+ * @returns new h function with default attributes.
106
+ */
107
+ declare const withDefaults: <F extends (...args: any) => any>(func: F, defaultAttr: KTAttribute) => F;
108
+
109
+ export { a, btn, div, form, h1, h2, h3, h4, h5, h6, img, input, label, li, ol, option, p, select, span, table, tbody, td, th, thead, tr, ul, withDefaults };
@@ -0,0 +1 @@
1
+ var __ktjs_shortcuts__=function(t,o){"use strict";const n=t=>{const n=(n,e)=>o.h(t,n,e);return window.__ktjs__.mark(n,t),n},e=n("div"),s=n("span"),i=n("label"),r=n("p"),c=n("h1"),u=n("h2"),_=n("h3"),h=n("h4"),l=n("h5"),a=n("h6"),f=n("ul"),b=n("ol"),d=n("li"),p=n("button"),w=n("form"),y=n("input"),j=n("select"),g=n("option"),k=n("table"),m=n("thead"),v=n("tbody"),A=n("tr"),D=n("th"),q=n("td"),x=n("a"),z=n("img");return t.a=x,t.btn=p,t.div=e,t.form=w,t.h1=c,t.h2=u,t.h3=_,t.h4=h,t.h5=l,t.h6=a,t.img=z,t.input=y,t.label=i,t.li=d,t.ol=b,t.option=g,t.p=r,t.select=j,t.span=s,t.table=k,t.tbody=v,t.td=q,t.th=D,t.thead=m,t.tr=A,t.ul=f,t.withDefaults=(t,o)=>{"t"in t==!1&&window.__ktjs__.throws("withDefaults can only be used with h function or its alias"),null!==o&&"object"==typeof o||window.__ktjs__.throws("defaultAttr must be an object");const n="string"==typeof o.class?t=>{if("string"==typeof t){const n={...o};return n.class=t+" "+o.class,n}if(t&&"object"==typeof t){const n={...o,...t};return n.class=(t.class??"")+" "+o.class,n}}:t=>"string"==typeof t?{...o,class:t}:t&&"object"==typeof t?{...o,...t}:void 0;return"h"===t.t?(o,e,s)=>t(o,n(e),s):(o,e)=>t(n(o),e)},t}({},__ktjs_core__);
@@ -0,0 +1 @@
1
+ var __ktjs_shortcuts__=function(t,n){"use strict";var r=function(t){var r=function(r,o){return n.h(t,r,o)};return window.__ktjs__.mark(r,t),r},o=r("div"),e=r("span"),u=r("label"),i=r("p"),c=r("h1"),f=r("h2"),s=r("h3"),a=r("h4"),l=r("h5"),h=r("h6"),p=r("ul"),_=r("ol"),d=r("li"),b=r("button"),v=r("form"),j=r("input"),y=r("select"),w=r("option"),g=r("table"),k=r("thead"),m=r("tbody"),E=r("tr"),O=r("th"),S=r("td"),A=r("a"),D=r("img"),q=function(){return q=Object.assign||function(t){for(var n,r=1,o=arguments.length;r<o;r++)for(var e in n=arguments[r])Object.prototype.hasOwnProperty.call(n,e)&&(t[e]=n[e]);return t},q.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;return t.a=A,t.btn=b,t.div=o,t.form=v,t.h1=c,t.h2=f,t.h3=s,t.h4=a,t.h5=l,t.h6=h,t.img=D,t.input=j,t.label=u,t.li=d,t.ol=_,t.option=w,t.p=i,t.select=y,t.span=e,t.table=g,t.tbody=m,t.td=S,t.th=O,t.thead=k,t.tr=E,t.ul=p,t.withDefaults=function(t,n){"t"in t==!1&&window.__ktjs__.throws("withDefaults can only be used with h function or its alias"),null!==n&&"object"==typeof n||window.__ktjs__.throws("defaultAttr must be an object");var r="string"==typeof n.class?function(t){var r,o;return"string"==typeof t?((o=q({},n)).class=t+" "+n.class,o):t&&"object"==typeof t?((o=q(q({},n),t)).class=(null!==(r=t.class)&&void 0!==r?r:"")+" "+n.class,o):void 0}:function(t){return"string"==typeof t?q(q({},n),{class:t}):t&&"object"==typeof t?q(q({},n),t):void 0};return"h"===t.t?function(n,o,e){return t(n,r(o),e)}:function(n,o){return t(r(n),o)}},t}({},__ktjs_core__);
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ import{h as t}from"@ktjs/core";const o=o=>{const e=(e,n)=>t(o,e,n);return window.__ktjs__.mark(e,o),e},e=o("div"),n=o("span"),i=o("label"),r=o("p"),s=o("h1"),c=o("h2"),l=o("h3"),f=o("h4"),h=o("h5"),u=o("h6"),a=o("ul"),p=o("ol"),b=o("li"),d=o("button"),w=o("form"),y=o("input"),j=o("select"),m=o("option"),_=o("table"),g=o("thead"),k=o("tbody"),v=o("tr"),x=o("th"),A=o("td"),D=o("a"),q=o("img"),z=(t,o)=>{"t"in t==!1&&window.__ktjs__.throws("withDefaults can only be used with h function or its alias"),null!==o&&"object"==typeof o||window.__ktjs__.throws("defaultAttr must be an object");const e="string"==typeof o.class?t=>{if("string"==typeof t){const e={...o};return e.class=t+" "+o.class,e}if(t&&"object"==typeof t){const e={...o,...t};return e.class=(t.class??"")+" "+o.class,e}}:t=>"string"==typeof t?{...o,class:t}:t&&"object"==typeof t?{...o,...t}:void 0;return"h"===t.t?(o,n,i)=>t(o,e(n),i):(o,n)=>t(e(o),n)};export{D as a,d as btn,e as div,w as form,s as h1,c as h2,l as h3,f as h4,h as h5,u as h6,q as img,y as input,i as label,b as li,p as ol,m as option,r as p,j as select,n as span,_ as table,k as tbody,A as td,x as th,g as thead,v as tr,a as ul,z as withDefaults};
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@ktjs/shortcuts",
3
+ "version": "0.5.0",
4
+ "description": "Shortcut functions for kt.js - common DOM operations",
5
+ "type": "module",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.mjs",
11
+ "default": "./dist/index.mjs"
12
+ },
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "keywords": [
17
+ "dom",
18
+ "shortcuts",
19
+ "utilities",
20
+ "web"
21
+ ],
22
+ "license": "MIT",
23
+ "author": {
24
+ "name": "Kasukabe Tsumugi",
25
+ "email": "futami16237@gmail.com"
26
+ },
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "https://github.com/baendlorel/kt.js",
30
+ "directory": "packages/shortcuts"
31
+ },
32
+ "dependencies": {
33
+ "@ktjs/core": "0.5.0"
34
+ },
35
+ "scripts": {
36
+ "build": "rollup -c rollup.config.mjs",
37
+ "dev": "rollup -c rollup.config.mjs -w"
38
+ }
39
+ }