@snupo/jmc 1.4.2 → 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.
Files changed (103) hide show
  1. package/components.md +251 -0
  2. package/dist/comps/checkbox.css +45 -0
  3. package/dist/comps/checkbox.d.ts +16 -0
  4. package/dist/comps/checkbox.d.ts.map +1 -0
  5. package/dist/comps/checkbox.js +20 -0
  6. package/dist/comps/checkbox.js.map +1 -0
  7. package/dist/comps/combobox.css +63 -0
  8. package/dist/comps/combobox.d.ts +30 -0
  9. package/dist/comps/combobox.d.ts.map +1 -0
  10. package/dist/comps/combobox.js +87 -0
  11. package/dist/comps/combobox.js.map +1 -0
  12. package/dist/comps/datalistinput.css +22 -0
  13. package/dist/comps/datalistinput.d.ts +20 -0
  14. package/dist/comps/datalistinput.d.ts.map +1 -0
  15. package/dist/comps/datalistinput.js +23 -0
  16. package/dist/comps/datalistinput.js.map +1 -0
  17. package/dist/comps/icon.css +6 -0
  18. package/dist/comps/icon.d.ts +11 -0
  19. package/dist/comps/icon.d.ts.map +1 -0
  20. package/dist/comps/icon.js +6 -0
  21. package/dist/comps/icon.js.map +1 -0
  22. package/dist/comps/index.d.ts +12 -0
  23. package/dist/comps/index.d.ts.map +1 -0
  24. package/dist/comps/index.js +12 -0
  25. package/dist/comps/index.js.map +1 -0
  26. package/dist/comps/numberstepper.css +44 -0
  27. package/dist/comps/numberstepper.d.ts +26 -0
  28. package/dist/comps/numberstepper.d.ts.map +1 -0
  29. package/dist/comps/numberstepper.js +72 -0
  30. package/dist/comps/numberstepper.js.map +1 -0
  31. package/dist/comps/radiogroup.css +20 -0
  32. package/dist/comps/radiogroup.d.ts +19 -0
  33. package/dist/comps/radiogroup.d.ts.map +1 -0
  34. package/dist/comps/radiogroup.js +21 -0
  35. package/dist/comps/radiogroup.js.map +1 -0
  36. package/dist/comps/rangeslider.css +46 -0
  37. package/dist/comps/rangeslider.d.ts +18 -0
  38. package/dist/comps/rangeslider.d.ts.map +1 -0
  39. package/dist/comps/rangeslider.js +15 -0
  40. package/dist/comps/rangeslider.js.map +1 -0
  41. package/dist/comps/select.css +16 -0
  42. package/dist/comps/select.d.ts +19 -0
  43. package/dist/comps/select.d.ts.map +1 -0
  44. package/dist/comps/select.js +21 -0
  45. package/dist/comps/select.js.map +1 -0
  46. package/dist/comps/tabs.css +22 -0
  47. package/dist/comps/tabs.d.ts +15 -0
  48. package/dist/comps/tabs.d.ts.map +1 -0
  49. package/dist/comps/tabs.js +11 -0
  50. package/dist/comps/tabs.js.map +1 -0
  51. package/dist/comps/textarea.css +19 -0
  52. package/dist/comps/textarea.d.ts +17 -0
  53. package/dist/comps/textarea.d.ts.map +1 -0
  54. package/dist/comps/textarea.js +32 -0
  55. package/dist/comps/textarea.js.map +1 -0
  56. package/dist/comps/togglegroup.css +29 -0
  57. package/dist/comps/togglegroup.d.ts +16 -0
  58. package/dist/comps/togglegroup.d.ts.map +1 -0
  59. package/dist/comps/togglegroup.js +11 -0
  60. package/dist/comps/togglegroup.js.map +1 -0
  61. package/dist/h.d.ts +2 -1
  62. package/dist/h.d.ts.map +1 -1
  63. package/dist/h.js.map +1 -1
  64. package/dist/jmc.d.ts.map +1 -1
  65. package/dist/jmc.js +1 -2
  66. package/dist/jmc.js.map +1 -1
  67. package/dist/jsx.d.ts +1 -0
  68. package/dist/jsx.d.ts.map +1 -1
  69. package/dist/jsx.js.map +1 -1
  70. package/dist/lib.d.ts +4 -3
  71. package/dist/lib.d.ts.map +1 -1
  72. package/dist/lib.js +1 -1
  73. package/dist/lib.js.map +1 -1
  74. package/package.json +89 -76
  75. package/readme.md +4 -0
  76. package/src/comps/checkbox.css +45 -0
  77. package/src/comps/checkbox.tsx +31 -0
  78. package/src/comps/combobox.css +63 -0
  79. package/src/comps/combobox.tsx +100 -0
  80. package/src/comps/css.d.ts +1 -0
  81. package/src/comps/datalistinput.css +22 -0
  82. package/src/comps/datalistinput.tsx +35 -0
  83. package/src/comps/icon.css +6 -0
  84. package/src/comps/icon.tsx +13 -0
  85. package/src/comps/index.ts +11 -0
  86. package/src/comps/numberstepper.css +44 -0
  87. package/src/comps/numberstepper.tsx +73 -0
  88. package/src/comps/radiogroup.css +20 -0
  89. package/src/comps/radiogroup.tsx +40 -0
  90. package/src/comps/rangeslider.css +46 -0
  91. package/src/comps/rangeslider.tsx +27 -0
  92. package/src/comps/select.css +16 -0
  93. package/src/comps/select.tsx +36 -0
  94. package/src/comps/tabs.css +22 -0
  95. package/src/comps/tabs.tsx +22 -0
  96. package/src/comps/textarea.css +19 -0
  97. package/src/comps/textarea.tsx +41 -0
  98. package/src/comps/togglegroup.css +29 -0
  99. package/src/comps/togglegroup.tsx +23 -0
  100. package/src/h.ts +2 -3
  101. package/src/jmc.ts +1 -2
  102. package/src/jsx.ts +1 -0
  103. package/src/lib.ts +2 -2
@@ -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}>&#9662;</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,6 @@
1
+ @layer jmc {
2
+ .jmc-icon {
3
+ flex: none;
4
+ vertical-align: middle;
5
+ }
6
+ }
@@ -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}>&minus;</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}>&#43;</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
+ }
@@ -0,0 +1,40 @@
1
+ import { jsx } from '../h.js'
2
+ import { Component, cc } from '../lib.js'
3
+ import './radiogroup.css'
4
+
5
+ type Props<T> = {
6
+ items: T[]
7
+ value: T | undefined
8
+ setvalue: (item: T) => void
9
+ label?: (item: T) => any // default: String(item)
10
+ color?: (item: T) => string | undefined // tints the item's radio
11
+ class?: string
12
+ disabled?: boolean
13
+ }
14
+
15
+ let counter = 0
16
+
17
+ /** native radio inputs, one per item, in a row. a radio needs a group name to be exclusive, unique per instance */
18
+ export class RadioGroup<T = any> extends Component<Props<T>> {
19
+
20
+ name = `jmc-radio-${counter++}`
21
+
22
+ view() {
23
+ const { items, value, setvalue, label = String, color, class: cls, disabled } = this.props
24
+ return <div role='radiogroup' class={cc('jmc-radio-group', cls)}>
25
+ {items.map(i =>
26
+ <label>
27
+ <input type='radio' name={this.name} checked={i === value} disabled={disabled}
28
+ style={color?.(i) ? `accent-color:${color(i)}` : undefined} onchange={() => this.onchange(i)} />
29
+ {label(i)}
30
+ </label>)}
31
+ </div>
32
+ }
33
+
34
+ onchange(item: T) {
35
+ this.props.setvalue(item)
36
+ // the click checked the radio. jmc writes only props that changed, so show what the model decided
37
+ const { items, value } = this.props
38
+ this.element.querySelectorAll('input').forEach((r, n) => r.checked = items[n] === value)
39
+ }
40
+ }
@@ -0,0 +1,46 @@
1
+ @layer jmc {
2
+ .jmc-range-slider {
3
+ --track: var(--jmc-track-height, 7px);
4
+ --thumb: var(--jmc-thumb-size, 20px);
5
+ appearance: none;
6
+ width: var(--jmc-slider-width, 10rem);
7
+ background: transparent;
8
+ cursor: pointer;
9
+
10
+ &:focus { outline: none; }
11
+ &:disabled { opacity: 0.5; cursor: default; }
12
+
13
+ &::-webkit-slider-runnable-track {
14
+ height: var(--track);
15
+ background: var(--jmc-track, #555);
16
+ border-radius: 4px;
17
+ }
18
+ &::-moz-range-track {
19
+ height: var(--track);
20
+ background: var(--jmc-track, #555);
21
+ border-radius: 4px;
22
+ }
23
+
24
+ &::-webkit-slider-thumb {
25
+ appearance: none;
26
+ box-sizing: border-box;
27
+ width: var(--thumb);
28
+ height: var(--thumb);
29
+ margin-top: calc((var(--track) - var(--thumb)) / 2); /* centers the thumb on the track */
30
+ background: var(--jmc-bg, #fff);
31
+ border: 3px solid var(--jmc-thumb, #1c1c1c);
32
+ border-radius: 50%;
33
+ }
34
+ &::-moz-range-thumb {
35
+ box-sizing: border-box;
36
+ width: var(--thumb);
37
+ height: var(--thumb);
38
+ background: var(--jmc-bg, #fff);
39
+ border: 3px solid var(--jmc-thumb, #1c1c1c);
40
+ border-radius: 50%;
41
+ }
42
+
43
+ &:focus-visible::-webkit-slider-thumb { border-color: var(--jmc-accent, #3b82f6); }
44
+ &:focus-visible::-moz-range-thumb { border-color: var(--jmc-accent, #3b82f6); }
45
+ }
46
+ }
@@ -0,0 +1,27 @@
1
+ import { jsx } from '../h.js'
2
+ import { Component, cc } from '../lib.js'
3
+ import './rangeslider.css'
4
+
5
+ type Props = {
6
+ value: number
7
+ setvalue: (value: number) => void
8
+ min?: number
9
+ max?: number
10
+ step?: number
11
+ class?: string
12
+ disabled?: boolean
13
+ }
14
+
15
+ /** a styled native range input. setvalue is called while dragging */
16
+ export class RangeSlider extends Component<Props> {
17
+
18
+ view() {
19
+ const { value, min, max, step, class: cls, disabled } = this.props
20
+ // value comes last: the browser clamps it to the range, so min, max and step must be set before
21
+ return <input type='range' class={cc('jmc-range-slider', cls)} disabled={disabled} min={min} max={max} step={step} value={value} oninput={this.oninput} />
22
+ }
23
+
24
+ oninput(ev: Event) {
25
+ this.props.setvalue(parseFloat((ev.target as HTMLInputElement).value))
26
+ }
27
+ }
@@ -0,0 +1,16 @@
1
+ @layer jmc {
2
+ .jmc-select {
3
+ font: inherit;
4
+ padding: var(--jmc-padding, 0.3em 0.5em);
5
+ color: var(--jmc-color, inherit);
6
+ background: var(--jmc-bg, #fff);
7
+ border: 1px solid var(--jmc-border, #cbd5e1);
8
+ border-radius: var(--jmc-radius, 6px);
9
+
10
+ &:focus {
11
+ outline: none;
12
+ border-color: var(--jmc-accent, #3b82f6);
13
+ }
14
+ &:disabled { opacity: 0.5; }
15
+ }
16
+ }
@@ -0,0 +1,36 @@
1
+ import { jsx } from '../h.js'
2
+ import type { SelectHTMLAttributes } from '../jsx.js'
3
+ import { Component, cc } from '../lib.js'
4
+ import './select.css'
5
+
6
+ type Props<T> = Omit<SelectHTMLAttributes, 'value' | 'onchange' | 'oninput'> & {
7
+ items: T[]
8
+ value: T | undefined
9
+ setvalue: (item: T) => void
10
+ label?: (item: T) => string // default: String(item)
11
+ }
12
+
13
+ /** a native select over any items. it hands out the item, not text */
14
+ export class Select<T = any> extends Component<Props<T>> {
15
+
16
+ view() {
17
+ const { items, value, setvalue, label = String, class: cls, ...rest } = this.props
18
+ return <select {...rest} class={cc('jmc-select', cls)} onchange={this.onchange}>
19
+ {items.map(i => <option>{label(i)}</option>)}
20
+ </select>
21
+ }
22
+
23
+ mounted() { this.select() }
24
+
25
+ // runs before the render. the options exist only after it, so the selection is set after it
26
+ update() { queueMicrotask(this.select) }
27
+
28
+ // the options are plain, so the item is found by position. no value strings to match
29
+ select() {
30
+ (this.element as HTMLSelectElement).selectedIndex = this.props.items.indexOf(this.props.value as T)
31
+ }
32
+
33
+ onchange(ev: Event) {
34
+ this.props.setvalue(this.props.items[(ev.target as HTMLSelectElement).selectedIndex])
35
+ }
36
+ }
@@ -0,0 +1,22 @@
1
+ @layer jmc {
2
+ .jmc-tabs {
3
+ display: flex;
4
+ gap: 0.2em;
5
+ border-bottom: 1px solid var(--jmc-border, #cbd5e1);
6
+
7
+ button {
8
+ padding: var(--jmc-padding, 0.4em 0.9em);
9
+ font: inherit;
10
+ color: var(--jmc-color, inherit);
11
+ background: transparent;
12
+ border: none;
13
+ border-bottom: 2px solid transparent;
14
+ margin-bottom: -1px; /* the selected underline covers the bar's border */
15
+ cursor: pointer;
16
+
17
+ &:hover { background: var(--jmc-bg-focus, #f0f7ff); }
18
+ &.selected { color: var(--jmc-accent, #3b82f6); border-bottom-color: var(--jmc-accent, #3b82f6); }
19
+ &:focus-visible { outline: none; color: #fff; background: var(--jmc-accent, #3b82f6); } /* after selected, so it wins */
20
+ }
21
+ }
22
+ }
@@ -0,0 +1,22 @@
1
+ import { jsx } from '../h.js'
2
+ import { Component, cc } from '../lib.js'
3
+ import './tabs.css'
4
+
5
+ type Props<T> = {
6
+ items: T[]
7
+ value: T | undefined
8
+ setvalue: (item: T) => void
9
+ label?: (item: T) => any // default: String(item)
10
+ class?: string
11
+ }
12
+
13
+ /** the tab bar only: the content below it belongs to the app, which shows what the selected tab says */
14
+ export class Tabs<T = any> extends Component<Props<T>> {
15
+
16
+ view() {
17
+ const { items, value, setvalue, label = String, class: cls } = this.props
18
+ return <nav role='tablist' class={cc('jmc-tabs', cls)}>
19
+ {items.map(i => <button type='button' role='tab' aria-selected={i === value} class={cc({ selected: i === value })} onclick={() => setvalue(i)}>{label(i)}</button>)}
20
+ </nav>
21
+ }
22
+ }
@@ -0,0 +1,19 @@
1
+ @layer jmc {
2
+ .jmc-textarea {
3
+ box-sizing: border-box;
4
+ width: 100%;
5
+ resize: none;
6
+ overflow: hidden;
7
+ font: inherit;
8
+ padding: var(--jmc-padding, 0.35em 0.5em);
9
+ color: var(--jmc-color, inherit);
10
+ background: var(--jmc-bg, #fff);
11
+ border: 1px solid var(--jmc-border, #cbd5e1);
12
+ border-radius: var(--jmc-radius, 6px);
13
+
14
+ &:focus {
15
+ outline: none;
16
+ border-color: var(--jmc-accent, #3b82f6);
17
+ }
18
+ }
19
+ }
@@ -0,0 +1,41 @@
1
+ import { jsx } from '../h.js'
2
+ import type { TextareaHTMLAttributes } from '../jsx.js'
3
+ import { Component, cc } from '../lib.js'
4
+ import './textarea.css'
5
+
6
+ type Props = Omit<TextareaHTMLAttributes, 'value' | 'oninput'> & {
7
+ value: string
8
+ setvalue?: (value: string) => void
9
+ }
10
+
11
+ /** a textarea that grows and shrinks with its text */
12
+ export class TextArea extends Component<Props> {
13
+
14
+ view() {
15
+ const { value, setvalue, class: cls, ...rest } = this.props
16
+ return <textarea rows={1} {...rest} class={cc('jmc-textarea', cls)} value={value} oninput={this.oninput} />
17
+ }
18
+
19
+ mounted() {
20
+ // fires once the element has a size: when mounted, when shown after being hidden, and when its width changes (the text wraps anew)
21
+ new ResizeObserver(this.adjust).observe(this.element)
22
+ if (this.props.autofocus) this.element.focus()
23
+ }
24
+
25
+ // runs before the render. the new value is set by then, so the height is adjusted after it
26
+ update() {
27
+ queueMicrotask(this.adjust)
28
+ }
29
+
30
+ oninput(ev: Event) {
31
+ this.props.setvalue?.((ev.target as HTMLTextAreaElement).value)
32
+ this.adjust()
33
+ }
34
+
35
+ adjust() {
36
+ const t = this.element as HTMLTextAreaElement
37
+ if (!t.clientHeight) return // hidden. the observer calls again when it is shown
38
+ t.style.height = 'auto' // so it shrinks too: scrollHeight is never below the current height
39
+ t.style.height = t.scrollHeight + t.offsetHeight - t.clientHeight + 'px' // plus the borders
40
+ }
41
+ }
@@ -0,0 +1,29 @@
1
+ @layer jmc {
2
+ .jmc-toggle-group {
3
+ display: inline-flex;
4
+ overflow: hidden;
5
+ border: 1px solid var(--jmc-border, #cbd5e1);
6
+ border-radius: var(--jmc-radius, 6px);
7
+
8
+ button {
9
+ display: inline-flex;
10
+ align-items: center;
11
+ justify-content: center;
12
+ padding: var(--jmc-padding, 0.3em 0.7em);
13
+ font: inherit;
14
+ color: var(--jmc-color, inherit);
15
+ background: var(--jmc-bg, #fff);
16
+ border: none;
17
+ border-left: 1px solid var(--jmc-border, #cbd5e1);
18
+ cursor: pointer;
19
+
20
+ &:first-child { border-left: none; }
21
+ &:hover:not(.selected) { background: var(--jmc-bg-focus, #f0f7ff); }
22
+ &.selected { color: #fff; background: var(--jmc-accent, #3b82f6); }
23
+ /* keyboard focus: the item takes the accent, a selected one gets darker. an outline would be clipped by the rounded corners */
24
+ &:focus-visible { outline: none; color: #fff; background: var(--jmc-accent, #3b82f6); }
25
+ &.selected:focus-visible { background: color-mix(in srgb, var(--jmc-accent, #3b82f6), #000 30%); }
26
+ &:disabled { opacity: 0.5; cursor: default; }
27
+ }
28
+ }
29
+ }
@@ -0,0 +1,23 @@
1
+ import { jsx } from '../h.js'
2
+ import { Component, cc } from '../lib.js'
3
+ import './togglegroup.css'
4
+
5
+ type Props<T> = {
6
+ items: T[]
7
+ value: T | undefined
8
+ setvalue: (item: T) => void
9
+ label?: (item: T) => any // text or any view, such as an Icon. default: String(item)
10
+ class?: string
11
+ disabled?: boolean
12
+ }
13
+
14
+ /** a row of buttons, of which exactly one is selected: a segmented control. keyboard accessible, as the items are buttons */
15
+ export class ToggleGroup<T = any> extends Component<Props<T>> {
16
+
17
+ view() {
18
+ const { items, value, setvalue, label = String, class: cls, disabled } = this.props
19
+ return <div role='radiogroup' class={cc('jmc-toggle-group', cls)}>
20
+ {items.map(i => <button type='button' role='radio' aria-checked={i === value} disabled={disabled} class={cc({ selected: i === value })} onclick={() => setvalue(i)}>{label(i)}</button>)}
21
+ </div>
22
+ }
23
+ }