Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
type BoundingBox = {
  bottom: number;
  height: number;
  left: number;
  right: number;
  top: number;
  width: number;
  x: number;
  y: number;
};
 
const getBoundingBox = (elem?: HTMLElement) => {
  const template: BoundingBox = {
    bottom: 0,
    height: 0,
    left: 0,
    right: 0,
    top: 0,
    width: 0,
    x: 0,
    y: 0,
  };
 
  const position = (elem?.getBoundingClientRect?.() ?? {}) as BoundingBox;
 
  const result = Object.entries(template).reduce<BoundingBox>((res, pair) => {
    const key = pair[0] as keyof BoundingBox;
    const value = pair[1];
 
    res[key] = position[key] ?? value;
    return res;
  }, {} as BoundingBox);
 
  return result;
};
 
/**
 * Returns element absolute position relative to document
 */
export const getAbsolutePosition = (elem: HTMLElement) => {
  // crossbrowser version
  const box = elem.getBoundingClientRect();
 
  const body = document.body;
  const docEl = document.documentElement;
 
  const scrollTop = window.pageYOffset || docEl.scrollTop || body.scrollTop;
  const scrollLeft = window.pageXOffset || docEl.scrollLeft || body.scrollLeft;
 
  const clientTop = docEl.clientTop || body.clientTop || 0;
  const clientLeft = docEl.clientLeft || body.clientLeft || 0;
 
  const top = box.top + scrollTop - clientTop;
  const left = box.left + scrollLeft - clientLeft;
 
  const bbox = elem.getBoundingClientRect();
 
  return {
    width: bbox.width,
    height: bbox.height,
    top: Math.round(top),
    left: Math.round(left),
  };
};
 
const positioner = (source: HTMLElement, target: HTMLElement) => {
  const sourcePosition = getBoundingBox(source);
  const targetPosition = getBoundingBox(target);
 
  return {
    source: sourcePosition,
    target: targetPosition,
    get top() {
      return sourcePosition.top - targetPosition.height;
    },
    get bottom() {
      return sourcePosition.top + sourcePosition.height;
    },
    get horizontalCenter() {
      return sourcePosition.left + sourcePosition.width / 2 - targetPosition.width / 2;
    },
    get horizontalLeft() {
      return sourcePosition.left;
    },
    get horizontalRight() {
      return sourcePosition.left + sourcePosition.width - targetPosition.width;
    },
  } as const;
};
 
export type Align = "top-left" | "top-center" | "top-right" | "bottom-left" | "bottom-center" | "bottom-right";
 
export const alignElements = (
  elem: HTMLElement,
  target: HTMLElement,
  align: Align = "bottom-left",
  padding = 0,
  constrainHeight = false,
  openUpwardForShortViewport = true,
) => {
  let offsetLeft = 0;
  let offsetTop = 0;
  let maxHeight;
 
  const pos = positioner(elem, target);
  const resultAlign = align.split("-");
 
  switch (align) {
    case "top-center":
      offsetTop = constrainHeight ? Math.max(pos.top - padding, 0) : pos.top - padding;
      offsetLeft = pos.horizontalCenter;
      maxHeight = pos.source.top - offsetTop;
      break;
    case "top-left":
      offsetTop = constrainHeight ? Math.max(pos.top - padding, 0) : pos.top - padding;
      offsetLeft = pos.horizontalLeft;
      maxHeight = pos.source.top - offsetTop;
      break;
    case "top-right":
      offsetTop = constrainHeight ? Math.max(pos.top - padding, 0) : pos.top - padding;
      offsetLeft = pos.horizontalRight;
      maxHeight = pos.source.top - offsetTop;
      break;
    case "bottom-center":
      offsetTop = pos.bottom + padding;
      offsetLeft = pos.horizontalCenter;
      maxHeight = window.scrollY + window.innerHeight - offsetTop;
      break;
    case "bottom-left":
      offsetTop = pos.bottom + padding;
      offsetLeft = pos.horizontalLeft;
      maxHeight = window.scrollY + window.innerHeight - offsetTop;
      break;
    case "bottom-right":
      offsetTop = pos.bottom + padding;
      offsetLeft = pos.horizontalRight;
      maxHeight = window.scrollY + window.innerHeight - offsetTop;
      break;
    default:
      break;
  }
 
  if (offsetTop < window.scrollY || !openUpwardForShortViewport) {
    offsetTop = pos.bottom + padding;
    maxHeight = window.scrollY + window.innerHeight - offsetTop;
    resultAlign[0] = "bottom";
  }
  // If the dropdown has more space on the top, then we should align it to the top
  // of the trigger element instead of the bottom.
  else if ((maxHeight ?? 0) < pos.source.top - (constrainHeight ? Math.max(pos.top - padding, 0) : pos.top - padding)) {
    offsetTop = constrainHeight ? Math.max(pos.top - padding, 0) : pos.top - padding;
    maxHeight = pos.source.top - offsetTop;
    resultAlign[0] = "top";
  }
 
  if (offsetLeft < 0) {
    offsetLeft = pos.horizontalLeft;
    resultAlign[1] = "left";
  } else if (offsetLeft + pos.target.width > window.innerWidth) {
    offsetLeft = pos.horizontalRight;
    resultAlign[1] = "right";
  }
 
  const { scrollY, scrollX } = window;
 
  return {
    top: offsetTop + scrollY,
    left: offsetLeft + scrollX,
    pos,
    align: resultAlign.join("-") as Align,
    ...(constrainHeight ? { maxHeight } : {}),
  };
};