2023-07-20 19:27:33 +08:00
|
|
|
import { createStyles } from 'antd-style';
|
2023-02-03 13:39:46 +08:00
|
|
|
import React from 'react';
|
2023-07-20 19:27:33 +08:00
|
|
|
import classNames from 'classnames';
|
2023-07-28 16:17:43 +08:00
|
|
|
import { Tooltip } from 'antd';
|
2022-11-27 20:25:59 +08:00
|
|
|
|
|
|
|
export interface LangBtnProps {
|
|
|
|
label1: React.ReactNode;
|
|
|
|
label2: React.ReactNode;
|
|
|
|
tooltip1?: React.ReactNode;
|
|
|
|
tooltip2?: React.ReactNode;
|
|
|
|
value: 1 | 2;
|
|
|
|
pure?: boolean;
|
|
|
|
onClick?: React.MouseEventHandler;
|
2023-10-15 19:17:32 +08:00
|
|
|
['aria-label']?: string;
|
2022-11-27 20:25:59 +08:00
|
|
|
}
|
|
|
|
|
2022-11-29 13:14:06 +08:00
|
|
|
const BASE_SIZE = '1.2em';
|
|
|
|
|
2023-07-20 19:27:33 +08:00
|
|
|
const useStyle = createStyles(({ token, css }) => {
|
2023-02-03 13:39:46 +08:00
|
|
|
const {
|
|
|
|
colorText,
|
|
|
|
colorBorder,
|
|
|
|
colorBgContainer,
|
|
|
|
colorBgTextHover,
|
|
|
|
borderRadius,
|
|
|
|
controlHeight,
|
|
|
|
motionDurationMid,
|
|
|
|
} = token;
|
2022-11-27 20:25:59 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
btn: css`
|
2023-02-03 13:39:46 +08:00
|
|
|
color: ${colorText};
|
|
|
|
border-color: ${colorBorder};
|
2022-11-27 20:25:59 +08:00
|
|
|
padding: 0 !important;
|
2022-11-29 13:14:06 +08:00
|
|
|
width: ${controlHeight}px;
|
|
|
|
height: ${controlHeight}px;
|
2022-11-27 20:25:59 +08:00
|
|
|
display: inline-flex;
|
|
|
|
align-items: center;
|
|
|
|
justify-content: center;
|
2022-11-29 13:14:06 +08:00
|
|
|
border: none;
|
|
|
|
background: transparent;
|
2023-02-03 13:39:46 +08:00
|
|
|
border-radius: ${borderRadius}px;
|
2022-11-29 13:14:06 +08:00
|
|
|
transition: all ${motionDurationMid};
|
|
|
|
cursor: pointer;
|
|
|
|
.btn-inner {
|
|
|
|
transition: all ${motionDurationMid};
|
|
|
|
}
|
|
|
|
&:hover {
|
2023-02-03 13:39:46 +08:00
|
|
|
background: ${colorBgTextHover};
|
2022-11-29 13:14:06 +08:00
|
|
|
}
|
2022-11-27 20:25:59 +08:00
|
|
|
img {
|
2022-11-29 13:14:06 +08:00
|
|
|
width: ${BASE_SIZE};
|
|
|
|
height: ${BASE_SIZE};
|
|
|
|
}
|
|
|
|
.anticon {
|
|
|
|
font-size: ${BASE_SIZE};
|
2022-11-27 20:25:59 +08:00
|
|
|
}
|
|
|
|
`,
|
2023-02-03 13:39:46 +08:00
|
|
|
innerDiv: css`
|
|
|
|
position: relative;
|
|
|
|
width: ${BASE_SIZE};
|
|
|
|
height: ${BASE_SIZE};
|
|
|
|
`,
|
|
|
|
labelStyle: css`
|
|
|
|
position: absolute;
|
|
|
|
font-size: ${BASE_SIZE};
|
|
|
|
line-height: 1;
|
|
|
|
border: 1px solid ${colorText};
|
|
|
|
color: ${colorText};
|
|
|
|
`,
|
|
|
|
label1Style: css`
|
|
|
|
left: -5%;
|
|
|
|
top: 0;
|
|
|
|
z-index: 1;
|
|
|
|
background-color: ${colorText};
|
|
|
|
color: ${colorBgContainer};
|
|
|
|
transform: scale(0.7);
|
|
|
|
transform-origin: 0 0;
|
|
|
|
`,
|
|
|
|
label2Style: css`
|
|
|
|
right: -5%;
|
|
|
|
bottom: 0;
|
|
|
|
z-index: 0;
|
|
|
|
transform: scale(0.5);
|
|
|
|
transform-origin: 100% 100%;
|
|
|
|
`,
|
2022-11-27 20:25:59 +08:00
|
|
|
};
|
2023-07-20 19:27:33 +08:00
|
|
|
});
|
2022-11-27 20:25:59 +08:00
|
|
|
|
2023-02-03 13:39:46 +08:00
|
|
|
const LangBtn: React.FC<LangBtnProps> = (props) => {
|
|
|
|
const { label1, label2, tooltip1, tooltip2, value, pure, onClick } = props;
|
2022-11-27 20:25:59 +08:00
|
|
|
|
2023-07-20 19:27:33 +08:00
|
|
|
const {
|
|
|
|
styles: { btn, innerDiv, labelStyle, label1Style, label2Style },
|
|
|
|
} = useStyle();
|
2022-11-27 20:25:59 +08:00
|
|
|
|
2023-02-03 13:39:46 +08:00
|
|
|
const node = (
|
2023-07-21 15:04:42 +08:00
|
|
|
<button onClick={onClick} className={btn} key="lang-button" aria-label={props['aria-label']}>
|
2022-11-29 13:14:06 +08:00
|
|
|
<div className="btn-inner">
|
2022-11-30 11:05:41 +08:00
|
|
|
{pure && (value === 1 ? label1 : label2)}
|
|
|
|
{!pure && (
|
2023-07-20 19:27:33 +08:00
|
|
|
<div className={innerDiv}>
|
|
|
|
<span className={classNames(labelStyle, value === 1 ? label1Style : label2Style)}>
|
|
|
|
{label1}
|
|
|
|
</span>
|
|
|
|
<span className={classNames(labelStyle, value === 1 ? label2Style : label1Style)}>
|
|
|
|
{label2}
|
|
|
|
</span>
|
2022-11-29 13:14:06 +08:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</button>
|
2022-11-27 20:25:59 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
if (tooltip1 || tooltip2) {
|
2023-02-03 13:39:46 +08:00
|
|
|
return <Tooltip title={value === 1 ? tooltip1 : tooltip2}>{node}</Tooltip>;
|
2022-11-27 20:25:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
2023-02-03 13:39:46 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default LangBtn;
|