(options: { arrowWidth: number; arrowHeight: number })
| 418 | } |
| 419 | |
| 420 | const transformOrigin = (options: { arrowWidth: number; arrowHeight: number }): Middleware => ({ |
| 421 | name: 'transformOrigin', |
| 422 | options, |
| 423 | fn(data) { |
| 424 | const { placement, rects, middlewareData } = data |
| 425 | |
| 426 | const cannotCenterArrow = middlewareData.arrow?.centerOffset !== 0 |
| 427 | const isArrowHidden = cannotCenterArrow |
| 428 | const arrowWidth = isArrowHidden ? 0 : options.arrowWidth |
| 429 | const arrowHeight = isArrowHidden ? 0 : options.arrowHeight |
| 430 | |
| 431 | const [placedSide, placedAlign] = getSideAndAlignFromPlacement(placement) |
| 432 | const noArrowAlign = { start: '0%', center: '50%', end: '100%' }[placedAlign] |
| 433 | |
| 434 | const arrowXCenter = (middlewareData.arrow?.x ?? 0) + arrowWidth / 2 |
| 435 | const arrowYCenter = (middlewareData.arrow?.y ?? 0) + arrowHeight / 2 |
| 436 | |
| 437 | let x = '' |
| 438 | let y = '' |
| 439 | |
| 440 | if (placedSide === 'bottom') { |
| 441 | x = isArrowHidden ? noArrowAlign : `${arrowXCenter}px` |
| 442 | y = `${-arrowHeight}px` |
| 443 | } else if (placedSide === 'top') { |
| 444 | x = isArrowHidden ? noArrowAlign : `${arrowXCenter}px` |
| 445 | y = `${rects.floating.height + arrowHeight}px` |
| 446 | } else if (placedSide === 'right') { |
| 447 | x = `${-arrowHeight}px` |
| 448 | y = isArrowHidden ? noArrowAlign : `${arrowYCenter}px` |
| 449 | } else if (placedSide === 'left') { |
| 450 | x = `${rects.floating.width + arrowHeight}px` |
| 451 | y = isArrowHidden ? noArrowAlign : `${arrowYCenter}px` |
| 452 | } |
| 453 | return { data: { x, y } } |
| 454 | }, |
| 455 | }) |
| 456 | |
| 457 | function getSideAndAlignFromPlacement(placement: Placement) { |
| 458 | const [side, align = 'center'] = placement.split('-') |
no outgoing calls
no test coverage detected
searching dependent graphs…