/
Placement.ts
87 lines (76 loc) · 2.85 KB
/
Placement.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Utilities
*/
import { RelativePosition } from "@itwin/appui-abstract";
/**
* Identifies which side of a target an element should be placed.
* Without an {@link Alignment}, the element center will be placed at the center of the target.
*/
type Side = "left" | "top" | "right" | "bottom";
type Alignment = "start" | "end";
/**
* Identifies the placement of a given element.
* @public
*/
export type Placement = Side | `${Side}-${Alignment}`;
/**
* Union type used for transition away from appui-abstract.
* Should be removed N+1 major versions after its deprecation.
* @internal
*/
export type PlacementOrRelativePosition = Placement | RelativePosition;
/**
* Utility for converting a Placement or RelativePosition to a RelativePosition.
* Should be removed N+1 major versions after RelativePosition's deprecation.
* @internal
*/
export function mapToRelativePosition(
input: Placement | RelativePosition
): RelativePosition {
if (typeof input === "string") return placementToRelativePositionMap[input];
return input;
}
/**
* Utility for converting a RelativePosition to a Placement.
* Should most likely be removed N+1 major versions after RelativePosition's deprecation.
* @internal
*/
export function mapToPlacement(
input?: Placement | RelativePosition
): Placement {
if (!input) return "top-end";
if (typeof input === "string") return input;
return relativePositionToPlacementMap[input];
}
const placementToRelativePositionMap: { [key in Placement]: RelativePosition } =
{
left: RelativePosition.Left,
top: RelativePosition.Top,
right: RelativePosition.Right,
bottom: RelativePosition.Bottom,
"left-start": RelativePosition.LeftTop,
"left-end": RelativePosition.Left,
"right-start": RelativePosition.RightTop,
"right-end": RelativePosition.Right,
"top-start": RelativePosition.TopLeft,
"top-end": RelativePosition.TopRight,
"bottom-start": RelativePosition.BottomLeft,
"bottom-end": RelativePosition.BottomRight,
};
const relativePositionToPlacementMap: { [key in RelativePosition]: Placement } =
{
[RelativePosition.Left]: "left",
[RelativePosition.Top]: "top",
[RelativePosition.Right]: "right",
[RelativePosition.Bottom]: "bottom",
[RelativePosition.LeftTop]: "left-start",
[RelativePosition.RightTop]: "right-start",
[RelativePosition.TopLeft]: "top-start",
[RelativePosition.TopRight]: "top-end",
[RelativePosition.BottomLeft]: "bottom-start",
[RelativePosition.BottomRight]: "bottom-end",
};