@discuss-ds/ui 0.32.0 → 0.33.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/dist/index.d.cts CHANGED
@@ -327,6 +327,10 @@ interface InlineEditMark {
327
327
  end: number;
328
328
  /** Corrections offered in the mark's card, best first (at most three are shown). Used with `onMarkApply`: with one the card has an Apply button; with several each is a choice applied by a click on it and there is no Apply button. An empty string offers to remove the marked text (the button reads "Remove"). */
329
329
  suggestions?: string[];
330
+ /** The colour of the underline: `destructive` (default, for mistakes such as typos) or `warning` (for a recommendation, such as wording that could be clearer). */
331
+ tone?: 'destructive' | 'warning';
332
+ /** Why the words are marked, shown in the mark's card above the suggestions. Without it the card is unchanged. */
333
+ message?: string;
330
334
  }
331
335
 
332
336
  interface InlineEditProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange' | 'defaultValue'> {
@@ -383,8 +387,9 @@ interface InlineEditProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'on
383
387
  textClassName?: string;
384
388
  /**
385
389
  * Ranges of the saved text to underline in the destructive colour, for example misspelled words found by a spell checker. `start` is the
386
- * first character and `end` the one after the last. Read mode only: the text box shown while editing has no underline. Ranges that are
387
- * out of bounds, empty or overlapping are ignored.
390
+ * first character and `end` the one after the last. While the field is being edited the underlines stay (as plain lines: they take no click),
391
+ * moving with the text typed after them and going away when the words they mark are changed. Ranges that are out of bounds, empty or
392
+ * overlapping are ignored.
388
393
  */
389
394
  marks?: InlineEditMark[];
390
395
  /**
@@ -559,10 +564,10 @@ declare const DropdownMenuSeparator: React.ForwardRefExoticComponent<Omit<Dropdo
559
564
 
560
565
  type ToastVariant = 'info' | 'success' | 'warning' | 'destructive';
561
566
  declare const toastVariants: (props?: ({
562
- variant?: "destructive" | "success" | "info" | "warning" | null | undefined;
567
+ variant?: "destructive" | "success" | "warning" | "info" | null | undefined;
563
568
  } & class_variance_authority_types.ClassProp) | undefined) => string;
564
569
  declare const inlineVariants: (props?: ({
565
- variant?: "destructive" | "success" | "info" | "warning" | "neutral" | null | undefined;
570
+ variant?: "destructive" | "success" | "warning" | "info" | "neutral" | null | undefined;
566
571
  } & class_variance_authority_types.ClassProp) | undefined) => string;
567
572
  type ToastInlineVariant = ToastVariant | 'neutral';
568
573
  declare const ToastProvider: React.FC<Toast$1.ToastProviderProps>;
package/dist/index.d.ts CHANGED
@@ -327,6 +327,10 @@ interface InlineEditMark {
327
327
  end: number;
328
328
  /** Corrections offered in the mark's card, best first (at most three are shown). Used with `onMarkApply`: with one the card has an Apply button; with several each is a choice applied by a click on it and there is no Apply button. An empty string offers to remove the marked text (the button reads "Remove"). */
329
329
  suggestions?: string[];
330
+ /** The colour of the underline: `destructive` (default, for mistakes such as typos) or `warning` (for a recommendation, such as wording that could be clearer). */
331
+ tone?: 'destructive' | 'warning';
332
+ /** Why the words are marked, shown in the mark's card above the suggestions. Without it the card is unchanged. */
333
+ message?: string;
330
334
  }
331
335
 
332
336
  interface InlineEditProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange' | 'defaultValue'> {
@@ -383,8 +387,9 @@ interface InlineEditProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'on
383
387
  textClassName?: string;
384
388
  /**
385
389
  * Ranges of the saved text to underline in the destructive colour, for example misspelled words found by a spell checker. `start` is the
386
- * first character and `end` the one after the last. Read mode only: the text box shown while editing has no underline. Ranges that are
387
- * out of bounds, empty or overlapping are ignored.
390
+ * first character and `end` the one after the last. While the field is being edited the underlines stay (as plain lines: they take no click),
391
+ * moving with the text typed after them and going away when the words they mark are changed. Ranges that are out of bounds, empty or
392
+ * overlapping are ignored.
388
393
  */
389
394
  marks?: InlineEditMark[];
390
395
  /**
@@ -559,10 +564,10 @@ declare const DropdownMenuSeparator: React.ForwardRefExoticComponent<Omit<Dropdo
559
564
 
560
565
  type ToastVariant = 'info' | 'success' | 'warning' | 'destructive';
561
566
  declare const toastVariants: (props?: ({
562
- variant?: "destructive" | "success" | "info" | "warning" | null | undefined;
567
+ variant?: "destructive" | "success" | "warning" | "info" | null | undefined;
563
568
  } & class_variance_authority_types.ClassProp) | undefined) => string;
564
569
  declare const inlineVariants: (props?: ({
565
- variant?: "destructive" | "success" | "info" | "warning" | "neutral" | null | undefined;
570
+ variant?: "destructive" | "success" | "warning" | "info" | "neutral" | null | undefined;
566
571
  } & class_variance_authority_types.ClassProp) | undefined) => string;
567
572
  type ToastInlineVariant = ToastVariant | 'neutral';
568
573
  declare const ToastProvider: React.FC<Toast$1.ToastProviderProps>;
package/dist/index.js CHANGED
@@ -1461,37 +1461,96 @@ function splitMarks(text, marks) {
1461
1461
  if (at < text.length) out.push({ text: text.slice(at) });
1462
1462
  return out;
1463
1463
  }
1464
- var markClasses = "rounded-xxs border-b-2 border-destructive-foreground";
1465
- function useMarkAlignment(ref, text) {
1466
- const [padding, setPadding] = React24.useState({ top: 0, bottom: 0 });
1467
- React24.useLayoutEffect(() => {
1468
- const mark = ref.current;
1469
- const button = mark?.closest("button");
1470
- if (!mark || !button) return;
1471
- const align = () => {
1472
- const m = mark.getBoundingClientRect();
1464
+ var lineClasses = (tone, hovered) => cn("pointer-events-none absolute -z-10 rounded-xxs border-b-2", tone === "warning" ? "border-warning-foreground bg-warning-background-highlight" : cn("border-destructive-foreground", hovered && "bg-destructive-soft"));
1465
+ function markLines(rects, origin, field) {
1466
+ const count = field ? Math.max(1, Math.round((field.contentBottom - field.contentTop) / field.lineHeight)) : 1;
1467
+ const boxes = rects.filter((r) => r.width > 0).map((r) => {
1468
+ if (!field) return { left: r.left - origin.left, top: r.top - origin.top, width: r.width, height: r.height };
1469
+ const index = Math.min(count - 1, Math.max(0, Math.floor((r.top + r.height / 2 - field.contentTop) / field.lineHeight)));
1470
+ const top = index === 0 ? field.top : field.contentTop + index * field.lineHeight;
1471
+ const bottom = index === count - 1 ? field.bottom : field.contentTop + (index + 1) * field.lineHeight;
1472
+ return { left: r.left - origin.left, top: top - origin.top, width: r.width, height: bottom - top };
1473
+ });
1474
+ return boxes.reduce((out, box) => {
1475
+ const same = field ? out.find((o) => Math.abs(o.top - box.top) < 0.5 && Math.abs(o.height - box.height) < 0.5) : void 0;
1476
+ if (!same) return [...out, box];
1477
+ const left = Math.min(same.left, box.left);
1478
+ const right = Math.max(same.left + same.width, box.left + box.width);
1479
+ return out.map((o) => o === same ? { ...o, left, width: right - left } : o);
1480
+ }, []);
1481
+ }
1482
+ var sameLines = (a, b) => a.length === b.length && a.every((l, i) => Object.keys(l).every((k) => Math.abs(l[k] - b[i][k]) < 0.5));
1483
+ function useMarkLines(anchorRef, markRef) {
1484
+ const [lines, setLines] = React24.useState([]);
1485
+ const measure = React24.useCallback(() => {
1486
+ const anchor = anchorRef.current;
1487
+ const mark = markRef.current;
1488
+ if (!anchor || !mark) return;
1489
+ const button = mark.closest("button");
1490
+ const a = anchor.getBoundingClientRect();
1491
+ let field;
1492
+ if (button) {
1473
1493
  const b = button.getBoundingClientRect();
1474
- const markStyle = getComputedStyle(mark);
1475
- const buttonStyle = getComputedStyle(button);
1476
- const border = parseFloat(markStyle.borderBottomWidth);
1477
- const baseTop = m.top + parseFloat(markStyle.paddingTop);
1478
- const baseBottom = m.bottom - parseFloat(markStyle.paddingBottom) - border;
1479
- const contentTop = b.top + parseFloat(buttonStyle.borderTopWidth) + parseFloat(buttonStyle.paddingTop);
1480
- const contentBottom = b.bottom - parseFloat(buttonStyle.borderBottomWidth) - parseFloat(buttonStyle.paddingBottom);
1481
- const lineHeight = parseFloat(buttonStyle.lineHeight) || contentBottom - contentTop;
1482
- const lines = Math.max(1, Math.round((contentBottom - contentTop) / lineHeight));
1483
- const lineIndex = Math.min(lines - 1, Math.max(0, Math.floor(((baseTop + baseBottom) / 2 - contentTop) / lineHeight)));
1484
- const targetTop = lineIndex === 0 ? b.top : contentTop + lineIndex * lineHeight;
1485
- const targetBottom = lineIndex === lines - 1 ? b.bottom : contentTop + (lineIndex + 1) * lineHeight;
1486
- const next = { top: Math.max(0, baseTop - targetTop), bottom: Math.max(0, targetBottom - baseBottom - border) };
1487
- setPadding((p) => Math.abs(p.top - next.top) < 0.5 && Math.abs(p.bottom - next.bottom) < 0.5 ? p : next);
1488
- };
1489
- align();
1490
- const observer = new ResizeObserver(align);
1494
+ const style = getComputedStyle(button);
1495
+ const contentTop = b.top + (parseFloat(style.borderTopWidth) || 0) + (parseFloat(style.paddingTop) || 0);
1496
+ const contentBottom = b.bottom - (parseFloat(style.borderBottomWidth) || 0) - (parseFloat(style.paddingBottom) || 0);
1497
+ field = { top: b.top, bottom: b.bottom, contentTop, contentBottom, lineHeight: parseFloat(style.lineHeight) || contentBottom - contentTop };
1498
+ }
1499
+ const next = markLines([...mark.getClientRects()], a, field);
1500
+ setLines((prev) => sameLines(prev, next) ? prev : next);
1501
+ }, [anchorRef, markRef]);
1502
+ React24.useLayoutEffect(measure);
1503
+ React24.useLayoutEffect(() => {
1504
+ const button = markRef.current?.closest("button");
1505
+ if (!button) return;
1506
+ const observer = new ResizeObserver(measure);
1491
1507
  observer.observe(button);
1492
1508
  return () => observer.disconnect();
1493
- }, [ref, text]);
1494
- return padding;
1509
+ }, [markRef, measure]);
1510
+ return lines;
1511
+ }
1512
+ function remapMarks(saved, draft, marks) {
1513
+ if (!marks?.length) return [];
1514
+ if (saved === draft) return marks;
1515
+ const max = Math.min(saved.length, draft.length);
1516
+ let before = 0;
1517
+ while (before < max && saved[before] === draft[before]) before += 1;
1518
+ let after = 0;
1519
+ while (after < max - before && saved[saved.length - 1 - after] === draft[draft.length - 1 - after]) after += 1;
1520
+ const editEnd = saved.length - after;
1521
+ const delta = draft.length - saved.length;
1522
+ return marks.flatMap((m) => m.end <= before ? [m] : m.start >= editEnd ? [{ ...m, start: m.start + delta, end: m.end + delta }] : []);
1523
+ }
1524
+ function DraftMarks({ text, marks, layer, className, scrollLeft = 0, singleLine }) {
1525
+ const rootRef = React24.useRef(null);
1526
+ const textRef = React24.useRef(null);
1527
+ const [boxes, setBoxes] = React24.useState([]);
1528
+ React24.useLayoutEffect(() => {
1529
+ if (textRef.current) textRef.current.scrollLeft = scrollLeft;
1530
+ }, [scrollLeft, text]);
1531
+ React24.useLayoutEffect(() => {
1532
+ const root = rootRef.current;
1533
+ const mirror = textRef.current;
1534
+ if (!root || !mirror) return;
1535
+ const r = root.getBoundingClientRect();
1536
+ const style = getComputedStyle(mirror);
1537
+ const contentTop = r.top + (parseFloat(style.borderTopWidth) || 0) + (parseFloat(style.paddingTop) || 0);
1538
+ const contentBottom = r.bottom - (parseFloat(style.borderBottomWidth) || 0) - (parseFloat(style.paddingBottom) || 0);
1539
+ const lineHeight = singleLine ? contentBottom - contentTop : parseFloat(style.lineHeight) || contentBottom - contentTop;
1540
+ const field = { top: r.top, bottom: r.bottom, contentTop, contentBottom, lineHeight };
1541
+ const next = [...mirror.querySelectorAll("[data-draft-mark]")].flatMap(
1542
+ (el) => markLines([...el.getClientRects()], { left: r.left, top: r.top }, field).map((box) => ({ tone: el.dataset.tone ?? "destructive", box }))
1543
+ );
1544
+ setBoxes((prev) => prev.length === next.length && prev.every((p, i) => p.tone === next[i].tone && sameLines([p.box], [next[i].box])) ? prev : next);
1545
+ });
1546
+ return /* @__PURE__ */ jsxs17("div", { ref: rootRef, "aria-hidden": true, "data-draft-marks": true, "data-layer": layer, className: "pointer-events-none absolute inset-0", children: [
1547
+ /* @__PURE__ */ jsx25("div", { ref: textRef, className: cn(className, "absolute inset-0 overflow-hidden text-transparent"), children: splitMarks(text, marks).map(
1548
+ (piece, i) => piece.mark ? /* @__PURE__ */ jsx25("span", { "data-draft-mark": true, "data-tone": piece.mark.tone ?? "destructive", children: piece.text }, i) : /* @__PURE__ */ jsx25("span", { children: piece.text }, i)
1549
+ ) }),
1550
+ /* @__PURE__ */ jsx25("div", { className: "absolute inset-0", style: { clipPath: "inset(-4px 0)" }, children: boxes.map(
1551
+ ({ tone, box }, i) => layer === "behind" ? tone === "warning" && /* @__PURE__ */ jsx25("span", { "data-draft-box": true, className: "absolute rounded-xxs bg-warning-background-highlight", style: box }, i) : /* @__PURE__ */ jsx25("span", { "data-draft-box": true, className: cn("absolute rounded-xxs border-b-2", tone === "warning" ? "border-warning-foreground" : "border-destructive-foreground"), style: box }, i)
1552
+ ) })
1553
+ ] });
1495
1554
  }
1496
1555
  var closeOpenMarkCard = null;
1497
1556
  function MarkedText({ mark, text, onApply, onDismiss }) {
@@ -1504,12 +1563,19 @@ function MarkedText({ mark, text, onApply, onDismiss }) {
1504
1563
  },
1505
1564
  [close]
1506
1565
  );
1507
- const padding = useMarkAlignment(ref, text);
1508
- const style = { paddingTop: padding.top, paddingBottom: padding.bottom };
1566
+ const anchorRef = React24.useRef(null);
1567
+ const [hovered, setHovered] = React24.useState(false);
1568
+ const lines = useMarkLines(anchorRef, ref);
1569
+ const layers = /* @__PURE__ */ jsx25("span", { ref: anchorRef, "aria-hidden": true, className: "relative isolate", children: lines.map((line, i) => /* @__PURE__ */ jsx25("span", { "data-mark-line": true, className: lineClasses(mark.tone, hovered), style: line }, i)) });
1509
1570
  if (!onApply && !onDismiss) {
1510
- return /* @__PURE__ */ jsx25("span", { ref, "data-mark": true, className: markClasses, style, children: text });
1571
+ return /* @__PURE__ */ jsxs17(Fragment4, { children: [
1572
+ layers,
1573
+ /* @__PURE__ */ jsx25("span", { ref, "data-mark": true, "data-tone": mark.tone ?? "destructive", className: "relative", children: text })
1574
+ ] });
1511
1575
  }
1512
1576
  const suggestions = (mark.suggestions ?? []).slice(0, 3);
1577
+ const warning = mark.tone === "warning";
1578
+ const struck = "text-destructive-foreground";
1513
1579
  const handleOpenChange = (next) => {
1514
1580
  if (next) {
1515
1581
  if (closeOpenMarkCard && closeOpenMarkCard !== close) closeOpenMarkCard();
@@ -1521,7 +1587,22 @@ function MarkedText({ mark, text, onApply, onDismiss }) {
1521
1587
  };
1522
1588
  const stop = (event) => event.stopPropagation();
1523
1589
  return /* @__PURE__ */ jsxs17(PopoverPrimitive2.Root, { open, onOpenChange: handleOpenChange, children: [
1524
- /* @__PURE__ */ jsx25(PopoverPrimitive2.Trigger, { asChild: true, children: /* @__PURE__ */ jsx25("span", { ref, "data-mark": true, role: "button", tabIndex: -1, onClick: stop, style, className: cn(markClasses, "cursor-pointer hover:bg-destructive-soft"), children: text }) }),
1590
+ layers,
1591
+ /* @__PURE__ */ jsx25(PopoverPrimitive2.Trigger, { asChild: true, children: /* @__PURE__ */ jsx25(
1592
+ "span",
1593
+ {
1594
+ ref,
1595
+ "data-mark": true,
1596
+ "data-tone": mark.tone ?? "destructive",
1597
+ role: "button",
1598
+ tabIndex: -1,
1599
+ onClick: stop,
1600
+ onMouseEnter: () => setHovered(true),
1601
+ onMouseLeave: () => setHovered(false),
1602
+ className: "relative cursor-pointer",
1603
+ children: text
1604
+ }
1605
+ ) }),
1525
1606
  /* @__PURE__ */ jsx25(PopoverPrimitive2.Portal, { children: /* @__PURE__ */ jsxs17(
1526
1607
  PopoverPrimitive2.Content,
1527
1608
  {
@@ -1536,18 +1617,19 @@ function MarkedText({ mark, text, onApply, onDismiss }) {
1536
1617
  suggestions.length > 1 && onApply ? (
1537
1618
  // Several possible fixes: each one is a choice, applied with a click, so there is no Apply button.
1538
1619
  /* @__PURE__ */ jsx25("div", { className: "flex flex-col gap-1", role: "group", "aria-label": "Choose a correction", children: suggestions.map((suggestion) => /* @__PURE__ */ jsxs17(Button, { type: "button", size: "sm", variant: "ghost", className: "h-auto justify-start px-2 py-1 whitespace-normal", onClick: () => onApply(mark, suggestion), children: [
1539
- /* @__PURE__ */ jsx25("s", { className: "text-destructive-foreground", children: text }),
1620
+ /* @__PURE__ */ jsx25("s", { className: struck, children: text }),
1540
1621
  " ",
1541
1622
  /* @__PURE__ */ jsx25("span", { className: "font-semibold text-accent-foreground", children: suggestion === "" ? "Remove" : suggestion })
1542
1623
  ] }, suggestion)) })
1543
1624
  ) : suggestions.length > 0 && /* @__PURE__ */ jsxs17("p", { className: "min-w-0 break-words", children: [
1544
- /* @__PURE__ */ jsx25("s", { className: "text-destructive-foreground", children: text }),
1625
+ /* @__PURE__ */ jsx25("s", { className: struck, children: text }),
1545
1626
  suggestions[0] !== "" && /* @__PURE__ */ jsxs17(Fragment4, { children: [
1546
1627
  " ",
1547
1628
  /* @__PURE__ */ jsx25("span", { className: "font-semibold text-accent-foreground", children: suggestions[0] })
1548
1629
  ] })
1549
1630
  ] }),
1550
- /* @__PURE__ */ jsxs17("div", { className: "flex items-center justify-between gap-3", children: [
1631
+ mark.message && /* @__PURE__ */ jsx25("p", { className: "min-w-0 break-words text-body-small text-surface-foreground", children: mark.message }),
1632
+ /* @__PURE__ */ jsxs17("div", { className: cn("flex items-center gap-3", warning ? "justify-end" : "justify-between"), children: [
1551
1633
  onDismiss ? /* @__PURE__ */ jsxs17(Button, { type: "button", size: "sm", variant: "ghost", onClick: () => onDismiss(mark), children: [
1552
1634
  /* @__PURE__ */ jsx25(Trash2, { size: 16, "aria-hidden": true }),
1553
1635
  " Dismiss"
@@ -1600,6 +1682,8 @@ var InlineEdit = React25.forwardRef(
1600
1682
  const [error, setError] = React25.useState(null);
1601
1683
  const errorId = React25.useId();
1602
1684
  const fieldRef = React25.useRef(null);
1685
+ const [scroll, setScroll] = React25.useState(0);
1686
+ const [draftMarks, setDraftMarks] = React25.useState([]);
1603
1687
  const readRef = React25.useRef(null);
1604
1688
  const cancelled = React25.useRef(false);
1605
1689
  const restoreFocus = React25.useRef(false);
@@ -1621,6 +1705,7 @@ var InlineEdit = React25.forwardRef(
1621
1705
  const start = () => {
1622
1706
  if (disabled) return;
1623
1707
  setDraft(value);
1708
+ setDraftMarks(marks ?? []);
1624
1709
  setError(null);
1625
1710
  cancelled.current = false;
1626
1711
  setEditing(true);
@@ -1646,6 +1731,7 @@ var InlineEdit = React25.forwardRef(
1646
1731
  };
1647
1732
  const textClasses = cn("text-ui-input-value text-surface-foreground", textClassName);
1648
1733
  const question = variant === "question";
1734
+ const draftMarksClass = cn("px-3 py-1", question ? "border-0 border-b-2 border-transparent" : "border border-transparent", multiline ? "whitespace-pre-wrap break-words" : "whitespace-pre", textClasses);
1649
1735
  const fill = multiline || question;
1650
1736
  return /* @__PURE__ */ jsx26(
1651
1737
  "div",
@@ -1654,39 +1740,68 @@ var InlineEdit = React25.forwardRef(
1654
1740
  className: cn(fill ? "flex w-full min-w-0" : "inline-flex min-w-0 max-w-full", editing && (error || showLimits) && "flex-col gap-1.5", className),
1655
1741
  ...props,
1656
1742
  children: editing ? /* @__PURE__ */ jsxs18(Fragment5, { children: [
1657
- React25.createElement(multiline ? "textarea" : "input", {
1658
- ref: fieldRef,
1659
- value: draft,
1660
- maxLength,
1661
- "aria-label": ariaLabel,
1662
- "aria-invalid": error ? true : void 0,
1663
- "aria-describedby": error || showLimits ? errorId : void 0,
1664
- ...multiline ? { rows: 1 } : { type: "text" },
1665
- className: cn(
1666
- "w-full px-3 py-1 outline-none",
1667
- multiline && "field-sizing-content resize-none",
1668
- question ? cn("rounded-none border-0 border-b-2 bg-transparent", error ? "border-destructive" : "border-accent-muted") : cn("rounded-md", !error && "focus-visible:border-input-accent", fieldSurfaceClasses(error ? "error" : "default")),
1669
- textClasses
1743
+ /* @__PURE__ */ jsxs18("div", { className: cn("relative flex w-full min-w-0", draftMarks.length > 0 && !question && "rounded-md bg-surface"), children: [
1744
+ draftMarks.length > 0 && /* @__PURE__ */ jsx26(
1745
+ DraftMarks,
1746
+ {
1747
+ layer: "behind",
1748
+ singleLine: !multiline,
1749
+ text: draft,
1750
+ marks: draftMarks,
1751
+ scrollLeft: multiline ? 0 : scroll,
1752
+ className: draftMarksClass
1753
+ }
1670
1754
  ),
1671
- onChange: (event) => {
1672
- setDraft(event.target.value);
1673
- if (error) setError(problemWith(event.target.value));
1674
- onDraftChange?.(event.target.value);
1675
- },
1676
- onKeyDown: (event) => {
1677
- if (event.key === "Escape") {
1678
- cancelled.current = true;
1679
- setError(null);
1680
- finish(false, true);
1681
- } else if (event.key === "Enter" && (!multiline || event.metaKey || event.ctrlKey || (question || submitOnEnter) && !event.shiftKey)) {
1682
- event.preventDefault();
1683
- finish(true, true);
1755
+ React25.createElement(multiline ? "textarea" : "input", {
1756
+ ref: fieldRef,
1757
+ value: draft,
1758
+ maxLength,
1759
+ "aria-label": ariaLabel,
1760
+ "aria-invalid": error ? true : void 0,
1761
+ "aria-describedby": error || showLimits ? errorId : void 0,
1762
+ ...multiline ? { rows: 1 } : { type: "text" },
1763
+ className: cn(
1764
+ "relative w-full px-3 py-1 outline-none",
1765
+ multiline && "field-sizing-content resize-none",
1766
+ question ? cn("rounded-none border-0 border-b-2 bg-transparent", error ? "border-destructive" : "border-accent-muted") : cn("rounded-md", !error && "focus-visible:border-input-accent", fieldSurfaceClasses(error ? "error" : "default")),
1767
+ textClasses,
1768
+ // Transparent over the highlight layer, so the highlight and the browser's text selection show together.
1769
+ draftMarks.length > 0 && "bg-transparent"
1770
+ ),
1771
+ onChange: (event) => {
1772
+ setDraft(event.target.value);
1773
+ setDraftMarks(remapMarks(draft, event.target.value, draftMarks));
1774
+ requestAnimationFrame(() => setScroll(fieldRef.current?.scrollLeft ?? 0));
1775
+ if (error) setError(problemWith(event.target.value));
1776
+ onDraftChange?.(event.target.value);
1777
+ },
1778
+ onKeyDown: (event) => {
1779
+ if (event.key === "Escape") {
1780
+ cancelled.current = true;
1781
+ setError(null);
1782
+ finish(false, true);
1783
+ } else if (event.key === "Enter" && (!multiline || event.metaKey || event.ctrlKey || (question || submitOnEnter) && !event.shiftKey)) {
1784
+ event.preventDefault();
1785
+ finish(true, true);
1786
+ }
1787
+ },
1788
+ onScroll: (event) => setScroll(event.currentTarget.scrollLeft),
1789
+ onBlur: () => {
1790
+ if (!cancelled.current) finish(true, false);
1684
1791
  }
1685
- },
1686
- onBlur: () => {
1687
- if (!cancelled.current) finish(true, false);
1688
- }
1689
- }),
1792
+ }),
1793
+ draftMarks.length > 0 && /* @__PURE__ */ jsx26(
1794
+ DraftMarks,
1795
+ {
1796
+ layer: "over",
1797
+ singleLine: !multiline,
1798
+ text: draft,
1799
+ marks: draftMarks,
1800
+ scrollLeft: multiline ? 0 : scroll,
1801
+ className: draftMarksClass
1802
+ }
1803
+ )
1804
+ ] }),
1690
1805
  (error || showLimits) && /* @__PURE__ */ jsxs18(
1691
1806
  "p",
1692
1807
  {
@@ -3218,6 +3333,9 @@ function QuestionOptionEditor({ label, onLabelChange, placeholder, placeholderTo
3218
3333
  const [focused, setFocused] = React45.useState(false);
3219
3334
  const showMarks = !!marks?.length && !focused && draft === null;
3220
3335
  const fieldRef = React45.useRef(null);
3336
+ const [editMarks, setEditMarks] = React45.useState([]);
3337
+ const [scroll, setScroll] = React45.useState(0);
3338
+ const typed = guarded ? draft ?? label : label;
3221
3339
  const skipBlur = React45.useRef(false);
3222
3340
  const problemWith = (text) => minLength !== void 0 && text.trim().length < minLength ? `Enter at least ${minLength} ${minLength === 1 ? "character" : "characters"}.` : null;
3223
3341
  const save = () => {
@@ -3288,6 +3406,7 @@ function QuestionOptionEditor({ label, onLabelChange, placeholder, placeholderTo
3288
3406
  ] }) : locked ? null : grip }),
3289
3407
  /* @__PURE__ */ jsxs33("div", { className: "flex min-w-0 flex-1 flex-col gap-1", children: [
3290
3408
  /* @__PURE__ */ jsxs33("div", { className: "relative min-w-0", children: [
3409
+ focused && editMarks.length > 0 && /* @__PURE__ */ jsx46(DraftMarks, { layer: "behind", singleLine: true, text: typed, marks: editMarks, scrollLeft: scroll, className: "flex items-center whitespace-pre text-body-base" }),
3291
3410
  /* @__PURE__ */ jsx46(
3292
3411
  "input",
3293
3412
  {
@@ -3300,13 +3419,17 @@ function QuestionOptionEditor({ label, onLabelChange, placeholder, placeholderTo
3300
3419
  value: guarded ? draft ?? label : label,
3301
3420
  onFocus: () => {
3302
3421
  setFocused(true);
3422
+ setEditMarks(marks ?? []);
3303
3423
  if (guarded && draft === null) setDraft(label);
3304
3424
  },
3305
3425
  onChange: (event) => {
3426
+ requestAnimationFrame(() => setScroll(fieldRef.current?.scrollLeft ?? 0));
3427
+ setEditMarks(remapMarks(typed, event.target.value, editMarks));
3306
3428
  if (!guarded) return onLabelChange?.(event.target.value);
3307
3429
  setDraft(event.target.value);
3308
3430
  if (error) setError(problemWith(event.target.value));
3309
3431
  },
3432
+ onScroll: (event) => setScroll(event.currentTarget.scrollLeft),
3310
3433
  onKeyDown: (event) => {
3311
3434
  if (!guarded) return;
3312
3435
  if (event.key === "Escape") {
@@ -3332,13 +3455,14 @@ function QuestionOptionEditor({ label, onLabelChange, placeholder, placeholderTo
3332
3455
  save();
3333
3456
  },
3334
3457
  className: cn(
3335
- "w-full min-w-0 bg-transparent text-body-base text-surface-foreground outline-none",
3458
+ "relative w-full min-w-0 bg-transparent text-body-base text-surface-foreground outline-none",
3336
3459
  placeholderTone === "regular" ? "placeholder:text-surface-foreground" : "placeholder:text-muted-foreground",
3337
3460
  // The marked text is drawn over the field, so the field's own letters step aside until it is edited.
3338
3461
  showMarks && "text-transparent"
3339
3462
  )
3340
3463
  }
3341
3464
  ),
3465
+ focused && editMarks.length > 0 && /* @__PURE__ */ jsx46(DraftMarks, { layer: "over", singleLine: true, text: typed, marks: editMarks, scrollLeft: scroll, className: "flex items-center whitespace-pre text-body-base" }),
3342
3466
  showMarks && /* @__PURE__ */ jsx46("div", { "aria-hidden": true, className: "pointer-events-none absolute inset-0 flex items-center overflow-hidden whitespace-pre text-body-base text-surface-foreground [&_[data-mark]]:pointer-events-auto", children: splitMarks(label, marks).map(
3343
3467
  (piece, i) => piece.mark ? /* @__PURE__ */ jsx46(MarkedText, { mark: piece.mark, text: piece.text, onApply: onMarkApply, onDismiss: onMarkDismiss }, `${piece.mark.start}:${piece.text}`) : /* @__PURE__ */ jsx46("span", { children: piece.text }, i)
3344
3468
  ) })