|
1 | | -import styled from "@emotion/styled"; |
| 1 | +import styled, { CSSObject } from "@emotion/styled"; |
| 2 | +import { isLight } from "../../common/utils/colorUtils"; |
| 3 | +import { darken, lighten } from "polished"; |
2 | 4 |
|
3 | | -export const StyledButton = styled.button<{ width: string | number }>` |
4 | | - width: ${(props) => |
5 | | - typeof props.width === "string" ? props.width : `${props.width}px`}; |
| 5 | +export interface StyledButtonProps { |
| 6 | + width?: string | number; |
| 7 | + height?: string | number; |
| 8 | + backgroundColor?: string; |
| 9 | + backgroundColorHover?: string; |
| 10 | + backgroundColorActive?: string; |
| 11 | + color?: string; |
| 12 | + colorHover?: string; |
| 13 | + colorActive?: string; |
| 14 | + disabled?: boolean; |
| 15 | + active?: boolean; |
| 16 | + borderRadius?: number; |
| 17 | + group?: "horizontal" | "vertical"; |
| 18 | + padding?: CSSObject["padding"]; |
| 19 | +} |
| 20 | + |
| 21 | +export const StyledButton = styled.button<StyledButtonProps>` |
| 22 | + ${buildStyledButton} |
6 | 23 | `; |
| 24 | + |
| 25 | +function buildStyledButton({ |
| 26 | + width = "100%", |
| 27 | + borderRadius = 6, |
| 28 | + ...props |
| 29 | +}: StyledButtonProps): CSSObject { |
| 30 | + return { |
| 31 | + ...props, |
| 32 | + width: typeof width === "string" ? width : `${width}px`, |
| 33 | + height: |
| 34 | + typeof props.height === "string" ? props.height : `${props.height}px`, |
| 35 | + border: "none", |
| 36 | + transition: "background-color 0.2s, color 0.2s", |
| 37 | + ":disabled": { |
| 38 | + opacity: 0.52, |
| 39 | + }, |
| 40 | + backgroundColor: props.active |
| 41 | + ? getBackgroundColorActive(props) |
| 42 | + : props.backgroundColor, |
| 43 | + ":hover": { |
| 44 | + backgroundColor: getBackgroundColorHover(props), |
| 45 | + }, |
| 46 | + ":active": { |
| 47 | + backgroundColor: getBackgroundColorActive(props), |
| 48 | + }, |
| 49 | + ...radiusStyles({ borderRadius, group: props.group }), // this breaks the return type somehow. Something funky when spreading the emotion type |
| 50 | + } as CSSObject; |
| 51 | +} |
| 52 | + |
| 53 | +function getBackgroundColorActive(props: StyledButtonProps) { |
| 54 | + if (props.disabled) return undefined; |
| 55 | + if (props.backgroundColorActive) return props.backgroundColorActive; |
| 56 | + if (props.backgroundColor) { |
| 57 | + return isLight(props.backgroundColor) |
| 58 | + ? darken(0.2, props.backgroundColor) |
| 59 | + : lighten(0.2, props.backgroundColor); |
| 60 | + } |
| 61 | + return undefined; |
| 62 | +} |
| 63 | +function getBackgroundColorHover(props: StyledButtonProps) { |
| 64 | + if (props.disabled) return undefined; |
| 65 | + if (props.backgroundColorHover) return props.backgroundColorHover; |
| 66 | + if (props.backgroundColor) { |
| 67 | + return isLight(props.backgroundColor) |
| 68 | + ? darken(0.1, props.backgroundColor) |
| 69 | + : lighten(0.1, props.backgroundColor); |
| 70 | + } |
| 71 | + return undefined; |
| 72 | +} |
| 73 | + |
| 74 | +function radiusStyles({ |
| 75 | + group, |
| 76 | + borderRadius, |
| 77 | +}: Pick<StyledButtonProps, "group" | "borderRadius">): CSSObject { |
| 78 | + const r = `${borderRadius}px`; |
| 79 | + |
| 80 | + if (!group) { |
| 81 | + return { |
| 82 | + borderRadius: r, |
| 83 | + }; |
| 84 | + } |
| 85 | + |
| 86 | + const horizontal = group === "horizontal"; |
| 87 | + |
| 88 | + return { |
| 89 | + ":first-of-type": horizontal |
| 90 | + ? { |
| 91 | + borderTopLeftRadius: r, |
| 92 | + borderBottomLeftRadius: r, |
| 93 | + } |
| 94 | + : { |
| 95 | + borderTopLeftRadius: r, |
| 96 | + borderTopRightRadius: r, |
| 97 | + }, |
| 98 | + |
| 99 | + ":last-of-type": horizontal |
| 100 | + ? { |
| 101 | + borderTopRightRadius: r, |
| 102 | + borderBottomRightRadius: r, |
| 103 | + } |
| 104 | + : { |
| 105 | + borderBottomLeftRadius: r, |
| 106 | + borderBottomRightRadius: r, |
| 107 | + }, |
| 108 | + }; |
| 109 | +} |
0 commit comments