UNPKG

2.81 kBJavaScriptView Raw
1/*
2 * Copyright 2021 Palantir Technologies, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16// Popper placement utils
17// ======================
18/** Converts a full placement to one of the four positions by stripping text after the `-`. */
19export function getBasePlacement(placement) {
20 return placement.split("-")[0];
21}
22/** Returns true if position is left or right. */
23export function isVerticalPlacement(side) {
24 return ["left", "right"].indexOf(side) !== -1;
25}
26/** Returns the opposite position. */
27export function getOppositePlacement(side) {
28 switch (side) {
29 case "top":
30 return "bottom";
31 case "left":
32 return "right";
33 case "bottom":
34 return "top";
35 default:
36 return "left";
37 }
38}
39/** Returns the CSS alignment keyword corresponding to given placement. */
40export function getAlignment(placement) {
41 var align = placement.split("-")[1];
42 switch (align) {
43 case "start":
44 return "left";
45 case "end":
46 return "right";
47 default:
48 return "center";
49 }
50}
51// Popper modifiers
52// ================
53/** Modifier helper function to compute popper transform-origin based on arrow position */
54export function getTransformOrigin(placement, arrowStyles) {
55 var basePlacement = getBasePlacement(placement);
56 if (arrowStyles === undefined) {
57 return isVerticalPlacement(basePlacement)
58 ? "".concat(getOppositePlacement(basePlacement), " ").concat(getAlignment(basePlacement))
59 : "".concat(getAlignment(basePlacement), " ").concat(getOppositePlacement(basePlacement));
60 }
61 else {
62 // const arrowSizeShift = state.elements.arrow.clientHeight / 2;
63 var arrowSizeShift = 30 / 2;
64 // can use keyword for dimension without the arrow, to ease computation burden.
65 // move origin by half arrow's height to keep it centered.
66 return isVerticalPlacement(basePlacement)
67 ? "".concat(getOppositePlacement(basePlacement), " ").concat(parseInt(arrowStyles.top, 10) + arrowSizeShift, "px")
68 : "".concat(parseInt(arrowStyles.left, 10) + arrowSizeShift, "px ").concat(getOppositePlacement(basePlacement));
69 }
70}
71//# sourceMappingURL=popperUtils.js.map
\No newline at end of file