@swan-io/lake 15.5.0 → 15.5.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/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { Option } from "@swan-io/boxed";
|
|
2
2
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
3
|
-
import { match } from "ts-pattern";
|
|
3
|
+
import { match, P } from "ts-pattern";
|
|
4
|
+
const MAX_OFFSET_FOR_CENTER_PLACEMENT = 100;
|
|
4
5
|
const HORIZONTAL_SAFETY_MARGIN = 16;
|
|
5
|
-
export const useContextualLayer = ({ placement
|
|
6
|
+
export const useContextualLayer = ({ placement, verticalPlacement, visible, matchReferenceWidth = false, matchReferenceMinWidth = false, referenceRef: externalReferenceRef, }) => {
|
|
6
7
|
const referenceRef = useRef(null);
|
|
7
8
|
const contentRef = useRef(null);
|
|
8
9
|
const usedRef = externalReferenceRef !== null && externalReferenceRef !== void 0 ? externalReferenceRef : referenceRef;
|
|
@@ -25,14 +26,13 @@ export const useContextualLayer = ({ placement = "left", verticalPlacement, visi
|
|
|
25
26
|
// if contentRef isn't used in the component, we don't infer the placement depending on content size
|
|
26
27
|
const contentRect = contentElement != null ? contentElement.getBoundingClientRect() : { width: 0, height: 0 };
|
|
27
28
|
const contentWidth = contentRect.width;
|
|
28
|
-
const
|
|
29
|
-
const isCenteredEnough = availableSpaceBefore > contentMidWidth && availableSpaceAfter > contentMidWidth;
|
|
29
|
+
const isCenteredEnough = Math.abs(availableSpaceBefore - availableSpaceAfter) < MAX_OFFSET_FOR_CENTER_PLACEMENT;
|
|
30
30
|
const bestPlacement = availableSpaceBefore > availableSpaceAfter ? "right" : "left";
|
|
31
31
|
const inferedPlacement = match(placement)
|
|
32
32
|
.returnType()
|
|
33
|
-
.with("center", () => (isCenteredEnough ? "center" : bestPlacement))
|
|
34
|
-
.with("left", () => (
|
|
35
|
-
.with("right", () => (
|
|
33
|
+
.with("center", P.nullish, () => (isCenteredEnough ? "center" : bestPlacement))
|
|
34
|
+
.with("left", () => (availableSpaceAfter > contentWidth ? "left" : bestPlacement))
|
|
35
|
+
.with("right", () => (availableSpaceBefore > contentWidth ? "right" : bestPlacement))
|
|
36
36
|
.exhaustive();
|
|
37
37
|
const openAbove = verticalPlacement === "above"
|
|
38
38
|
? true
|