@gridkitjs/dom 0.7.0 → 0.8.1

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
@@ -1,5 +1,5 @@
1
- import { ColumnResizeSession, NavigationModifiers, VisibleRange } from '@gridkitjs/core/renderer';
2
- import { ColumnOrderState, GroupByState, GridFocus, ResolvedToolbarItem, PrintPage } from '@gridkitjs/core';
1
+ import { ColumnResizeSession, NavigationModifiers, WritingDirection, VisibleRange } from '@gridkitjs/core/renderer';
2
+ import { ColumnOrderState, GroupByState, ColumnPin, GridFocus, ResolvedToolbarItem, PrintPage } from '@gridkitjs/core';
3
3
 
4
4
  /** A live region to report changes through. */
5
5
  interface LiveAnnouncer {
@@ -98,7 +98,8 @@ interface AttachedChecklist {
98
98
  */
99
99
  readonly focusItem: (index: number) => void;
100
100
  /**
101
- * Removes every listener and observer. The items keep their `tabindex`.
101
+ * Removes every listener and observer. The list and its items keep their
102
+ * `tabindex`.
102
103
  * After it, `setCount` and `focusItem` do nothing: they neither scroll
103
104
  * nor focus, and a move still waiting for its item is dropped.
104
105
  */
@@ -121,7 +122,10 @@ interface AttachedChecklist {
121
122
  * One item at a time has `tabindex="0"` — the focused one, or the first
122
123
  * mounted while the focused one is scrolled away — and the rest `-1`, kept
123
124
  * so as items mount by an observer of the list's children rather than by
124
- * the renderer. A move to an item outside the window asks `scrollToIndex`
125
+ * the renderer. The list itself gets `tabindex="-1"`: a browser may make an
126
+ * element that scrolls a tab stop of its own, as Firefox does, which would
127
+ * put a second stop before the items' one, on an element the keys do not
128
+ * move from. A move to an item outside the window asks `scrollToIndex`
125
129
  * for it and focuses it once it mounts, unless focus has gone somewhere
126
130
  * else in the meantime.
127
131
  *
@@ -222,6 +226,10 @@ interface ColumnResizeGestureHandlers {
222
226
  * only the primary button of the primary pointer starts one, and not once
223
227
  * `handlers.signal` has aborted.
224
228
  *
229
+ * The edge is the column's inline end, and dragging it outward grows the
230
+ * column: rightward on a left-to-right page, leftward under `dir="rtl"`, as
231
+ * `writingDirection` reads `handle` when the drag starts.
232
+ *
225
233
  * Captures the pointer on `handle`, so the drag keeps reporting wherever the
226
234
  * pointer goes, and prevents the press's default, so it does not select the
227
235
  * header text it started on. No minimum distance, unlike a reorder drag: the
@@ -359,6 +367,12 @@ interface ColumnDropContext {
359
367
  readonly groupBy: GroupByState;
360
368
  /** Column ids whose header may be dragged into the group-by bar at all. */
361
369
  readonly groupByDraggableIds: ReadonlySet<string>;
370
+ /**
371
+ * The edge each column is pinned to. A column stays in its pin group, so
372
+ * a header in another group is no target. Every column counts as unpinned
373
+ * when left out.
374
+ */
375
+ readonly pinOf?: ((columnId: string) => ColumnPin) | undefined;
362
376
  }
363
377
  /**
364
378
  * Where a header or chip dropped at `(clientX, clientY)` would land within
@@ -375,10 +389,27 @@ declare function resolveGroupByDropTarget(clientX: number, clientY: number, move
375
389
  * of it the pointer is over, or else in the group-by bar — or `null` for
376
390
  * nowhere a drop would change anything.
377
391
  *
378
- * A column already grouped never targets the bar: reordering it there is its
379
- * chip's job, not its header's.
392
+ * A header pinned to another edge than the moved column's is never a target:
393
+ * a column is pinned or unpinned on purpose, not by a drag. A column already
394
+ * grouped never targets the bar: reordering it there is its chip's job, not
395
+ * its header's.
380
396
  */
381
- declare function resolveColumnDropTarget(clientX: number, clientY: number, movedId: string, { order, groupBy, groupByDraggableIds }: ColumnDropContext): ColumnDropTarget | null;
397
+ declare function resolveColumnDropTarget(clientX: number, clientY: number, movedId: string, { order, groupBy, groupByDraggableIds, pinOf }: ColumnDropContext): ColumnDropTarget | null;
398
+
399
+ /**
400
+ * Reports `element`'s height now and on every change after, until the
401
+ * returned function disconnects it: what a renderer insets its scroll area
402
+ * by for a header or footer that sticks to its top or bottom edge.
403
+ *
404
+ * The first report is synchronous, like `observeElementWidth`'s, so a
405
+ * renderer calling this before paint sizes its scroll area right from the
406
+ * first frame.
407
+ *
408
+ * Reads the border box from `getBoundingClientRect()`, unrounded: an inset
409
+ * a fraction of a pixel short of a header leaves a sliver of the rows
410
+ * scrolled under it showing.
411
+ */
412
+ declare function observeElementHeight(element: HTMLElement, onHeight: (height: number) => void): () => void;
382
413
 
383
414
  /**
384
415
  * Reports `element`'s inner width now and on every change after, until the
@@ -454,8 +485,12 @@ interface GridFocusController {
454
485
  * them, in which case the caller prevents its default: taken even when the
455
486
  * move lands nowhere, or an arrow at the last row would scroll the page
456
487
  * out from under the grid.
488
+ *
489
+ * `direction` is the way the grid's rows run, which the left and right
490
+ * arrows follow — `writingDirection` of the element the key was pressed
491
+ * in. Defaults to `"ltr"`.
457
492
  */
458
- readonly handleKey: (key: string, modifiers: NavigationModifiers, pageSize: number) => boolean;
493
+ readonly handleKey: (key: string, modifiers: NavigationModifiers, pageSize: number, direction?: WritingDirection) => boolean;
459
494
  /**
460
495
  * Connects the tab stop to the rendered rows, which the browser's focus
461
496
  * then follows it among. Returns the function that disconnects them.
@@ -485,12 +520,13 @@ interface GridFocusController {
485
520
  */
486
521
  declare function findBodyRow(bodyTable: HTMLTableElement | null, rowIndex: number): HTMLTableRowElement | null;
487
522
  /**
488
- * How many rows fit `bodyTable`'s scroll wrapper — its parent, the row area
489
- * alone, not the header, footer and pager around it — which is what a page
490
- * key moves. Reads the first rendered row's height, and falls back to ten
523
+ * How many rows fit the area the rows scroll in, which is what a page key
524
+ * moves: `scrollElement`, or else `bodyTable`'s parent, less the block
525
+ * `scroll-margin` of the rows (what a header or footer sticking to its edges
526
+ * covers). Reads the first rendered row's height, and falls back to ten
491
527
  * rows when nothing can be measured.
492
528
  */
493
- declare function measurePageSize(bodyTable: HTMLTableElement | null): number;
529
+ declare function measurePageSize(bodyTable: HTMLTableElement | null, scrollElement?: HTMLElement | null): number;
494
530
  /**
495
531
  * The grid's single tab stop and the keys that move it, which is what
496
532
  * `role="grid"` obliges: arrow keys travel cell to cell and Tab passes the
@@ -532,7 +568,8 @@ interface InfiniteScroll {
532
568
  /**
533
569
  * Takes the current options. Cheap when nothing changed, so a renderer may
534
570
  * call it after every render; a new sentinel or `rootMargin` restarts the
535
- * observer, and a load finishing or `hasMore` changing re-checks at once.
571
+ * observer, as does a load finishing, so the next load waits for where
572
+ * the new rows left the sentinel; `hasMore` changing re-checks at once.
536
573
  * While a load it asked for has not reported `isLoadingMore`, it measures
537
574
  * where the sentinel sits, to tell whether rows answered it.
538
575
  */
@@ -569,10 +606,11 @@ interface MenuOptions {
569
606
  */
570
607
  readonly onActivate: (id: string) => void;
571
608
  /**
572
- * An enabled submenu item asks for its submenu: Enter, Space or ArrowRight
573
- * on it want its first item focused (`"first"`), a click or the pointer
574
- * resting on it want focus left where it is (`"none"`). It may come for a
575
- * submenu already open — ArrowRight on one the pointer opened — when a
609
+ * An enabled submenu item asks for its submenu: Enter, Space or the arrow
610
+ * pointing to the inline end — ArrowRight, or ArrowLeft in a right-to-left
611
+ * menu — on it want its first item focused (`"first"`), a click or the
612
+ * pointer resting on it want focus left where it is (`"none"`). It may come
613
+ * for a submenu already open — that arrow on one the pointer opened — when a
576
614
  * `"first"` asks for focus to move into it (`focusItem(0)` on its menu).
577
615
  * Opening it closes any other submenu open from this menu.
578
616
  */
@@ -586,9 +624,10 @@ interface MenuOptions {
586
624
  */
587
625
  readonly onCollapseSubmenu?: (() => void) | undefined;
588
626
  /**
589
- * ArrowLeft was pressed in this menu, which is a submenu: close it, and
590
- * focus goes back to the item that opened it. Passed only for a submenu —
591
- * in a menu without it, ArrowLeft does nothing.
627
+ * The arrow pointing to the inline start — ArrowLeft, or ArrowRight in a
628
+ * right-to-left menu — was pressed in this menu, which is a submenu: close
629
+ * it, and focus goes back to the item that opened it. Passed only for a
630
+ * submenu — in a menu without it, that arrow does nothing.
592
631
  */
593
632
  readonly onCloseSubmenu?: (() => void) | undefined;
594
633
  /**
@@ -614,6 +653,11 @@ interface AttachedMenu {
614
653
  * decides; the caller is told through the callbacks and draws the outcome —
615
654
  * runs the item, opens or closes a submenu.
616
655
  *
656
+ * A submenu opens toward the panel's inline end, so the arrow pointing there
657
+ * opens it and the other closes it: ArrowRight opens one in a left-to-right
658
+ * panel, ArrowLeft under `dir="rtl"`, read from `writingDirection(panel)` on
659
+ * each key.
660
+ *
617
661
  * Items are the panel's descendants with a menu item role (`menuitem`,
618
662
  * `menuitemcheckbox`, `menuitemradio`) and a `data-gridkit-menu-item` id,
619
663
  * read afresh on every key so items drawn since attaching count. A submenu
@@ -662,6 +706,17 @@ interface OverlayPlacementOptions {
662
706
  * still kept on screen. Defaults to `0`.
663
707
  */
664
708
  readonly offset?: number | undefined;
709
+ /**
710
+ * The writing direction the placement is read in. `"rtl"` mirrors it, as
711
+ * a right-to-left page is mirrored: `left` and `right` swap, `start` and
712
+ * `end` beside `top` or `bottom` are the anchor's right and left edges, an
713
+ * `offset` along those sides runs leftward, and an overlay wider than the
714
+ * viewport keeps its right edge in view. The placement it ends up with is
715
+ * reported in the same mirrored terms, so `"bottom-start"` lines up with
716
+ * the anchor's inline start and `"right-start"` opens a submenu toward
717
+ * the inline end in either direction. Defaults to `"ltr"`.
718
+ */
719
+ readonly direction?: WritingDirection | undefined;
665
720
  }
666
721
  /** Where an overlay goes: its top-left corner, and the placement it ended up with. */
667
722
  interface OverlayPosition {
@@ -680,6 +735,10 @@ interface OverlayPosition {
680
735
  * A point — a context menu opened where the pointer is — is an anchor with
681
736
  * no width or height, which is what makes `start`/`end` mean "after" and
682
737
  * "before" it.
738
+ *
739
+ * In `"rtl"` the anchor is mirrored across the viewport, placed as in
740
+ * left-to-right, and the result mirrored back, so every rule above holds
741
+ * with left and right exchanged.
683
742
  */
684
743
  declare function placeOverlay(anchor: OverlayRect, panel: OverlaySize, viewport: OverlaySize, options?: OverlayPlacementOptions): OverlayPosition;
685
744
 
@@ -764,6 +823,13 @@ interface OverlayOptions extends OverlayPlacementOptions {
764
823
  * where the anchor was, once it has left the page. Focus moves
765
824
  * where `initialFocus` says.
766
825
  *
826
+ * The placement is read in the panel's own writing direction unless
827
+ * `direction` names one, so a panel laid out right to left — given
828
+ * `dir="rtl"`, or rendered into a right-to-left page — opens toward the
829
+ * inline end of what it is drawn in. A renderer portalling the panel out of
830
+ * a right-to-left grid in a left-to-right page sets `dir` on it from the
831
+ * grid, which mirrors its content and its placement together.
832
+ *
767
833
  * Nothing that happens on or in the anchor dismisses the overlay, so a button
768
834
  * that toggles it can close it with its own click rather than having the
769
835
  * overlay close and the click reopen it.
@@ -860,6 +926,10 @@ declare function createRowVirtualizer(scrollElement: HTMLElement, options: RowVi
860
926
  * control, wrapping round the ends, and Home and End to the first and last.
861
927
  * Returns the function that stops it.
862
928
  *
929
+ * The arrows point the way focus moves: under `dir="rtl"`, where the
930
+ * controls run from the right, ArrowLeft moves to the next one. The
931
+ * direction is read from `writingDirection(toolbar)` on each key.
932
+ *
863
933
  * The toolbar's entries are its descendants with a `data-gridkit-toolbar-item`,
864
934
  * and each entry's control is the entry itself when it can take focus, else
865
935
  * the first element in it that can; an entry with none — a separator, a
@@ -928,6 +998,15 @@ interface ToolbarOverflow {
928
998
  */
929
999
  declare function observeToolbarOverflow(toolbar: HTMLElement, options: ToolbarOverflowOptions): ToolbarOverflow;
930
1000
 
1001
+ /**
1002
+ * The way `element`'s lines run, from its computed `direction`: what it
1003
+ * inherits from a `dir` attribute on it or an ancestor, or from the CSS
1004
+ * `direction` property. Read afresh on every call, never kept, so a page
1005
+ * whose direction changes is followed from the next key press, gesture or
1006
+ * overlay on.
1007
+ */
1008
+ declare function writingDirection(element: Element): WritingDirection;
1009
+
931
1010
  /** What `printDocument` prints: a document's title, its body's markup and its stylesheet. */
932
1011
  interface PrintDocumentOptions {
933
1012
  /** The document's title, as text — what the browser puts in a page's header and names a saved PDF. */
@@ -969,4 +1048,4 @@ declare function printDocument(options: PrintDocumentOptions): Promise<void>;
969
1048
  */
970
1049
  declare function printStyles(page: PrintPage): string;
971
1050
 
972
- export { type AttachedChecklist, type AttachedMenu, type BodyCellTarget, type BodyRowTarget, type ChecklistOptions, type ClipboardContent, type ColumnDropContext, type ColumnDropTarget, type ColumnResizeGestureHandlers, type ContextMenuRequest, type ContextMenuSource, type ContextMenuTriggerOptions, type DragReorderHandlers, type DragReorderState, type FocusVirtualRange, type GridFocusController, type GridFocusOptions, type GridFocusRows, type GroupByDropTarget, type GroupRowTarget, type InfiniteScroll, type InfiniteScrollOptions, type LiveAnnouncer, type MenuCloseReason, type MenuOptions, type MenuSubmenuFocus, NO_DRAG, type OverlayAlign, type OverlayAnchor, type OverlayDismissReason, type OverlayInitialFocus, type OverlayOptions, type OverlayPlacement, type OverlayPlacementOptions, type OverlayPosition, type OverlayRect, type OverlaySide, type OverlaySize, type PointerPress, type PrintDocumentOptions, type RowVirtualizer, type RowVirtualizerOptions, type ToolbarOverflow, type ToolbarOverflowItem, type ToolbarOverflowOptions, attachChecklist, attachContextMenuTrigger, attachInfiniteScroll, attachMenu, attachOverlay, attachToolbar, bodyCellFrom, bodyRowFrom, createGridFocus, createLiveAnnouncer, createRowVirtualizer, downloadBlob, findBodyRow, groupRowFrom, isNativeCopy, measureColumnContent, measurePageSize, observeElementWidth, observeToolbarOverflow, placeOverlay, printDocument, printStyles, resolveColumnDropTarget, resolveGroupByDropTarget, startColumnResizeGesture, startDragReorder, writeClipboard };
1051
+ export { type AttachedChecklist, type AttachedMenu, type BodyCellTarget, type BodyRowTarget, type ChecklistOptions, type ClipboardContent, type ColumnDropContext, type ColumnDropTarget, type ColumnResizeGestureHandlers, type ContextMenuRequest, type ContextMenuSource, type ContextMenuTriggerOptions, type DragReorderHandlers, type DragReorderState, type FocusVirtualRange, type GridFocusController, type GridFocusOptions, type GridFocusRows, type GroupByDropTarget, type GroupRowTarget, type InfiniteScroll, type InfiniteScrollOptions, type LiveAnnouncer, type MenuCloseReason, type MenuOptions, type MenuSubmenuFocus, NO_DRAG, type OverlayAlign, type OverlayAnchor, type OverlayDismissReason, type OverlayInitialFocus, type OverlayOptions, type OverlayPlacement, type OverlayPlacementOptions, type OverlayPosition, type OverlayRect, type OverlaySide, type OverlaySize, type PointerPress, type PrintDocumentOptions, type RowVirtualizer, type RowVirtualizerOptions, type ToolbarOverflow, type ToolbarOverflowItem, type ToolbarOverflowOptions, attachChecklist, attachContextMenuTrigger, attachInfiniteScroll, attachMenu, attachOverlay, attachToolbar, bodyCellFrom, bodyRowFrom, createGridFocus, createLiveAnnouncer, createRowVirtualizer, downloadBlob, findBodyRow, groupRowFrom, isNativeCopy, measureColumnContent, measurePageSize, observeElementHeight, observeElementWidth, observeToolbarOverflow, placeOverlay, printDocument, printStyles, resolveColumnDropTarget, resolveGroupByDropTarget, startColumnResizeGesture, startDragReorder, writeClipboard, writingDirection };
package/dist/index.js CHANGED
@@ -197,6 +197,7 @@ function attachChecklist(list, options) {
197
197
  subtree: true,
198
198
  attributeFilter: [INDEX]
199
199
  });
200
+ list.tabIndex = -1;
200
201
  setTabStop();
201
202
  list.addEventListener("keydown", onKeyDown2);
202
203
  list.addEventListener("focusin", onFocusIn);
@@ -342,10 +343,16 @@ import {
342
343
  function mayStartGesture(press, signal) {
343
344
  return press.button === 0 && press.isPrimary && signal?.aborted !== true;
344
345
  }
346
+ var HELD = { kind: "held" };
345
347
  function startPointerGesture(element, pointerId, handlers, signal) {
346
348
  const { ownerDocument } = element;
347
349
  element.setPointerCapture(pointerId);
350
+ let capture = HELD;
348
351
  function stop() {
352
+ if (capture.kind === "lost") {
353
+ window.clearTimeout(capture.timer);
354
+ capture = HELD;
355
+ }
349
356
  ownerDocument.removeEventListener("pointermove", move, true);
350
357
  ownerDocument.removeEventListener("pointerup", end, true);
351
358
  ownerDocument.removeEventListener("pointercancel", cancel, true);
@@ -373,9 +380,18 @@ function startPointerGesture(element, pointerId, handlers, signal) {
373
380
  }
374
381
  }
375
382
  function recapture(event) {
376
- if (event.pointerId === pointerId && element.isConnected) {
377
- element.setPointerCapture(pointerId);
383
+ if (event.pointerId !== pointerId || capture.kind === "lost") {
384
+ return;
378
385
  }
386
+ capture = {
387
+ kind: "lost",
388
+ timer: window.setTimeout(() => {
389
+ capture = HELD;
390
+ if (element.isConnected) {
391
+ element.setPointerCapture(pointerId);
392
+ }
393
+ }, 0)
394
+ };
379
395
  }
380
396
  function abandon() {
381
397
  stop();
@@ -394,12 +410,18 @@ function startPointerGesture(element, pointerId, handlers, signal) {
394
410
  signal?.addEventListener("abort", abandon);
395
411
  }
396
412
 
413
+ // src/writingDirection.ts
414
+ function writingDirection(element) {
415
+ return getComputedStyle(element).direction === "rtl" ? "rtl" : "ltr";
416
+ }
417
+
397
418
  // src/columnResize.ts
398
419
  function startColumnResizeGesture(handle, press, session, handlers) {
399
420
  if (!mayStartGesture(press, handlers.signal)) {
400
421
  return false;
401
422
  }
402
423
  press.preventDefault();
424
+ const direction = writingDirection(handle);
403
425
  let width = session.startWidth;
404
426
  let resized = false;
405
427
  startPointerGesture(
@@ -407,7 +429,7 @@ function startColumnResizeGesture(handle, press, session, handlers) {
407
429
  press.pointerId,
408
430
  {
409
431
  onMove(event) {
410
- width = applyColumnResize(session, event.clientX);
432
+ width = applyColumnResize(session, event.clientX, direction);
411
433
  resized ||= width !== session.startWidth;
412
434
  handlers.onMove(width);
413
435
  },
@@ -585,16 +607,20 @@ function startDragReorder(id, grabElement, press, handlers) {
585
607
 
586
608
  // src/dropTargets.ts
587
609
  import {
588
- movesColumn,
610
+ moveColumnInPinGroup,
589
611
  movesGroupBy,
590
612
  resolveDropBefore
591
613
  } from "@gridkitjs/core/renderer";
614
+ function unpinned() {
615
+ return "none";
616
+ }
592
617
  function elementAt(x, y, selector) {
593
618
  return document.elementFromPoint(x, y)?.closest(selector) ?? null;
594
619
  }
595
620
  function sideOf(element, x) {
596
621
  const rect = element.getBoundingClientRect();
597
- return x < rect.left + rect.width / 2 ? "before" : "after";
622
+ const leftHalf = x < rect.left + rect.width / 2;
623
+ return leftHalf === (writingDirection(element) === "ltr") ? "before" : "after";
598
624
  }
599
625
  function resolveGroupByDropTarget(clientX, clientY, movedId, groupBy) {
600
626
  const overBar = elementAt(clientX, clientY, "[data-gridkit-group-by-bar]");
@@ -610,17 +636,32 @@ function resolveGroupByDropTarget(clientX, clientY, movedId, groupBy) {
610
636
  ) : null;
611
637
  return movesGroupBy(groupBy, movedId, beforeColumnId) ? { kind: "group-by", beforeColumnId } : null;
612
638
  }
613
- function resolveColumnDropTarget(clientX, clientY, movedId, { order, groupBy, groupByDraggableIds }) {
639
+ function resolveColumnDropTarget(clientX, clientY, movedId, { order, groupBy, groupByDraggableIds, pinOf = unpinned }) {
614
640
  const over = elementAt(clientX, clientY, "th[data-gridkit-column]");
615
641
  const overId = over?.getAttribute("data-gridkit-column");
616
642
  const beforeId = over && overId ? resolveDropBefore(order, overId, sideOf(over, clientX)) : void 0;
617
- if (beforeId !== void 0) {
618
- return movesColumn(order, movedId, beforeId) ? { kind: "column-order", beforeId } : null;
643
+ if (overId && beforeId !== void 0) {
644
+ if (pinOf(overId) !== pinOf(movedId)) {
645
+ return null;
646
+ }
647
+ return moveColumnInPinGroup(order, pinOf, movedId, beforeId) !== order ? { kind: "column-order", beforeId } : null;
619
648
  }
620
649
  const alreadyGrouped = groupBy.some((entry) => entry.columnId === movedId);
621
650
  return groupByDraggableIds.has(movedId) && !alreadyGrouped ? resolveGroupByDropTarget(clientX, clientY, movedId, groupBy) : null;
622
651
  }
623
652
 
653
+ // src/elementHeight.ts
654
+ function observeElementHeight(element, onHeight) {
655
+ onHeight(element.getBoundingClientRect().height);
656
+ const observer = new ResizeObserver(() => {
657
+ onHeight(element.getBoundingClientRect().height);
658
+ });
659
+ observer.observe(element);
660
+ return () => {
661
+ observer.disconnect();
662
+ };
663
+ }
664
+
624
665
  // src/elementWidth.ts
625
666
  function observeElementWidth(element, onWidth) {
626
667
  onWidth(element.clientWidth);
@@ -657,13 +698,19 @@ function findBodyRow(bodyTable, rowIndex) {
657
698
  `:scope > tr[data-gridkit-row-index="${String(rowIndex)}"]`
658
699
  ) ?? null;
659
700
  }
660
- function measurePageSize(bodyTable) {
701
+ function pixelsOf(value) {
702
+ const pixels2 = Number.parseFloat(value);
703
+ return Number.isNaN(pixels2) ? 0 : pixels2;
704
+ }
705
+ function measurePageSize(bodyTable, scrollElement) {
661
706
  const firstRow = bodyTable?.tBodies[0]?.rows[0];
662
- const viewport = bodyTable?.parentElement;
707
+ const viewport = scrollElement ?? bodyTable?.parentElement;
663
708
  if (firstRow === void 0 || viewport === null || viewport === void 0 || firstRow.offsetHeight === 0) {
664
709
  return FALLBACK_PAGE;
665
710
  }
666
- return Math.max(1, Math.floor(viewport.clientHeight / firstRow.offsetHeight));
711
+ const style = getComputedStyle(firstRow);
712
+ const height = viewport.clientHeight - pixelsOf(style.scrollMarginTop) - pixelsOf(style.scrollMarginBottom);
713
+ return Math.max(1, Math.floor(height / firstRow.offsetHeight));
667
714
  }
668
715
  function createGridFocus(initialOptions) {
669
716
  let options = initialOptions;
@@ -730,7 +777,7 @@ function createGridFocus(initialOptions) {
730
777
  options = next;
731
778
  },
732
779
  focusCell,
733
- handleKey(key, modifiers, pageSize) {
780
+ handleKey(key, modifiers, pageSize, direction) {
734
781
  const next = nextFocusForKey(
735
782
  key,
736
783
  modifiers,
@@ -738,7 +785,8 @@ function createGridFocus(initialOptions) {
738
785
  options.rowCount,
739
786
  options.columnCount,
740
787
  pageSize,
741
- options.isSkippableRow
788
+ options.isSkippableRow,
789
+ direction
742
790
  );
743
791
  if (next === null) {
744
792
  return false;
@@ -766,7 +814,11 @@ function createGridFocus(initialOptions) {
766
814
  );
767
815
  if (cell !== null) {
768
816
  pendingMove = null;
817
+ const moving = cell.ownerDocument.activeElement !== cell;
769
818
  cell.focus();
819
+ if (moving) {
820
+ cell.scrollIntoView({ block: "nearest", inline: "nearest" });
821
+ }
770
822
  return;
771
823
  }
772
824
  const range = options.virtualRange;
@@ -857,10 +909,11 @@ function attachInfiniteScroll(root, options) {
857
909
  return;
858
910
  }
859
911
  const hasMoreChanged = next.hasMore !== latest.hasMore;
912
+ const loadFinished = state.kind === "loading" && !(next.isLoadingMore ?? false);
860
913
  latest = next;
861
914
  state = afterUpdate(state, next.isLoadingMore ?? false, hasMoreChanged);
862
915
  observe();
863
- if (requestAnswered()) {
916
+ if (loadFinished || requestAnswered()) {
864
917
  state = IDLE;
865
918
  disconnect();
866
919
  observe();
@@ -876,6 +929,7 @@ function attachInfiniteScroll(root, options) {
876
929
 
877
930
  // src/menu/attachMenu.ts
878
931
  import {
932
+ inlineStep,
879
933
  nextMenuIndex,
880
934
  typeaheadIndex
881
935
  } from "@gridkitjs/core/renderer";
@@ -998,6 +1052,7 @@ function attachMenu(panel, options) {
998
1052
  const item = itemOf(event.target);
999
1053
  const from = item === null ? -1 : all.indexOf(item);
1000
1054
  const move = MOVES[event.key];
1055
+ const step = inlineStep(event.key, writingDirection(panel));
1001
1056
  if (move !== void 0) {
1002
1057
  const next = all[nextMenuIndex(
1003
1058
  all.map(() => AS_ENTRY),
@@ -1011,11 +1066,11 @@ function attachMenu(panel, options) {
1011
1066
  if (item !== null) {
1012
1067
  choose(item, "first");
1013
1068
  }
1014
- } else if (event.key === "ArrowRight") {
1069
+ } else if (step === 1) {
1015
1070
  if (item !== null && opensSubmenu(item)) {
1016
1071
  openSubmenu(item, "first");
1017
1072
  }
1018
- } else if (event.key === "ArrowLeft") {
1073
+ } else if (step === -1) {
1019
1074
  options.onCloseSubmenu?.();
1020
1075
  } else if (event.key === "Tab") {
1021
1076
  options.onClose("tab");
@@ -1172,6 +1227,21 @@ function clampIntoView(value, limit) {
1172
1227
  return Math.max(0, Math.min(value, limit));
1173
1228
  }
1174
1229
  function placeOverlay(anchor, panel, viewport, options = {}) {
1230
+ if (options.direction !== "rtl") {
1231
+ return placeLeftToRight(anchor, panel, viewport, options);
1232
+ }
1233
+ const mirrored = placeLeftToRight(
1234
+ { ...anchor, left: viewport.width - anchor.left - anchor.width },
1235
+ panel,
1236
+ viewport,
1237
+ options
1238
+ );
1239
+ return {
1240
+ ...mirrored,
1241
+ left: viewport.width - mirrored.left - panel.width
1242
+ };
1243
+ }
1244
+ function placeLeftToRight(anchor, panel, viewport, options) {
1175
1245
  const [preferredSide, preferredAlign] = (options.placement ?? "bottom-start").split("-");
1176
1246
  const gap = options.gap ?? 0;
1177
1247
  const offset = options.offset ?? 0;
@@ -1321,7 +1391,7 @@ function attachOverlay(anchor, panel, options) {
1321
1391
  width: document.documentElement.clientWidth,
1322
1392
  height: document.documentElement.clientHeight
1323
1393
  },
1324
- options
1394
+ { ...options, direction: options.direction ?? writingDirection(panel) }
1325
1395
  );
1326
1396
  panel.style.top = `${String(position.top)}px`;
1327
1397
  panel.style.left = `${String(position.left)}px`;
@@ -1520,7 +1590,10 @@ function createRowVirtualizer(scrollElement, options) {
1520
1590
  }
1521
1591
 
1522
1592
  // src/toolbar/attachToolbar.ts
1523
- import { nextMenuIndex as nextMenuIndex2 } from "@gridkitjs/core/renderer";
1593
+ import {
1594
+ inlineStep as inlineStep2,
1595
+ nextMenuIndex as nextMenuIndex2
1596
+ } from "@gridkitjs/core/renderer";
1524
1597
  var ENTRY = "[data-gridkit-toolbar-item]";
1525
1598
  var FOCUSABLE2 = [
1526
1599
  "button:not(:disabled)",
@@ -1530,12 +1603,17 @@ var FOCUSABLE2 = [
1530
1603
  "a[href]",
1531
1604
  "[tabindex]"
1532
1605
  ].join(", ");
1533
- var MOVES2 = {
1534
- ArrowRight: "next",
1535
- ArrowLeft: "previous",
1606
+ var JUMPS = {
1536
1607
  Home: "first",
1537
1608
  End: "last"
1538
1609
  };
1610
+ function moveFor(key, toolbar) {
1611
+ const step = inlineStep2(key, writingDirection(toolbar));
1612
+ if (step === null) {
1613
+ return JUMPS[key];
1614
+ }
1615
+ return step === 1 ? "next" : "previous";
1616
+ }
1539
1617
  var AS_ENTRY2 = { type: "item" };
1540
1618
  var CARETLESS_INPUTS = /* @__PURE__ */ new Set([
1541
1619
  "button",
@@ -1588,7 +1666,7 @@ function attachToolbar(toolbar) {
1588
1666
  }
1589
1667
  }
1590
1668
  function onKeyDown2(event) {
1591
- const move = MOVES2[event.key];
1669
+ const move = moveFor(event.key, toolbar);
1592
1670
  if (move === void 0 || event.isComposing || event.altKey || event.ctrlKey || event.metaKey || event.shiftKey || belongsToField(event.target, move)) {
1593
1671
  return;
1594
1672
  }
@@ -1892,6 +1970,7 @@ export {
1892
1970
  isNativeCopy,
1893
1971
  measureColumnContent,
1894
1972
  measurePageSize,
1973
+ observeElementHeight,
1895
1974
  observeElementWidth,
1896
1975
  observeToolbarOverflow,
1897
1976
  placeOverlay,
@@ -1901,6 +1980,7 @@ export {
1901
1980
  resolveGroupByDropTarget,
1902
1981
  startColumnResizeGesture,
1903
1982
  startDragReorder,
1904
- writeClipboard
1983
+ writeClipboard,
1984
+ writingDirection
1905
1985
  };
1906
1986
  //# sourceMappingURL=index.js.map