Skip to content

Commit 7060853

Browse files
[WEB-4748] chore: placement helper fn added and code refactor #7627
Co-authored-by: Sriram Veeraghanta <[email protected]>
1 parent a0f7aca commit 7060853

File tree

3 files changed

+55
-101
lines changed

3 files changed

+55
-101
lines changed

packages/propel/src/popover/root.tsx

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,15 @@
11
import * as React from "react";
22
import { Popover as BasePopover } from "@base-ui-components/react/popover";
3-
4-
// types
5-
export type Placement =
6-
| "auto"
7-
| "auto-start"
8-
| "auto-end"
9-
| "top-start"
10-
| "top-end"
11-
| "bottom-start"
12-
| "bottom-end"
13-
| "right-start"
14-
| "right-end"
15-
| "left-start"
16-
| "left-end"
17-
| "top"
18-
| "bottom"
19-
| "right"
20-
| "left";
21-
22-
type Side = "top" | "bottom" | "left" | "right";
23-
type Align = "start" | "center" | "end";
3+
import { TPlacement, TSide, TAlign, convertPlacementToSideAndAlign } from "../utils/placement";
244

255
export interface PopoverContentProps extends React.ComponentProps<typeof BasePopover.Popup> {
26-
placement?: Placement;
27-
align?: Align;
6+
placement?: TPlacement;
7+
align?: TAlign;
288
sideOffset?: BasePopover.Positioner.Props["sideOffset"];
29-
side?: Side;
9+
side?: TSide;
3010
containerRef?: React.RefObject<HTMLElement>;
3111
}
3212

33-
// placement conversion map
34-
const PLACEMENT_MAP = new Map<Placement, { side: Side; align: Align }>([
35-
["auto", { side: "bottom", align: "center" }],
36-
["auto-start", { side: "bottom", align: "start" }],
37-
["auto-end", { side: "bottom", align: "end" }],
38-
["top", { side: "top", align: "center" }],
39-
["bottom", { side: "bottom", align: "center" }],
40-
["left", { side: "left", align: "center" }],
41-
["right", { side: "right", align: "center" }],
42-
["top-start", { side: "top", align: "start" }],
43-
["top-end", { side: "top", align: "end" }],
44-
["bottom-start", { side: "bottom", align: "start" }],
45-
["bottom-end", { side: "bottom", align: "end" }],
46-
["left-start", { side: "left", align: "start" }],
47-
["left-end", { side: "left", align: "end" }],
48-
["right-start", { side: "right", align: "start" }],
49-
["right-end", { side: "right", align: "end" }],
50-
]);
51-
52-
// conversion function
53-
export function convertPlacementToSideAndAlign(placement: Placement): {
54-
side: Side;
55-
align: Align;
56-
} {
57-
return PLACEMENT_MAP.get(placement) || { side: "bottom", align: "center" };
58-
}
59-
6013
// PopoverContent component
6114
const PopoverContent = React.memo<PopoverContentProps>(function PopoverContent({
6215
children,

packages/propel/src/tooltip/root.tsx

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,24 @@
11
import * as React from "react";
22
import { Tooltip as BaseTooltip } from "@base-ui-components/react/tooltip";
33
import { cn } from "@plane/utils";
4-
5-
export type TPosition =
6-
| "top"
7-
| "right"
8-
| "bottom"
9-
| "left"
10-
| "auto"
11-
| "auto-end"
12-
| "auto-start"
13-
| "bottom-start"
14-
| "bottom-end"
15-
| "left-start"
16-
| "left-end"
17-
| "right-start"
18-
| "right-end"
19-
| "top-start"
20-
| "top-end";
21-
22-
type Side = "top" | "bottom" | "left" | "right";
23-
type Align = "start" | "center" | "end";
24-
25-
// placement conversion map
26-
const PLACEMENT_MAP = new Map<TPosition, { side: Side; align: Align }>([
27-
["auto", { side: "bottom", align: "center" }],
28-
["auto-start", { side: "bottom", align: "start" }],
29-
["auto-end", { side: "bottom", align: "end" }],
30-
["top", { side: "top", align: "center" }],
31-
["bottom", { side: "bottom", align: "center" }],
32-
["left", { side: "left", align: "center" }],
33-
["right", { side: "right", align: "center" }],
34-
["top-start", { side: "top", align: "start" }],
35-
["top-end", { side: "top", align: "end" }],
36-
["bottom-start", { side: "bottom", align: "start" }],
37-
["bottom-end", { side: "bottom", align: "end" }],
38-
["left-start", { side: "left", align: "start" }],
39-
["left-end", { side: "left", align: "end" }],
40-
["right-start", { side: "right", align: "start" }],
41-
["right-end", { side: "right", align: "end" }],
42-
]);
4+
import { TPlacement, TSide, TAlign, convertPlacementToSideAndAlign } from "../utils/placement";
435

446
type ITooltipProps = {
457
tooltipHeading?: string;
468
tooltipContent: string | React.ReactNode;
47-
position?: TPosition;
9+
position?: TPlacement;
4810
children: React.ReactElement;
4911
disabled?: boolean;
5012
className?: string;
5113
openDelay?: number;
5214
closeDelay?: number;
5315
isMobile?: boolean;
5416
renderByDefault?: boolean;
55-
side?: Side;
56-
align?: Align;
17+
side?: TSide;
18+
align?: TAlign;
5719
sideOffset?: number;
5820
};
5921

60-
// conversion function
61-
export function convertPlacementToSideAndAlign(placement: TPosition): {
62-
side: Side;
63-
align: Align;
64-
} {
65-
return PLACEMENT_MAP.get(placement) || { side: "bottom", align: "center" };
66-
}
67-
6822
export function Tooltip(props: ITooltipProps) {
6923
const {
7024
tooltipHeading,
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// types
2+
export type TPlacement =
3+
| "auto"
4+
| "auto-start"
5+
| "auto-end"
6+
| "top-start"
7+
| "top-end"
8+
| "bottom-start"
9+
| "bottom-end"
10+
| "right-start"
11+
| "right-end"
12+
| "left-start"
13+
| "left-end"
14+
| "top"
15+
| "bottom"
16+
| "right"
17+
| "left";
18+
19+
export type TSide = "top" | "bottom" | "left" | "right";
20+
export type TAlign = "start" | "center" | "end";
21+
22+
// placement conversion map
23+
const PLACEMENT_MAP = new Map<TPlacement, { side: TSide; align: TAlign }>([
24+
["auto", { side: "bottom", align: "center" }],
25+
["auto-start", { side: "bottom", align: "start" }],
26+
["auto-end", { side: "bottom", align: "end" }],
27+
["top", { side: "top", align: "center" }],
28+
["bottom", { side: "bottom", align: "center" }],
29+
["left", { side: "left", align: "center" }],
30+
["right", { side: "right", align: "center" }],
31+
["top-start", { side: "top", align: "start" }],
32+
["top-end", { side: "top", align: "end" }],
33+
["bottom-start", { side: "bottom", align: "start" }],
34+
["bottom-end", { side: "bottom", align: "end" }],
35+
["left-start", { side: "left", align: "start" }],
36+
["left-end", { side: "left", align: "end" }],
37+
["right-start", { side: "right", align: "start" }],
38+
["right-end", { side: "right", align: "end" }],
39+
]);
40+
41+
// conversion function
42+
export function convertPlacementToSideAndAlign(placement: TPlacement): {
43+
side: TSide;
44+
align: TAlign;
45+
} {
46+
return PLACEMENT_MAP.get(placement) || { side: "bottom", align: "center" };
47+
}

0 commit comments

Comments
 (0)