Arrow.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import classnames from 'classnames';
  4. import { get } from 'lodash';
  5. import { numbers, cssClasses, strings } from '@douyinfe/semi-foundation/popover/constants';
  6. export interface ArrowProps {
  7. position?: string;
  8. className?: string;
  9. arrowStyle?: React.CSSProperties;
  10. popStyle?: React.CSSProperties;
  11. }
  12. const Arrow: React.FC<ArrowProps> = (props = {}) => {
  13. const { position = '', className, arrowStyle, popStyle, ...rest } = props;
  14. const isVertical = position.indexOf('top') === 0 || position.indexOf('bottom') === 0;
  15. const cls = classnames(className, cssClasses.ARROW);
  16. const borderOpacity = get(arrowStyle, 'borderOpacity', strings.DEFAULT_ARROW_STYLE.borderOpacity);
  17. const bgColor = get(
  18. arrowStyle,
  19. 'backgroundColor',
  20. get(popStyle, 'backgroundColor', strings.DEFAULT_ARROW_STYLE.backgroundColor)
  21. );
  22. const borderColor = get(
  23. arrowStyle,
  24. 'borderColor',
  25. get(popStyle, 'borderColor', strings.DEFAULT_ARROW_STYLE.borderColor)
  26. );
  27. const wrapProps = {
  28. ...rest,
  29. width: numbers.ARROW_BOUNDING.width,
  30. height: numbers.ARROW_BOUNDING.height,
  31. xmlns: 'http://www.w3.org/2000/svg',
  32. className: cls,
  33. };
  34. return isVertical ? (
  35. <svg {...wrapProps}>
  36. <path
  37. d="M0 0.5L0 1.5C4 1.5, 5.5 3, 7.5 5S10,8 12,8S14.5 7, 16.5 5S20,1.5 24,1.5L24 0.5L0 0.5z"
  38. fill={borderColor}
  39. opacity={borderOpacity}
  40. />
  41. <path d="M0 0L0 1C4 1, 5.5 2, 7.5 4S10,7 12,7S14.5 6, 16.5 4S20,1 24,1L24 0L0 0z" fill={bgColor} />
  42. </svg>
  43. ) : (
  44. <svg {...wrapProps}>
  45. <path
  46. d="M0.5 0L1.5 0C1.5 4, 3 5.5, 5 7.5S8,10 8,12S7 14.5, 5 16.5S1.5,20 1.5,24L0.5 24L0.5 0z"
  47. fill={borderColor}
  48. opacity={borderOpacity}
  49. />
  50. <path d="M0 0L1 0C1 4, 2 5.5, 4 7.5S7,10 7,12S6 14.5, 4 16.5S1,20 1,24L0 24L0 0z" fill={bgColor} />
  51. </svg>
  52. );
  53. };
  54. Arrow.propTypes = {
  55. position: PropTypes.string,
  56. className: PropTypes.string,
  57. arrowStyle: PropTypes.object,
  58. popStyle: PropTypes.object,
  59. };
  60. export default Arrow;