dropdownButtonV2.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import {forwardRef} from 'react';
  2. import styled from '@emotion/styled';
  3. import Button, {ButtonProps} from 'sentry/components/button';
  4. import {IconChevron} from 'sentry/icons';
  5. import space from 'sentry/styles/space';
  6. export type DropdownButtonProps = {
  7. /**
  8. * Whether or not the button should render as open
  9. */
  10. isOpen?: boolean;
  11. /**
  12. * The fixed prefix text to show in the button eg: 'Sort By'
  13. */
  14. prefix?: React.ReactNode;
  15. /**
  16. * Should a chevron icon be shown?
  17. */
  18. showChevron?: boolean;
  19. } & Omit<ButtonProps, 'type' | 'prefix'>;
  20. const DropdownButton = forwardRef<
  21. React.RefObject<HTMLElement> | null,
  22. DropdownButtonProps
  23. >(
  24. (
  25. {
  26. children,
  27. prefix,
  28. isOpen = false,
  29. showChevron = true,
  30. disabled = false,
  31. priority = 'form',
  32. ...props
  33. }: DropdownButtonProps,
  34. ref
  35. ) => (
  36. <StyledButton
  37. {...props}
  38. type="button"
  39. disabled={disabled}
  40. priority={priority}
  41. isOpen={isOpen}
  42. ref={ref}
  43. >
  44. {prefix && <LabelText>{prefix}</LabelText>}
  45. {children}
  46. {showChevron && (
  47. <StyledChevron
  48. size="10px"
  49. direction={isOpen ? 'up' : 'down'}
  50. aria-hidden="true"
  51. />
  52. )}
  53. </StyledButton>
  54. )
  55. );
  56. const StyledChevron = styled(IconChevron)`
  57. margin-left: ${space(0.75)};
  58. `;
  59. const StyledButton = styled(Button)<
  60. Required<Pick<DropdownButtonProps, 'isOpen' | 'disabled' | 'priority'>>
  61. >`
  62. position: relative;
  63. z-index: 2;
  64. ${p => (p.isOpen || p.disabled) && 'box-shadow: none;'}
  65. `;
  66. const LabelText = styled('span')`
  67. &:after {
  68. content: ':';
  69. }
  70. font-weight: 400;
  71. padding-right: ${space(0.75)};
  72. `;
  73. export default DropdownButton;