@mistriwork/ui 1.0.0-rc.59 → 1.0.0-rc.61

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.
@@ -22,11 +22,25 @@ import { jsx as _jsx } from "react/jsx-runtime";
22
22
  `"sortable-item"` or `"sortable-handle"` with `data-sorting`, `data-grabbed`, `data-dragging` and
23
23
  `data-disabled` for a stylesheet. */
24
24
  import { createParts } from '@ui-sdk/react/primitive';
25
+ import { movedKeys, PendingProvider, useCommit, usePending } from './commit.js';
25
26
  import { heard } from './heard.js';
26
27
  import { sortable } from './sortable.js';
27
28
  const parts = createParts(sortable, { root: 'div', item: 'div', handle: 'button' });
28
- export function Sortable({ children, onValueChange, ...props }) {
29
- return (_jsx(parts.Root, { ...props, ...heard({ onValueChange }), children: ({ order }) => order.map((key, at) => children(key, at)) }));
29
+ /* With `onCommit` (./commit.tsx) a move is asked about before it is final: the item is `data-pending` meanwhile, and a
30
+ refusal puts it back. */
31
+ export function Sortable({ children, onValueChange, onCommit, commitDelay, value, defaultValue, ...props }) {
32
+ const commit = useCommit({
33
+ value,
34
+ defaultValue,
35
+ onValueChange,
36
+ onCommit,
37
+ commitDelay,
38
+ moved: movedKeys,
39
+ });
40
+ const root = (_jsx(parts.Root, { ...props, value: commit.value, defaultValue: commit.defaultValue, ...heard({ onValueChange: commit.onValueChange }), children: ({ order }) => order.map((key, at) => children(key, at)) }));
41
+ return commit.pending ? _jsx(PendingProvider, { value: commit.pending, children: root }) : root;
42
+ }
43
+ export function SortableItem(props) {
44
+ return _jsx(parts.Item, { ...props, ...usePending(props.value) });
30
45
  }
31
- export const SortableItem = parts.Item;
32
46
  export const SortableHandle = parts.Handle;
@@ -0,0 +1,71 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ /* The parts that draw a range for Spans (./SpansParts.tsx): the labels and lines of an axis, a line for the present,
3
+ and a stretch that refuses what is carried into it. A day by the hour and a Gantt by the week are the same three
4
+ things in different units, so each is told its numbers and nothing more.
5
+
6
+ <Spans value={visits} onValueChange={setVisits} axis="y" min={7 * 60} max={19 * 60} step={30}>
7
+ <SpansAxis step={60} format={(minute) => hours.format(clock(minute))} />
8
+ <SpansRegion from={12 * 60} to={13 * 60}>Lunch</SpansRegion>
9
+ <SpansNow value={() => minutesNow()} />
10
+ {…tracks}
11
+ </Spans>
12
+
13
+ Each fills the element it is in (which stands where the range stands, and is the caller's to position: a Spans root
14
+ or a track) and places itself along it by `axis`. Slots: `spans-axis` with `spans-axis-label` and
15
+ `spans-axis-line`, `spans-now` and `spans-region`; all are `aria-hidden` but the region's label, since the
16
+ stretches say where they are in their own names. */
17
+ import { useContext, useEffect, useId, useRef, useState } from 'react';
18
+ import { SpansRangeContext, share } from './spansRange.js';
19
+ const along = (range, from, to) => range.axis === 'y'
20
+ ? { top: `${from * 100}%`, ...(to === undefined ? {} : { height: `${(to - from) * 100}%` }) }
21
+ : { left: `${from * 100}%`, ...(to === undefined ? {} : { width: `${(to - from) * 100}%` }) };
22
+ /**
23
+ * Labels and lines at every `step` from the range's start to its end, the range's own `step` where none is given.
24
+ * `format` says a value (an hour, a date); `labels` and `lines` turn either off, and `every` labels one tick in so many.
25
+ */
26
+ export function SpansAxis({ step, format = String, labels = true, lines = true, every = 1, className, }) {
27
+ const range = useContext(SpansRangeContext);
28
+ const ticks = [];
29
+ if (step > 0)
30
+ for (let value = range.min; value <= range.max; value += step)
31
+ ticks.push(value);
32
+ return (_jsx("div", { "data-slot": "spans-axis", "data-axis": range.axis, className: className, "aria-hidden": "true", children: ticks.map((value, at) => (_jsxs("span", { children: [lines && _jsx("i", { "data-slot": "spans-axis-line", "data-value": value, style: along(range, share(range, value)) }), labels && at % Math.max(1, every) === 0 && (_jsx("span", { "data-slot": "spans-axis-label", "data-value": value, style: along(range, share(range, value)), children: format(value) }))] }, value))) }));
33
+ }
34
+ /**
35
+ * A line at `value`, drawn again each minute (or each `every` milliseconds) when `value` is a function, and not drawn
36
+ * at all where it falls outside the range.
37
+ */
38
+ export function SpansNow({ value, every = 60_000, className }) {
39
+ const range = useContext(SpansRangeContext);
40
+ const read = () => (typeof value === 'function' ? value() : value);
41
+ const [at, setAt] = useState(read);
42
+ const latest = useRef(read);
43
+ latest.current = read;
44
+ useEffect(() => {
45
+ setAt(latest.current());
46
+ if (typeof value !== 'function')
47
+ return;
48
+ const timer = setInterval(() => setAt(latest.current()), every);
49
+ return () => clearInterval(timer);
50
+ }, [value, every]);
51
+ if (at < range.min || at > range.max)
52
+ return null;
53
+ return (_jsx("span", { "data-slot": "spans-now", "data-axis": range.axis, className: className, "aria-hidden": "true", style: along(range, share(range, at)) }));
54
+ }
55
+ /**
56
+ * A stretch from `from` to `to` that nothing is moved or lengthened into: a lunch hour, a closed day. Its children
57
+ * are its label, which is also what is said when a move into it is refused. Inside a track it refuses only that
58
+ * track's stretches; `track` names another, and a region in the root refuses every track's.
59
+ */
60
+ export function SpansRegion({ from, to, track, children, className, }) {
61
+ const range = useContext(SpansRangeContext);
62
+ const id = useId();
63
+ const element = useRef(null);
64
+ const said = typeof children === 'string' || typeof children === 'number' ? String(children) : '';
65
+ useEffect(() => {
66
+ const mine = track ?? element.current?.closest('[data-slot="spans-track"]')?.dataset.track;
67
+ range.regions.set(id, { from, to, track: mine, label: said || element.current?.textContent?.trim() || '' });
68
+ return () => void range.regions.delete(id);
69
+ }, [range, id, from, to, track, said]);
70
+ return (_jsx("div", { ref: element, "data-slot": "spans-region", "data-axis": range.axis, className: className, style: along(range, share(range, from), share(range, to)), children: children }));
71
+ }
@@ -1,12 +1,57 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
+ /* The React parts of stretches along tracks (./spans.ts is the primitive). A track's element runs its whole
3
+ range and hands its stretches to its children as they stand now, the one being carried where it is; a stretch
4
+ places itself. An element marked `data-span-edge="start"` or `"end"` inside a stretch is an edge it is
5
+ lengthened from.
6
+
7
+ <Spans value={bars} onValueChange={setBars} min={0} max={days} stay>
8
+ {tasks.map((task) => (
9
+ <SpansTrack key={task.id} value={task.id}>
10
+ {(onTrack) => onTrack.map((bar) => (
11
+ <SpansSpan key={bar.id} value={bar.id} aria-label={said(bar)}>
12
+ <i data-span-edge="start" />{task.name}<i data-span-edge="end" />
13
+ </SpansSpan>
14
+ ))}
15
+ </SpansTrack>
16
+ ))}
17
+ </Spans>
18
+
19
+ Slots: `spans` (`data-changing`), `spans-track` and `spans-span` (`data-dragging`, `data-resizing`,
20
+ `data-locked`, `data-pending`). The range is drawn by ./SpansGridParts.tsx, and a move can be asked about before
21
+ it is final (`onCommit`, ./commit.tsx). */
22
+ import { announce } from '@ui-sdk/core';
2
23
  import { createParts } from '@ui-sdk/react/primitive';
24
+ import { useMemo, useRef } from 'react';
25
+ import { movedSpans, PendingProvider, useCommit, usePending } from './commit.js';
3
26
  import { heard } from './heard.js';
4
27
  import { spans } from './spans.js';
28
+ import { refusedBy, SpansRangeContext } from './spansRange.js';
5
29
  const parts = createParts(spans, { root: 'div', track: 'div', span: 'div' });
6
- export function Spans({ onValueChange, ...props }) {
7
- return _jsx(parts.Root, { ...props, ...heard({ onValueChange }) });
30
+ /* With `onCommit` (./commit.tsx) a move is asked about before it is final: the stretch is `data-pending` meanwhile,
31
+ a run of Alt-arrow steps is one call, and a refusal puts it back. */
32
+ export function Spans({ onValueChange, onCommit, commitDelay, value, defaultValue, ...props }) {
33
+ const commit = useCommit({ value, defaultValue, onValueChange, onCommit, commitDelay, moved: movedSpans });
34
+ const regions = useRef(new Map());
35
+ const { min = 0, max = 100, axis = 'x' } = props;
36
+ const range = useMemo(() => ({ min, max, axis, regions: regions.current }), [min, max, axis]);
37
+ /* A change that would leave a stretch in a region is refused here, before the page or a commit hears of it. It is
38
+ said on the assertive announcer because the keyboard says the move it was asked for on the polite one. */
39
+ const heardChange = (next, details) => {
40
+ const span = next.find((each) => each.id === details.id);
41
+ const region = span && regions.current.size > 0 ? refusedBy(range, span.track, span.start, span.end) : undefined;
42
+ if (region) {
43
+ details.cancel();
44
+ announce(region.label ? `${region.label} is not open to that` : 'Not available there', { priority: 'assertive' });
45
+ return;
46
+ }
47
+ commit.onValueChange?.(next, details);
48
+ };
49
+ const root = (_jsx(SpansRangeContext.Provider, { value: range, children: _jsx(parts.Root, { ...props, value: commit.value, defaultValue: commit.defaultValue, ...heard({ onValueChange: heardChange }) }) }));
50
+ return commit.pending ? _jsx(PendingProvider, { value: commit.pending, children: root }) : root;
8
51
  }
9
52
  export function SpansTrack({ children, ...props }) {
10
53
  return _jsx(parts.Track, { ...props, children: (state) => children(state.spans) });
11
54
  }
12
- export const SpansSpan = parts.Span;
55
+ export function SpansSpan(props) {
56
+ return _jsx(parts.Span, { ...props, ...usePending(props.value) });
57
+ }
@@ -21,11 +21,16 @@ import { jsx as _jsx } from "react/jsx-runtime";
21
21
  Slots: `transfer`, `transfer-list` (`data-list`, `data-takes`, `data-refuses`, `data-over`), `transfer-item`
22
22
  (`data-carried`, `data-dragging`) and `transfer-handle` (`data-carried`). */
23
23
  import { createParts } from '@ui-sdk/react/primitive';
24
+ import { movedLists, PendingProvider, useCommit, usePending } from './commit.js';
24
25
  import { heard } from './heard.js';
25
26
  import { transfer } from './transfer.js';
26
27
  const parts = createParts(transfer, { root: 'div', list: 'ul', item: 'li', handle: 'button' });
27
- export function Transfer({ onValueChange, ...props }) {
28
- return _jsx(parts.Root, { ...props, ...heard({ onValueChange }) });
28
+ /* With `onCommit` (./commit.tsx) a move is asked about before it is final: the item is `data-pending` meanwhile, and a
29
+ refusal puts it back in the list it came from. */
30
+ export function Transfer({ onValueChange, onCommit, commitDelay, value, defaultValue, ...props }) {
31
+ const commit = useCommit({ value, defaultValue, onValueChange, onCommit, commitDelay, moved: movedLists });
32
+ const root = (_jsx(parts.Root, { ...props, value: commit.value, defaultValue: commit.defaultValue, ...heard({ onValueChange: commit.onValueChange }) }));
33
+ return commit.pending ? _jsx(PendingProvider, { value: commit.pending, children: root }) : root;
29
34
  }
30
35
  export function TransferList({ children, around, ...props }) {
31
36
  return (_jsx(parts.List, { ...props, children: (state) => {
@@ -33,5 +38,7 @@ export function TransferList({ children, around, ...props }) {
33
38
  return around ? around(items, state) : items;
34
39
  } }));
35
40
  }
36
- export const TransferItem = parts.Item;
41
+ export function TransferItem(props) {
42
+ return _jsx(parts.Item, { ...props, ...usePending(props.value) });
43
+ }
37
44
  export const TransferHandle = parts.Handle;
@@ -0,0 +1,169 @@
1
+ /* Asking someone else before a move is final, for the parts that move things (Spans, Sortable, Transfer). A page
2
+ that saves a move on a server has the same three problems each time, a field report said: the item must stay
3
+ where it was put while the server answers, a run of keystrokes must be one request and not six, and a refusal
4
+ must put it back and say so. This is that, once, over any of them.
5
+
6
+ With `onCommit` the part does not hand the page its value on every change. It draws the move itself (the item is
7
+ `data-pending`, `aria-busy`), batches the keyboard's steps that come within `commitDelay` of each other into one
8
+ call carrying the net change, commits a pointer's drop at once, and then:
9
+ - `true`: the move stands. `onValueChange` is heard now, with the value, so a controlled page takes it as it
10
+ would have; the part keeps drawing the move until the value it is given catches up, so nothing jumps back in
11
+ between;
12
+ - `false` or a rejection: the part goes back to the last place that was committed and says so to a screen reader
13
+ (the live announcer the behavior's own steps use).
14
+ Moves made while one is being asked about are allowed, and wait their turn, each asked about as the page left
15
+ things after the one before. Without `onCommit` nothing here runs: the part is exactly what it was. */
16
+ import { announce } from '@ui-sdk/core';
17
+ import { createContext, useContext, useEffect, useRef, useState } from 'react';
18
+ /** Said when a move is put back, to a screen reader: a caller's own sentence is the rejection's message. */
19
+ const REVERTED = 'Not saved. Put back where it was.';
20
+ const PendingContext = createContext(new Set());
21
+ /** Whether an item's move is being asked about: for the part that draws the item. */
22
+ export function usePending(key) {
23
+ const pending = useContext(PendingContext);
24
+ return key !== undefined && pending.has(key) ? { 'data-pending': '', 'aria-busy': 'true' } : undefined;
25
+ }
26
+ export const PendingProvider = PendingContext.Provider;
27
+ const same = (a, b) => a === b || JSON.stringify(a) === JSON.stringify(b);
28
+ /** What a list of keys moved: the one key that, taken out of both, leaves the same order (both, for a swap). */
29
+ export function movedKeys(from, to) {
30
+ if (same(from, to))
31
+ return [];
32
+ const without = (list, key) => list.filter((each) => each !== key);
33
+ const found = to.filter((key) => same(without(from, key), without(to, key)));
34
+ return found.length > 0 ? found : to.filter((key, at) => from[at] !== key);
35
+ }
36
+ /** The stretches that are somewhere else, or another size. */
37
+ export function movedSpans(from, to) {
38
+ const before = new Map(from.map((span) => [span.id, JSON.stringify(span)]));
39
+ return to.filter((span) => before.get(span.id) !== JSON.stringify(span)).map((span) => span.id);
40
+ }
41
+ /** The items that are in another list or another place in their own. */
42
+ export function movedLists(from, to) {
43
+ const home = (lists) => {
44
+ const out = new Map();
45
+ for (const [list, keys] of Object.entries(lists))
46
+ for (const key of keys)
47
+ out.set(key, list);
48
+ return out;
49
+ };
50
+ const was = home(from);
51
+ const out = new Set();
52
+ for (const [key, list] of home(to))
53
+ if (was.get(key) !== list)
54
+ out.add(key);
55
+ for (const [list, keys] of Object.entries(to))
56
+ if (!same(from[list], keys) && !keys.some((key) => out.has(key)))
57
+ movedKeys(from[list] ?? [], keys).forEach((key) => out.add(key));
58
+ return [...out];
59
+ }
60
+ /**
61
+ * The commit's state for one root: what to hand the primitive as its value and change callback, and which items are
62
+ * pending. With no `onCommit` it hands everything back as it was given.
63
+ */
64
+ export function useCommit({ value, defaultValue, onValueChange, onCommit, commitDelay = 600, moved, }) {
65
+ const controlled = value !== undefined;
66
+ const [own, setOwn] = useState(defaultValue);
67
+ const committed = controlled ? value : own;
68
+ const [drawn, setDrawn] = useState(null);
69
+ const [pending, setPending] = useState(new Set());
70
+ const run = useRef({
71
+ timer: undefined,
72
+ batch: null,
73
+ flight: Promise.resolve(),
74
+ busy: 0,
75
+ /* A refusal makes every move queued behind it moot: the ones still waiting are of an older generation. */
76
+ generation: 0,
77
+ confirmed: committed,
78
+ shown: committed,
79
+ });
80
+ const live = useRef({ committed, value, onValueChange, onCommit, commitDelay, moved, controlled });
81
+ live.current = { committed, value, onValueChange, onCommit, commitDelay, moved, controlled };
82
+ // While nothing is being asked about, what the page gives is what is committed.
83
+ if (run.current.busy === 0 && !run.current.batch && !drawn)
84
+ run.current.confirmed = committed;
85
+ useEffect(() => () => {
86
+ clearTimeout(run.current.timer);
87
+ run.current.generation += 1;
88
+ }, []);
89
+ // The value a controlled page was given has caught up with a move that stood: what is drawn is now the page's.
90
+ useEffect(() => {
91
+ if (drawn?.settled && (value !== drawn.base || same(value, drawn.value)))
92
+ setDrawn(null);
93
+ }, [value, drawn]);
94
+ const marks = () => {
95
+ const r = run.current;
96
+ setPending(new Set(r.shown !== undefined && r.confirmed !== undefined ? live.current.moved(r.confirmed, r.shown) : []));
97
+ };
98
+ const revert = (why) => {
99
+ const r = run.current;
100
+ r.generation += 1;
101
+ r.busy = 0;
102
+ r.batch = null;
103
+ clearTimeout(r.timer);
104
+ r.shown = r.confirmed;
105
+ setDrawn(null);
106
+ setPending(new Set());
107
+ announce(why ? `${why}. ${REVERTED}` : REVERTED);
108
+ };
109
+ const flush = () => {
110
+ const r = run.current;
111
+ clearTimeout(r.timer);
112
+ const batch = r.batch;
113
+ if (!batch)
114
+ return;
115
+ r.batch = null;
116
+ const generation = r.generation;
117
+ r.busy += 1;
118
+ r.flight = r.flight.then(async () => {
119
+ if (r.generation !== generation)
120
+ return;
121
+ const from = r.confirmed;
122
+ const movedNow = live.current.moved(from, batch.value);
123
+ let kept = true;
124
+ let why;
125
+ // Moves that cancel each other out (down, then up) leave nothing to ask about.
126
+ if (movedNow.length > 0) {
127
+ try {
128
+ kept = (await live.current.onCommit?.({ value: batch.value, from, moved: movedNow, details: batch.details })) === true;
129
+ }
130
+ catch (error) {
131
+ kept = false;
132
+ why = error instanceof Error ? error.message : undefined;
133
+ }
134
+ }
135
+ if (r.generation !== generation)
136
+ return;
137
+ r.busy -= 1;
138
+ if (!kept)
139
+ return revert(why);
140
+ r.confirmed = batch.value;
141
+ if (!live.current.controlled)
142
+ setOwn(batch.value);
143
+ live.current.onValueChange?.(batch.value, batch.details);
144
+ if (r.busy === 0 && !r.batch) {
145
+ setPending(new Set());
146
+ if (live.current.controlled)
147
+ setDrawn({ value: batch.value, settled: true, base: live.current.value });
148
+ else
149
+ setDrawn(null);
150
+ }
151
+ });
152
+ };
153
+ const changed = (next, details) => {
154
+ const r = run.current;
155
+ r.batch = { value: next, details };
156
+ r.shown = next;
157
+ setDrawn({ value: next, settled: false, base: undefined });
158
+ marks();
159
+ clearTimeout(r.timer);
160
+ if (details.source === 'keyboard')
161
+ r.timer = setTimeout(flush, live.current.commitDelay);
162
+ else
163
+ flush();
164
+ };
165
+ if (!onCommit)
166
+ return { value, defaultValue, onValueChange, pending: undefined };
167
+ const shown = drawn && !(drawn.settled && same(committed, drawn.value)) ? drawn.value : committed;
168
+ return { value: shown, defaultValue: undefined, onValueChange: changed, pending };
169
+ }
@@ -39,6 +39,29 @@ export type PartProps<Tag extends keyof ReactTypes.JSX.IntrinsicElements, State
39
39
  children?: ReactTypes.ReactNode | ((state: State) => ReactTypes.ReactNode);
40
40
  };
41
41
 
42
+ /** What `onCommit` is asked: where the moves leave things, where they were, and what moved. */
43
+ export type Committing<Value, Details = Change> = {
44
+ /** The whole value as the moves leave it. */
45
+ value: Value;
46
+ /** The whole value as it was last committed. */
47
+ from: Value;
48
+ /** The keys (or ids) of what the moves, taken together, left somewhere else. */
49
+ moved: string[];
50
+ /** The last change behind it: its reason, its hand, and what the part adds to it. */
51
+ details: Details;
52
+ };
53
+ /**
54
+ * Asks before a move is final, for a page that saves it on a server. The item is `data-pending` (and `aria-busy`)
55
+ * while it is asked, keyboard steps within `commitDelay` are one call carrying the net change, and a pointer's drop
56
+ * is asked at once. `true` keeps the move and then `onValueChange` is heard; `false` or a rejection puts everything
57
+ * back to the last committed place and says so to a screen reader. Without `onCommit` nothing changes.
58
+ */
59
+ export type CommitProps<Value, Details = Change> = {
60
+ onCommit?: (change: Committing<Value, Details>) => Promise<boolean>;
61
+ /** How long keyboard steps are gathered into one commit, in milliseconds. Default 600. */
62
+ commitDelay?: number;
63
+ };
64
+
42
65
  /* Without this a declaration file exports every name in it; `Part` is how the parts below are written, not one of them. */
43
66
  export {};
44
67
 
@@ -49,7 +72,9 @@ type Part<Tag extends keyof ReactTypes.JSX.IntrinsicElements, Props = {}, State
49
72
  /* ---------- a list whose order is the reader's ---------- */
50
73
 
51
74
  export type SortableReason = 'drag' | 'move-key' | 'imperative';
52
- export interface SortableProps extends Omit<PartProps<'div', { order: string[]; moving: string | null }>, 'children' | 'defaultValue'> {
75
+ export interface SortableProps
76
+ extends Omit<PartProps<'div', { order: string[]; moving: string | null }>, 'children' | 'defaultValue'>,
77
+ CommitProps<string[], Change<SortableReason>> {
53
78
  /** The order, as the items' keys. */
54
79
  value?: string[];
55
80
  defaultValue?: string[];
@@ -75,7 +100,9 @@ export type TransferReason = 'drag' | 'move-key' | 'imperative';
75
100
  /** What moved: the item, the list it left, the list it is in, and its place there. */
76
101
  export type TransferMove = { key: string; from: string; to: string; index: number };
77
102
  export type TransferListState = { keys: string[]; over: boolean; takes: boolean; refuses: boolean; carrying: string | null };
78
- export interface TransferProps extends Omit<PartProps<'div', { carrying: string | null }>, 'defaultValue'> {
103
+ export interface TransferProps
104
+ extends Omit<PartProps<'div', { carrying: string | null }>, 'defaultValue'>,
105
+ CommitProps<Record<string, string[]>, Change<TransferReason, TransferMove>> {
79
106
  /** Every list by its name, each the keys of its items in order. A key is in one list. */
80
107
  value?: Record<string, string[]>;
81
108
  defaultValue?: Record<string, string[]>;
@@ -151,7 +178,9 @@ export function weeksOf(month: string, weekStart: number): string[][];
151
178
  export type Span = { id: string; track: string; start: number; end: number };
152
179
  export type SpansReason = 'drag' | 'move-key' | 'remove-press' | 'imperative';
153
180
  export type SpanAction = 'draw' | 'move' | 'resize' | 'delete';
154
- export interface SpansProps extends Omit<PartProps<'div', { spans: Span[]; changing: string | null }>, 'defaultValue'> {
181
+ export interface SpansProps
182
+ extends Omit<PartProps<'div', { spans: Span[]; changing: string | null }>, 'defaultValue'>,
183
+ CommitProps<Span[], Change<SpansReason, { id: string; action: SpanAction }>> {
155
184
  value?: Span[];
156
185
  defaultValue?: Span[];
157
186
  onValueChange?: (value: Span[], details: Change<SpansReason, { id: string; action: SpanAction }>) => void;
@@ -175,8 +204,38 @@ export function Spans(props: SpansProps): ReactTypes.ReactElement | null;
175
204
  export function SpansTrack(
176
205
  props: Omit<PartProps<'div', { spans: Span[] }>, 'children'> & { value: string; children: (spans: Span[]) => ReactTypes.ReactNode },
177
206
  ): ReactTypes.ReactElement | null;
178
- /** One stretch. An element inside with `data-span-edge="start"` or `"end"` is the edge a pointer lengthens it by. */
207
+ /** One stretch. An element inside with `data-span-edge="start"` or `"end"` is the edge a pointer lengthens it by. `data-pending` under `onCommit`. */
179
208
  export const SpansSpan: Part<'div', { value: string }, { span: Span | undefined; action: SpanAction | undefined }>;
209
+ /**
210
+ * Labels and lines at every `step` from the range's start to its end. Fills the element it is in (a Spans root or a
211
+ * track), which stands where the range stands, and is placed along it by the root's `axis`. Decorative: `aria-hidden`.
212
+ * @preview
213
+ */
214
+ export function SpansAxis(props: {
215
+ step: number;
216
+ /** Says a value: an hour, a date. */
217
+ format?: (value: number) => ReactTypes.ReactNode;
218
+ labels?: boolean;
219
+ lines?: boolean;
220
+ /** Labels one tick in so many. */
221
+ every?: number;
222
+ className?: string;
223
+ }): ReactTypes.ReactElement | null;
224
+ /** A line at `value`: drawn again each minute (or each `every` ms) when `value` is a function, and not drawn outside the range. @preview */
225
+ export function SpansNow(props: { value: number | (() => number); every?: number; className?: string }): ReactTypes.ReactElement | null;
226
+ /**
227
+ * A stretch from `from` to `to` that nothing is moved or lengthened into; a change that would leave a stretch in it is
228
+ * refused and said. Inside a track it refuses only that track's stretches; `track` names another; in the root, every
229
+ * track's. Its children are its label.
230
+ * @preview
231
+ */
232
+ export function SpansRegion(props: {
233
+ from: number;
234
+ to: number;
235
+ track?: string;
236
+ children?: ReactTypes.ReactNode;
237
+ className?: string;
238
+ }): ReactTypes.ReactElement | null;
180
239
 
181
240
  /* ---------- a position read along a line ---------- */
182
241
 
@@ -34,6 +34,7 @@ export { Scrub, ScrubTrack } from './ScrubParts.js';
34
34
  export { scrub } from './scrub.js';
35
35
  export { Sortable, SortableHandle, SortableItem } from './SortableParts.js';
36
36
  export { sortable } from './sortable.js';
37
+ export { SpansAxis, SpansNow, SpansRegion } from './SpansGridParts.js';
37
38
  export { Spans, SpansSpan, SpansTrack } from './SpansParts.js';
38
39
  export { spans } from './spans.js';
39
40
  export { Transfer, TransferHandle, TransferItem, TransferList } from './TransferParts.js';
@@ -174,9 +174,12 @@ export const spans = definePrimitive({
174
174
  style: span ? part.api.core.spanStyle(span, stackSpans(part.api.core.spansOn(state, span.track)).get(span.id)) : undefined,
175
175
  tabindex: part.api.focused(state) === part.key ? '0' : '-1',
176
176
  'data-locked': part.api.locked(part.key) ? '' : undefined,
177
+ // The keys that nudge along the axis, as the keydown below reads them.
177
178
  'aria-keyshortcuts': part.api.locked(part.key)
178
179
  ? undefined
179
- : 'Alt+ArrowLeft Alt+ArrowRight Alt+Shift+ArrowLeft Alt+Shift+ArrowRight',
180
+ : part.api.vertical()
181
+ ? 'Alt+ArrowUp Alt+ArrowDown Alt+Shift+ArrowUp Alt+Shift+ArrowDown'
182
+ : 'Alt+ArrowLeft Alt+ArrowRight Alt+Shift+ArrowLeft Alt+Shift+ArrowRight',
180
183
  };
181
184
  },
182
185
  bind: (el, part) => part.api.core.bindSpan(el, part.key),
@@ -0,0 +1,13 @@
1
+ /* What the parts that draw a range (SpansAxis, SpansNow, SpansRegion) need of the Spans around them: the range
2
+ and the way it runs, and the regions that refuse a stretch, which the root asks about before a change is final. */
3
+ import { createContext } from 'react';
4
+ export const SpansRangeContext = createContext({ min: 0, max: 100, axis: 'x', regions: new Map() });
5
+ /** Where a value is along the range, as a share of it, kept to the range's ends. */
6
+ export const share = (range, value) => range.max === range.min ? 0 : Math.min(1, Math.max(0, (value - range.min) / (range.max - range.min)));
7
+ /** The region that a stretch from `start` to `end` on `track` would overlap, if any. */
8
+ export function refusedBy(range, track, start, end) {
9
+ for (const region of range.regions.values())
10
+ if ((region.track === undefined || region.track === track) && start < region.to && end > region.from)
11
+ return region;
12
+ return undefined;
13
+ }
@@ -248,6 +248,110 @@
248
248
  display: block;
249
249
  }
250
250
 
251
+ /* What draws the range: each fills the box it is in (the root, or a track) and is placed along it by the part, so the
252
+ parent is where the range stands. None takes a press, so a stretch under a line is still carried. */
253
+ :is([data-slot='spans'], [data-slot='spans-track']) {
254
+ position: relative;
255
+ }
256
+
257
+ [data-slot='spans-axis'] {
258
+ position: absolute;
259
+ inset: 0;
260
+ pointer-events: none;
261
+ color: var(--m-ink-subtle);
262
+ font-size: var(--m-text);
263
+ }
264
+
265
+ [data-slot='spans-axis-line'],
266
+ [data-slot='spans-axis-label'],
267
+ [data-slot='spans-now'] {
268
+ position: absolute;
269
+ }
270
+
271
+ [data-slot='spans-axis-line'] {
272
+ background: var(--m-line);
273
+ }
274
+
275
+ [data-slot='spans-axis'][data-axis='y'] [data-slot='spans-axis-line'] {
276
+ right: 0;
277
+ left: 0;
278
+ height: 1px;
279
+ }
280
+
281
+ [data-slot='spans-axis'][data-axis='x'] [data-slot='spans-axis-line'] {
282
+ top: 0;
283
+ bottom: 0;
284
+ width: 1px;
285
+ }
286
+
287
+ [data-slot='spans-axis-label'] {
288
+ font-variant-numeric: tabular-nums;
289
+ white-space: nowrap;
290
+ }
291
+
292
+ [data-slot='spans-axis'][data-axis='y'] [data-slot='spans-axis-label'] {
293
+ left: 0;
294
+ translate: 0 -50%;
295
+ }
296
+
297
+ [data-slot='spans-axis'][data-axis='x'] [data-slot='spans-axis-label'] {
298
+ top: 0;
299
+ translate: -50% 0;
300
+ }
301
+
302
+ [data-slot='spans-now'] {
303
+ z-index: 1;
304
+ background: var(--m-critical);
305
+ pointer-events: none;
306
+
307
+ &[data-axis='y'] {
308
+ right: 0;
309
+ left: 0;
310
+ height: 2px;
311
+ translate: 0 -1px;
312
+ }
313
+
314
+ &[data-axis='x'] {
315
+ top: 0;
316
+ bottom: 0;
317
+ width: 2px;
318
+ translate: -1px 0;
319
+ }
320
+ }
321
+
322
+ /* A region is a stretch nothing is carried into: hatched in the ground's own line, so it reads as unavailable
323
+ without a colour of its own. */
324
+ [data-slot='spans-region'] {
325
+ position: absolute;
326
+ display: flex;
327
+ align-items: flex-start;
328
+ justify-content: center;
329
+ padding: 0.125rem 0.25rem;
330
+ overflow: hidden;
331
+ background: repeating-linear-gradient(135deg, transparent 0 0.375rem, color-mix(in srgb, var(--m-line) 60%, transparent) 0.375rem 0.4375rem);
332
+ color: var(--m-ink-subtle);
333
+ font-size: var(--m-text);
334
+ pointer-events: none;
335
+
336
+ &[data-axis='y'] {
337
+ right: 0;
338
+ left: 0;
339
+ }
340
+
341
+ &[data-axis='x'] {
342
+ top: 0;
343
+ bottom: 0;
344
+ }
345
+ }
346
+
347
+ [data-slot='spans-span'][data-pending] {
348
+ opacity: 0.6;
349
+ }
350
+
351
+ :is([data-slot='sortable-item'], [data-slot='transfer-item'])[data-pending] {
352
+ opacity: 0.6;
353
+ }
354
+
251
355
  [data-slot='spans-span']:focus-visible,
252
356
  [data-slot='days-day']:focus-visible,
253
357
  [data-slot='pick-option']:focus-visible,
@@ -130,7 +130,9 @@
130
130
  padding: var(--m-space-3h) var(--m-space-4h);
131
131
  }
132
132
 
133
- & > div + div {
133
+ /* Not `div + div`: a bare tag on the right of `+` is checked against the next sibling of every div added or removed
134
+ anywhere in the page, and a list's filter restyled each of its rows for it. `:first-child` is asked only here. */
135
+ & > div:not(:first-child) {
134
136
  border-block-start: 1px solid var(--m-line);
135
137
  }
136
138
 
package/dist/tokens.css CHANGED
@@ -64,6 +64,9 @@
64
64
  --mono: var(--data-face, var(--code));
65
65
  --sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
66
66
  --radius: 6px;
67
+ /* A card's, a tile's and a row's corner, apart from a control's: a round-control look makes --radius a box's 8px
68
+ and the control a pill, and an App's own card reads this one. It follows the panel corner a theme sets. */
69
+ --radius-card: var(--panel-radius, var(--radius));
67
70
  /* A form control's own size, said once for every one of them (the field box, a listbox, a choice, a button's pair):
68
71
  its fill, its height and the width of its edge. A theme sets them to draw a filled, taller or heavier field; the
69
72
  defaults are the look they have always had. The corner is --field-radius, read where it is used, --radius unsaid. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mistriwork/ui",
3
- "version": "1.0.0-rc.59",
3
+ "version": "1.0.0-rc.61",
4
4
  "description": "The standalone React primitives both shells are drawn with: Modal, DropMenu, Dropdown, Typeahead, Button, Field, Icon and DateField over UI SDK, Switch, Tabs, Drawer, Popover, Avatar, Pagination, Carousel, Resizable, ScrollArea and Tooltip, the form controls built on them (text, masked, money and sensitive boxes, listboxes, choices, slider, progress and action bar; Preview), the landing hero's blocks (badge, photo tiles, icon rows; Preview), their tokens and stylesheet, and the zoom-aware ruler their popups are placed by.",
5
5
  "homepage": "https://mistriwork.dev",
6
6
  "bugs": {