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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
import React, { useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
import styled from 'styled-components';
import { colors } from '../../config.json';
import log from '../../shared/logging';
import { useMounted } from '../lib/utilityHooks';
import {
StyledButtonContent,
StyledLabel,
StyledLabelContainer,
transparentButton,
} from './AppButtonStyles';
import ImageView from './ImageView';
interface IButtonContext {
textAdjustment: number;
textRef?: React.Ref<HTMLDivElement>;
}
const ButtonContext = React.createContext<IButtonContext>({
textAdjustment: 0,
});
interface ILabelProps {
children?: React.ReactText;
}
export function Label(props: ILabelProps) {
const { textAdjustment, textRef } = useContext(ButtonContext);
return (
<StyledLabelContainer ref={textRef} textAdjustment={textAdjustment}>
<StyledLabel>{props.children}</StyledLabel>
</StyledLabelContainer>
);
}
interface IIconProps {
source: string;
width?: number;
height?: number;
}
export function Icon(props: IIconProps) {
return <ImageView {...props} tintColor={colors.white} />;
}
export interface IProps extends React.HTMLAttributes<HTMLButtonElement> {
children?: React.ReactNode;
className?: string;
disabled?: boolean;
onClick?: () => void;
textOffset?: number;
}
const BaseButton = React.memo(function BaseButtonT(props: IProps) {
const { children, textOffset, ...otherProps } = props;
const [textAdjustment, setTextAdjustment] = useState(0);
const buttonRef = useRef() as React.RefObject<HTMLButtonElement>;
const textRef = useRef() as React.RefObject<HTMLDivElement>;
const contextValue = useMemo(() => ({ textAdjustment, textRef }), [textAdjustment, textRef]);
useEffect(() => {
const buttonRect = buttonRef.current?.getBoundingClientRect();
const textRect = textRef.current?.getBoundingClientRect();
if (buttonRect && textRect) {
const leftDiff = textRect.left - buttonRect.left;
// calculate the remaining space at the right hand side
const trailingSpace = buttonRect.width - (leftDiff + textRect.width);
// calculate text adjustment
const textAdjustment = leftDiff - trailingSpace - (textOffset ?? 0);
// re-render the view with the new text adjustment if it changed
setTextAdjustment(textAdjustment);
}
});
return (
<ButtonContext.Provider value={contextValue}>
<StyledSimpleButton ref={buttonRef} {...otherProps}>
<StyledButtonContent>
{React.Children.map(children, (child) =>
typeof child === 'string' ? <Label>{child as string}</Label> : child,
)}
</StyledButtonContent>
</StyledSimpleButton>
</ButtonContext.Provider>
);
});
function SimpleButtonT(
props: React.ButtonHTMLAttributes<HTMLButtonElement>,
ref: React.Ref<HTMLButtonElement>,
) {
const blockingContext = useContext(BlockingContext);
return (
<button
ref={ref}
{...props}
disabled={props.disabled || blockingContext.disabled}
onClick={blockingContext.onClick ?? props.onClick}>
{props.children}
</button>
);
}
export const SimpleButton = React.memo(React.forwardRef(SimpleButtonT));
const StyledSimpleButton = styled(SimpleButton)({
display: 'flex',
cursor: 'default',
borderRadius: 4,
border: 'none',
padding: 0,
':disabled': {
opacity: 0.5,
},
});
interface IBlockingContext {
disabled?: boolean;
onClick?: () => Promise<void>;
}
const BlockingContext = React.createContext<IBlockingContext>({});
interface IBlockingProps {
children?: React.ReactNode;
onClick: () => Promise<void>;
disabled?: boolean;
}
export function BlockingButton(props: IBlockingProps) {
const isMounted = useMounted();
const [isBlocked, setIsBlocked] = useState(false);
const onClick = useCallback(async () => {
setIsBlocked(true);
try {
await props.onClick();
} catch (error) {
log.error(`onClick() failed - ${error}`);
}
if (isMounted()) {
setIsBlocked(false);
}
}, [props.onClick]);
const contextValue = useMemo(
() => ({
disabled: isBlocked || props.disabled,
onClick,
}),
[isBlocked, props.disabled, onClick],
);
return <BlockingContext.Provider value={contextValue}>{props.children}</BlockingContext.Provider>;
}
export const RedButton = styled(BaseButton)({
backgroundColor: colors.red,
':not(:disabled):hover': {
backgroundColor: colors.red95,
},
});
export const GreenButton = styled(BaseButton)({
backgroundColor: colors.green,
':not(:disabled):hover': {
backgroundColor: colors.green90,
},
});
export const BlueButton = styled(BaseButton)({
backgroundColor: colors.blue80,
':not(:disabled):hover': {
backgroundColor: colors.blue60,
},
});
export const TransparentButton = styled(BaseButton)(transparentButton, {
backgroundColor: colors.white20,
':not(:disabled):hover': {
backgroundColor: colors.white40,
},
});
export const RedTransparentButton = styled(BaseButton)(transparentButton, {
backgroundColor: colors.red60,
':not(:disabled):hover': {
backgroundColor: colors.red80,
},
});
const StyledButtonWrapper = styled.div({
display: 'flex',
flexDirection: 'column',
flex: 0,
':not(:last-child)': {
marginBottom: '18px',
},
});
interface IButtonGroupProps {
children: React.ReactElement[];
}
export function ButtonGroup(props: IButtonGroupProps) {
return (
<>
{React.Children.map(props.children, (button, index) => (
<StyledButtonWrapper key={index}>{button}</StyledButtonWrapper>
))}
</>
);
}
|