|
| 1 | +import classNames from 'classnames' |
| 2 | +import isEqual from 'lodash/isEqual' |
| 3 | +import { ComponentChildren, h, JSX, RefObject } from 'preact' |
| 4 | +import { useCallback, useEffect, useState } from 'preact/hooks' |
| 5 | + |
| 6 | +import * as styles from './styles.css' |
| 7 | + |
| 8 | +export const DropInner = Inner |
| 9 | + |
| 10 | +export interface DropArea { |
| 11 | + x: number |
| 12 | + y: number |
| 13 | + width: number |
| 14 | + height: number |
| 15 | +} |
| 16 | + |
| 17 | +export type DropPosition = 'above' | 'below' | 'left' | 'right' |
| 18 | + |
| 19 | +export interface DropProps { |
| 20 | + children: ComponentChildren |
| 21 | + area: DropArea |
| 22 | + ghost?: boolean |
| 23 | + innerRef?: RefObject<HTMLDivElement | undefined> |
| 24 | + onClick?: JSX.MouseEventHandler<any> |
| 25 | + preferPosition?: DropPosition |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Floating popup, used for dropdown menus, tooltips, etc. |
| 30 | + */ |
| 31 | +export function Drop({ |
| 32 | + area, |
| 33 | + children, |
| 34 | + ghost, |
| 35 | + innerRef, |
| 36 | + onClick, |
| 37 | + preferPosition, |
| 38 | +}: DropProps) { |
| 39 | + const [trianglePosition, setTrianglePosition] = useState<TrianglePosition>( |
| 40 | + 'below' |
| 41 | + ) |
| 42 | + |
| 43 | + const [wrapper, setWrapper] = useState<HTMLDivElement | undefined>(undefined) |
| 44 | + const [wrapperArea, setWrapperArea] = useState<DropArea | undefined>( |
| 45 | + undefined |
| 46 | + ) |
| 47 | + |
| 48 | + useEffect(() => { |
| 49 | + if (!wrapper || !wrapperArea?.width) return |
| 50 | + |
| 51 | + const windowWidth = window.innerWidth |
| 52 | + const windowHeight = window.innerHeight |
| 53 | + const width = wrapperArea.width |
| 54 | + const height = wrapperArea.height |
| 55 | + |
| 56 | + wrapper.style.opacity = '1' |
| 57 | + |
| 58 | + let verticalLeft = area.x + area.width / 2 - width / 2 |
| 59 | + if (verticalLeft < 0) verticalLeft = 0 |
| 60 | + else if (verticalLeft + width > windowWidth) |
| 61 | + verticalLeft = windowWidth - width |
| 62 | + |
| 63 | + let horizontalTop = area.y + area.height / 2 - height / 2 |
| 64 | + if (horizontalTop < 0) horizontalTop = 0 |
| 65 | + else if (horizontalTop + height > windowHeight) |
| 66 | + horizontalTop = windowHeight - height |
| 67 | + |
| 68 | + const positioning = { |
| 69 | + below: () => { |
| 70 | + const belowTop = area.y + area.height |
| 71 | + if (belowTop + height < windowHeight) { |
| 72 | + wrapper.style.top = `${belowTop}px` |
| 73 | + wrapper.style.left = `${verticalLeft}px` |
| 74 | + setTrianglePosition('above') |
| 75 | + return true |
| 76 | + } |
| 77 | + }, |
| 78 | + |
| 79 | + above: () => { |
| 80 | + const aboveTop = area.y - height |
| 81 | + if (aboveTop > 0) { |
| 82 | + wrapper.style.top = `${aboveTop}px` |
| 83 | + wrapper.style.left = `${verticalLeft}px` |
| 84 | + setTrianglePosition('below') |
| 85 | + return true |
| 86 | + } |
| 87 | + }, |
| 88 | + |
| 89 | + left: () => { |
| 90 | + const left = area.x - width |
| 91 | + if (left > 0) { |
| 92 | + wrapper.style.top = `${horizontalTop}px` |
| 93 | + wrapper.style.left = `${left}px` |
| 94 | + setTrianglePosition('right') |
| 95 | + return true |
| 96 | + } |
| 97 | + }, |
| 98 | + |
| 99 | + right: () => { |
| 100 | + const right = area.x + area.width |
| 101 | + if (right + width < windowWidth) { |
| 102 | + wrapper.style.top = `${horizontalTop}px` |
| 103 | + wrapper.style.left = `${right}px` |
| 104 | + setTrianglePosition('left') |
| 105 | + return true |
| 106 | + } |
| 107 | + }, |
| 108 | + } |
| 109 | + |
| 110 | + if (preferPosition === 'left') { |
| 111 | + circlePositioning([positioning.left, positioning.right]) |
| 112 | + } else if (preferPosition === 'right') { |
| 113 | + circlePositioning([positioning.right, positioning.left]) |
| 114 | + } else if (preferPosition === 'above') { |
| 115 | + circlePositioning([positioning.above, positioning.below]) |
| 116 | + } else { |
| 117 | + circlePositioning([positioning.below, positioning.above]) |
| 118 | + } |
| 119 | + }, [ |
| 120 | + trianglePosition, |
| 121 | + wrapper, |
| 122 | + wrapperArea?.width, |
| 123 | + wrapperArea?.height, |
| 124 | + area.x, |
| 125 | + area.width, |
| 126 | + area.y, |
| 127 | + area.height, |
| 128 | + preferPosition, |
| 129 | + ]) |
| 130 | + |
| 131 | + const wrapperCallback = useCallback( |
| 132 | + (el: HTMLDivElement | null) => { |
| 133 | + if (!el) return |
| 134 | + if (innerRef) innerRef.current = el |
| 135 | + setWrapper(el) |
| 136 | + }, |
| 137 | + [innerRef] |
| 138 | + ) |
| 139 | + |
| 140 | + useEffect(() => { |
| 141 | + const newArea = wrapper?.getBoundingClientRect() |
| 142 | + if (!isEqual(newArea, wrapperArea)) setWrapperArea(newArea) |
| 143 | + }) |
| 144 | + |
| 145 | + return ( |
| 146 | + <div |
| 147 | + class={classNames( |
| 148 | + styles.drop, |
| 149 | + ghost && styles.ghost, |
| 150 | + ['above', 'below'].includes(trianglePosition) && styles.vertical |
| 151 | + )} |
| 152 | + ref={wrapperCallback} |
| 153 | + onClick={onClick} |
| 154 | + > |
| 155 | + {(trianglePosition === 'above' || trianglePosition === 'left') && ( |
| 156 | + <Triangle position={trianglePosition} /> |
| 157 | + )} |
| 158 | + |
| 159 | + <Inner> |
| 160 | + {typeof children === 'string' ? ( |
| 161 | + <div class={styles.string}>{children}</div> |
| 162 | + ) : ( |
| 163 | + children |
| 164 | + )} |
| 165 | + </Inner> |
| 166 | + |
| 167 | + {(trianglePosition === 'below' || trianglePosition === 'right') && ( |
| 168 | + <Triangle position={trianglePosition} /> |
| 169 | + )} |
| 170 | + </div> |
| 171 | + ) |
| 172 | +} |
| 173 | + |
| 174 | +function circlePositioning(fns: Array<() => void | boolean>) { |
| 175 | + for (let i = 0; i < fns.length; i++) { |
| 176 | + if (fns[i]?.()) return |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +export type TrianglePosition = 'above' | 'below' | 'left' | 'right' |
| 181 | + |
| 182 | +interface TriangleProps { |
| 183 | + position: TrianglePosition |
| 184 | +} |
| 185 | + |
| 186 | +function Triangle(position: TriangleProps) { |
| 187 | + return ( |
| 188 | + <div |
| 189 | + class={classNames(styles.triangle, styles.position[position.position])} |
| 190 | + ></div> |
| 191 | + ) |
| 192 | +} |
| 193 | + |
| 194 | +interface InnerProps { |
| 195 | + children: ComponentChildren |
| 196 | +} |
| 197 | + |
| 198 | +function Inner({ children }: InnerProps) { |
| 199 | + return <div class={classNames(styles.inner)}>{children} </div> |
| 200 | +} |
0 commit comments