@nanahoshi/mona-ui 0.36.0 → 0.36.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/docs/api/popup.md CHANGED
@@ -246,7 +246,7 @@ Interface.
246
246
  | `maxWidth?` | `number \| string` | Maximum width of the popup. |
247
247
  | `minHeight?` | `number \| string` | Minimum height of the popup. |
248
248
  | `minWidth?` | `number \| string` | Minimum width of the popup. |
249
- | `offset?` | `PopupOffset` | Offset of the popup. |
249
+ | `offset?` | `PopupOffset` | Offset of the popup, in pixels, for the position the connection points give. A fallback that flips the popup to the anchor's other side, such as above it when there is no room below, gets the offset negated on that axis, so the gap stays between the popup and the anchor. Positions passed in `positions` take it as given. |
250
250
  | `popupClass?` | `string \| string[]` | Classes to be applied to the popup. |
251
251
  | `popupConnectionPoint?` | `ConnectionPoint \| null` | The connection point of the popup element to which the anchor will be connected. |
252
252
  | `popupWrapperClass?` | `string \| string[]` | Classes to be applied to the popup wrapper content div. |
@@ -1,7 +1,7 @@
1
1
  import * as i0 from "@angular/core";
2
2
  import { Component, DOCUMENT, DestroyRef, ElementRef, Injectable, InjectionToken, Injector, TemplateRef, ViewContainerRef, booleanAttribute, computed, effect, inject, input, output, signal, viewChild } from "@angular/core";
3
3
  import { takeUntilDestroyed } from "@angular/core/rxjs-interop";
4
- import { ReplaySubject, Subject, Subscription, asapScheduler, asyncScheduler, fromEvent, merge, take, takeUntil } from "rxjs";
4
+ import { ReplaySubject, Subject, Subscription, asapScheduler, asyncScheduler, filter, fromEvent, merge, take, takeUntil } from "rxjs";
5
5
  import { PreventableEvent } from "@nanahoshi/mona-ui/common";
6
6
  import { Directionality } from "@angular/cdk/bidi";
7
7
  import { Overlay, OverlayConfig } from "@angular/cdk/overlay";
@@ -356,7 +356,7 @@ var PopupReference = class {
356
356
  return this.overlayReference;
357
357
  }
358
358
  };
359
- const connectionPosition = (anchorConnectionPoint, popupConnectionPoint) => {
359
+ const connectionPosition = (anchorConnectionPoint, popupConnectionPoint, offset) => {
360
360
  const originMap = {
361
361
  topleft: {
362
362
  originX: "start",
@@ -437,7 +437,15 @@ const connectionPosition = (anchorConnectionPoint, popupConnectionPoint) => {
437
437
  ...originMap[anchorConnectionPoint],
438
438
  ...overlayMap[popupConnectionPoint]
439
439
  };
440
- return [primaryPosition, ...generateFallbackPositions(primaryPosition)];
440
+ const positions = [primaryPosition, ...generateFallbackPositions(primaryPosition)];
441
+ if (!offset) return positions;
442
+ const horizontal = offset.horizontal ?? 0;
443
+ const vertical = offset.vertical ?? 0;
444
+ return positions.map((position) => ({
445
+ ...position,
446
+ offsetX: mirrorsX(primaryPosition, position) ? negate(horizontal) : horizontal,
447
+ offsetY: mirrorsY(primaryPosition, position) ? negate(vertical) : vertical
448
+ }));
441
449
  };
442
450
  const generateFallbackPositions = (primaryPosition) => {
443
451
  const { originX, originY, overlayX, overlayY } = primaryPosition;
@@ -583,6 +591,9 @@ const generateFallbackPositions = (primaryPosition) => {
583
591
  };
584
592
  const flipX = (value) => value === "start" ? "end" : value === "end" ? "start" : value;
585
593
  const flipY = (value) => value === "top" ? "bottom" : value === "bottom" ? "top" : value;
594
+ const mirrorsX = (primary, position) => (position.originX !== primary.originX || position.overlayX !== primary.overlayX) && position.originX === flipX(primary.originX) && position.overlayX === flipX(primary.overlayX);
595
+ const mirrorsY = (primary, position) => (position.originY !== primary.originY || position.overlayY !== primary.overlayY) && position.originY === flipY(primary.originY) && position.overlayY === flipY(primary.overlayY);
596
+ const negate = (value) => value === 0 ? 0 : -value;
586
597
  var PopupService = class PopupService {
587
598
  #destroyRef = inject(DestroyRef);
588
599
  #directionality = inject(Directionality, { optional: true });
@@ -683,7 +694,7 @@ var PopupService = class PopupService {
683
694
  createPositionStrategy(settings) {
684
695
  if (settings.positionStrategy === "global") return this.#overlay.position().global();
685
696
  const resolvedAnchor = this.resolveAnchor(settings.anchor);
686
- const position = settings.positions?.length ? settings.positions : this.getPosition(settings.anchorConnectionPoint, settings.popupConnectionPoint);
697
+ const position = settings.positions?.length ? settings.positions : this.getPosition(settings.anchorConnectionPoint, settings.popupConnectionPoint, settings.offset);
687
698
  const strategy = this.#overlay.position().flexibleConnectedTo(resolvedAnchor).withPositions(position).withDefaultOffsetX(settings.offset?.horizontal ?? 0).withDefaultOffsetY(settings.offset?.vertical ?? 0).withPush(settings.withPush ?? true);
688
699
  if (settings.withScrollTracking ?? true) {
689
700
  const scrollableContainers = this.getScrollableContainers(resolvedAnchor);
@@ -721,8 +732,8 @@ var PopupService = class PopupService {
721
732
  if (typeof anchor === "object" && "nativeElement" in anchor && anchor.nativeElement instanceof HTMLElement) return anchor.nativeElement;
722
733
  return null;
723
734
  }
724
- getPosition(anchorConnectionPoint, popupConnectionPoint) {
725
- return connectionPosition(anchorConnectionPoint ?? "bottomleft", popupConnectionPoint ?? "topleft");
735
+ getPosition(anchorConnectionPoint, popupConnectionPoint, offset) {
736
+ return connectionPosition(anchorConnectionPoint ?? "bottomleft", popupConnectionPoint ?? "topleft", offset);
726
737
  }
727
738
  getScrollableAncestorElements(anchorElement) {
728
739
  const scrollables = [];
@@ -939,12 +950,22 @@ var PopupService = class PopupService {
939
950
  popupReference.close(closeEvent, 0);
940
951
  };
941
952
  const win = this.#document.defaultView;
942
- const pageEvents = [fromEvent(win, "scroll", { passive: true }), fromEvent(this.#document, "scroll", { passive: true })];
953
+ const pageStart = {
954
+ left: win.scrollX,
955
+ top: win.scrollY
956
+ };
957
+ const pageMoved = () => win.scrollX !== pageStart.left || win.scrollY !== pageStart.top;
958
+ const pageEvents = [fromEvent(win, "scroll", { passive: true }).pipe(filter(pageMoved)), fromEvent(this.#document, "scroll", { passive: true }).pipe(filter(pageMoved))];
943
959
  if (scrollableContainers.length === 0) pageEvents.push(fromEvent(win, "resize", { passive: true }));
944
960
  const pageScrollSubscription = merge(...pageEvents).pipe(takeUntil(popupReference.closed), takeUntilDestroyed(this.#destroyRef)).subscribe((event) => handleScrollClose(event));
945
961
  subscriptions.push(pageScrollSubscription);
946
962
  scrollableContainers.forEach((scrollable) => {
947
- const containerScrollSubscription = scrollable.elementScrolled().pipe(takeUntil(popupReference.closed), takeUntilDestroyed(this.#destroyRef)).subscribe(() => handleScrollClose());
963
+ const element = scrollable.getElementRef().nativeElement;
964
+ const start = {
965
+ left: element.scrollLeft,
966
+ top: element.scrollTop
967
+ };
968
+ const containerScrollSubscription = scrollable.elementScrolled().pipe(filter(() => element.scrollLeft !== start.left || element.scrollTop !== start.top), takeUntil(popupReference.closed), takeUntilDestroyed(this.#destroyRef)).subscribe(() => handleScrollClose());
948
969
  subscriptions.push(containerScrollSubscription);
949
970
  });
950
971
  popupReference.closed.pipe(take(1)).subscribe(() => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nanahoshi/mona-ui",
3
- "version": "0.36.0",
3
+ "version": "0.36.1",
4
4
  "description": "A very light Angular UI framework",
5
5
  "author": "Nanahoshi",
6
6
  "repository": {
@@ -236,7 +236,9 @@ interface PopupSettings<T = unknown, C = void> {
236
236
  */
237
237
  minWidth?: number | string;
238
238
  /**
239
- * Offset of the popup.
239
+ * Offset of the popup, in pixels, for the position the connection points give. A fallback that flips the popup
240
+ * to the anchor's other side, such as above it when there is no room below, gets the offset negated on that
241
+ * axis, so the gap stays between the popup and the anchor. Positions passed in `positions` take it as given.
240
242
  * @type {PopupOffset}
241
243
  */
242
244
  offset?: PopupOffset;