TabButton.jsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // styling
  2. import styled from 'styled-components/macro';
  3. import theme from 'styled-theming';
  4. // hooks
  5. import {useThemeProvider} from '@contexts/themeContext';
  6. // utils
  7. import classNames from 'classnames';
  8. import PropTypes from 'prop-types';
  9. const StyledTabButton = styled.button`
  10. border-radius: 0;
  11. position: relative;
  12. background: ${theme('theme', {
  13. light: 'var(--body)',
  14. dark: 'var(--border)'
  15. })};
  16. &.active, &[aria-selected='true'], &:hover, &:focus {
  17. background: ${theme('theme', {
  18. light: 'var(--widget) !important',
  19. dark: 'var(--accent) !important'
  20. })};
  21. box-shadow: ${theme('theme', {
  22. light: '0 1px 8px rgba(110, 110, 110, 0.1);',
  23. dark: 'none'
  24. })};
  25. }
  26. .title {
  27. position: relative;
  28. z-index: 2;
  29. }
  30. .color {
  31. position: absolute;
  32. z-index: 1;
  33. height: 100%;
  34. width: 4px;
  35. top: 0;
  36. transition: width var(--transition);
  37. }
  38. &:first-of-type .color {
  39. &.ltr {
  40. left: 0;
  41. }
  42. &.rtl {
  43. right: 0;
  44. }
  45. }
  46. &:last-of-type .color {
  47. transform: scaleX(-1);
  48. &.ltr {
  49. right: 0;
  50. }
  51. &.rtl {
  52. left: 0;
  53. }
  54. }
  55. &[data-colored='true'] {
  56. &.active, &[aria-selected='true'], &:hover, &:focus {
  57. background: var(--widget) !important;
  58. box-shadow: none;
  59. .color {
  60. width: 100%;
  61. }
  62. &.dark-bg {
  63. color: #fff;
  64. }
  65. }
  66. }
  67. `;
  68. const TabButton = ({title, active, onClick, type = 'basic', color, value}) => {
  69. const {direction} = useThemeProvider();
  70. const darkColors = ['blue', 'purple'];
  71. return (
  72. <StyledTabButton
  73. className={classNames('btn text-overflow', {'active': active, 'dark-bg': darkColors.includes(color)})}
  74. data-colored={type === 'color'}
  75. value={value}
  76. onClick={onClick}>
  77. {
  78. type === 'color' && (
  79. <span className={`color ${direction}`} style={{backgroundColor: `var(--${color})`}}/>
  80. )
  81. }
  82. <span className="title">
  83. {title}
  84. </span>
  85. </StyledTabButton>
  86. )
  87. }
  88. TabButton.propTypes = {
  89. title: PropTypes.string.isRequired,
  90. active: PropTypes.bool,
  91. onClick: PropTypes.func,
  92. type: PropTypes.oneOf(['basic', 'color']),
  93. color: PropTypes.string
  94. }
  95. export default TabButton