File size: 2,925 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use client"

import { forwardRef, useMemo } from "react"
import {
  type ConditionalValue,
  type HTMLChakraProps,
  type SystemStyleObject,
  chakra,
} from "../../styled-system"
import { mapObject } from "../../utils"

type Dict = Record<string, any>

export interface FloatOptions {
  /**
   * The x offset of the indicator
   */
  offsetX?: SystemStyleObject["left"] | undefined
  /**
   * The y offset of the indicator
   */
  offsetY?: SystemStyleObject["top"] | undefined
  /**
   * The x and y offset of the indicator
   */
  offset?: SystemStyleObject["top"] | undefined
  /**
   * The placement of the indicator
   * @default "top-end"
   */
  placement?:
    | ConditionalValue<
        | "bottom-end"
        | "bottom-start"
        | "top-end"
        | "top-start"
        | "bottom-center"
        | "top-center"
        | "middle-center"
        | "middle-end"
        | "middle-start"
      >
    | undefined
}

export interface FloatProps
  extends Omit<HTMLChakraProps<"div">, keyof FloatOptions>,
    FloatOptions {}

export const Float = forwardRef<HTMLDivElement, FloatProps>(
  function Float(props, ref) {
    const {
      offsetX,
      offsetY,
      offset = "0",
      placement = "top-end",
      ...rest
    } = props

    const styles: SystemStyleObject = useMemo(
      () => ({
        display: "inline-flex",
        justifyContent: "center",
        alignItems: "center",
        position: "absolute",
        insetBlockStart: mapObject(placement, (v) => {
          const [side] = v.split("-")
          const map: Dict = {
            top: offsetY ?? offset,
            middle: "50%",
            bottom: "auto",
          }
          return map[side]
        }),
        insetBlockEnd: mapObject(placement, (v) => {
          const [side] = v.split("-")
          const map: Dict = {
            top: "auto",
            middle: "50%",
            bottom: offsetY ?? offset,
          }
          return map[side]
        }),
        insetStart: mapObject(placement, (v) => {
          const [, align] = v.split("-")
          const map: Dict = {
            start: offsetX ?? offset,
            center: "50%",
            end: "auto",
          }
          return map[align]
        }),
        insetEnd: mapObject(placement, (v) => {
          const [, align] = v.split("-")
          const map: Dict = {
            start: "auto",
            center: "50%",
            end: offsetX ?? offset,
          }
          return map[align]
        }),
        translate: mapObject(placement, (v) => {
          const [side, align] = v.split("-")
          const mapX: Dict = { start: "-50%", center: "-50%", end: "50%" }
          const mapY: Dict = { top: "-50%", middle: "-50%", bottom: "50%" }
          return `${mapX[align]} ${mapY[side]}`
        }),
      }),
      [offset, offsetX, offsetY, placement],
    )

    return <chakra.div ref={ref} css={styles} {...rest} />
  },
)