dropdownButtonV2.tsx 1.7 KB

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