@snupo/jmc 1.5.0 → 1.6.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/components.md +251 -0
- package/dist/comps/checkbox.css +45 -0
- package/dist/comps/checkbox.d.ts +16 -0
- package/dist/comps/checkbox.d.ts.map +1 -0
- package/dist/comps/checkbox.js +20 -0
- package/dist/comps/checkbox.js.map +1 -0
- package/dist/comps/combobox.css +63 -0
- package/dist/comps/combobox.d.ts +30 -0
- package/dist/comps/combobox.d.ts.map +1 -0
- package/dist/comps/combobox.js +87 -0
- package/dist/comps/combobox.js.map +1 -0
- package/dist/comps/datalistinput.css +22 -0
- package/dist/comps/datalistinput.d.ts +20 -0
- package/dist/comps/datalistinput.d.ts.map +1 -0
- package/dist/comps/datalistinput.js +23 -0
- package/dist/comps/datalistinput.js.map +1 -0
- package/dist/comps/icon.css +6 -0
- package/dist/comps/icon.d.ts +11 -0
- package/dist/comps/icon.d.ts.map +1 -0
- package/dist/comps/icon.js +6 -0
- package/dist/comps/icon.js.map +1 -0
- package/dist/comps/index.d.ts +12 -0
- package/dist/comps/index.d.ts.map +1 -0
- package/dist/comps/index.js +12 -0
- package/dist/comps/index.js.map +1 -0
- package/dist/comps/numberstepper.css +44 -0
- package/dist/comps/numberstepper.d.ts +26 -0
- package/dist/comps/numberstepper.d.ts.map +1 -0
- package/dist/comps/numberstepper.js +72 -0
- package/dist/comps/numberstepper.js.map +1 -0
- package/dist/comps/radiogroup.css +20 -0
- package/dist/comps/radiogroup.d.ts +19 -0
- package/dist/comps/radiogroup.d.ts.map +1 -0
- package/dist/comps/radiogroup.js +21 -0
- package/dist/comps/radiogroup.js.map +1 -0
- package/dist/comps/rangeslider.css +46 -0
- package/dist/comps/rangeslider.d.ts +18 -0
- package/dist/comps/rangeslider.d.ts.map +1 -0
- package/dist/comps/rangeslider.js +15 -0
- package/dist/comps/rangeslider.js.map +1 -0
- package/dist/comps/select.css +16 -0
- package/dist/comps/select.d.ts +19 -0
- package/dist/comps/select.d.ts.map +1 -0
- package/dist/comps/select.js +21 -0
- package/dist/comps/select.js.map +1 -0
- package/dist/comps/tabs.css +22 -0
- package/dist/comps/tabs.d.ts +15 -0
- package/dist/comps/tabs.d.ts.map +1 -0
- package/dist/comps/tabs.js +11 -0
- package/dist/comps/tabs.js.map +1 -0
- package/dist/comps/textarea.css +19 -0
- package/dist/comps/textarea.d.ts +17 -0
- package/dist/comps/textarea.d.ts.map +1 -0
- package/dist/comps/textarea.js +32 -0
- package/dist/comps/textarea.js.map +1 -0
- package/dist/comps/togglegroup.css +29 -0
- package/dist/comps/togglegroup.d.ts +16 -0
- package/dist/comps/togglegroup.d.ts.map +1 -0
- package/dist/comps/togglegroup.js +11 -0
- package/dist/comps/togglegroup.js.map +1 -0
- package/dist/jsx.d.ts +1 -0
- package/dist/jsx.d.ts.map +1 -1
- package/dist/jsx.js.map +1 -1
- package/package.json +89 -76
- package/readme.md +4 -0
- package/src/comps/checkbox.css +45 -0
- package/src/comps/checkbox.tsx +31 -0
- package/src/comps/combobox.css +63 -0
- package/src/comps/combobox.tsx +100 -0
- package/src/comps/css.d.ts +1 -0
- package/src/comps/datalistinput.css +22 -0
- package/src/comps/datalistinput.tsx +35 -0
- package/src/comps/icon.css +6 -0
- package/src/comps/icon.tsx +13 -0
- package/src/comps/index.ts +11 -0
- package/src/comps/numberstepper.css +44 -0
- package/src/comps/numberstepper.tsx +73 -0
- package/src/comps/radiogroup.css +20 -0
- package/src/comps/radiogroup.tsx +40 -0
- package/src/comps/rangeslider.css +46 -0
- package/src/comps/rangeslider.tsx +27 -0
- package/src/comps/select.css +16 -0
- package/src/comps/select.tsx +36 -0
- package/src/comps/tabs.css +22 -0
- package/src/comps/tabs.tsx +22 -0
- package/src/comps/textarea.css +19 -0
- package/src/comps/textarea.tsx +41 -0
- package/src/comps/togglegroup.css +29 -0
- package/src/comps/togglegroup.tsx +23 -0
- package/src/jsx.ts +1 -0
package/package.json
CHANGED
|
@@ -1,76 +1,89 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@snupo/jmc",
|
|
3
|
-
"description": "Minimal JSX/TSX view library that syncs directly with the real DOM",
|
|
4
|
-
"keywords": [
|
|
5
|
-
"jsx",
|
|
6
|
-
"tsx",
|
|
7
|
-
"dom",
|
|
8
|
-
"view",
|
|
9
|
-
"ui",
|
|
10
|
-
"typescript"
|
|
11
|
-
],
|
|
12
|
-
"author": "snupo",
|
|
13
|
-
"license": "MIT",
|
|
14
|
-
"repository": {
|
|
15
|
-
"type": "git",
|
|
16
|
-
"url": "git+https://github.com/snuup/jmc.git"
|
|
17
|
-
},
|
|
18
|
-
"homepage": "https://github.com/snuup/jmc#readme",
|
|
19
|
-
"bugs": "https://github.com/snuup/jmc/issues",
|
|
20
|
-
"version": "1.
|
|
21
|
-
"publishConfig": {
|
|
22
|
-
"access": "public"
|
|
23
|
-
},
|
|
24
|
-
"type": "module",
|
|
25
|
-
"main": "./dist/index.js",
|
|
26
|
-
"types": "./dist/index.d.ts",
|
|
27
|
-
"exports": {
|
|
28
|
-
".": {
|
|
29
|
-
"types": "./dist/index.d.ts",
|
|
30
|
-
"import": "./dist/index.js"
|
|
31
|
-
},
|
|
32
|
-
"./jsx-runtime": {
|
|
33
|
-
"types": "./dist/jsx-runtime.d.ts",
|
|
34
|
-
"import": "./dist/jsx-runtime.js"
|
|
35
|
-
},
|
|
36
|
-
"./jsx-dev-runtime": {
|
|
37
|
-
"types": "./dist/jsx-dev-runtime.d.ts",
|
|
38
|
-
"import": "./dist/jsx-dev-runtime.js"
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
"
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
"
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
"
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
"
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
"
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@snupo/jmc",
|
|
3
|
+
"description": "Minimal JSX/TSX view library that syncs directly with the real DOM",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"jsx",
|
|
6
|
+
"tsx",
|
|
7
|
+
"dom",
|
|
8
|
+
"view",
|
|
9
|
+
"ui",
|
|
10
|
+
"typescript"
|
|
11
|
+
],
|
|
12
|
+
"author": "snupo",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/snuup/jmc.git"
|
|
17
|
+
},
|
|
18
|
+
"homepage": "https://github.com/snuup/jmc#readme",
|
|
19
|
+
"bugs": "https://github.com/snuup/jmc/issues",
|
|
20
|
+
"version": "1.6.0",
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"type": "module",
|
|
25
|
+
"main": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"import": "./dist/index.js"
|
|
31
|
+
},
|
|
32
|
+
"./jsx-runtime": {
|
|
33
|
+
"types": "./dist/jsx-runtime.d.ts",
|
|
34
|
+
"import": "./dist/jsx-runtime.js"
|
|
35
|
+
},
|
|
36
|
+
"./jsx-dev-runtime": {
|
|
37
|
+
"types": "./dist/jsx-dev-runtime.d.ts",
|
|
38
|
+
"import": "./dist/jsx-dev-runtime.js"
|
|
39
|
+
},
|
|
40
|
+
"./comps": {
|
|
41
|
+
"types": "./dist/comps/index.d.ts",
|
|
42
|
+
"import": "./dist/comps/index.js"
|
|
43
|
+
},
|
|
44
|
+
"./comps/*": {
|
|
45
|
+
"types": "./dist/comps/*.d.ts",
|
|
46
|
+
"import": "./dist/comps/*.js"
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
"sideEffects": [
|
|
50
|
+
"**/*.css"
|
|
51
|
+
],
|
|
52
|
+
"files": [
|
|
53
|
+
"dist",
|
|
54
|
+
"src",
|
|
55
|
+
"design-guide.md",
|
|
56
|
+
"components.md"
|
|
57
|
+
],
|
|
58
|
+
"scripts": {
|
|
59
|
+
"prebuild": "rimraf dist",
|
|
60
|
+
"build": "tsc -p tsconfig.json && node scripts/copycss.mjs",
|
|
61
|
+
"test": "pnpm typecheck && vitest run",
|
|
62
|
+
"typecheck": "tsc -p tsconfig.json --noEmit && tsc -p tests && tsc -p tests/automatic && tsc -p demo",
|
|
63
|
+
"release": "release-it patch",
|
|
64
|
+
"demo": "vite demo"
|
|
65
|
+
},
|
|
66
|
+
"devDependencies": {
|
|
67
|
+
"@vitest/coverage-v8": "4.1.10",
|
|
68
|
+
"happy-dom": "^16.8.1",
|
|
69
|
+
"release-it": "^21.0.2",
|
|
70
|
+
"rimraf": "^6.1.3",
|
|
71
|
+
"typescript": "^6.0.3",
|
|
72
|
+
"vite": "8.2.2",
|
|
73
|
+
"vitest": "4.1.10"
|
|
74
|
+
},
|
|
75
|
+
"release-it": {
|
|
76
|
+
"hooks": {
|
|
77
|
+
"before:init": [
|
|
78
|
+
"pnpm test"
|
|
79
|
+
],
|
|
80
|
+
"after:bump": "pnpm build"
|
|
81
|
+
},
|
|
82
|
+
"git": {
|
|
83
|
+
"commitMessage": "chore: release v${version}"
|
|
84
|
+
},
|
|
85
|
+
"github": {
|
|
86
|
+
"release": true
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
package/readme.md
CHANGED
|
@@ -312,6 +312,10 @@ jmcconfig.attributes.add('list') // always set this prop as an attribute
|
|
|
312
312
|
| `mount({ … })` | puts values on `globalThis`, handy for debugging in the console |
|
|
313
313
|
| `loggedmethods(o)` | logs each method call of `o` with its arguments and result |
|
|
314
314
|
|
|
315
|
+
## Ready-made components
|
|
316
|
+
|
|
317
|
+
`@snupo/jmc/comps` has input components for common needs: `TextArea` that grows with its text, `NumberStepper`, `RangeSlider`, `Select`, `ComboBox`, `DataListInput`, `Checkbox` with a third state, `ToggleGroup`, `RadioGroup`, `Tabs` and `Icon`. They are light DOM, themed with CSS variables, and follow the model and controller architecture. See [components.md](components.md).
|
|
318
|
+
|
|
315
319
|
## License
|
|
316
320
|
|
|
317
321
|
[MIT](LICENSE)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
@layer jmc {
|
|
2
|
+
.jmc-checkbox {
|
|
3
|
+
--c: var(--jmc-accent, #3b82f6);
|
|
4
|
+
display: inline-flex;
|
|
5
|
+
align-items: center;
|
|
6
|
+
gap: 0.4em;
|
|
7
|
+
cursor: pointer;
|
|
8
|
+
|
|
9
|
+
input {
|
|
10
|
+
appearance: none;
|
|
11
|
+
display: grid;
|
|
12
|
+
place-content: center;
|
|
13
|
+
flex: none; /* a squeezed box would turn a round one into an ellipse */
|
|
14
|
+
width: 1.1em;
|
|
15
|
+
height: 1.1em;
|
|
16
|
+
margin: 0;
|
|
17
|
+
color: var(--jmc-bg, #fff);
|
|
18
|
+
background: var(--jmc-bg, #fff);
|
|
19
|
+
border: 1.5px solid var(--c);
|
|
20
|
+
border-radius: var(--jmc-checkbox-radius, 3px);
|
|
21
|
+
cursor: inherit;
|
|
22
|
+
|
|
23
|
+
&::after {
|
|
24
|
+
content: '';
|
|
25
|
+
width: 0.65em;
|
|
26
|
+
height: 0.65em;
|
|
27
|
+
background: currentColor;
|
|
28
|
+
transform: scale(0);
|
|
29
|
+
}
|
|
30
|
+
&:checked, &:indeterminate { background: var(--c); }
|
|
31
|
+
&:checked::after {
|
|
32
|
+
transform: scale(1);
|
|
33
|
+
clip-path: polygon(14% 44%, 0 65%, 50% 100%, 100% 16%, 80% 0%, 43% 62%);
|
|
34
|
+
}
|
|
35
|
+
&:indeterminate::after {
|
|
36
|
+
transform: scale(1);
|
|
37
|
+
clip-path: polygon(0 40%, 100% 40%, 100% 60%, 0 60%);
|
|
38
|
+
}
|
|
39
|
+
&:focus-visible { outline: 2px solid var(--c); outline-offset: 2px; }
|
|
40
|
+
&:disabled { opacity: 0.4; }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
&.jmc-round input { border-radius: 50%; }
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { jsx } from '../h.js'
|
|
2
|
+
import type { InputHTMLAttributes } from '../jsx.js'
|
|
3
|
+
import { Component, cc } from '../lib.js'
|
|
4
|
+
import './checkbox.css'
|
|
5
|
+
|
|
6
|
+
type Props = Omit<InputHTMLAttributes, 'checked' | 'indeterminate' | 'onchange' | 'oninput' | 'type' | 'value'> & {
|
|
7
|
+
checked: boolean | 'mixed' // 'mixed' is the third state, for a group of which only some are checked
|
|
8
|
+
setchecked: (checked: boolean) => void
|
|
9
|
+
round?: boolean
|
|
10
|
+
children?: any // the label: text or a view
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** a checkbox with a third state. a click on 'mixed' hands out true. class and style go to the label */
|
|
14
|
+
export class Checkbox extends Component<Props> {
|
|
15
|
+
|
|
16
|
+
view() {
|
|
17
|
+
const { checked, setchecked, round, class: cls, style, children, ...rest } = this.props
|
|
18
|
+
return <label class={cc('jmc-checkbox', { 'jmc-round': round }, cls)} style={style}>
|
|
19
|
+
<input type='checkbox' {...rest} checked={checked === true} indeterminate={checked === 'mixed'} onchange={this.onchange} />
|
|
20
|
+
{children}
|
|
21
|
+
</label>
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
onchange(ev: Event) {
|
|
25
|
+
const i = ev.target as HTMLInputElement
|
|
26
|
+
this.props.setchecked(i.checked)
|
|
27
|
+
// the click changed the element. jmc writes only props that changed, so show what the model decided
|
|
28
|
+
i.checked = this.props.checked === true
|
|
29
|
+
i.indeterminate = this.props.checked === 'mixed'
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
@layer jmc {
|
|
2
|
+
.jmc-combobox {
|
|
3
|
+
position: relative;
|
|
4
|
+
display: inline-flex;
|
|
5
|
+
align-items: stretch;
|
|
6
|
+
color: var(--jmc-color, inherit);
|
|
7
|
+
background: var(--jmc-bg, #fff);
|
|
8
|
+
border: 1px solid var(--jmc-border, #cbd5e1);
|
|
9
|
+
border-radius: var(--jmc-radius, 6px);
|
|
10
|
+
|
|
11
|
+
&:focus-within { border-color: var(--jmc-accent, #3b82f6); }
|
|
12
|
+
&:has(input:disabled) { opacity: 0.5; }
|
|
13
|
+
|
|
14
|
+
input {
|
|
15
|
+
flex: 1;
|
|
16
|
+
min-width: 0;
|
|
17
|
+
font: inherit;
|
|
18
|
+
padding: var(--jmc-padding, 0.3em 0.5em);
|
|
19
|
+
color: inherit;
|
|
20
|
+
background: transparent;
|
|
21
|
+
border: none;
|
|
22
|
+
outline: none;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
button {
|
|
26
|
+
padding: 0 0.5em;
|
|
27
|
+
font: inherit;
|
|
28
|
+
color: inherit;
|
|
29
|
+
background: transparent;
|
|
30
|
+
border: none;
|
|
31
|
+
cursor: pointer;
|
|
32
|
+
|
|
33
|
+
&:hover:not(:disabled) { opacity: 0.6; }
|
|
34
|
+
&:disabled { cursor: default; }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
ul {
|
|
38
|
+
position: absolute;
|
|
39
|
+
z-index: 10;
|
|
40
|
+
top: 100%;
|
|
41
|
+
left: -1px; /* over the border */
|
|
42
|
+
min-width: calc(100% + 2px);
|
|
43
|
+
max-height: 14rem;
|
|
44
|
+
margin: 2px 0 0;
|
|
45
|
+
padding: 0.2rem 0;
|
|
46
|
+
overflow-y: auto;
|
|
47
|
+
list-style: none;
|
|
48
|
+
background: var(--jmc-bg, #fff);
|
|
49
|
+
border: 1px solid var(--jmc-border, #cbd5e1);
|
|
50
|
+
border-radius: var(--jmc-radius, 6px);
|
|
51
|
+
box-shadow: 0 4px 12px rgb(0 0 0 / 0.12);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
li {
|
|
55
|
+
padding: 0.25em 0.6em;
|
|
56
|
+
cursor: pointer;
|
|
57
|
+
white-space: nowrap;
|
|
58
|
+
|
|
59
|
+
&:hover, &.active { background: var(--jmc-bg-focus, #f0f7ff); }
|
|
60
|
+
&.selected { font-weight: 600; }
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { jsx } from '../h.js'
|
|
2
|
+
import type { InputHTMLAttributes } from '../jsx.js'
|
|
3
|
+
import { Component, cc } from '../lib.js'
|
|
4
|
+
import './combobox.css'
|
|
5
|
+
|
|
6
|
+
type Props = Omit<InputHTMLAttributes, 'value' | 'oninput' | 'type'> & {
|
|
7
|
+
items: string[]
|
|
8
|
+
value: string
|
|
9
|
+
setvalue: (value: string) => void
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* a text input with a dropdown of suggestions. any text is allowed.
|
|
14
|
+
* opening it (click, arrow button, ArrowDown) shows all items, typing narrows them to those containing the text.
|
|
15
|
+
* open, filter and the highlighted item are the component's own state: nothing else needs them
|
|
16
|
+
*/
|
|
17
|
+
export class ComboBox extends Component<Props> {
|
|
18
|
+
|
|
19
|
+
open = false
|
|
20
|
+
filtering = false // set by typing, cleared by opening: whether the list is narrowed to the text
|
|
21
|
+
active = -1 // index in shown of the highlighted item
|
|
22
|
+
|
|
23
|
+
get shown() {
|
|
24
|
+
const { items, value } = this.props
|
|
25
|
+
return this.filtering ? items.filter(i => i.toLowerCase().includes(value.toLowerCase())) : items
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
view() {
|
|
29
|
+
const { items, value, setvalue, class: cls, ...rest } = this.props
|
|
30
|
+
const shown = this.shown
|
|
31
|
+
return <div class={cc('jmc-combobox', cls)}>
|
|
32
|
+
<input type='text' {...rest} value={value} role='combobox' aria-expanded={this.open} aria-autocomplete='list' autocomplete='off'
|
|
33
|
+
oninput={this.oninput} onkeydown={this.onkeydown} onmousedown={this.openall} onblur={this.close} />
|
|
34
|
+
<button type='button' tabindex={-1} disabled={rest.disabled} onmousedown={this.toggle}>▾</button>
|
|
35
|
+
{this.open && shown.length > 0 && <ul role='listbox' onmousedown={this.keepfocus}>
|
|
36
|
+
{shown.map((s, i) => <li role='option' aria-selected={s == value} class={cc({ active: i == this.active, selected: s == value })} onmousedown={() => this.pick(s)}>{s}</li>)}
|
|
37
|
+
</ul>}
|
|
38
|
+
</div>
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// the dropdown must not take the focus from the input, or the blur would close it before a click lands
|
|
42
|
+
keepfocus(ev: Event) { ev.preventDefault() }
|
|
43
|
+
|
|
44
|
+
openall() {
|
|
45
|
+
this.open = true
|
|
46
|
+
this.filtering = false
|
|
47
|
+
this.active = -1
|
|
48
|
+
this.updateview()
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
toggle(ev: Event) {
|
|
52
|
+
ev.preventDefault() // keeps the focus where it is
|
|
53
|
+
if (this.open) return this.close()
|
|
54
|
+
this.input.focus()
|
|
55
|
+
this.openall()
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
close() {
|
|
59
|
+
this.open = false
|
|
60
|
+
this.updateview()
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
pick(s: string) {
|
|
64
|
+
this.props.setvalue(s)
|
|
65
|
+
this.close()
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
oninput(ev: Event) {
|
|
69
|
+
this.open = true
|
|
70
|
+
this.filtering = true
|
|
71
|
+
this.active = -1
|
|
72
|
+
this.props.setvalue((ev.target as HTMLInputElement).value)
|
|
73
|
+
this.updateview()
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
onkeydown(ev: KeyboardEvent) {
|
|
77
|
+
const n = this.shown.length
|
|
78
|
+
switch (ev.key) {
|
|
79
|
+
case 'ArrowDown':
|
|
80
|
+
case 'ArrowUp': {
|
|
81
|
+
ev.preventDefault()
|
|
82
|
+
if (!this.open) return this.openall()
|
|
83
|
+
const step = ev.key == 'ArrowDown' ? 1 : -1
|
|
84
|
+
this.active = n == 0 ? -1 : (this.active + step + n) % n
|
|
85
|
+
this.updateview()
|
|
86
|
+
this.element.querySelector('.active')?.scrollIntoView?.({ block: 'nearest' })
|
|
87
|
+
break
|
|
88
|
+
}
|
|
89
|
+
case 'Enter':
|
|
90
|
+
if (this.open && this.active >= 0) { ev.preventDefault(); this.pick(this.shown[this.active]) }
|
|
91
|
+
else if (this.open) this.close()
|
|
92
|
+
break
|
|
93
|
+
case 'Escape':
|
|
94
|
+
if (this.open) { ev.preventDefault(); this.close() }
|
|
95
|
+
break
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
get input() { return this.element.querySelector('input')! }
|
|
100
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
declare module '*.css'
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
@layer jmc {
|
|
2
|
+
.jmc-datalist-input {
|
|
3
|
+
display: inline-block;
|
|
4
|
+
|
|
5
|
+
input {
|
|
6
|
+
box-sizing: border-box;
|
|
7
|
+
width: 100%;
|
|
8
|
+
font: inherit;
|
|
9
|
+
padding: var(--jmc-padding, 0.3em 0.5em);
|
|
10
|
+
color: var(--jmc-color, inherit);
|
|
11
|
+
background: var(--jmc-bg, #fff);
|
|
12
|
+
border: 1px solid var(--jmc-border, #cbd5e1);
|
|
13
|
+
border-radius: var(--jmc-radius, 6px);
|
|
14
|
+
|
|
15
|
+
&:focus {
|
|
16
|
+
outline: none;
|
|
17
|
+
border-color: var(--jmc-accent, #3b82f6);
|
|
18
|
+
}
|
|
19
|
+
&:disabled { opacity: 0.5; }
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { jsx } from '../h.js'
|
|
2
|
+
import type { InputHTMLAttributes } from '../jsx.js'
|
|
3
|
+
import { Component, cc } from '../lib.js'
|
|
4
|
+
import './datalistinput.css'
|
|
5
|
+
|
|
6
|
+
type Props = Omit<InputHTMLAttributes, 'value' | 'oninput' | 'list' | 'type'> & {
|
|
7
|
+
items: string[]
|
|
8
|
+
value: string
|
|
9
|
+
setvalue: (value: string) => void
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
let counter = 0
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* a text input with the browser's own suggestions: a native input with a datalist. any text is allowed.
|
|
16
|
+
* the simple alternative to ComboBox, and the better one on mobile. the browser narrows the suggestions
|
|
17
|
+
* to those matching the text, so with a filled-in value it shows only that one
|
|
18
|
+
*/
|
|
19
|
+
export class DataListInput extends Component<Props> {
|
|
20
|
+
|
|
21
|
+
// the input finds its list by id only, so every instance needs its own
|
|
22
|
+
listid = `jmc-list-${counter++}`
|
|
23
|
+
|
|
24
|
+
view() {
|
|
25
|
+
const { items, value, setvalue, class: cls, ...rest } = this.props
|
|
26
|
+
return <div class={cc('jmc-datalist-input', cls)}>
|
|
27
|
+
<input type='text' {...rest} list={this.listid} value={value} oninput={this.oninput} />
|
|
28
|
+
<datalist id={this.listid}>{items.map(i => <option value={i} />)}</datalist>
|
|
29
|
+
</div>
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
oninput(ev: Event) {
|
|
33
|
+
this.props.setvalue((ev.target as HTMLInputElement).value)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { jsx } from '../h.js'
|
|
2
|
+
import { cc } from '../lib.js'
|
|
3
|
+
import './icon.css'
|
|
4
|
+
|
|
5
|
+
/** the icon format of lucide (IconNode): a list of [tag, attributes] */
|
|
6
|
+
export type IconNode = [tag: string, attrs: Record<string, string | number>][]
|
|
7
|
+
|
|
8
|
+
/** an svg icon from a lucide icon node. it is drawn in the text color and sized by size, in px */
|
|
9
|
+
export const Icon = ({ icon, size = 18, stroke = 2, class: cls }: { icon: IconNode, size?: number, stroke?: number, class?: string }) =>
|
|
10
|
+
<svg class={cc('jmc-icon', cls)} xmlns='http://www.w3.org/2000/svg' width={size} height={size} viewBox='0 0 24 24'
|
|
11
|
+
fill='none' stroke='currentColor' stroke-width={stroke} stroke-linecap='round' stroke-linejoin='round'>
|
|
12
|
+
{icon.map(([tag, attrs]) => jsx(tag, attrs))}
|
|
13
|
+
</svg>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from './checkbox.js'
|
|
2
|
+
export * from './combobox.js'
|
|
3
|
+
export * from './datalistinput.js'
|
|
4
|
+
export * from './icon.js'
|
|
5
|
+
export * from './numberstepper.js'
|
|
6
|
+
export * from './radiogroup.js'
|
|
7
|
+
export * from './rangeslider.js'
|
|
8
|
+
export * from './select.js'
|
|
9
|
+
export * from './tabs.js'
|
|
10
|
+
export * from './textarea.js'
|
|
11
|
+
export * from './togglegroup.js'
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
@layer jmc {
|
|
2
|
+
.jmc-stepper {
|
|
3
|
+
display: inline-flex;
|
|
4
|
+
align-items: stretch;
|
|
5
|
+
width: var(--jmc-stepper-width, 5.5rem);
|
|
6
|
+
overflow: hidden;
|
|
7
|
+
font: inherit;
|
|
8
|
+
color: var(--jmc-color, inherit);
|
|
9
|
+
background: var(--jmc-bg, #fff);
|
|
10
|
+
border: 1px solid var(--jmc-border, #cbd5e1);
|
|
11
|
+
border-radius: var(--jmc-radius, 6px);
|
|
12
|
+
|
|
13
|
+
button {
|
|
14
|
+
padding: 0 0.4em;
|
|
15
|
+
font: inherit;
|
|
16
|
+
color: inherit;
|
|
17
|
+
background: var(--jmc-bg-button, #eee);
|
|
18
|
+
border: none;
|
|
19
|
+
cursor: pointer;
|
|
20
|
+
user-select: none;
|
|
21
|
+
|
|
22
|
+
&:hover:not(:disabled) { opacity: 0.6; }
|
|
23
|
+
&:disabled { cursor: default; }
|
|
24
|
+
&:focus-visible { outline: none; color: #fff; background: var(--jmc-accent, #3b82f6); } /* the control's border shows the focus. a ring would be clipped by the rounded corners */
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
input {
|
|
28
|
+
flex: 1;
|
|
29
|
+
min-width: 0;
|
|
30
|
+
padding: 0;
|
|
31
|
+
font: inherit;
|
|
32
|
+
text-align: center;
|
|
33
|
+
color: inherit;
|
|
34
|
+
background: transparent;
|
|
35
|
+
border: none;
|
|
36
|
+
outline: none;
|
|
37
|
+
|
|
38
|
+
&:focus { background: var(--jmc-bg-focus, #f0f7ff); }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
&:focus-within { border-color: var(--jmc-accent, #3b82f6); }
|
|
42
|
+
&.disabled { opacity: 0.5; }
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { jsx } from '../h.js'
|
|
2
|
+
import { Component, cc } from '../lib.js'
|
|
3
|
+
import './numberstepper.css'
|
|
4
|
+
|
|
5
|
+
type Props = {
|
|
6
|
+
value: number
|
|
7
|
+
setvalue: (value: number) => void
|
|
8
|
+
min?: number
|
|
9
|
+
max?: number
|
|
10
|
+
step?: number // default 1
|
|
11
|
+
decimals?: number // default: the decimals of step
|
|
12
|
+
class?: string
|
|
13
|
+
disabled?: boolean
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const decimalsof = (x: number) => (String(x).split('.')[1] ?? '').length
|
|
17
|
+
|
|
18
|
+
const round = (v: number, decimals: number) => {
|
|
19
|
+
const factor = 10 ** decimals
|
|
20
|
+
return Math.round((v + Number.EPSILON) * factor) / factor
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** a number input with minus and plus buttons */
|
|
24
|
+
export class NumberStepper extends Component<Props> {
|
|
25
|
+
|
|
26
|
+
view() {
|
|
27
|
+
const { disabled, class: cls } = this.props
|
|
28
|
+
return <div class={cc('jmc-stepper', { disabled }, cls)}>
|
|
29
|
+
<button type='button' disabled={disabled} onclick={this.ondecrement}>−</button>
|
|
30
|
+
<input type='text' disabled={disabled} value={this.text} onchange={this.onchange} onkeydown={this.onkeydown} />
|
|
31
|
+
<button type='button' disabled={disabled} onclick={this.onincrement}>+</button>
|
|
32
|
+
</div>
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
get step() { return this.props.step ?? 1 }
|
|
36
|
+
get decimals() { return this.props.decimals ?? decimalsof(this.step) }
|
|
37
|
+
get text() { return this.props.value.toFixed(this.decimals) }
|
|
38
|
+
|
|
39
|
+
// min and max bound the buttons only while the value is inside them. typed text may leave the range,
|
|
40
|
+
// and from there the buttons step freely
|
|
41
|
+
increment(by: number) {
|
|
42
|
+
const { value, min = -Infinity, max = Infinity } = this.props
|
|
43
|
+
let v = round(value + by, this.decimals)
|
|
44
|
+
if (value >= min && value <= max) v = Math.min(max, Math.max(min, v))
|
|
45
|
+
this.props.setvalue(v)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// fires on blur and enter. invalid text reverts to the current value, valid text is taken unclamped
|
|
49
|
+
onchange(ev: Event) {
|
|
50
|
+
const i = ev.target as HTMLInputElement
|
|
51
|
+
const v = Number(i.value.trim().replace(',', '.'))
|
|
52
|
+
if (i.value.trim() && Number.isFinite(v)) this.props.setvalue(round(v, this.decimals))
|
|
53
|
+
i.value = this.text // jmc only writes a value when it changed, so rejected or rounded text would stay otherwise
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
onkeydown(ev: KeyboardEvent) {
|
|
57
|
+
const i = ev.target as HTMLInputElement
|
|
58
|
+
switch (ev.key) {
|
|
59
|
+
case 'Enter': ev.preventDefault(); i.blur(); break // blur fires change. preventDefault keeps a form from submitting
|
|
60
|
+
case 'Escape': i.value = this.text; i.blur(); break
|
|
61
|
+
// steps commit pending text first, so they count from what was typed
|
|
62
|
+
case '+':
|
|
63
|
+
case 'ArrowUp': ev.preventDefault(); this.onchange(ev); this.onincrement(); i.value = this.text; break
|
|
64
|
+
case '-':
|
|
65
|
+
case 'ArrowDown': ev.preventDefault(); this.onchange(ev); this.ondecrement(); i.value = this.text; break
|
|
66
|
+
// digits and decimal separators only. other single chars are dropped, shortcuts (ctrl, meta) and named keys pass
|
|
67
|
+
default: if (ev.key.length == 1 && !ev.ctrlKey && !ev.metaKey && !/[\d.,]/.test(ev.key)) ev.preventDefault()
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
onincrement() { this.increment(this.step) }
|
|
72
|
+
ondecrement() { this.increment(-this.step) }
|
|
73
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
@layer jmc {
|
|
2
|
+
.jmc-radio-group {
|
|
3
|
+
display: inline-flex;
|
|
4
|
+
flex-wrap: wrap;
|
|
5
|
+
gap: 0.3em 1em;
|
|
6
|
+
|
|
7
|
+
label {
|
|
8
|
+
display: inline-flex;
|
|
9
|
+
align-items: center;
|
|
10
|
+
gap: 0.4em;
|
|
11
|
+
cursor: pointer;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
input {
|
|
15
|
+
margin: 0;
|
|
16
|
+
accent-color: var(--jmc-accent, #3b82f6);
|
|
17
|
+
cursor: inherit;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|