[WEB-4748] chore: placement helper fn added and code refactor #7627
Co-authored-by: Sriram Veeraghanta <veeraghanta.sriram@gmail.com>
This commit is contained in:
parent
a0f7acae42
commit
706085395e
3 changed files with 55 additions and 101 deletions
|
|
@ -1,62 +1,15 @@
|
||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
import { Popover as BasePopover } from "@base-ui-components/react/popover";
|
import { Popover as BasePopover } from "@base-ui-components/react/popover";
|
||||||
|
import { TPlacement, TSide, TAlign, convertPlacementToSideAndAlign } from "../utils/placement";
|
||||||
// types
|
|
||||||
export type Placement =
|
|
||||||
| "auto"
|
|
||||||
| "auto-start"
|
|
||||||
| "auto-end"
|
|
||||||
| "top-start"
|
|
||||||
| "top-end"
|
|
||||||
| "bottom-start"
|
|
||||||
| "bottom-end"
|
|
||||||
| "right-start"
|
|
||||||
| "right-end"
|
|
||||||
| "left-start"
|
|
||||||
| "left-end"
|
|
||||||
| "top"
|
|
||||||
| "bottom"
|
|
||||||
| "right"
|
|
||||||
| "left";
|
|
||||||
|
|
||||||
type Side = "top" | "bottom" | "left" | "right";
|
|
||||||
type Align = "start" | "center" | "end";
|
|
||||||
|
|
||||||
export interface PopoverContentProps extends React.ComponentProps<typeof BasePopover.Popup> {
|
export interface PopoverContentProps extends React.ComponentProps<typeof BasePopover.Popup> {
|
||||||
placement?: Placement;
|
placement?: TPlacement;
|
||||||
align?: Align;
|
align?: TAlign;
|
||||||
sideOffset?: BasePopover.Positioner.Props["sideOffset"];
|
sideOffset?: BasePopover.Positioner.Props["sideOffset"];
|
||||||
side?: Side;
|
side?: TSide;
|
||||||
containerRef?: React.RefObject<HTMLElement>;
|
containerRef?: React.RefObject<HTMLElement>;
|
||||||
}
|
}
|
||||||
|
|
||||||
// placement conversion map
|
|
||||||
const PLACEMENT_MAP = new Map<Placement, { side: Side; align: Align }>([
|
|
||||||
["auto", { side: "bottom", align: "center" }],
|
|
||||||
["auto-start", { side: "bottom", align: "start" }],
|
|
||||||
["auto-end", { side: "bottom", align: "end" }],
|
|
||||||
["top", { side: "top", align: "center" }],
|
|
||||||
["bottom", { side: "bottom", align: "center" }],
|
|
||||||
["left", { side: "left", align: "center" }],
|
|
||||||
["right", { side: "right", align: "center" }],
|
|
||||||
["top-start", { side: "top", align: "start" }],
|
|
||||||
["top-end", { side: "top", align: "end" }],
|
|
||||||
["bottom-start", { side: "bottom", align: "start" }],
|
|
||||||
["bottom-end", { side: "bottom", align: "end" }],
|
|
||||||
["left-start", { side: "left", align: "start" }],
|
|
||||||
["left-end", { side: "left", align: "end" }],
|
|
||||||
["right-start", { side: "right", align: "start" }],
|
|
||||||
["right-end", { side: "right", align: "end" }],
|
|
||||||
]);
|
|
||||||
|
|
||||||
// conversion function
|
|
||||||
export function convertPlacementToSideAndAlign(placement: Placement): {
|
|
||||||
side: Side;
|
|
||||||
align: Align;
|
|
||||||
} {
|
|
||||||
return PLACEMENT_MAP.get(placement) || { side: "bottom", align: "center" };
|
|
||||||
}
|
|
||||||
|
|
||||||
// PopoverContent component
|
// PopoverContent component
|
||||||
const PopoverContent = React.memo<PopoverContentProps>(function PopoverContent({
|
const PopoverContent = React.memo<PopoverContentProps>(function PopoverContent({
|
||||||
children,
|
children,
|
||||||
|
|
|
||||||
|
|
@ -1,50 +1,12 @@
|
||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
import { Tooltip as BaseTooltip } from "@base-ui-components/react/tooltip";
|
import { Tooltip as BaseTooltip } from "@base-ui-components/react/tooltip";
|
||||||
import { cn } from "@plane/utils";
|
import { cn } from "@plane/utils";
|
||||||
|
import { TPlacement, TSide, TAlign, convertPlacementToSideAndAlign } from "../utils/placement";
|
||||||
export type TPosition =
|
|
||||||
| "top"
|
|
||||||
| "right"
|
|
||||||
| "bottom"
|
|
||||||
| "left"
|
|
||||||
| "auto"
|
|
||||||
| "auto-end"
|
|
||||||
| "auto-start"
|
|
||||||
| "bottom-start"
|
|
||||||
| "bottom-end"
|
|
||||||
| "left-start"
|
|
||||||
| "left-end"
|
|
||||||
| "right-start"
|
|
||||||
| "right-end"
|
|
||||||
| "top-start"
|
|
||||||
| "top-end";
|
|
||||||
|
|
||||||
type Side = "top" | "bottom" | "left" | "right";
|
|
||||||
type Align = "start" | "center" | "end";
|
|
||||||
|
|
||||||
// placement conversion map
|
|
||||||
const PLACEMENT_MAP = new Map<TPosition, { side: Side; align: Align }>([
|
|
||||||
["auto", { side: "bottom", align: "center" }],
|
|
||||||
["auto-start", { side: "bottom", align: "start" }],
|
|
||||||
["auto-end", { side: "bottom", align: "end" }],
|
|
||||||
["top", { side: "top", align: "center" }],
|
|
||||||
["bottom", { side: "bottom", align: "center" }],
|
|
||||||
["left", { side: "left", align: "center" }],
|
|
||||||
["right", { side: "right", align: "center" }],
|
|
||||||
["top-start", { side: "top", align: "start" }],
|
|
||||||
["top-end", { side: "top", align: "end" }],
|
|
||||||
["bottom-start", { side: "bottom", align: "start" }],
|
|
||||||
["bottom-end", { side: "bottom", align: "end" }],
|
|
||||||
["left-start", { side: "left", align: "start" }],
|
|
||||||
["left-end", { side: "left", align: "end" }],
|
|
||||||
["right-start", { side: "right", align: "start" }],
|
|
||||||
["right-end", { side: "right", align: "end" }],
|
|
||||||
]);
|
|
||||||
|
|
||||||
type ITooltipProps = {
|
type ITooltipProps = {
|
||||||
tooltipHeading?: string;
|
tooltipHeading?: string;
|
||||||
tooltipContent: string | React.ReactNode;
|
tooltipContent: string | React.ReactNode;
|
||||||
position?: TPosition;
|
position?: TPlacement;
|
||||||
children: React.ReactElement;
|
children: React.ReactElement;
|
||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
className?: string;
|
className?: string;
|
||||||
|
|
@ -52,19 +14,11 @@ type ITooltipProps = {
|
||||||
closeDelay?: number;
|
closeDelay?: number;
|
||||||
isMobile?: boolean;
|
isMobile?: boolean;
|
||||||
renderByDefault?: boolean;
|
renderByDefault?: boolean;
|
||||||
side?: Side;
|
side?: TSide;
|
||||||
align?: Align;
|
align?: TAlign;
|
||||||
sideOffset?: number;
|
sideOffset?: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
// conversion function
|
|
||||||
export function convertPlacementToSideAndAlign(placement: TPosition): {
|
|
||||||
side: Side;
|
|
||||||
align: Align;
|
|
||||||
} {
|
|
||||||
return PLACEMENT_MAP.get(placement) || { side: "bottom", align: "center" };
|
|
||||||
}
|
|
||||||
|
|
||||||
export function Tooltip(props: ITooltipProps) {
|
export function Tooltip(props: ITooltipProps) {
|
||||||
const {
|
const {
|
||||||
tooltipHeading,
|
tooltipHeading,
|
||||||
|
|
|
||||||
47
packages/propel/src/utils/placement.ts
Normal file
47
packages/propel/src/utils/placement.ts
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
// types
|
||||||
|
export type TPlacement =
|
||||||
|
| "auto"
|
||||||
|
| "auto-start"
|
||||||
|
| "auto-end"
|
||||||
|
| "top-start"
|
||||||
|
| "top-end"
|
||||||
|
| "bottom-start"
|
||||||
|
| "bottom-end"
|
||||||
|
| "right-start"
|
||||||
|
| "right-end"
|
||||||
|
| "left-start"
|
||||||
|
| "left-end"
|
||||||
|
| "top"
|
||||||
|
| "bottom"
|
||||||
|
| "right"
|
||||||
|
| "left";
|
||||||
|
|
||||||
|
export type TSide = "top" | "bottom" | "left" | "right";
|
||||||
|
export type TAlign = "start" | "center" | "end";
|
||||||
|
|
||||||
|
// placement conversion map
|
||||||
|
const PLACEMENT_MAP = new Map<TPlacement, { side: TSide; align: TAlign }>([
|
||||||
|
["auto", { side: "bottom", align: "center" }],
|
||||||
|
["auto-start", { side: "bottom", align: "start" }],
|
||||||
|
["auto-end", { side: "bottom", align: "end" }],
|
||||||
|
["top", { side: "top", align: "center" }],
|
||||||
|
["bottom", { side: "bottom", align: "center" }],
|
||||||
|
["left", { side: "left", align: "center" }],
|
||||||
|
["right", { side: "right", align: "center" }],
|
||||||
|
["top-start", { side: "top", align: "start" }],
|
||||||
|
["top-end", { side: "top", align: "end" }],
|
||||||
|
["bottom-start", { side: "bottom", align: "start" }],
|
||||||
|
["bottom-end", { side: "bottom", align: "end" }],
|
||||||
|
["left-start", { side: "left", align: "start" }],
|
||||||
|
["left-end", { side: "left", align: "end" }],
|
||||||
|
["right-start", { side: "right", align: "start" }],
|
||||||
|
["right-end", { side: "right", align: "end" }],
|
||||||
|
]);
|
||||||
|
|
||||||
|
// conversion function
|
||||||
|
export function convertPlacementToSideAndAlign(placement: TPlacement): {
|
||||||
|
side: TSide;
|
||||||
|
align: TAlign;
|
||||||
|
} {
|
||||||
|
return PLACEMENT_MAP.get(placement) || { side: "bottom", align: "center" };
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue