File size: 5,125 Bytes
d530f14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { animate, AnimatePresence, cubicBezier, motion } from "motion/react";
import { useEffect, useMemo, useRef, useState } from "react";

import { cn } from "@/utils/cn";
import { lockBody } from "../lockBody";
import PortalToBody from "../utils/portal-to-body";

export default function Combobox({
  placeholder,
  options,
  value,
  onChange,
  className,
}: {
  placeholder?: string;
  options: { label: string; value: string }[];
  value: string;
  onChange: (value: string) => void;
  className?: string;
}) {
  const selected = useMemo(() => {
    return options.find((option) => option.value === value);
  }, [options, value]);
  const [isOpen, setIsOpen] = useState(false);
  const [bounds, setBounds] = useState<DOMRect | null>(null);
  const ref = useRef<HTMLDivElement>(null);

  useEffect(() => {
    lockBody("combobox", isOpen);
  }, [isOpen]);

  useEffect(() => {
    document.addEventListener("click", (e) => {
      if (ref.current && e.composedPath().includes(ref.current)) {
        return;
      }

      setIsOpen(false);
    });
  }, []);

  return (
    <div className={cn("w-full", className)} ref={ref}>
      <button
        className={cn(
          "relative bg-accent-white flex w-full gap-4 rounded-8 p-6 pl-10",
          "inside-border before:border-black-alpha-8 hover:before:border-black-alpha-12 hover:bg-black-alpha-2",
          "text-body-medium",
          isOpen &&
            "!bg-accent-white before:!border-heat-100 before:!border-[1.25px]",
        )}
        type="button"
        onClick={(e) => {
          e.preventDefault();
          setIsOpen(!isOpen);
          setBounds(ref.current?.getBoundingClientRect() ?? null);
        }}
      >
        <div className={cn("flex-1", !selected && "text-black-alpha-40")}>
          {selected?.label || placeholder}
        </div>

        <motion.svg
          animate={{ rotate: isOpen ? 180 : 0 }}
          fill="none"
          height="20"
          viewBox="0 0 20 20"
          width="20"
          xmlns="http://www.w3.org/2000/svg"
        >
          <path
            d="M7 8.5L10 11.5L13 8.5"
            stroke="#262626"
            strokeLinecap="round"
            strokeLinejoin="round"
            strokeOpacity="0.56"
            strokeWidth="1.25"
          />
        </motion.svg>
      </button>

      <PortalToBody>
        <AnimatePresence initial={false}>
          {isOpen && bounds && (
            <motion.div
              animate={{ opacity: 1, y: 0, filter: "blur(0px)" }}
              className="fixed bg-accent-white rounded-12 z-[401]"
              exit={{ opacity: 0, y: 0, filter: "blur(4px)" }}
              initial={{ opacity: 0, y: -12, filter: "blur(4px)" }}
              style={{
                top: bounds.top + bounds.height + 8,
                left: bounds.left,
                width: bounds.width,
                boxShadow:
                  "0px 32px 40px 6px rgba(0, 0, 0, 0.02), 0px 12px 32px 0px rgba(0, 0, 0, 0.02), 0px 24px 32px -8px rgba(0, 0, 0, 0.02), 0px 8px 16px -2px rgba(0, 0, 0, 0.02), 0px 0px 0px 1px rgba(0, 0, 0, 0.04)",
              }}
              transition={{ duration: 0.2 }}
            >
              <div className="p-4">
                <Items
                  options={options}
                  onChange={(value) => {
                    onChange(value);
                    setIsOpen(false);
                  }}
                />
              </div>
            </motion.div>
          )}
        </AnimatePresence>
      </PortalToBody>
    </div>
  );
}

const Items = ({
  options,
  onChange,
}: {
  options: { label: string; value: string }[];
  onChange: (value: string) => void;
}) => {
  const backgroundRef = useRef<HTMLDivElement>(null);

  return (
    <div className="relative">
      <div
        className="absolute top-0 opacity-0 left-0 bg-black-alpha-4 rounded-8 w-full h-32 pointer-events-none"
        ref={backgroundRef}
      />

      {options.map((option) => (
        <button
          className="w-full group py-6 px-10 text-label-small"
          key={option.value}
          type="button"
          onClick={() => {
            onChange(option.value);
          }}
          onMouseEnter={(e) => {
            const t = e.target as HTMLElement;

            let target =
              t instanceof HTMLButtonElement
                ? t
                : (t.closest("button") as HTMLButtonElement);
            target = target.closest(".group") as HTMLButtonElement;

            animate(backgroundRef.current!, { scale: 0.995 }).then(() =>
              animate(backgroundRef.current!, { scale: 1 }),
            );

            animate(
              backgroundRef.current!,
              {
                y: target.offsetTop,
                opacity: 1,
              },
              {
                ease: cubicBezier(0.165, 0.84, 0.44, 1),
                duration: 0.2,
              },
            );
          }}
          onMouseLeave={() => {
            animate(backgroundRef.current!, { opacity: 0 });
          }}
        >
          {option.label}
        </button>
      ))}
    </div>
  );
};