@seanyeh77/ui 0.34.0 → 0.35.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.ts CHANGED
@@ -584,9 +584,14 @@ interface CodeViewProps<L extends CodeLink = CodeLink> {
584
584
  /** The reader clicked a line, or moved to one with ↑ ↓ (also Home, End, PageUp, PageDown, [ ]): `how` says which.
585
585
  * Dragging to select text is not a move and does not call it. Given this, ↑ ↓ move by line even without links. */
586
586
  onLine?: (line: number, how: 'click' | 'key') => void;
587
- /** Marks one line for a moment, apart from `target` (both can show), e.g. while the pointer rests on an item that
588
- * points at it. Scrolled into view when needed; focus and selected text stay. null or undefined: none. */
589
- peek?: number | null;
587
+ /** Marks a line, or a range of lines (`{ from, to }`, both included), for a moment, apart from `target` (both can
588
+ * show), e.g. while the pointer rests on an item that points at it. Scrolled with the least movement so the whole
589
+ * range is in view; a range taller than the view puts its first line at the top. Focus and selected text stay.
590
+ * null or undefined: none. */
591
+ peek?: number | {
592
+ from: number;
593
+ to: number;
594
+ } | null;
590
595
  className?: string;
591
596
  /** focusTarget() moves keyboard and screen-reader focus to the target line. */
592
597
  ref?: React.Ref<CodeViewHandle>;
package/dist/index.js CHANGED
@@ -2728,14 +2728,38 @@ function selectedCode(pre) {
2728
2728
  const rows = [...frag.querySelectorAll("[role=\"listitem\"]")];
2729
2729
  return rows.length ? rows.map((x) => x.textContent ?? "").join("\n") : frag.textContent ?? "";
2730
2730
  }
2731
- /** Scrolls el into view inside box (or the page) only when it is not already fully visible. */
2732
- function inView(el, box) {
2733
- const r = el.getBoundingClientRect();
2734
- const b = box && box.scrollHeight > box.clientHeight ? box.getBoundingClientRect() : {
2731
+ /** The nearest box that scrolls el's content: el itself, a parent with overflow auto or scroll, or the page (null). */
2732
+ function scroller(el) {
2733
+ for (let n = el; n && n !== document.body && n !== document.documentElement; n = n.parentElement) {
2734
+ const o = getComputedStyle(n).overflowY;
2735
+ if ((o === "auto" || o === "scroll" || o === "overlay") && n.scrollHeight > n.clientHeight) return n;
2736
+ }
2737
+ return null;
2738
+ }
2739
+ /** Scrolls the lines first…last into view with the least movement; when they are taller than the view, the first
2740
+ * line goes to the top. Nothing moves when they are already in view. */
2741
+ function showRange(first, last, pre) {
2742
+ const box = scroller(pre);
2743
+ const view = box ? (() => {
2744
+ const top = box.getBoundingClientRect().top + box.clientTop;
2745
+ return {
2746
+ top,
2747
+ bottom: top + box.clientHeight
2748
+ };
2749
+ })() : {
2735
2750
  top: 0,
2736
2751
  bottom: window.innerHeight
2737
2752
  };
2738
- if (r.top < b.top || r.bottom > b.bottom) el.scrollIntoView({ block: "nearest" });
2753
+ const top = first.getBoundingClientRect().top;
2754
+ const bottom = last.getBoundingClientRect().bottom;
2755
+ let by = 0;
2756
+ if (bottom - top > view.bottom - view.top) by = top - view.top;
2757
+ else if (top < view.top) by = top - view.top;
2758
+ else if (bottom > view.bottom) by = bottom - view.bottom;
2759
+ by = Math.round(by);
2760
+ if (!by) return;
2761
+ if (box) box.scrollTop += by;
2762
+ else window.scrollBy(0, by);
2739
2763
  }
2740
2764
  /** Each line's words from `terms`: only in comments and strings, never where a name link or a path is. With 'first',
2741
2765
  * each word once per file, at its first place. */
@@ -2901,11 +2925,17 @@ function CodeView({ code, start = 1, target, context = 2, file, fileHref, onFile
2901
2925
  const el = b?.querySelector(".is-target");
2902
2926
  if (b && el && b.scrollHeight > b.clientHeight && !selectedIn(b)) b.scrollTop += el.getBoundingClientRect().top - b.getBoundingClientRect().top - b.clientHeight / 2 + el.offsetHeight / 2;
2903
2927
  }, [ta, code]);
2928
+ const [pFrom, pTo] = peek == null ? [NaN, NaN] : typeof peek === "number" ? [peek, peek] : [Math.min(peek.from, peek.to), Math.max(peek.from, peek.to)];
2904
2929
  React.useEffect(() => {
2905
- if (peek == null) return;
2906
- const el = body.current?.querySelector(`[data-n="${peek}"]`);
2907
- if (el) inView(el, body.current);
2908
- }, [peek]);
2930
+ if (Number.isNaN(pFrom)) return;
2931
+ const pre = body.current;
2932
+ if (!pre) return;
2933
+ const rows = [...pre.querySelectorAll("[data-n]")].filter((el) => {
2934
+ const n = Number(el.dataset.n);
2935
+ return n >= pFrom && n <= pTo;
2936
+ });
2937
+ if (rows.length) showRange(rows[0], rows[rows.length - 1], pre);
2938
+ }, [pFrom, pTo]);
2909
2939
  const digits = String(start + lines.length - 1).length;
2910
2940
  const prose = (cls, text, inLink) => !inLink && (refHref || onRef) && (cls === "is-comment" || cls === "is-string") ? withPaths(text, refHref, onRef, nav ? -1 : void 0) : breakable(text);
2911
2941
  const piece = (cls, text, key, inLink, at = 0, words) => {
@@ -2992,7 +3022,7 @@ function CodeView({ code, start = 1, target, context = 2, file, fileHref, onFile
2992
3022
  const on = ta != null && tb != null && n >= ta && n <= tb;
2993
3023
  rows.push(/* @__PURE__ */ jsxs("div", {
2994
3024
  role: "listitem",
2995
- className: cx("ui-cv-line", on && "is-target", peek === n && "is-peek"),
3025
+ className: cx("ui-cv-line", on && "is-target", n >= pFrom && n <= pTo && "is-peek"),
2996
3026
  "data-n": n,
2997
3027
  "aria-current": on && n === ta ? "location" : void 0,
2998
3028
  tabIndex: on && n === ta ? -1 : void 0,