@cssints/forms 0.0.0-stage → 0.49.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 +21 -0
- package/README.md +155 -2
- package/dist/index.d.mts +97 -0
- package/dist/index.mjs +69 -0
- package/dist/runtime.d.mts +355 -0
- package/dist/runtime.mjs +277 -0
- package/package.json +37 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alexei Accio
|
|
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
CHANGED
|
@@ -1,3 +1,156 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @cssints/forms
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Native form controls, drawn from the theme: a checkbox, a radio, a switch for the engines that have none, and the accent
|
|
4
|
+
of a range and a progress. The elements stay the platform's; what it draws (the tick, the dot, the rail) is ours.
|
|
5
|
+
|
|
6
|
+
```sh
|
|
7
|
+
bun add @cssints/forms
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
## The factory
|
|
11
|
+
|
|
12
|
+
`forms()` reads the theme once and gives a class per control. It is a definition site, like `@cssints/fonts`' `font()`
|
|
13
|
+
and `@cssints/placeholder`' `placeholder()`: call it in a module of its own, imported with the attribute, and call its
|
|
14
|
+
members where the control is used.
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
// forms.ts
|
|
18
|
+
import { forms } from "@cssints/forms" with { type: "cssints" };
|
|
19
|
+
import { t } from "./theme.ts" with { type: "cssints" };
|
|
20
|
+
|
|
21
|
+
export const f = forms({
|
|
22
|
+
accent: t.color.highlight, // the on colour
|
|
23
|
+
surface: t.color.surface, // the box, the thumb, the radio's gap
|
|
24
|
+
line: t.color.border, // the track, the unchecked border
|
|
25
|
+
ink: t.color.onHighlight, // the tick, the dash, the radio's dot
|
|
26
|
+
radius: t.radius.sm, // optional: the checkbox's corner (default .25em)
|
|
27
|
+
size: "1.1em", // optional: the control's square (default 1.1em)
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
```tsx
|
|
32
|
+
// the control
|
|
33
|
+
import { css } from "cssints" with { type: "cssints" };
|
|
34
|
+
import { f } from "./forms.ts" with { type: "cssints" };
|
|
35
|
+
|
|
36
|
+
<label class={css.cn(css.display("inline-flex"), css.gap("0.5rem"))}>
|
|
37
|
+
<input type="checkbox" checked class={css.cn(f.checkbox())} />
|
|
38
|
+
Wi-Fi
|
|
39
|
+
</label>;
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Every class goes on its own control: a plugin cannot style a descendant by type (`within()` takes a state wrapper or
|
|
43
|
+
`attr()`, not a selector), so there is no container to wrap a row, and the members are per element — the same reason
|
|
44
|
+
`@cssints/headless` returns attribute objects per element.
|
|
45
|
+
|
|
46
|
+
| Member | Element | What it draws |
|
|
47
|
+
| ------------ | -------------------------------- | ------------------------------------------------------------------------------- |
|
|
48
|
+
| `checkbox()` | `<input type="checkbox">` | A box, the tick on `:checked`, a dash on `:indeterminate`, faded on `:disabled` |
|
|
49
|
+
| `radio()` | `<input type="radio">` | A ring with a dot of the accent in it, on `:checked` |
|
|
50
|
+
| `switch()` | `<input type="checkbox" switch>` | A rail and a thumb, for engines that draw none (see below) |
|
|
51
|
+
| `range()` | `<input type="range">` | `accent-color` alone |
|
|
52
|
+
| `progress()` | `<progress>` | `accent-color` alone |
|
|
53
|
+
|
|
54
|
+
## The switch is a fallback
|
|
55
|
+
|
|
56
|
+
`accent-color` already tints Safari's own `switch` control, and a CSS-only package cannot ask whether an engine draws
|
|
57
|
+
`<input switch>`: there is no `@supports` for an attribute's implementation, and `appearance: none` — the first line
|
|
58
|
+
of `switch()` — is exactly what would erase it. So the decision is the host's, asked at run time:
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import { hasNativeSwitch } from "@cssints/forms/runtime";
|
|
62
|
+
|
|
63
|
+
const native = hasNativeSwitch(); // "switch" in HTMLInputElement.prototype
|
|
64
|
+
<input type="checkbox" switch class={css.cn(native ? undefined : f.switch())} />;
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Where it is `true` the input keeps the platform's switch, coloured by the accent; where it is `false` the class draws
|
|
68
|
+
the rail: the thumb of `surface` on the leading side, the track of `line` on the trailing one, and the accent track
|
|
69
|
+
when it is on. `:dir(rtl)` mirrors both. The predicate lives on a subpath (`@cssints/forms/runtime`) because an import
|
|
70
|
+
with the attribute is build-time and the engine removes it from the client. In SSR the predicate reads `false` on the
|
|
71
|
+
server and `true` in a browser with the attribute, so the class the first paint carries and the class the browser wants
|
|
72
|
+
differ: render the switch on the client, or accept the mismatch.
|
|
73
|
+
|
|
74
|
+
## The rail drag
|
|
75
|
+
|
|
76
|
+
Where the engine draws no switch, the rail also drags: a press makes the thumb follow the pointer, and a release past
|
|
77
|
+
the rail's midpoint turns the switch on (or off). Safari's own switch drags itself, so this is the fallback rail's
|
|
78
|
+
gesture, and it is attached only where `hasNativeSwitch()` is `false`:
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
import { attachSwitchDrag } from "@cssints/forms/runtime";
|
|
82
|
+
|
|
83
|
+
<input
|
|
84
|
+
type="checkbox"
|
|
85
|
+
switch
|
|
86
|
+
class={css.cn(f.switch())}
|
|
87
|
+
ref={(input) => attachSwitchDrag(input)}
|
|
88
|
+
/>;
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
`attachSwitchDrag(input, { threshold?, onChange? })` returns the detach. It is a thin adapter over a machine
|
|
92
|
+
(`@cssints/machine`) that the same subpath exports as `createSwitchDrag()` and `switchDragDefinition`:
|
|
93
|
+
|
|
94
|
+
| State | What it is |
|
|
95
|
+
| ---------- | ---------------------------------------------- |
|
|
96
|
+
| `idle` | nothing is down |
|
|
97
|
+
| `pressed` | a press that has not travelled the threshold |
|
|
98
|
+
| `dragging` | past it: the rail has a split point and a side |
|
|
99
|
+
|
|
100
|
+
The gesture holds the value it started with (`from`) until the release, so nothing turns while the pointer is down; a
|
|
101
|
+
release past the midpoint commits `!from`, one before it commits `from`.
|
|
102
|
+
|
|
103
|
+
**The click after the release is suppressed, and that is the point.** The browser sends a `click` after every pointer
|
|
104
|
+
release, and on a checkbox that click toggles: without a guard, a drag that turned the switch on would be turned back
|
|
105
|
+
off by its own gesture — the double toggle. The machine records the release as `suppressed`, and the adapter cancels
|
|
106
|
+
that one click with `preventDefault()`, which also puts the platform's own flip back. A press that never travelled
|
|
107
|
+
suppresses nothing: that click is the platform's turn, and only the platform's.
|
|
108
|
+
|
|
109
|
+
A release that fires no click — a synthetic pointer is not captured, and a capture can fail — leaves nothing behind
|
|
110
|
+
either: a press anywhere in the page, or a key on the input, settles that suppression before the click it would
|
|
111
|
+
otherwise cancel, so the switch is never dead for one click.
|
|
112
|
+
|
|
113
|
+
Two properties are the rail's and are removed on any end (a release, `pointercancel`, a lost capture):
|
|
114
|
+
|
|
115
|
+
- `--forms-at` — the thumb's leading edge in px, `(clientX - left) / width * height`, measured from the leading edge so
|
|
116
|
+
`:dir(rtl)` needs no second formula (the class mirrors the offset, because a box-shadow offset is physical).
|
|
117
|
+
- `data-forms-side` — `on` past the midpoint, `off` before it: the fill leading up to the split point is the accent or
|
|
118
|
+
the line.
|
|
119
|
+
|
|
120
|
+
A drag that changed nothing dispatches no `input`/`change`; one that changed the value dispatches both, bubbling, so
|
|
121
|
+
`createNativeSwitch`'s `onValueChange` hears exactly one turn. `onChange` in the options is the same value for a host
|
|
122
|
+
that does not go through the primitive.
|
|
123
|
+
|
|
124
|
+
The threshold is 4 px, the one threshold the kit has (`DRAG_PX` in `@cssints/headless`'s canvas mathematics, the
|
|
125
|
+
diagram's and the card board's `dragThreshold`). `@cssints/forms` does not import `@cssints/headless` for it: the
|
|
126
|
+
number is the contract.
|
|
127
|
+
|
|
128
|
+
The gesture is a spec as well as code — `@cssints/apg-specs`' `specs/patterns/switch-drag.qnt` with ten scenarios, all
|
|
129
|
+
of them replayed against the machine. The DOM is `apps/ui`'s `native-switch` browser test.
|
|
130
|
+
|
|
131
|
+
## What is the platform's, and what is ours
|
|
132
|
+
|
|
133
|
+
- **The platform's**: the element, so the form value, the label (a click on it), autofill, Space and the focus ring; the
|
|
134
|
+
range's track and thumb; the progress's bar where the engine has one.
|
|
135
|
+
- **Ours**: the tick (an SVG in a custom property, so a host can swap the image: `--forms-tick`), the dash of
|
|
136
|
+
`:indeterminate`, the dot of a radio (a box-shadow of `surface` leaving the accent in the middle), the rail of a
|
|
137
|
+
switch where the engine has none and its drag, and the `:disabled` state.
|
|
138
|
+
- **Forced colours**: `appearance: auto` comes back, the image and the accent go (a `@media (forced-colors: active)` block),
|
|
139
|
+
so Windows high contrast gets the platform's own controls rather than a drawing of one.
|
|
140
|
+
|
|
141
|
+
## Limits
|
|
142
|
+
|
|
143
|
+
- The tick's white is the one colour that does not come from the options: it is an image, and `--forms-tick` replaces it
|
|
144
|
+
(the checkbox reads `background-image: var(--forms-tick)`).
|
|
145
|
+
- `range()` and `progress()` set `accent-color` only. The track and the thumb are engine-specific pseudo-elements
|
|
146
|
+
(`::-webkit-slider-runnable-track`, `::-moz-range-track`), and `accent-color` is the answer for a native control; a
|
|
147
|
+
design that needs its own track is a different control (`@cssints/headless`'s slider, a styled set of elements).
|
|
148
|
+
- A switch's rail replaces what the engine could draw only where it draws nothing; see above. Its drag is one pointer and
|
|
149
|
+
one button: touch, a long press, momentum and `prefers-reduced-motion` are not modelled, and neither is Safari's own
|
|
150
|
+
switch, which drags itself and is left to the platform.
|
|
151
|
+
- The runtime subpath carries the gesture: `@cssints/forms/runtime` is 9.6 kB (3.0 kB gzipped) and imports
|
|
152
|
+
`@cssints/machine` (13 kB, 3.6 kB gzipped), so a host that imports the subpath for `hasNativeSwitch()` alone pays for
|
|
153
|
+
both chunks. A separate entry would split them; the predicate and the adapter share a module because the engine's
|
|
154
|
+
client needs one import and the gesture is the module's second half.
|
|
155
|
+
- The engine's layers: a control's rule sits in `_.k`, and its colours are custom properties in `_.a`, so any style of
|
|
156
|
+
the app over the same element wins by layer, not by specificity.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { Part } from "cssints/plugin";
|
|
2
|
+
/** A value the engine prints: a CSS string, or a token (`t.color.highlight`), whose text is its `var()`. */
|
|
3
|
+
export type Value = Exclude<Part, number>;
|
|
4
|
+
/** The colours and the two measurements a control is drawn from: every colour comes from here. */
|
|
5
|
+
export interface Options {
|
|
6
|
+
/** The on colour: the box of a checked checkbox, the dot of a radio, the rail of a fallback switch. */
|
|
7
|
+
accent: Value;
|
|
8
|
+
/** The box before it is checked, the thumb of a switch, the gap between a radio's dot and its ring. */
|
|
9
|
+
surface: Value;
|
|
10
|
+
/** The unchecked border, and the track of a fallback switch. */
|
|
11
|
+
line: Value;
|
|
12
|
+
/** The ink on the accent: the tick of a checkbox and its dash, the dot of a radio. */
|
|
13
|
+
ink: Value;
|
|
14
|
+
/** The corner of a checkbox, `0.25em` by default. */
|
|
15
|
+
radius?: Value;
|
|
16
|
+
/** The square of every control, `1.1em` by default. */
|
|
17
|
+
size?: Value;
|
|
18
|
+
}
|
|
19
|
+
declare const define: (o: Options) => import("cssints/plugin").Members<{
|
|
20
|
+
readonly "#box": {
|
|
21
|
+
readonly css: string;
|
|
22
|
+
};
|
|
23
|
+
readonly "#dot": {
|
|
24
|
+
readonly css: string;
|
|
25
|
+
};
|
|
26
|
+
readonly "#rail": {
|
|
27
|
+
readonly css: string;
|
|
28
|
+
};
|
|
29
|
+
readonly "#slider": {
|
|
30
|
+
readonly css: "&{accent-color:var(--_accent)}";
|
|
31
|
+
};
|
|
32
|
+
readonly "#bar": {
|
|
33
|
+
readonly css: "&{accent-color:var(--_accent)}";
|
|
34
|
+
};
|
|
35
|
+
readonly checkbox: {
|
|
36
|
+
readonly args: readonly [];
|
|
37
|
+
};
|
|
38
|
+
readonly radio: {
|
|
39
|
+
readonly args: readonly [];
|
|
40
|
+
};
|
|
41
|
+
readonly switch: {
|
|
42
|
+
readonly args: readonly [];
|
|
43
|
+
};
|
|
44
|
+
readonly range: {
|
|
45
|
+
readonly args: readonly [];
|
|
46
|
+
};
|
|
47
|
+
readonly progress: {
|
|
48
|
+
readonly args: readonly [];
|
|
49
|
+
};
|
|
50
|
+
}>;
|
|
51
|
+
/** What `forms()` gives: call a member where its control is used. */
|
|
52
|
+
export type Forms = ReturnType<typeof define>;
|
|
53
|
+
/**
|
|
54
|
+
* The class of each native form control, drawn from the theme: `checkbox()` is an `<input type="checkbox">`,
|
|
55
|
+
* `radio()` an `<input type="radio">`, `switch()` an `<input type="checkbox" switch>` where the engine does not
|
|
56
|
+
* draw one, and `range()` and `progress()` set `accent-color` on an `<input type="range">` and a `<progress>`.
|
|
57
|
+
* Every control keeps its native element: the form value, the label, the keyboard and the focus ring are the platform's.
|
|
58
|
+
* Each class goes on its own control (`css.cn(f.checkbox())`), with `hasNativeSwitch()` deciding the switch. Call the
|
|
59
|
+
* factory in a module of its own, imported with the attribute, and its members where the style is used (a definition
|
|
60
|
+
* site, as `@cssints/fonts`' `font()`).
|
|
61
|
+
*
|
|
62
|
+
* @param {Options} options the accent, the surface, the line and the ink, and optionally the corner and the square
|
|
63
|
+
* @returns {Forms} the members `checkbox()`, `radio()`, `switch()`, `range()` and `progress()`
|
|
64
|
+
*/
|
|
65
|
+
export declare const forms: (options: Options) => import("cssints/plugin").Members<{
|
|
66
|
+
readonly "#box": {
|
|
67
|
+
readonly css: string;
|
|
68
|
+
};
|
|
69
|
+
readonly "#dot": {
|
|
70
|
+
readonly css: string;
|
|
71
|
+
};
|
|
72
|
+
readonly "#rail": {
|
|
73
|
+
readonly css: string;
|
|
74
|
+
};
|
|
75
|
+
readonly "#slider": {
|
|
76
|
+
readonly css: "&{accent-color:var(--_accent)}";
|
|
77
|
+
};
|
|
78
|
+
readonly "#bar": {
|
|
79
|
+
readonly css: "&{accent-color:var(--_accent)}";
|
|
80
|
+
};
|
|
81
|
+
readonly checkbox: {
|
|
82
|
+
readonly args: readonly [];
|
|
83
|
+
};
|
|
84
|
+
readonly radio: {
|
|
85
|
+
readonly args: readonly [];
|
|
86
|
+
};
|
|
87
|
+
readonly switch: {
|
|
88
|
+
readonly args: readonly [];
|
|
89
|
+
};
|
|
90
|
+
readonly range: {
|
|
91
|
+
readonly args: readonly [];
|
|
92
|
+
};
|
|
93
|
+
readonly progress: {
|
|
94
|
+
readonly args: readonly [];
|
|
95
|
+
};
|
|
96
|
+
}>;
|
|
97
|
+
export {};
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { scope } from "cssints/plugin";
|
|
2
|
+
// src/index.ts
|
|
3
|
+
const text = (v) => typeof v === "string" ? v : String(v);
|
|
4
|
+
const SIZE = "1.1em";
|
|
5
|
+
const RADIUS = "0.25em";
|
|
6
|
+
const TICK = "url(\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 16 16'%3E%3Cpath d='M3 8.6 6.4 12 13 4.6' fill='none' stroke='%23fff' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E\")";
|
|
7
|
+
const STATES = "&:disabled{cursor:not-allowed;opacity:.55}";
|
|
8
|
+
const FORCED = "@media (forced-colors: active){&{appearance:auto;background-image:none;accent-color:auto}}";
|
|
9
|
+
const BOX = `&{appearance:none;box-sizing:border-box;margin:0;inline-size:var(--_size);block-size:var(--_size);border:1px solid var(--_line);border-radius:var(--_radius);background-color:var(--_surface);background-position:center;background-repeat:no-repeat;background-size:62%;vertical-align:-.15em;cursor:pointer;transition:background-color 120ms,border-color 120ms,box-shadow 120ms;--_tick:${TICK}}&:checked{background-color:var(--_accent);border-color:var(--_accent);background-image:var(--_tick)}&:indeterminate{background-color:var(--_accent);border-color:var(--_accent);background-image:linear-gradient(var(--_ink),var(--_ink));background-size:60% 2px}&:disabled{cursor:not-allowed;opacity:.55}@media (forced-colors: active){&{appearance:auto;background-image:none;accent-color:auto}}`;
|
|
10
|
+
const DOT = "&{appearance:none;box-sizing:border-box;margin:0;inline-size:var(--_size);block-size:var(--_size);border:1px solid var(--_line);border-radius:50%;background-color:var(--_surface);vertical-align:-.15em;cursor:pointer;transition:background-color 120ms,border-color 120ms,box-shadow 120ms}&:checked{border-color:var(--_accent);box-shadow:inset 0 0 0 calc(var(--_size) * .3) var(--_surface);background-color:var(--_accent)}" + STATES + FORCED;
|
|
11
|
+
const RAIL = "&{appearance:none;box-sizing:border-box;margin:0;padding:0;border:0;inline-size:calc(var(--_size) * 2);block-size:var(--_size);border-radius:var(--_size);background-color:var(--_surface);box-shadow:inset calc(var(--_size) * -1) 0 0 0 var(--_line);cursor:pointer;transition:box-shadow 120ms,background-color 120ms}&:checked{box-shadow:inset var(--_size) 0 0 0 var(--_accent)}&:dir(rtl){box-shadow:inset var(--_size) 0 0 0 var(--_line)}&:dir(rtl):checked{box-shadow:inset calc(var(--_size) * -1) 0 0 0 var(--_accent)}&[data-forms-side]{transition:none}&[data-forms-side=\"off\"]{box-shadow:inset var(--forms-at,0px) 0 0 0 var(--_line)}&[data-forms-side=\"on\"]{box-shadow:inset var(--forms-at,0px) 0 0 0 var(--_accent)}&:dir(rtl)[data-forms-side=\"off\"]{box-shadow:inset calc(var(--forms-at,0px) * -1) 0 0 0 var(--_line)}&:dir(rtl)[data-forms-side=\"on\"]{box-shadow:inset calc(var(--forms-at,0px) * -1) 0 0 0 var(--_accent)}" + STATES + FORCED;
|
|
12
|
+
const ACCENT = "&{accent-color:var(--_accent)}";
|
|
13
|
+
const colours = (o) => [
|
|
14
|
+
["--_accent", text(o.accent)],
|
|
15
|
+
["--_surface", text(o.surface)],
|
|
16
|
+
["--_line", text(o.line)]
|
|
17
|
+
];
|
|
18
|
+
const boxed = (o) => [
|
|
19
|
+
...colours(o),
|
|
20
|
+
["--_ink", text(o.ink)],
|
|
21
|
+
["--_size", o.size === void 0 ? SIZE : text(o.size)]
|
|
22
|
+
];
|
|
23
|
+
const define = (o) => scope("forms", {
|
|
24
|
+
"#box": { css: BOX },
|
|
25
|
+
"#dot": { css: DOT },
|
|
26
|
+
"#rail": { css: RAIL },
|
|
27
|
+
"#slider": { css: ACCENT },
|
|
28
|
+
"#bar": { css: ACCENT },
|
|
29
|
+
checkbox: { args: [] },
|
|
30
|
+
radio: { args: [] },
|
|
31
|
+
switch: { args: [] },
|
|
32
|
+
range: { args: [] },
|
|
33
|
+
progress: { args: [] }
|
|
34
|
+
}).build({
|
|
35
|
+
checkbox: () => ({
|
|
36
|
+
uses: ["box"],
|
|
37
|
+
decls: [...boxed(o), ["--_radius", o.radius === void 0 ? RADIUS : text(o.radius)]]
|
|
38
|
+
}),
|
|
39
|
+
radio: () => ({
|
|
40
|
+
uses: ["dot"],
|
|
41
|
+
decls: boxed(o)
|
|
42
|
+
}),
|
|
43
|
+
switch: () => ({
|
|
44
|
+
uses: ["rail"],
|
|
45
|
+
decls: boxed(o)
|
|
46
|
+
}),
|
|
47
|
+
range: () => ({
|
|
48
|
+
uses: ["slider"],
|
|
49
|
+
decls: [["--_accent", text(o.accent)]]
|
|
50
|
+
}),
|
|
51
|
+
progress: () => ({
|
|
52
|
+
uses: ["bar"],
|
|
53
|
+
decls: [["--_accent", text(o.accent)]]
|
|
54
|
+
})
|
|
55
|
+
}).export();
|
|
56
|
+
/**
|
|
57
|
+
* The class of each native form control, drawn from the theme: `checkbox()` is an `<input type="checkbox">`,
|
|
58
|
+
* `radio()` an `<input type="radio">`, `switch()` an `<input type="checkbox" switch>` where the engine does not
|
|
59
|
+
* draw one, and `range()` and `progress()` set `accent-color` on an `<input type="range">` and a `<progress>`.
|
|
60
|
+
* Every control keeps its native element: the form value, the label, the keyboard and the focus ring are the platform's.
|
|
61
|
+
* Each class goes on its own control (`css.cn(f.checkbox())`), with `hasNativeSwitch()` deciding the switch. Call the
|
|
62
|
+
* factory in a module of its own, imported with the attribute, and its members where the style is used (a definition
|
|
63
|
+
* site, as `@cssints/fonts`' `font()`).
|
|
64
|
+
*
|
|
65
|
+
* @param {Options} options the accent, the surface, the line and the ink, and optionally the corner and the square
|
|
66
|
+
* @returns {Forms} the members `checkbox()`, `radio()`, `switch()`, `range()` and `progress()`
|
|
67
|
+
*/
|
|
68
|
+
const forms = (options) => define(options);
|
|
69
|
+
export { forms };
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import { Clock, ConfigEvent, Machine } from "@cssints/machine";
|
|
2
|
+
/** What `attachSwitchDrag` takes: the threshold and a listener for the turns a drag commits. */
|
|
3
|
+
interface AttachSwitchDragOptions {
|
|
4
|
+
/**
|
|
5
|
+
* How far, in screen px, the pointer must travel before the press is a drag rather than a click. Default 4: the one
|
|
6
|
+
* threshold the kit has (`DRAG_PX` in `@cssints/headless`'s canvas maths, the diagram's and the card board's
|
|
7
|
+
* `dragThreshold`). The number is the contract here — importing `@cssints/headless` for it would put a Solid package
|
|
8
|
+
* in a browser module that needs none of it.
|
|
9
|
+
*/
|
|
10
|
+
readonly threshold?: number | undefined;
|
|
11
|
+
/**
|
|
12
|
+
* Called with the value a drag committed. The adapter also dispatches `input` and `change` on the element, both
|
|
13
|
+
* bubbling, so a primitive that listens (as `createNativeSwitch` does) hears the turn without this.
|
|
14
|
+
*/
|
|
15
|
+
readonly onChange?: ((checked: boolean) => void) | undefined;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The rail drag of the fallback switch: pointer events on the input, the machine in `./switch-drag.ts`, and the two
|
|
19
|
+
* things a machine cannot own — the rail's geometry and the platform's click.
|
|
20
|
+
*
|
|
21
|
+
* A primary press on an enabled input starts the gesture and captures the pointer (`try`, because a synthetic
|
|
22
|
+
* `PointerEvent` is not an active pointer); the rail's box, its `direction` and the press's x are read once. Each move
|
|
23
|
+
* sends the distance travelled and the side of the midpoint, and while the machine is `dragging` writes the CSS the
|
|
24
|
+
* rail reads: `--forms-at`, the thumb's leading edge in px, and `data-forms-side` (`on` past the midpoint, `off`
|
|
25
|
+
* before it). A release past the threshold writes `input.checked` from the machine and dispatches `input` and `change`,
|
|
26
|
+
* so the host hears exactly one turn; the click the browser sends after that release is cancelled
|
|
27
|
+
* (`preventDefault`), which is what puts the platform's own flip back. A press that never travelled writes nothing and
|
|
28
|
+
* leaves the click to the platform. `pointercancel` and a lost capture put the value back and clear both properties. A
|
|
29
|
+
* release that fires no click (a synthetic pointer is not captured) leaves a suppression behind, which the next press
|
|
30
|
+
* anywhere or key on the input settles first, so it cannot swallow that click.
|
|
31
|
+
*
|
|
32
|
+
* Attach it only where `hasNativeSwitch()` is `false`: Safari's own switch already drags, and the rail is the fallback.
|
|
33
|
+
*
|
|
34
|
+
* @param {HTMLInputElement} input the switch's own `<input type="checkbox" switch>`
|
|
35
|
+
* @param {AttachSwitchDragOptions} options the threshold (default 4) and a listener for the committed turns
|
|
36
|
+
* @returns {() => void} the detach, for the host's cleanup
|
|
37
|
+
*/
|
|
38
|
+
export declare const attachSwitchDrag: (input: HTMLInputElement, options?: AttachSwitchDragOptions) => (() => void);
|
|
39
|
+
/**
|
|
40
|
+
* The rail drag of a fallback switch, as a machine: a press, the 4 px threshold that turns it into a drag, the rail's
|
|
41
|
+
* midpoint a release commits across, and the platform's click that follows the release.
|
|
42
|
+
*
|
|
43
|
+
* The states are the gesture — `idle`, `pressed` (a press that has not travelled) and `dragging` — and the context is
|
|
44
|
+
* the spec's (`specs/patterns/switch-drag.qnt`): `checked`, `from` (the value at the press), `moved`, `crossed`,
|
|
45
|
+
* `suppressed` and `disabled`. Nothing here is geometry: the distance the pointer travelled and the side of the
|
|
46
|
+
* midpoint it is on arrive on the `move` event, and the adapter measures the rail.
|
|
47
|
+
*
|
|
48
|
+
* **The double toggle is a state, not a flag.** A release from `dragging` sets `suppressed`, and the `click` the
|
|
49
|
+
* browser sends after any release only clears it: without that, a drag that turned the switch on would be turned back
|
|
50
|
+
* off by the click the same gesture produces. A release from `pressed` never sets it — that click is the platform's
|
|
51
|
+
* turn and must happen. `cancel` (a lost capture, `pointercancel`) puts the value back to `from` and leaves nothing to
|
|
52
|
+
* swallow.
|
|
53
|
+
*
|
|
54
|
+
* The machine holds one setting, `threshold` (4 by default, the kit's `DRAG_PX`: canvas, diagram and cardboard use the
|
|
55
|
+
* same number), because a `defineMachine` guard sees the context and the event, not the options. `disabled` is the
|
|
56
|
+
* host's: a press on a disabled input is no transition at all.
|
|
57
|
+
*/
|
|
58
|
+
interface SwitchDragContext {
|
|
59
|
+
readonly checked: boolean;
|
|
60
|
+
readonly from: boolean;
|
|
61
|
+
readonly moved: boolean;
|
|
62
|
+
readonly crossed: boolean;
|
|
63
|
+
readonly suppressed: boolean;
|
|
64
|
+
readonly disabled: boolean;
|
|
65
|
+
readonly threshold: number;
|
|
66
|
+
}
|
|
67
|
+
/** The gesture: at rest, pressed, or a drag past the threshold. */
|
|
68
|
+
type SwitchDragPhase = "idle" | "pressed" | "dragging";
|
|
69
|
+
type SwitchDragPayloads = {
|
|
70
|
+
readonly press: {
|
|
71
|
+
readonly checked: boolean;
|
|
72
|
+
};
|
|
73
|
+
readonly move: {
|
|
74
|
+
readonly distance: number;
|
|
75
|
+
readonly pastMidpoint: boolean;
|
|
76
|
+
};
|
|
77
|
+
readonly release: undefined;
|
|
78
|
+
readonly cancel: undefined;
|
|
79
|
+
readonly click: undefined;
|
|
80
|
+
readonly setDisabled: {
|
|
81
|
+
readonly disabled: boolean;
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
/** What the machine accepts: a press (with the value the platform holds), a move, an end, a click and the host's `disabled`. */
|
|
85
|
+
type SwitchDragEvent = {
|
|
86
|
+
readonly type: "press";
|
|
87
|
+
readonly checked: boolean;
|
|
88
|
+
} | {
|
|
89
|
+
readonly type: "move";
|
|
90
|
+
readonly distance: number;
|
|
91
|
+
readonly pastMidpoint: boolean;
|
|
92
|
+
} | {
|
|
93
|
+
readonly type: "release";
|
|
94
|
+
} | {
|
|
95
|
+
readonly type: "cancel";
|
|
96
|
+
} | {
|
|
97
|
+
readonly type: "click";
|
|
98
|
+
} | {
|
|
99
|
+
readonly type: "setDisabled";
|
|
100
|
+
readonly disabled: boolean;
|
|
101
|
+
};
|
|
102
|
+
/** The running machine, as a consumer names it. */
|
|
103
|
+
type SwitchDragMachine = Machine<SwitchDragContext, SwitchDragPhase, SwitchDragEvent>;
|
|
104
|
+
type SwitchDragConfig = ConfigEvent<SwitchDragPayloads>;
|
|
105
|
+
/** The machine's definition, for a harness or a test: the spec's states and context, with `threshold` as its setting. */
|
|
106
|
+
export declare const switchDragDefinition: import("@cssints/machine").MachineDefinition<SwitchDragContext, {
|
|
107
|
+
readonly idle: {
|
|
108
|
+
readonly on: {
|
|
109
|
+
readonly press: {
|
|
110
|
+
readonly target: "pressed";
|
|
111
|
+
readonly guard: (context: SwitchDragContext) => boolean;
|
|
112
|
+
readonly assign: (_context: SwitchDragContext, event: ({
|
|
113
|
+
readonly type: "press";
|
|
114
|
+
} & {
|
|
115
|
+
readonly checked: boolean;
|
|
116
|
+
}) | ({
|
|
117
|
+
readonly type: "move";
|
|
118
|
+
} & {
|
|
119
|
+
readonly distance: number;
|
|
120
|
+
readonly pastMidpoint: boolean;
|
|
121
|
+
}) | ({
|
|
122
|
+
readonly type: "release";
|
|
123
|
+
} & Record<never, never>) | ({
|
|
124
|
+
readonly type: "cancel";
|
|
125
|
+
} & Record<never, never>) | ({
|
|
126
|
+
readonly type: "click";
|
|
127
|
+
} & Record<never, never>) | ({
|
|
128
|
+
readonly type: "setDisabled";
|
|
129
|
+
} & {
|
|
130
|
+
readonly disabled: boolean;
|
|
131
|
+
})) => {
|
|
132
|
+
checked: boolean;
|
|
133
|
+
from: boolean;
|
|
134
|
+
moved: false;
|
|
135
|
+
crossed: false;
|
|
136
|
+
suppressed: false;
|
|
137
|
+
};
|
|
138
|
+
};
|
|
139
|
+
readonly click: {
|
|
140
|
+
readonly target: "idle";
|
|
141
|
+
readonly assign: (context: SwitchDragContext) => {
|
|
142
|
+
checked: boolean;
|
|
143
|
+
suppressed: false;
|
|
144
|
+
};
|
|
145
|
+
};
|
|
146
|
+
readonly setDisabled: {
|
|
147
|
+
readonly target: "idle";
|
|
148
|
+
readonly assign: (_context: SwitchDragContext, event: ({
|
|
149
|
+
readonly type: "press";
|
|
150
|
+
} & {
|
|
151
|
+
readonly checked: boolean;
|
|
152
|
+
}) | ({
|
|
153
|
+
readonly type: "move";
|
|
154
|
+
} & {
|
|
155
|
+
readonly distance: number;
|
|
156
|
+
readonly pastMidpoint: boolean;
|
|
157
|
+
}) | ({
|
|
158
|
+
readonly type: "release";
|
|
159
|
+
} & Record<never, never>) | ({
|
|
160
|
+
readonly type: "cancel";
|
|
161
|
+
} & Record<never, never>) | ({
|
|
162
|
+
readonly type: "click";
|
|
163
|
+
} & Record<never, never>) | ({
|
|
164
|
+
readonly type: "setDisabled";
|
|
165
|
+
} & {
|
|
166
|
+
readonly disabled: boolean;
|
|
167
|
+
})) => {
|
|
168
|
+
disabled: boolean;
|
|
169
|
+
};
|
|
170
|
+
};
|
|
171
|
+
};
|
|
172
|
+
};
|
|
173
|
+
readonly pressed: {
|
|
174
|
+
readonly on: {
|
|
175
|
+
readonly move: readonly [{
|
|
176
|
+
readonly target: "dragging";
|
|
177
|
+
readonly guard: (context: SwitchDragContext, event: SwitchDragConfig) => boolean;
|
|
178
|
+
readonly assign: (_context: SwitchDragContext, event: ({
|
|
179
|
+
readonly type: "press";
|
|
180
|
+
} & {
|
|
181
|
+
readonly checked: boolean;
|
|
182
|
+
}) | ({
|
|
183
|
+
readonly type: "move";
|
|
184
|
+
} & {
|
|
185
|
+
readonly distance: number;
|
|
186
|
+
readonly pastMidpoint: boolean;
|
|
187
|
+
}) | ({
|
|
188
|
+
readonly type: "release";
|
|
189
|
+
} & Record<never, never>) | ({
|
|
190
|
+
readonly type: "cancel";
|
|
191
|
+
} & Record<never, never>) | ({
|
|
192
|
+
readonly type: "click";
|
|
193
|
+
} & Record<never, never>) | ({
|
|
194
|
+
readonly type: "setDisabled";
|
|
195
|
+
} & {
|
|
196
|
+
readonly disabled: boolean;
|
|
197
|
+
})) => {
|
|
198
|
+
moved: true;
|
|
199
|
+
crossed: boolean;
|
|
200
|
+
};
|
|
201
|
+
}, {
|
|
202
|
+
readonly target: "pressed";
|
|
203
|
+
}];
|
|
204
|
+
readonly release: {
|
|
205
|
+
readonly target: "idle";
|
|
206
|
+
};
|
|
207
|
+
readonly cancel: {
|
|
208
|
+
readonly target: "idle";
|
|
209
|
+
readonly assign: (context: SwitchDragContext) => {
|
|
210
|
+
checked: boolean;
|
|
211
|
+
suppressed: false;
|
|
212
|
+
};
|
|
213
|
+
};
|
|
214
|
+
readonly setDisabled: {
|
|
215
|
+
readonly target: "pressed";
|
|
216
|
+
readonly assign: (_context: SwitchDragContext, event: ({
|
|
217
|
+
readonly type: "press";
|
|
218
|
+
} & {
|
|
219
|
+
readonly checked: boolean;
|
|
220
|
+
}) | ({
|
|
221
|
+
readonly type: "move";
|
|
222
|
+
} & {
|
|
223
|
+
readonly distance: number;
|
|
224
|
+
readonly pastMidpoint: boolean;
|
|
225
|
+
}) | ({
|
|
226
|
+
readonly type: "release";
|
|
227
|
+
} & Record<never, never>) | ({
|
|
228
|
+
readonly type: "cancel";
|
|
229
|
+
} & Record<never, never>) | ({
|
|
230
|
+
readonly type: "click";
|
|
231
|
+
} & Record<never, never>) | ({
|
|
232
|
+
readonly type: "setDisabled";
|
|
233
|
+
} & {
|
|
234
|
+
readonly disabled: boolean;
|
|
235
|
+
})) => {
|
|
236
|
+
disabled: boolean;
|
|
237
|
+
};
|
|
238
|
+
};
|
|
239
|
+
};
|
|
240
|
+
};
|
|
241
|
+
readonly dragging: {
|
|
242
|
+
readonly on: {
|
|
243
|
+
readonly move: readonly [{
|
|
244
|
+
readonly target: "dragging";
|
|
245
|
+
readonly guard: (context: SwitchDragContext, event: SwitchDragConfig) => boolean;
|
|
246
|
+
readonly assign: (_context: SwitchDragContext, event: ({
|
|
247
|
+
readonly type: "press";
|
|
248
|
+
} & {
|
|
249
|
+
readonly checked: boolean;
|
|
250
|
+
}) | ({
|
|
251
|
+
readonly type: "move";
|
|
252
|
+
} & {
|
|
253
|
+
readonly distance: number;
|
|
254
|
+
readonly pastMidpoint: boolean;
|
|
255
|
+
}) | ({
|
|
256
|
+
readonly type: "release";
|
|
257
|
+
} & Record<never, never>) | ({
|
|
258
|
+
readonly type: "cancel";
|
|
259
|
+
} & Record<never, never>) | ({
|
|
260
|
+
readonly type: "click";
|
|
261
|
+
} & Record<never, never>) | ({
|
|
262
|
+
readonly type: "setDisabled";
|
|
263
|
+
} & {
|
|
264
|
+
readonly disabled: boolean;
|
|
265
|
+
})) => {
|
|
266
|
+
crossed: boolean;
|
|
267
|
+
};
|
|
268
|
+
}, {
|
|
269
|
+
readonly target: "dragging";
|
|
270
|
+
}];
|
|
271
|
+
readonly release: {
|
|
272
|
+
readonly target: "idle";
|
|
273
|
+
readonly assign: (context: SwitchDragContext) => {
|
|
274
|
+
checked: boolean;
|
|
275
|
+
suppressed: true;
|
|
276
|
+
};
|
|
277
|
+
};
|
|
278
|
+
readonly cancel: {
|
|
279
|
+
readonly target: "idle";
|
|
280
|
+
readonly assign: (context: SwitchDragContext) => {
|
|
281
|
+
checked: boolean;
|
|
282
|
+
suppressed: false;
|
|
283
|
+
};
|
|
284
|
+
};
|
|
285
|
+
readonly setDisabled: {
|
|
286
|
+
readonly target: "dragging";
|
|
287
|
+
readonly assign: (_context: SwitchDragContext, event: ({
|
|
288
|
+
readonly type: "press";
|
|
289
|
+
} & {
|
|
290
|
+
readonly checked: boolean;
|
|
291
|
+
}) | ({
|
|
292
|
+
readonly type: "move";
|
|
293
|
+
} & {
|
|
294
|
+
readonly distance: number;
|
|
295
|
+
readonly pastMidpoint: boolean;
|
|
296
|
+
}) | ({
|
|
297
|
+
readonly type: "release";
|
|
298
|
+
} & Record<never, never>) | ({
|
|
299
|
+
readonly type: "cancel";
|
|
300
|
+
} & Record<never, never>) | ({
|
|
301
|
+
readonly type: "click";
|
|
302
|
+
} & Record<never, never>) | ({
|
|
303
|
+
readonly type: "setDisabled";
|
|
304
|
+
} & {
|
|
305
|
+
readonly disabled: boolean;
|
|
306
|
+
})) => {
|
|
307
|
+
disabled: boolean;
|
|
308
|
+
};
|
|
309
|
+
};
|
|
310
|
+
};
|
|
311
|
+
};
|
|
312
|
+
}, SwitchDragPayloads>;
|
|
313
|
+
/** What `createSwitchDrag` takes: the value the platform starts with, the host's `disabled`, the threshold and a clock. */
|
|
314
|
+
interface SwitchDragOptions {
|
|
315
|
+
/** The platform's value at attach: `input.checked`. */
|
|
316
|
+
readonly checked?: boolean | undefined;
|
|
317
|
+
/** The host's `disabled` at attach: `input.disabled`. */
|
|
318
|
+
readonly disabled?: boolean | undefined;
|
|
319
|
+
/** How far, in screen px, a press must travel before it is a drag. Default 4, the kit's `DRAG_PX`. */
|
|
320
|
+
readonly threshold?: number | undefined;
|
|
321
|
+
/** The clock, for a test; the system one by default. */
|
|
322
|
+
readonly clock?: Clock | undefined;
|
|
323
|
+
}
|
|
324
|
+
/** The rail drag as a consumer reads it: the machine, the value it holds and whether the pointer is dragging. */
|
|
325
|
+
interface SwitchDrag {
|
|
326
|
+
readonly machine: SwitchDragMachine;
|
|
327
|
+
/** The value the gesture holds; the input is written from it on a release. */
|
|
328
|
+
readonly checked: () => boolean;
|
|
329
|
+
/** `true` between the move that crossed the threshold and the release, the cancel or the lost capture. */
|
|
330
|
+
readonly dragging: () => boolean;
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* Run one rail's drag. The three accessors are plain reads of the machine — `@cssints/machine`'s store is the
|
|
334
|
+
* reactive half, and the adapter wants the values, not a subscription.
|
|
335
|
+
*
|
|
336
|
+
* @param {SwitchDragOptions} options the value and `disabled` the platform starts with, the threshold and a clock
|
|
337
|
+
* @returns {SwitchDrag} the machine, `checked()` and `dragging()`
|
|
338
|
+
*/
|
|
339
|
+
export declare const createSwitchDrag: (options?: SwitchDragOptions) => {
|
|
340
|
+
machine: SwitchDragMachine;
|
|
341
|
+
checked: () => boolean;
|
|
342
|
+
dragging: () => boolean;
|
|
343
|
+
};
|
|
344
|
+
/**
|
|
345
|
+
* Whether the engine draws `<input type="checkbox" switch>` itself: the `switch` property of
|
|
346
|
+
* `HTMLInputElement.prototype`, and `false` where there is no DOM. Spread `switch()` only when this is `false`: where the engine has the control,
|
|
347
|
+
* `appearance: none` is exactly what would erase it, and the accent colours the platform's own switch instead.
|
|
348
|
+
*
|
|
349
|
+
* A module of its own (`@cssints/forms/runtime`), imported without the attribute: the import of a module with the
|
|
350
|
+
* attribute is build-time, and the engine removes it from the client.
|
|
351
|
+
*
|
|
352
|
+
* @returns {boolean} whether the platform implements the `switch` attribute
|
|
353
|
+
*/
|
|
354
|
+
export declare const hasNativeSwitch: () => boolean;
|
|
355
|
+
export type { AttachSwitchDragOptions, SwitchDrag, SwitchDragContext, SwitchDragEvent, SwitchDragMachine, SwitchDragOptions, SwitchDragPhase };
|
package/dist/runtime.mjs
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { createMachine, defineMachine } from "@cssints/machine";
|
|
2
|
+
// src/switch-drag.ts
|
|
3
|
+
const MOVE_PX = 4;
|
|
4
|
+
const distanceOf = (event) => event.type === "move" ? event.distance : 0;
|
|
5
|
+
const sideOf = (event) => event.type === "move" && event.pastMidpoint;
|
|
6
|
+
const beyond = (context, event) => distanceOf(event) > context.threshold;
|
|
7
|
+
const disabledBy = (event) => event.type === "setDisabled" && event.disabled;
|
|
8
|
+
const heldBy = (event) => event.type === "press" && event.checked;
|
|
9
|
+
const resting = {
|
|
10
|
+
checked: false,
|
|
11
|
+
from: false,
|
|
12
|
+
moved: false,
|
|
13
|
+
crossed: false,
|
|
14
|
+
suppressed: false,
|
|
15
|
+
disabled: false,
|
|
16
|
+
threshold: MOVE_PX
|
|
17
|
+
};
|
|
18
|
+
/** The machine's definition, for a harness or a test: the spec's states and context, with `threshold` as its setting. */
|
|
19
|
+
const switchDragDefinition = defineMachine()({
|
|
20
|
+
start: "idle",
|
|
21
|
+
initial: resting,
|
|
22
|
+
states: {
|
|
23
|
+
idle: { on: {
|
|
24
|
+
press: {
|
|
25
|
+
target: "pressed",
|
|
26
|
+
guard: (context) => !context.disabled,
|
|
27
|
+
assign: (_context, event) => ({
|
|
28
|
+
checked: heldBy(event),
|
|
29
|
+
from: heldBy(event),
|
|
30
|
+
moved: false,
|
|
31
|
+
crossed: false,
|
|
32
|
+
suppressed: false
|
|
33
|
+
})
|
|
34
|
+
},
|
|
35
|
+
click: {
|
|
36
|
+
target: "idle",
|
|
37
|
+
assign: (context) => ({
|
|
38
|
+
checked: context.suppressed ? context.checked : !context.checked,
|
|
39
|
+
suppressed: false
|
|
40
|
+
})
|
|
41
|
+
},
|
|
42
|
+
setDisabled: {
|
|
43
|
+
target: "idle",
|
|
44
|
+
assign: (_context, event) => ({ disabled: disabledBy(event) })
|
|
45
|
+
}
|
|
46
|
+
} },
|
|
47
|
+
pressed: { on: {
|
|
48
|
+
move: [{
|
|
49
|
+
target: "dragging",
|
|
50
|
+
guard: beyond,
|
|
51
|
+
assign: (_context, event) => ({
|
|
52
|
+
moved: true,
|
|
53
|
+
crossed: sideOf(event)
|
|
54
|
+
})
|
|
55
|
+
}, { target: "pressed" }],
|
|
56
|
+
release: { target: "idle" },
|
|
57
|
+
cancel: {
|
|
58
|
+
target: "idle",
|
|
59
|
+
assign: (context) => ({
|
|
60
|
+
checked: context.from,
|
|
61
|
+
suppressed: false
|
|
62
|
+
})
|
|
63
|
+
},
|
|
64
|
+
setDisabled: {
|
|
65
|
+
target: "pressed",
|
|
66
|
+
assign: (_context, event) => ({ disabled: disabledBy(event) })
|
|
67
|
+
}
|
|
68
|
+
} },
|
|
69
|
+
dragging: { on: {
|
|
70
|
+
move: [{
|
|
71
|
+
target: "dragging",
|
|
72
|
+
guard: beyond,
|
|
73
|
+
assign: (_context, event) => ({ crossed: sideOf(event) })
|
|
74
|
+
}, { target: "dragging" }],
|
|
75
|
+
release: {
|
|
76
|
+
target: "idle",
|
|
77
|
+
assign: (context) => ({
|
|
78
|
+
checked: context.crossed ? !context.from : context.from,
|
|
79
|
+
suppressed: true
|
|
80
|
+
})
|
|
81
|
+
},
|
|
82
|
+
cancel: {
|
|
83
|
+
target: "idle",
|
|
84
|
+
assign: (context) => ({
|
|
85
|
+
checked: context.from,
|
|
86
|
+
suppressed: false
|
|
87
|
+
})
|
|
88
|
+
},
|
|
89
|
+
setDisabled: {
|
|
90
|
+
target: "dragging",
|
|
91
|
+
assign: (_context, event) => ({ disabled: disabledBy(event) })
|
|
92
|
+
}
|
|
93
|
+
} }
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
/**
|
|
97
|
+
* Run one rail's drag. The three accessors are plain reads of the machine — `@cssints/machine`'s store is the
|
|
98
|
+
* reactive half, and the adapter wants the values, not a subscription.
|
|
99
|
+
*
|
|
100
|
+
* @param {SwitchDragOptions} options the value and `disabled` the platform starts with, the threshold and a clock
|
|
101
|
+
* @returns {SwitchDrag} the machine, `checked()` and `dragging()`
|
|
102
|
+
*/
|
|
103
|
+
const createSwitchDrag = (options = {}) => {
|
|
104
|
+
const checked = options.checked === true;
|
|
105
|
+
const machine = createMachine({
|
|
106
|
+
...switchDragDefinition,
|
|
107
|
+
initial: {
|
|
108
|
+
...switchDragDefinition.initial,
|
|
109
|
+
checked,
|
|
110
|
+
from: checked,
|
|
111
|
+
disabled: options.disabled === true,
|
|
112
|
+
threshold: options.threshold ?? MOVE_PX
|
|
113
|
+
}
|
|
114
|
+
}, options.clock === void 0 ? {} : { clock: options.clock });
|
|
115
|
+
return {
|
|
116
|
+
machine,
|
|
117
|
+
checked: () => machine.getState().context.checked,
|
|
118
|
+
dragging: () => machine.getState().state === "dragging"
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
// src/attach-switch-drag.ts
|
|
122
|
+
const clamp = (value) => Math.min(1, Math.max(0, value));
|
|
123
|
+
/**
|
|
124
|
+
* The rail drag of the fallback switch: pointer events on the input, the machine in `./switch-drag.ts`, and the two
|
|
125
|
+
* things a machine cannot own — the rail's geometry and the platform's click.
|
|
126
|
+
*
|
|
127
|
+
* A primary press on an enabled input starts the gesture and captures the pointer (`try`, because a synthetic
|
|
128
|
+
* `PointerEvent` is not an active pointer); the rail's box, its `direction` and the press's x are read once. Each move
|
|
129
|
+
* sends the distance travelled and the side of the midpoint, and while the machine is `dragging` writes the CSS the
|
|
130
|
+
* rail reads: `--forms-at`, the thumb's leading edge in px, and `data-forms-side` (`on` past the midpoint, `off`
|
|
131
|
+
* before it). A release past the threshold writes `input.checked` from the machine and dispatches `input` and `change`,
|
|
132
|
+
* so the host hears exactly one turn; the click the browser sends after that release is cancelled
|
|
133
|
+
* (`preventDefault`), which is what puts the platform's own flip back. A press that never travelled writes nothing and
|
|
134
|
+
* leaves the click to the platform. `pointercancel` and a lost capture put the value back and clear both properties. A
|
|
135
|
+
* release that fires no click (a synthetic pointer is not captured) leaves a suppression behind, which the next press
|
|
136
|
+
* anywhere or key on the input settles first, so it cannot swallow that click.
|
|
137
|
+
*
|
|
138
|
+
* Attach it only where `hasNativeSwitch()` is `false`: Safari's own switch already drags, and the rail is the fallback.
|
|
139
|
+
*
|
|
140
|
+
* @param {HTMLInputElement} input the switch's own `<input type="checkbox" switch>`
|
|
141
|
+
* @param {AttachSwitchDragOptions} options the threshold (default 4) and a listener for the committed turns
|
|
142
|
+
* @returns {() => void} the detach, for the host's cleanup
|
|
143
|
+
*/
|
|
144
|
+
const attachSwitchDrag = (input, options = {}) => {
|
|
145
|
+
const drag = createSwitchDrag({
|
|
146
|
+
checked: input.checked,
|
|
147
|
+
disabled: input.disabled,
|
|
148
|
+
threshold: options.threshold
|
|
149
|
+
});
|
|
150
|
+
let gesture;
|
|
151
|
+
const context = () => drag.machine.getState().context;
|
|
152
|
+
const capture = (pointerId) => {
|
|
153
|
+
try {
|
|
154
|
+
input.setPointerCapture(pointerId);
|
|
155
|
+
} catch {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
const clearRail = () => {
|
|
160
|
+
input.style.removeProperty("--forms-at");
|
|
161
|
+
delete input.dataset["formsSide"];
|
|
162
|
+
};
|
|
163
|
+
const settle = () => {
|
|
164
|
+
if (context().suppressed) drag.machine.send({ type: "click" });
|
|
165
|
+
};
|
|
166
|
+
let watched;
|
|
167
|
+
const watch = () => {
|
|
168
|
+
const live = input.ownerDocument;
|
|
169
|
+
if (live === watched) return;
|
|
170
|
+
watched?.removeEventListener("pointerdown", settle, true);
|
|
171
|
+
live.addEventListener("pointerdown", settle, true);
|
|
172
|
+
watched = live;
|
|
173
|
+
};
|
|
174
|
+
const syncDisabled = () => {
|
|
175
|
+
if (input.disabled !== context().disabled) drag.machine.send({
|
|
176
|
+
type: "setDisabled",
|
|
177
|
+
disabled: input.disabled
|
|
178
|
+
});
|
|
179
|
+
};
|
|
180
|
+
const onPointerDown = (event) => {
|
|
181
|
+
if (event.button !== 0 || input.disabled) return;
|
|
182
|
+
syncDisabled();
|
|
183
|
+
watch();
|
|
184
|
+
const rect = input.getBoundingClientRect();
|
|
185
|
+
gesture = {
|
|
186
|
+
rect,
|
|
187
|
+
x: event.clientX,
|
|
188
|
+
size: rect.height,
|
|
189
|
+
rtl: getComputedStyle(input).direction === "rtl"
|
|
190
|
+
};
|
|
191
|
+
drag.machine.send({
|
|
192
|
+
type: "press",
|
|
193
|
+
checked: input.checked
|
|
194
|
+
});
|
|
195
|
+
capture(event.pointerId);
|
|
196
|
+
};
|
|
197
|
+
const onPointerMove = (event) => {
|
|
198
|
+
const at = gesture;
|
|
199
|
+
if (at === void 0) return;
|
|
200
|
+
const distance = Math.abs(event.clientX - at.x);
|
|
201
|
+
const fraction = clamp(at.rtl ? (at.rect.right - event.clientX) / at.rect.width : (event.clientX - at.rect.left) / at.rect.width);
|
|
202
|
+
drag.machine.send({
|
|
203
|
+
type: "move",
|
|
204
|
+
distance,
|
|
205
|
+
pastMidpoint: fraction > .5
|
|
206
|
+
});
|
|
207
|
+
if (!drag.dragging()) return;
|
|
208
|
+
input.style.setProperty("--forms-at", `${fraction * at.size}px`);
|
|
209
|
+
input.dataset["formsSide"] = context().crossed ? "on" : "off";
|
|
210
|
+
};
|
|
211
|
+
const onPointerUp = () => {
|
|
212
|
+
if (gesture === void 0) return;
|
|
213
|
+
gesture = void 0;
|
|
214
|
+
const dragged = drag.dragging();
|
|
215
|
+
const before = input.checked;
|
|
216
|
+
drag.machine.send({ type: "release" });
|
|
217
|
+
const after = context();
|
|
218
|
+
clearRail();
|
|
219
|
+
if (!dragged || after.checked === before) return;
|
|
220
|
+
input.checked = after.checked;
|
|
221
|
+
input.dispatchEvent(new Event("input", { bubbles: true }));
|
|
222
|
+
input.dispatchEvent(new Event("change", { bubbles: true }));
|
|
223
|
+
options.onChange?.(after.checked);
|
|
224
|
+
};
|
|
225
|
+
const onEnd = () => {
|
|
226
|
+
gesture = void 0;
|
|
227
|
+
const wasDragging = drag.dragging();
|
|
228
|
+
drag.machine.send({ type: "cancel" });
|
|
229
|
+
clearRail();
|
|
230
|
+
if (wasDragging) input.checked = context().checked;
|
|
231
|
+
};
|
|
232
|
+
const onClick = (event) => {
|
|
233
|
+
syncDisabled();
|
|
234
|
+
if (context().suppressed) {
|
|
235
|
+
event.preventDefault();
|
|
236
|
+
drag.machine.send({ type: "click" });
|
|
237
|
+
input.checked = context().checked;
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
drag.machine.send({ type: "click" });
|
|
241
|
+
};
|
|
242
|
+
input.addEventListener("pointerdown", onPointerDown);
|
|
243
|
+
input.addEventListener("pointermove", onPointerMove);
|
|
244
|
+
input.addEventListener("pointerup", onPointerUp);
|
|
245
|
+
input.addEventListener("pointercancel", onEnd);
|
|
246
|
+
input.addEventListener("lostpointercapture", onEnd);
|
|
247
|
+
input.addEventListener("click", onClick);
|
|
248
|
+
input.addEventListener("keydown", settle);
|
|
249
|
+
return () => {
|
|
250
|
+
input.removeEventListener("pointerdown", onPointerDown);
|
|
251
|
+
input.removeEventListener("pointermove", onPointerMove);
|
|
252
|
+
input.removeEventListener("pointerup", onPointerUp);
|
|
253
|
+
input.removeEventListener("pointercancel", onEnd);
|
|
254
|
+
input.removeEventListener("lostpointercapture", onEnd);
|
|
255
|
+
input.removeEventListener("click", onClick);
|
|
256
|
+
input.removeEventListener("keydown", settle);
|
|
257
|
+
watched?.removeEventListener("pointerdown", settle, true);
|
|
258
|
+
clearRail();
|
|
259
|
+
drag.machine.destroy();
|
|
260
|
+
};
|
|
261
|
+
};
|
|
262
|
+
// src/runtime.ts
|
|
263
|
+
/**
|
|
264
|
+
* Whether the engine draws `<input type="checkbox" switch>` itself: the `switch` property of
|
|
265
|
+
* `HTMLInputElement.prototype`, and `false` where there is no DOM. Spread `switch()` only when this is `false`: where the engine has the control,
|
|
266
|
+
* `appearance: none` is exactly what would erase it, and the accent colours the platform's own switch instead.
|
|
267
|
+
*
|
|
268
|
+
* A module of its own (`@cssints/forms/runtime`), imported without the attribute: the import of a module with the
|
|
269
|
+
* attribute is build-time, and the engine removes it from the client.
|
|
270
|
+
*
|
|
271
|
+
* @returns {boolean} whether the platform implements the `switch` attribute
|
|
272
|
+
*/
|
|
273
|
+
const hasNativeSwitch = () => {
|
|
274
|
+
const input = globalThis.HTMLInputElement;
|
|
275
|
+
return input !== void 0 && Object.hasOwn(input.prototype, "switch");
|
|
276
|
+
};
|
|
277
|
+
export { attachSwitchDrag, createSwitchDrag, hasNativeSwitch, switchDragDefinition };
|
package/package.json
CHANGED
|
@@ -1,6 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cssints/forms",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.49.0",
|
|
4
|
+
"description": "Native form controls for cssints: a checkbox, a radio, a switch where the engine has none, and the accent of a range and a progress, drawn from the theme",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/alexeiaccio/css-in-ts-compiler.git",
|
|
9
|
+
"directory": "packages/forms"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://cssints.dev",
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"type": "module",
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.mts",
|
|
20
|
+
"default": "./dist/index.mjs"
|
|
21
|
+
},
|
|
22
|
+
"./runtime": {
|
|
23
|
+
"types": "./dist/runtime.d.mts",
|
|
24
|
+
"default": "./dist/runtime.mjs"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"cssints": "^0.49.0"
|
|
29
|
+
},
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=22"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@cssints/machine": "^0.49.0"
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
}
|
|
39
|
+
}
|