-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathapplyAnchorPositioning.ts
More file actions
275 lines (240 loc) · 8.69 KB
/
Copy pathapplyAnchorPositioning.ts
File metadata and controls
275 lines (240 loc) · 8.69 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import type { PositioningProps } from '@fluentui/react-positioning';
import { ALIGNMENTS, POSITIONS, POSITION_AREA_MAP } from './constants';
import {
applyOffset,
getCoverSelfAlignment,
getPlacementString,
resolveOffset,
shorthandToPositionArea,
} from './utils';
import { normalizeAlign } from './utils/placement';
import type { LogicalAlignment } from './types';
// alert('Anchor Positioning');
const DEFAULT_FLIP = ['flip-block', 'flip-inline', 'flip-block flip-inline'];
const PLACEMENT_TOLERANCE = 2;
const closeTo = (a: number, b: number) => Math.abs(a - b) <= PLACEMENT_TOLERANCE;
function detectPosition(surfaceRect: DOMRect, targetRect: DOMRect) {
if (surfaceRect.bottom <= targetRect.top + PLACEMENT_TOLERANCE) {
return POSITIONS.above;
}
if (surfaceRect.top >= targetRect.bottom - PLACEMENT_TOLERANCE) {
return POSITIONS.below;
}
if (surfaceRect.right <= targetRect.left + PLACEMENT_TOLERANCE) {
return POSITIONS.before;
}
if (surfaceRect.left >= targetRect.right - PLACEMENT_TOLERANCE) {
return POSITIONS.after;
}
return null;
}
function detectAlign(
position: 'above' | 'below' | 'before' | 'after',
surfaceRect: DOMRect,
targetRect: DOMRect,
): LogicalAlignment {
const isBlockMain = position === POSITIONS.above || position === POSITIONS.below;
const startAligned = isBlockMain
? closeTo(surfaceRect.left, targetRect.left)
: closeTo(surfaceRect.top, targetRect.top);
if (startAligned) {
return ALIGNMENTS.start;
}
const endAligned = isBlockMain
? closeTo(surfaceRect.right, targetRect.right)
: closeTo(surfaceRect.bottom, targetRect.bottom);
if (endAligned) {
return ALIGNMENTS.end;
}
return ALIGNMENTS.center;
}
const ANCHOR_PROPS = [
'position',
'inset',
'margin',
'margin-block-start',
'margin-block-end',
'margin-inline-start',
'margin-inline-end',
'width',
'position-anchor',
'position-area',
'place-self',
'align-self',
'justify-self',
'position-try-fallbacks',
'visibility',
] as const;
const ARROW_PROPS = ['position', 'position-anchor', 'top', 'right', 'bottom', 'left', 'translate'] as const;
export interface ApplyAnchorPositioningArgs {
target: HTMLElement;
container: HTMLElement;
arrow: HTMLElement | null;
anchorName: string;
options: PositioningProps;
targetDocument: Document | undefined;
}
/**
* Applies CSS Anchor Positioning to the container, anchoring it to the target.
* All work is imperative — no React. Returns a cleanup that reverses every
* style mutation and removes any subscribed observers.
*/
export function applyAnchorPositioning({
target,
container,
arrow,
anchorName,
options,
targetDocument,
}: ApplyAnchorPositioningArgs): () => void {
const align = normalizeAlign(options.align ?? ALIGNMENTS.center);
const position = options.position ?? POSITIONS.above;
const { mainAxis, crossAxis } = resolveOffset(options.offset);
const coverAlignment = options.coverTarget ? getCoverSelfAlignment(position, align) : null;
const positionArea = POSITION_AREA_MAP[position][align];
const placement = getPlacementString(position, align);
const fallbackAreas = (options.fallbackPositions ?? []).map(shorthandToPositionArea);
const strategy = options.strategy ?? 'absolute';
// Anchor name on the target.
target.style.setProperty('anchor-name', anchorName);
// Container styles.
container.style.setProperty('position', strategy);
container.style.setProperty('inset', 'auto');
container.style.setProperty('margin', '0');
applyOffset(container, position, mainAxis, crossAxis);
if (options.matchTargetSize === 'width') {
container.style.setProperty('width', 'anchor-size(width)');
} else {
container.style.removeProperty('width');
}
container.style.setProperty('position-anchor', anchorName);
container.setAttribute('data-placement', placement);
if (coverAlignment) {
container.style.setProperty('position-area', 'center');
container.style.setProperty('align-self', coverAlignment.alignSelf);
container.style.setProperty('justify-self', coverAlignment.justifySelf);
container.style.removeProperty('position-try-fallbacks');
} else {
container.style.setProperty('position-area', positionArea);
// Workaround for https://crbug.com/438334710: Chromium (<=130-ish) doesn't
// apply implicit `anchor-center` self-alignment for single-keyword
// `position-area` values.
if (align === ALIGNMENTS.center) {
container.style.setProperty('place-self', 'anchor-center');
} else {
container.style.removeProperty('place-self');
container.style.removeProperty('align-self');
container.style.removeProperty('justify-self');
}
if (options.pinned) {
container.style.removeProperty('position-try-fallbacks');
} else if (fallbackAreas.length > 0) {
container.style.setProperty('position-try-fallbacks', fallbackAreas.join(', '));
} else {
container.style.setProperty('position-try-fallbacks', DEFAULT_FLIP.join(', '));
}
}
// Mirror the browser-resolved placement into data-placement after flip fires.
const observerCleanup = observePlacement(container, target, targetDocument, !!options.coverTarget);
// Position the arrow (if any) at the popover edge nearest the trigger,
// centered on the trigger's cross-axis. The arrow becomes a CSS-anchored
// element pointing at the same trigger, so `anchor(center)` resolves to the
// trigger's center coordinate translated into the popover's containing
// block.
if (arrow) {
applyArrowAnchor(arrow, anchorName, position);
}
// Reveal the container — `usePositioning` hid it before scheduling this
// helper to avoid a flash at the default location while the chunk loaded.
// CSS Anchor Positioning resolves before the next paint, so by the time
// this line runs the anchored position is already what the browser will
// render.
container.style.removeProperty('visibility');
return () => {
observerCleanup();
target.style.removeProperty('anchor-name');
container.removeAttribute('data-placement');
for (const prop of ANCHOR_PROPS) {
container.style.removeProperty(prop);
}
if (arrow) {
for (const prop of ARROW_PROPS) {
arrow.style.removeProperty(prop);
}
}
};
}
function applyArrowAnchor(
arrow: HTMLElement,
anchorName: string,
position: 'above' | 'below' | 'before' | 'after',
): void {
arrow.style.setProperty('position', 'absolute');
arrow.style.setProperty('position-anchor', anchorName);
// Reset any leftover edge offsets from a previous placement.
arrow.style.removeProperty('top');
arrow.style.removeProperty('right');
arrow.style.removeProperty('bottom');
arrow.style.removeProperty('left');
switch (position) {
case POSITIONS.above:
arrow.style.setProperty('bottom', '0');
arrow.style.setProperty('left', 'anchor(center)');
arrow.style.setProperty('translate', '-50% 50%');
break;
case POSITIONS.below:
arrow.style.setProperty('top', '0');
arrow.style.setProperty('left', 'anchor(center)');
arrow.style.setProperty('translate', '-50% -50%');
break;
case POSITIONS.before:
arrow.style.setProperty('right', '0');
arrow.style.setProperty('top', 'anchor(center)');
arrow.style.setProperty('translate', '50% -50%');
break;
case POSITIONS.after:
arrow.style.setProperty('left', '0');
arrow.style.setProperty('top', 'anchor(center)');
arrow.style.setProperty('translate', '-50% -50%');
break;
}
}
function observePlacement(
container: HTMLElement,
target: HTMLElement,
targetDocument: Document | undefined,
disabled: boolean,
): () => void {
if (disabled) {
return () => undefined;
}
const win = targetDocument?.defaultView;
if (!win) {
return () => undefined;
}
const update = () => {
const surfaceRect = container.getBoundingClientRect();
const targetRect = target.getBoundingClientRect();
const detectedPosition = detectPosition(surfaceRect, targetRect);
if (!detectedPosition) {
return;
}
const detectedAlign = detectAlign(detectedPosition, surfaceRect, targetRect);
const next = getPlacementString(detectedPosition, detectedAlign);
if (container.getAttribute('data-placement') !== next) {
container.setAttribute('data-placement', next);
}
};
update();
const ResizeObserverCtor = win.ResizeObserver;
const observer = ResizeObserverCtor ? new ResizeObserverCtor(update) : null;
observer?.observe(container);
observer?.observe(target);
win.addEventListener('scroll', update, true);
win.addEventListener('resize', update);
return () => {
observer?.disconnect();
win.removeEventListener('scroll', update, true);
win.removeEventListener('resize', update);
};
}