summaryrefslogtreecommitdiffhomepage
path: root/gui/src/renderer/components/SmallButton.tsx
blob: e88d71995248abed4a58bbd5643b988c9b109735 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import React from 'react';
import styled from 'styled-components';

import { colors } from '../../config.json';
import { smallText } from './common-styles';
import { MultiButtonCompatibleProps } from './MultiButton';

export enum SmallButtonColor {
  blue,
  red,
  green,
}

function getButtonColors(color?: SmallButtonColor, disabled?: boolean) {
  switch (color) {
    case SmallButtonColor.red:
      return {
        background: disabled ? colors.red60 : colors.red,
        backgroundHover: disabled ? colors.red60 : colors.red80,
      };
    case SmallButtonColor.green:
      return {
        background: disabled ? colors.green40 : colors.green,
        backgroundHover: disabled ? colors.green40 : colors.green90,
      };
    default:
      return {
        background: disabled ? colors.blue50 : colors.blue,
        backgroundHover: disabled ? colors.blue50 : colors.blue60,
      };
  }
}

const BUTTON_GROUP_GAP = 12;

interface StyledSmallButtonProps {
  $color?: SmallButtonColor;
  disabled?: boolean;
}

const StyledSmallButton = styled.button<StyledSmallButtonProps>(smallText, (props) => {
  const buttonColors = getButtonColors(props.$color, props.disabled);

  return {
    display: 'flex',
    minHeight: '32px',
    padding: '5px 16px',
    border: 'none',
    background: buttonColors.background,
    color: props.disabled ? colors.white50 : colors.white,
    borderRadius: '4px',
    marginLeft: `${BUTTON_GROUP_GAP}px`,
    alignItems: 'center',
    justifyContent: 'center',

    [`${SmallButtonGroupStart} &&`]: {
      marginLeft: 0,
      marginRight: `${BUTTON_GROUP_GAP}px`,
    },

    [`${SmallButtonGrid} &&`]: {
      flex: '1 0 auto',
      marginLeft: 0,
      minWidth: `calc(50% - ${BUTTON_GROUP_GAP / 2}px)`,
      maxWidth: '100%',
    },

    '&&:hover': {
      background: buttonColors.backgroundHover,
    },
  };
});

const StyledContent = styled.span({
  flex: '1 0 fit-content',
});

const StyledTextOffset = styled.span<{ $width: number }>((props) => ({
  display: 'flex',
  flex: `0 1 ${props.$width}px`,
}));

interface SmallButtonProps
  extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'onClick' | 'color'>,
    MultiButtonCompatibleProps {
  onClick: () => void;
  children: React.ReactNode;
  color?: SmallButtonColor;
}

export function SmallButton(props: SmallButtonProps) {
  const { color, textOffset, children, ...otherProps } = props;
  return (
    <StyledSmallButton $color={props.color} {...otherProps}>
      {textOffset && textOffset > 0 ? <StyledTextOffset $width={Math.abs(textOffset)} /> : null}
      <StyledContent>{children}</StyledContent>
      {textOffset && textOffset < 0 ? <StyledTextOffset $width={Math.abs(textOffset)} /> : null}
    </StyledSmallButton>
  );
}

export const SmallButtonGroup = styled.div<{ $noMarginTop?: boolean }>((props) => ({
  display: 'flex',
  justifyContent: 'end',
  margin: '0 23px',
  marginTop: props.$noMarginTop ? 0 : '30px',
}));

export const SmallButtonGroupStart = styled(SmallButtonGroup)({
  flex: 1,
  justifyContent: 'start',
  margin: 0,
});

export const SmallButtonGrid = styled.div({
  display: 'flex',
  flexWrap: 'wrap',
  columnGap: `${BUTTON_GROUP_GAP}px`,
  rowGap: `${BUTTON_GROUP_GAP}px`,
});