@starwind-ui/core 1.15.4 → 1.16.0
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/dist/index.d.ts +1 -0
- package/dist/index.js +67 -43
- package/dist/index.js.map +1 -1
- package/dist/src/components/accordion/Accordion.astro +1 -1
- package/dist/src/components/accordion/AccordionContent.astro +1 -1
- package/dist/src/components/accordion/AccordionItem.astro +1 -1
- package/dist/src/components/accordion/AccordionTrigger.astro +1 -1
- package/dist/src/components/alert/Alert.astro +1 -1
- package/dist/src/components/alert/AlertDescription.astro +1 -1
- package/dist/src/components/alert/AlertTitle.astro +1 -1
- package/dist/src/components/alert-dialog/AlertDialog.astro +1 -1
- package/dist/src/components/alert-dialog/AlertDialogAction.astro +1 -1
- package/dist/src/components/alert-dialog/AlertDialogCancel.astro +1 -1
- package/dist/src/components/alert-dialog/AlertDialogContent.astro +3 -3
- package/dist/src/components/alert-dialog/AlertDialogDescription.astro +1 -1
- package/dist/src/components/alert-dialog/AlertDialogFooter.astro +1 -1
- package/dist/src/components/alert-dialog/AlertDialogHeader.astro +1 -1
- package/dist/src/components/alert-dialog/AlertDialogTitle.astro +1 -1
- package/dist/src/components/alert-dialog/AlertDialogTrigger.astro +2 -2
- package/dist/src/components/aspect-ratio/AspectRatio.astro +1 -1
- package/dist/src/components/avatar/Avatar.astro +1 -1
- package/dist/src/components/avatar/AvatarFallback.astro +1 -1
- package/dist/src/components/avatar/AvatarImage.astro +2 -2
- package/dist/src/components/badge/Badge.astro +1 -1
- package/dist/src/components/breadcrumb/Breadcrumb.astro +1 -1
- package/dist/src/components/breadcrumb/BreadcrumbEllipsis.astro +1 -1
- package/dist/src/components/breadcrumb/BreadcrumbItem.astro +1 -1
- package/dist/src/components/breadcrumb/BreadcrumbLink.astro +1 -1
- package/dist/src/components/breadcrumb/BreadcrumbList.astro +2 -2
- package/dist/src/components/breadcrumb/BreadcrumbPage.astro +1 -1
- package/dist/src/components/breadcrumb/BreadcrumbSeparator.astro +1 -1
- package/dist/src/components/button/Button.astro +1 -1
- package/dist/src/components/button-group/ButtonGroup.astro +1 -1
- package/dist/src/components/button-group/ButtonGroupSeparator.astro +1 -1
- package/dist/src/components/button-group/ButtonGroupText.astro +1 -1
- package/dist/src/components/card/Card.astro +1 -1
- package/dist/src/components/card/CardAction.astro +1 -1
- package/dist/src/components/card/CardContent.astro +1 -1
- package/dist/src/components/card/CardDescription.astro +1 -1
- package/dist/src/components/card/CardFooter.astro +1 -1
- package/dist/src/components/card/CardHeader.astro +1 -1
- package/dist/src/components/card/CardTitle.astro +1 -1
- package/dist/src/components/carousel/Carousel.astro +1 -1
- package/dist/src/components/carousel/CarouselContent.astro +1 -1
- package/dist/src/components/carousel/CarouselItem.astro +1 -1
- package/dist/src/components/carousel/CarouselNext.astro +1 -1
- package/dist/src/components/carousel/CarouselPrevious.astro +1 -1
- package/dist/src/components/checkbox/Checkbox.astro +1 -1
- package/dist/src/components/collapsible/Collapsible.astro +1 -1
- package/dist/src/components/collapsible/CollapsibleContent.astro +1 -1
- package/dist/src/components/collapsible/CollapsibleTrigger.astro +1 -1
- package/dist/src/components/dialog/Dialog.astro +1 -1
- package/dist/src/components/dialog/DialogClose.astro +1 -1
- package/dist/src/components/dialog/DialogContent.astro +2 -2
- package/dist/src/components/dialog/DialogDescription.astro +1 -1
- package/dist/src/components/dialog/DialogFooter.astro +1 -1
- package/dist/src/components/dialog/DialogHeader.astro +1 -1
- package/dist/src/components/dialog/DialogTitle.astro +1 -1
- package/dist/src/components/dialog/DialogTrigger.astro +1 -1
- package/dist/src/components/dropdown/Dropdown.astro +261 -34
- package/dist/src/components/dropdown/DropdownContent.astro +41 -5
- package/dist/src/components/dropdown/DropdownItem.astro +2 -2
- package/dist/src/components/dropdown/DropdownLabel.astro +2 -2
- package/dist/src/components/dropdown/DropdownSeparator.astro +1 -1
- package/dist/src/components/dropdown/DropdownShortcut.astro +17 -0
- package/dist/src/components/dropdown/DropdownSub.astro +15 -0
- package/dist/src/components/dropdown/DropdownSubContent.astro +36 -0
- package/dist/src/components/dropdown/DropdownSubTrigger.astro +34 -0
- package/dist/src/components/dropdown/DropdownTrigger.astro +1 -1
- package/dist/src/components/dropdown/index.ts +12 -0
- package/dist/src/components/dropzone/DropzoneFilesList.astro +1 -1
- package/dist/src/components/image/Image.astro +1 -1
- package/dist/src/components/input/Input.astro +1 -1
- package/dist/src/components/input-group/InputGroup.astro +28 -0
- package/dist/src/components/input-group/InputGroupAddon.astro +79 -0
- package/dist/src/components/input-group/InputGroupButton.astro +34 -0
- package/dist/src/components/input-group/InputGroupInput.astro +16 -0
- package/dist/src/components/input-group/InputGroupText.astro +17 -0
- package/dist/src/components/input-group/InputGroupTextarea.astro +20 -0
- package/dist/src/components/input-group/index.ts +33 -0
- package/dist/src/components/input-otp/InputOtp.astro +1 -1
- package/dist/src/components/input-otp/InputOtpGroup.astro +1 -1
- package/dist/src/components/input-otp/InputOtpSeparator.astro +1 -1
- package/dist/src/components/input-otp/InputOtpSlot.astro +1 -1
- package/dist/src/components/item/Item.astro +1 -1
- package/dist/src/components/item/ItemActions.astro +1 -1
- package/dist/src/components/item/ItemContent.astro +1 -1
- package/dist/src/components/item/ItemDescription.astro +1 -1
- package/dist/src/components/item/ItemFooter.astro +1 -1
- package/dist/src/components/item/ItemGroup.astro +1 -1
- package/dist/src/components/item/ItemHeader.astro +1 -1
- package/dist/src/components/item/ItemMedia.astro +1 -1
- package/dist/src/components/item/ItemSeparator.astro +1 -1
- package/dist/src/components/item/ItemTitle.astro +1 -1
- package/dist/src/components/kbd/Kbd.astro +1 -1
- package/dist/src/components/kbd/KbdGroup.astro +1 -1
- package/dist/src/components/label/Label.astro +1 -1
- package/dist/src/components/native-select/NativeSelect.astro +64 -0
- package/dist/src/components/native-select/NativeSelectOptGroup.astro +15 -0
- package/dist/src/components/native-select/NativeSelectOption.astro +15 -0
- package/dist/src/components/native-select/index.ts +21 -0
- package/dist/src/components/pagination/Pagination.astro +1 -1
- package/dist/src/components/pagination/PaginationContent.astro +1 -1
- package/dist/src/components/pagination/PaginationEllipsis.astro +1 -1
- package/dist/src/components/pagination/PaginationItem.astro +1 -1
- package/dist/src/components/pagination/PaginationLink.astro +1 -1
- package/dist/src/components/pagination/PaginationNext.astro +1 -1
- package/dist/src/components/pagination/PaginationPrevious.astro +1 -1
- package/dist/src/components/popover/Popover.astro +717 -0
- package/dist/src/components/popover/PopoverContent.astro +102 -0
- package/dist/src/components/popover/PopoverDescription.astro +14 -0
- package/dist/src/components/popover/PopoverHeader.astro +14 -0
- package/dist/src/components/popover/PopoverTitle.astro +14 -0
- package/dist/src/components/popover/PopoverTrigger.astro +51 -0
- package/dist/src/components/popover/index.ts +34 -0
- package/dist/src/components/progress/Progress.astro +1 -1
- package/dist/src/components/prose/Prose.astro +1 -1
- package/dist/src/components/radio-group/RadioGroup.astro +1 -1
- package/dist/src/components/radio-group/RadioGroupItem.astro +1 -1
- package/dist/src/components/select/Select.astro +1 -1
- package/dist/src/components/select/SelectContent.astro +1 -1
- package/dist/src/components/select/SelectItem.astro +1 -1
- package/dist/src/components/select/SelectLabel.astro +1 -1
- package/dist/src/components/select/SelectSearch.astro +1 -1
- package/dist/src/components/select/SelectSeparator.astro +1 -1
- package/dist/src/components/select/SelectTrigger.astro +1 -1
- package/dist/src/components/select/SelectValue.astro +1 -1
- package/dist/src/components/separator/Separator.astro +1 -1
- package/dist/src/components/sheet/Sheet.astro +1 -1
- package/dist/src/components/sheet/SheetContent.astro +1 -1
- package/dist/src/components/sheet/SheetDescription.astro +1 -1
- package/dist/src/components/sheet/SheetFooter.astro +1 -1
- package/dist/src/components/sheet/SheetHeader.astro +1 -1
- package/dist/src/components/sheet/SheetTitle.astro +1 -1
- package/dist/src/components/sidebar/Sidebar.astro +19 -2
- package/dist/src/components/sidebar/SidebarContent.astro +1 -1
- package/dist/src/components/sidebar/SidebarFooter.astro +1 -1
- package/dist/src/components/sidebar/SidebarGroup.astro +1 -1
- package/dist/src/components/sidebar/SidebarGroupContent.astro +1 -1
- package/dist/src/components/sidebar/SidebarGroupLabel.astro +2 -2
- package/dist/src/components/sidebar/SidebarHeader.astro +1 -1
- package/dist/src/components/sidebar/SidebarInput.astro +1 -1
- package/dist/src/components/sidebar/SidebarInset.astro +1 -1
- package/dist/src/components/sidebar/SidebarMenu.astro +1 -1
- package/dist/src/components/sidebar/SidebarMenuAction.astro +1 -1
- package/dist/src/components/sidebar/SidebarMenuBadge.astro +1 -1
- package/dist/src/components/sidebar/SidebarMenuButton.astro +2 -14
- package/dist/src/components/sidebar/SidebarMenuItem.astro +1 -1
- package/dist/src/components/sidebar/SidebarMenuSkeleton.astro +1 -1
- package/dist/src/components/sidebar/SidebarMenuSub.astro +1 -1
- package/dist/src/components/sidebar/SidebarMenuSubButton.astro +1 -1
- package/dist/src/components/sidebar/SidebarMenuSubItem.astro +1 -1
- package/dist/src/components/sidebar/SidebarProvider.astro +1 -1
- package/dist/src/components/sidebar/SidebarRail.astro +2 -2
- package/dist/src/components/sidebar/SidebarSeparator.astro +1 -1
- package/dist/src/components/sidebar/SidebarTrigger.astro +1 -1
- package/dist/src/components/skeleton/Skeleton.astro +1 -1
- package/dist/src/components/slider/Slider.astro +1 -1
- package/dist/src/components/spinner/Spinner.astro +1 -1
- package/dist/src/components/switch/Switch.astro +1 -1
- package/dist/src/components/table/Table.astro +1 -1
- package/dist/src/components/table/TableBody.astro +1 -1
- package/dist/src/components/table/TableCaption.astro +1 -1
- package/dist/src/components/table/TableCell.astro +1 -1
- package/dist/src/components/table/TableFoot.astro +1 -1
- package/dist/src/components/table/TableHead.astro +1 -1
- package/dist/src/components/table/TableHeader.astro +1 -1
- package/dist/src/components/table/TableRow.astro +1 -1
- package/dist/src/components/tabs/Tabs.astro +1 -1
- package/dist/src/components/tabs/TabsContent.astro +1 -1
- package/dist/src/components/tabs/TabsList.astro +1 -1
- package/dist/src/components/tabs/TabsTrigger.astro +1 -1
- package/dist/src/components/textarea/Textarea.astro +1 -1
- package/dist/src/components/theme-toggle/ThemeToggle.astro +1 -1
- package/dist/src/components/toast/ToastDescription.astro +1 -1
- package/dist/src/components/toast/ToastItem.astro +1 -1
- package/dist/src/components/toast/Toaster.astro +1 -1
- package/dist/src/components/toggle/Toggle.astro +1 -1
- package/dist/src/components/tooltip/Tooltip.astro +260 -122
- package/dist/src/components/tooltip/TooltipContent.astro +13 -23
- package/dist/src/components/video/Video.astro +2 -2
- package/dist/src/lib/utils/starwind/positioning.ts +318 -0
- package/package.json +1 -1
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
export type FloatingSide = "top" | "bottom" | "left" | "right";
|
|
2
|
+
export type FloatingAlign = "start" | "center" | "end";
|
|
3
|
+
|
|
4
|
+
interface Placement {
|
|
5
|
+
side: FloatingSide;
|
|
6
|
+
align: FloatingAlign;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
interface Position {
|
|
10
|
+
top: number;
|
|
11
|
+
left: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface OverflowAmount {
|
|
15
|
+
left: number;
|
|
16
|
+
right: number;
|
|
17
|
+
top: number;
|
|
18
|
+
bottom: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface ResolvePlacementOptions {
|
|
22
|
+
side: FloatingSide;
|
|
23
|
+
align: FloatingAlign;
|
|
24
|
+
sideOffset: number;
|
|
25
|
+
triggerRect: DOMRect;
|
|
26
|
+
contentWidth: number;
|
|
27
|
+
contentHeight: number;
|
|
28
|
+
viewportWidth: number;
|
|
29
|
+
viewportHeight: number;
|
|
30
|
+
viewportPadding: number;
|
|
31
|
+
avoidCollisions?: boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface ResolvePlacementResult {
|
|
35
|
+
side: FloatingSide;
|
|
36
|
+
align: FloatingAlign;
|
|
37
|
+
top: number;
|
|
38
|
+
left: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function getOppositeSide(side: FloatingSide): FloatingSide {
|
|
42
|
+
switch (side) {
|
|
43
|
+
case "top":
|
|
44
|
+
return "bottom";
|
|
45
|
+
case "bottom":
|
|
46
|
+
return "top";
|
|
47
|
+
case "left":
|
|
48
|
+
return "right";
|
|
49
|
+
case "right":
|
|
50
|
+
return "left";
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function getOppositeAlign(align: FloatingAlign): FloatingAlign {
|
|
55
|
+
switch (align) {
|
|
56
|
+
case "start":
|
|
57
|
+
return "end";
|
|
58
|
+
case "end":
|
|
59
|
+
return "start";
|
|
60
|
+
case "center":
|
|
61
|
+
return "center";
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function getPlacementCandidates(side: FloatingSide, align: FloatingAlign): Placement[] {
|
|
66
|
+
const placements: Placement[] = [];
|
|
67
|
+
|
|
68
|
+
const pushUnique = (candidate: Placement) => {
|
|
69
|
+
if (
|
|
70
|
+
!placements.some(
|
|
71
|
+
(placement) => placement.side === candidate.side && placement.align === candidate.align,
|
|
72
|
+
)
|
|
73
|
+
) {
|
|
74
|
+
placements.push(candidate);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const oppositeSide = getOppositeSide(side);
|
|
79
|
+
const oppositeAlign = getOppositeAlign(align);
|
|
80
|
+
|
|
81
|
+
pushUnique({ side, align });
|
|
82
|
+
pushUnique({ side: oppositeSide, align });
|
|
83
|
+
pushUnique({ side, align: "center" });
|
|
84
|
+
pushUnique({ side: oppositeSide, align: "center" });
|
|
85
|
+
pushUnique({ side, align: oppositeAlign });
|
|
86
|
+
pushUnique({ side: oppositeSide, align: oppositeAlign });
|
|
87
|
+
|
|
88
|
+
if (side === "top" || side === "bottom") {
|
|
89
|
+
for (const fallbackAlign of ["start", "center", "end"] as const) {
|
|
90
|
+
pushUnique({ side: "right", align: fallbackAlign });
|
|
91
|
+
pushUnique({ side: "left", align: fallbackAlign });
|
|
92
|
+
}
|
|
93
|
+
} else {
|
|
94
|
+
for (const fallbackAlign of ["start", "center", "end"] as const) {
|
|
95
|
+
pushUnique({ side: "bottom", align: fallbackAlign });
|
|
96
|
+
pushUnique({ side: "top", align: fallbackAlign });
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return placements;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function getPlacementPosition(
|
|
104
|
+
placement: Placement,
|
|
105
|
+
triggerRect: DOMRect,
|
|
106
|
+
contentWidth: number,
|
|
107
|
+
contentHeight: number,
|
|
108
|
+
sideOffset: number,
|
|
109
|
+
): Position {
|
|
110
|
+
let top = 0;
|
|
111
|
+
let left = 0;
|
|
112
|
+
|
|
113
|
+
if (placement.side === "bottom" || placement.side === "top") {
|
|
114
|
+
top =
|
|
115
|
+
placement.side === "bottom"
|
|
116
|
+
? triggerRect.bottom + sideOffset
|
|
117
|
+
: triggerRect.top - contentHeight - sideOffset;
|
|
118
|
+
|
|
119
|
+
if (placement.align === "start") {
|
|
120
|
+
left = triggerRect.left;
|
|
121
|
+
} else if (placement.align === "end") {
|
|
122
|
+
left = triggerRect.right - contentWidth;
|
|
123
|
+
} else {
|
|
124
|
+
left = triggerRect.left + (triggerRect.width - contentWidth) / 2;
|
|
125
|
+
}
|
|
126
|
+
} else {
|
|
127
|
+
left =
|
|
128
|
+
placement.side === "right"
|
|
129
|
+
? triggerRect.right + sideOffset
|
|
130
|
+
: triggerRect.left - contentWidth - sideOffset;
|
|
131
|
+
|
|
132
|
+
if (placement.align === "start") {
|
|
133
|
+
top = triggerRect.top;
|
|
134
|
+
} else if (placement.align === "end") {
|
|
135
|
+
top = triggerRect.bottom - contentHeight;
|
|
136
|
+
} else {
|
|
137
|
+
top = triggerRect.top + (triggerRect.height - contentHeight) / 2;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return { top, left };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function clampPositionToViewport(
|
|
145
|
+
position: Position,
|
|
146
|
+
contentWidth: number,
|
|
147
|
+
contentHeight: number,
|
|
148
|
+
viewportWidth: number,
|
|
149
|
+
viewportHeight: number,
|
|
150
|
+
viewportPadding: number,
|
|
151
|
+
): Position {
|
|
152
|
+
const maxLeft = Math.max(viewportPadding, viewportWidth - contentWidth - viewportPadding);
|
|
153
|
+
const maxTop = Math.max(viewportPadding, viewportHeight - contentHeight - viewportPadding);
|
|
154
|
+
|
|
155
|
+
return {
|
|
156
|
+
left: Math.min(Math.max(viewportPadding, position.left), maxLeft),
|
|
157
|
+
top: Math.min(Math.max(viewportPadding, position.top), maxTop),
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function getOverflowAmount(
|
|
162
|
+
position: Position,
|
|
163
|
+
contentWidth: number,
|
|
164
|
+
contentHeight: number,
|
|
165
|
+
viewportWidth: number,
|
|
166
|
+
viewportHeight: number,
|
|
167
|
+
viewportPadding: number,
|
|
168
|
+
): OverflowAmount {
|
|
169
|
+
const right = position.left + contentWidth;
|
|
170
|
+
const bottom = position.top + contentHeight;
|
|
171
|
+
|
|
172
|
+
return {
|
|
173
|
+
left: Math.max(0, viewportPadding - position.left),
|
|
174
|
+
top: Math.max(0, viewportPadding - position.top),
|
|
175
|
+
right: Math.max(0, right - (viewportWidth - viewportPadding)),
|
|
176
|
+
bottom: Math.max(0, bottom - (viewportHeight - viewportPadding)),
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function getMainAxisShift(
|
|
181
|
+
side: FloatingSide,
|
|
182
|
+
preferredPosition: Position,
|
|
183
|
+
clampedPosition: Position,
|
|
184
|
+
): number {
|
|
185
|
+
if (side === "top" || side === "bottom") {
|
|
186
|
+
return Math.abs(clampedPosition.top - preferredPosition.top);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return Math.abs(clampedPosition.left - preferredPosition.left);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function getCrossAxisShift(
|
|
193
|
+
side: FloatingSide,
|
|
194
|
+
preferredPosition: Position,
|
|
195
|
+
clampedPosition: Position,
|
|
196
|
+
): number {
|
|
197
|
+
if (side === "top" || side === "bottom") {
|
|
198
|
+
return Math.abs(clampedPosition.left - preferredPosition.left);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return Math.abs(clampedPosition.top - preferredPosition.top);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Resolves the best side/alignment placement for floating content.
|
|
206
|
+
*/
|
|
207
|
+
export function resolvePlacement(options: ResolvePlacementOptions): ResolvePlacementResult {
|
|
208
|
+
const {
|
|
209
|
+
side,
|
|
210
|
+
align,
|
|
211
|
+
sideOffset,
|
|
212
|
+
triggerRect,
|
|
213
|
+
contentWidth,
|
|
214
|
+
contentHeight,
|
|
215
|
+
viewportWidth,
|
|
216
|
+
viewportHeight,
|
|
217
|
+
viewportPadding,
|
|
218
|
+
avoidCollisions = true,
|
|
219
|
+
} = options;
|
|
220
|
+
|
|
221
|
+
const preferredPlacement: Placement = { side, align };
|
|
222
|
+
|
|
223
|
+
if (!avoidCollisions) {
|
|
224
|
+
const preferredPosition = getPlacementPosition(
|
|
225
|
+
preferredPlacement,
|
|
226
|
+
triggerRect,
|
|
227
|
+
contentWidth,
|
|
228
|
+
contentHeight,
|
|
229
|
+
sideOffset,
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
return {
|
|
233
|
+
side,
|
|
234
|
+
align,
|
|
235
|
+
top: preferredPosition.top,
|
|
236
|
+
left: preferredPosition.left,
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const candidates = getPlacementCandidates(side, align);
|
|
241
|
+
|
|
242
|
+
let bestPlacement = candidates[0] ?? preferredPlacement;
|
|
243
|
+
let bestPosition = clampPositionToViewport(
|
|
244
|
+
getPlacementPosition(bestPlacement, triggerRect, contentWidth, contentHeight, sideOffset),
|
|
245
|
+
contentWidth,
|
|
246
|
+
contentHeight,
|
|
247
|
+
viewportWidth,
|
|
248
|
+
viewportHeight,
|
|
249
|
+
viewportPadding,
|
|
250
|
+
);
|
|
251
|
+
let bestScore = Number.POSITIVE_INFINITY;
|
|
252
|
+
|
|
253
|
+
for (const placement of candidates) {
|
|
254
|
+
const preferredPosition = getPlacementPosition(
|
|
255
|
+
placement,
|
|
256
|
+
triggerRect,
|
|
257
|
+
contentWidth,
|
|
258
|
+
contentHeight,
|
|
259
|
+
sideOffset,
|
|
260
|
+
);
|
|
261
|
+
const clampedPosition = clampPositionToViewport(
|
|
262
|
+
preferredPosition,
|
|
263
|
+
contentWidth,
|
|
264
|
+
contentHeight,
|
|
265
|
+
viewportWidth,
|
|
266
|
+
viewportHeight,
|
|
267
|
+
viewportPadding,
|
|
268
|
+
);
|
|
269
|
+
|
|
270
|
+
const overflow = getOverflowAmount(
|
|
271
|
+
preferredPosition,
|
|
272
|
+
contentWidth,
|
|
273
|
+
contentHeight,
|
|
274
|
+
viewportWidth,
|
|
275
|
+
viewportHeight,
|
|
276
|
+
viewportPadding,
|
|
277
|
+
);
|
|
278
|
+
|
|
279
|
+
const overflowTotal = overflow.left + overflow.right + overflow.top + overflow.bottom;
|
|
280
|
+
const mainAxisShift = getMainAxisShift(placement.side, preferredPosition, clampedPosition);
|
|
281
|
+
const crossAxisShift = getCrossAxisShift(placement.side, preferredPosition, clampedPosition);
|
|
282
|
+
|
|
283
|
+
const sidePenalty = placement.side === side ? 0 : 32;
|
|
284
|
+
const alignPenalty = placement.align === align ? 0 : 8;
|
|
285
|
+
const score =
|
|
286
|
+
overflowTotal * 24 + mainAxisShift * 8 + crossAxisShift * 3 + sidePenalty + alignPenalty;
|
|
287
|
+
|
|
288
|
+
if (score < bestScore) {
|
|
289
|
+
bestScore = score;
|
|
290
|
+
bestPlacement = placement;
|
|
291
|
+
bestPosition = clampedPosition;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
return {
|
|
296
|
+
side: bestPlacement.side,
|
|
297
|
+
align: bestPlacement.align,
|
|
298
|
+
top: bestPosition.top,
|
|
299
|
+
left: bestPosition.left,
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Returns a transform-origin value that matches the resolved placement.
|
|
305
|
+
*/
|
|
306
|
+
export function getTransformOrigin(side: FloatingSide, align: FloatingAlign): string {
|
|
307
|
+
if (side === "top" || side === "bottom") {
|
|
308
|
+
const vertical = side === "bottom" ? "top" : "bottom";
|
|
309
|
+
const horizontal = align === "start" ? "left" : align === "end" ? "right" : "center";
|
|
310
|
+
|
|
311
|
+
return `${horizontal} ${vertical}`;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const horizontal = side === "right" ? "left" : "right";
|
|
315
|
+
const vertical = align === "start" ? "top" : align === "end" ? "bottom" : "center";
|
|
316
|
+
|
|
317
|
+
return `${horizontal} ${vertical}`;
|
|
318
|
+
}
|