Skip to content

Commit 96098b6

Browse files
committed
refactor: remove redundant comments and improve type safety in sandbox and menu components
1 parent 409c198 commit 96098b6

6 files changed

Lines changed: 1 addition & 58 deletions

File tree

docs/src/routes/(docs)/sandbox/+page.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
let selectedPriority = $state("p1");
1212
let selectedLabel = $state("strategic-initiative");
1313
let selectedLead = $state("hunter");
14-
const debugRootProps = $derived.by(() => ({ debugMode }) as any);
14+
const debugRootProps = $derived.by(() => ({ debugMode }) satisfies { debugMode: boolean });
1515
1616
const statusItems = [
1717
{ value: "icebox", label: "Icebox" },

packages/bits-ui/src/lib/bits/menu/menu.svelte.ts

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,6 @@ class MenuSubmenuIntent {
297297

298298
const onContentLeave = (e: PointerEvent) => {
299299
if (!isMouseEvent(e)) return;
300-
// don't start return-tracking when leaving into a child sub-content
301300
if (isElement(e.relatedTarget)) {
302301
const selector = this.#opts.subContentSelector();
303302
const matchedSubContent = e.relatedTarget.closest(selector);
@@ -319,13 +318,11 @@ class MenuSubmenuIntent {
319318

320319
const onTriggerEnter = (e: PointerEvent) => {
321320
if (!isMouseEvent(e)) return;
322-
// arrived at trigger, transit complete
323321
this.#disengage();
324322
};
325323

326324
const onContentEnter = (e: PointerEvent) => {
327325
if (!isMouseEvent(e)) return;
328-
// arrived at content, transit complete
329326
this.#disengage();
330327
};
331328

@@ -405,10 +402,6 @@ class MenuSubmenuIntent {
405402
return isPointInPolygon(pt, corridor) || isPointInPolygon(pt, intent);
406403
}
407404

408-
/**
409-
* When the pointer hovers over the trigger or content, we start previewing the transit.
410-
* This is used to show the debug overlay while hovering the trigger or content.
411-
*/
412405
#preview(e: PointerEvent, target: IntentTarget) {
413406
const pt = { x: e.clientX, y: e.clientY };
414407
const geo = this.#computePolygons(pt, target);
@@ -421,17 +414,13 @@ class MenuSubmenuIntent {
421414
this.#syncDebug();
422415
}
423416

424-
/**
425-
* When the pointer leaves the trigger or content, we start actively tracking the transit.
426-
*/
427417
#engage(e: PointerEvent, target: IntentTarget) {
428418
if (!this.#opts.enabled()) return;
429419

430420
const triggerNode = this.#opts.triggerNode();
431421
const contentNode = this.#opts.contentNode();
432422
if (!triggerNode || !contentNode) return;
433423

434-
// pointer moved directly into the destination — no transit needed
435424
const related = e.relatedTarget;
436425
if (isElement(related)) {
437426
if (target === "content" && contentNode.contains(related)) return;
@@ -440,16 +429,13 @@ class MenuSubmenuIntent {
440429

441430
const pt = { x: e.clientX, y: e.clientY };
442431

443-
// immediately check if pointer is already outside the safe zone
444-
// (e.g. moving downward to a sibling instead of toward the content)
445432
const geo = this.#computePolygons(pt, target);
446433
if (!geo) return;
447434

448435
if (
449436
!isInsideRect(pt, geo.targetRect) &&
450437
!this.#isInSafeZone(pt, geo.corridor, geo.intent)
451438
) {
452-
// pointer left toward somewhere outside the safe zone — don't engage
453439
this.#clearVisuals();
454440
return;
455441
}
@@ -468,9 +454,6 @@ class MenuSubmenuIntent {
468454

469455
#disengageTimer: ReturnType<typeof setTimeout> | null = null;
470456

471-
/**
472-
* When the pointer arrives at the destination, we stop tracking the transit.
473-
*/
474457
#disengage() {
475458
if (!this.#active) return;
476459
const wasReturning = this.#target === "trigger";
@@ -480,8 +463,6 @@ class MenuSubmenuIntent {
480463
this.#clearVisuals();
481464

482465
if (wasReturning) {
483-
// keep isPointerInTransit true briefly so the pointer can settle on the
484-
// trigger without accidentally opening a neighboring sub-trigger
485466
this.#clearDisengageTimer();
486467
this.#disengageTimer = setTimeout(() => {
487468
this.#disengageTimer = null;
@@ -498,9 +479,6 @@ class MenuSubmenuIntent {
498479
this.#disengageTimer = null;
499480
}
500481

501-
/**
502-
* When the pointer leaves the safe zone, we close the submenu.
503-
*/
504482
#intentExit() {
505483
this.#detachDocMove();
506484
this.#clearFallback();
@@ -511,9 +489,6 @@ class MenuSubmenuIntent {
511489
this.#opts.onIntentExit();
512490
}
513491

514-
/**
515-
* When the node changes or the component is destroyed, we reset the state.
516-
*/
517492
#reset() {
518493
this.#detachDocMove();
519494
this.#clearFallback();
@@ -529,9 +504,6 @@ class MenuSubmenuIntent {
529504
this.#syncDebug();
530505
}
531506

532-
/**
533-
* When the pointer moves over the document, we track the transit.
534-
*/
535507
#isPointerInDescendantSubContent(pt: Point): boolean {
536508
const contentNode = this.#opts.contentNode();
537509
if (!contentNode) return false;
@@ -541,9 +513,6 @@ class MenuSubmenuIntent {
541513
const selector = this.#opts.subContentSelector();
542514
const subContent = el.closest(selector);
543515
if (!subContent || subContent === contentNode) return false;
544-
// verify this sub-content was triggered from within our content by checking
545-
// if a sub-trigger with aria-controls matching the sub-content's id exists
546-
// inside our content node
547516
if (subContent.id) return !!contentNode.querySelector(`[aria-controls="${subContent.id}"]`);
548517
return false;
549518
}
@@ -563,7 +532,6 @@ class MenuSubmenuIntent {
563532
const pt = { x: e.clientX, y: e.clientY };
564533
this.#pointerPoint = pt;
565534

566-
// check if pointer arrived at the destination element
567535
const triggerRect = triggerNode.getBoundingClientRect();
568536
const contentRect = contentNode.getBoundingClientRect();
569537
if (this.#target === "content" && isInsideRect(pt, contentRect)) {
@@ -575,13 +543,11 @@ class MenuSubmenuIntent {
575543
return;
576544
}
577545

578-
// pointer is inside a child sub-content portal, treat as safe, don't close
579546
if (this.#isPointerInDescendantSubContent(pt)) {
580547
this.#startFallback();
581548
return;
582549
}
583550

584-
// recompute safe zone from the locked apex
585551
const geo = this.#computePolygons(pt, this.#target);
586552
if (!geo) {
587553
this.#intentExit();
@@ -615,9 +581,6 @@ class MenuSubmenuIntent {
615581
this.#cleanupDocMove?.();
616582
}
617583

618-
/**
619-
* When the pointer goes completely still mid-transit, we close the submenu.
620-
*/
621584
#startFallback() {
622585
this.#clearFallback();
623586
this.#fallbackTimer = setTimeout(() => {
@@ -770,8 +733,6 @@ function getIntentPolygon(
770733
const edgeBuffer = 8;
771734
const effectiveSide = target === "trigger" ? flipSide(side) : side;
772735

773-
// when returning to trigger, the base edge should span the full extent
774-
// of both the source (submenu content) and the target (parent content)
775736
const top = sourceRect
776737
? Math.min(targetRect.top, sourceRect.top) - edgeBuffer
777738
: targetRect.top - edgeBuffer;
@@ -1176,7 +1137,6 @@ export class MenuContentState {
11761137
dir: this.parentMenu.root.opts.dir.current,
11771138
style: {
11781139
pointerEvents: "auto",
1179-
// CSS containment isolates style/layout/paint calculations from the rest of the page
11801140
contain: "layout style",
11811141
},
11821142
...this.attachment,
@@ -1969,7 +1929,6 @@ export class ContextMenuTriggerState {
19691929
"data-state": getDataOpenClosed(this.parentMenu.opts.open.current),
19701930
[CONTEXT_MENU_TRIGGER_ATTR]: "",
19711931
tabindex: -1,
1972-
//
19731932
onpointerdown: this.onpointerdown,
19741933
onpointermove: this.onpointermove,
19751934
onpointercancel: this.onpointercancel,

packages/bits-ui/src/lib/internal/animations-complete.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ export class AnimationsComplete {
2828
}
2929

3030
run(fn: () => void | Promise<void>): void {
31-
// if already running, cleanup and restart
3231
this.#cleanup();
3332

3433
const node = this.#opts.ref.current;

packages/bits-ui/src/lib/internal/presence-manager.svelte.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ interface PresenceManagerOpts
1010
}> {
1111
onComplete?: () => void;
1212
enabled?: boolean;
13-
/**
14-
* When closing, if true, skip waiting on exit animations (instant unmount).
15-
* Used e.g. when swapping top-level menubar panels so exit animations still run for dismissals.
16-
*/
1713
shouldSkipExitAnimation?: () => boolean;
1814
}
1915

tests/src/tests/browser-utils.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { type Locator } from "@vitest/browser/context";
22
import { expect, vi } from "vitest";
33

4-
/** Mirrors `getSide` in menu.svelte.ts so tests match popper placement (incl. vertical). */
54
export function getSubmenuPlacementSide(
65
triggerRect: DOMRect,
76
contentRect: DOMRect
@@ -18,9 +17,6 @@ export function getSubmenuPlacementSide(
1817
return deltaY > 0 ? "bottom" : "top";
1918
}
2019

21-
/**
22-
* Synthetic pointerleave coords on the trigger edge facing the submenu (same basis as menu intent).
23-
*/
2420
export function getPointerLeaveTowardSubmenuClientCoords(
2521
triggerRect: DOMRect,
2622
contentRect: DOMRect
@@ -40,9 +36,6 @@ export function getPointerLeaveTowardSubmenuClientCoords(
4036
}
4137
}
4238

43-
/**
44-
* Midpoint between trigger and submenu along the placement axis (for "moving toward" synthetic moves).
45-
*/
4639
export function getPointerMidpointTowardSubmenuClientCoords(
4740
triggerRect: DOMRect,
4841
contentRect: DOMRect
@@ -62,9 +55,6 @@ export function getPointerMidpointTowardSubmenuClientCoords(
6255
}
6356
}
6457

65-
/**
66-
* Point outside the submenu panel on the parent-menu side so intent can exit (not "entered submenu").
67-
*/
6858
export function getPointerAwayFromSubmenuIntentClientCoords(
6959
triggerRect: DOMRect,
7060
subContentRect: DOMRect

tests/src/tests/context-menu/context-menu.browser.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ it("should close submenu when pointer intent changes away from submenu", async (
180180
const subContentWrapper = subContentEl.parentElement as HTMLElement;
181181
const triggerRect = subTriggerEl.getBoundingClientRect();
182182
const contentRect = subContentEl.getBoundingClientRect();
183-
// edge-based leave coords can fall outside the intent corridor for some placements; midpoint matches menu geometry
184183
const leavePt = getPointerMidpointTowardSubmenuClientCoords(triggerRect, contentRect);
185184
const awayFromSub = getPointerAwayFromSubmenuIntentClientCoords(triggerRect, contentRect);
186185

0 commit comments

Comments
 (0)