Skip to content

Commit c48f198

Browse files
committed
Use as prop for polymorphism in Text
1 parent 44f98be commit c48f198

File tree

11 files changed

+68
-65
lines changed

11 files changed

+68
-65
lines changed

desktop/packages/mullvad-vpn/src/renderer/lib/components/navigation-header/components/NavigationHeaderTitle.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ export const StyledText = styled(TitleMedium)<{ $visible?: boolean }>(({ $visibl
1515
textOverflow: 'ellipsis',
1616
}));
1717

18-
export const NavigationHeaderTitle = ({ children }: NavigationHeaderTitleProps) => {
18+
export const NavigationHeaderTitle = ({ children, ...props }: NavigationHeaderTitleProps) => {
1919
const { titleVisible } = useNavigationHeader();
2020
return (
21-
<StyledText tag="h1" $visible={titleVisible}>
21+
<StyledText forwardedAs="h1" $visible={titleVisible} {...props}>
2222
{children}
2323
</StyledText>
2424
);

desktop/packages/mullvad-vpn/src/renderer/lib/components/typography/BodySmall.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { KnownTarget } from 'styled-components/dist/types';
2+
13
import { Text, TextProps } from './Text';
24

3-
export type BodySmallProps = Omit<TextProps, 'variant'>;
5+
export type BodySmallProps = Omit<TextProps<KnownTarget>, 'variant'>;
46

57
export const BodySmall = ({ children, ...props }: BodySmallProps) => (
68
<Text variant="bodySmall" {...props}>

desktop/packages/mullvad-vpn/src/renderer/lib/components/typography/BodySmallSemiBold.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { KnownTarget } from 'styled-components/dist/types';
2+
13
import { Text, TextProps } from './Text';
24

3-
export type BodySmallSemiBoldProps = Omit<TextProps, 'variant'>;
5+
export type BodySmallSemiBoldProps = Omit<TextProps<KnownTarget>, 'variant'>;
46

57
export const BodySmallSemiBold = ({ children, ...props }: BodySmallSemiBoldProps) => (
68
<Text variant="bodySmallSemibold" {...props}>

desktop/packages/mullvad-vpn/src/renderer/lib/components/typography/FootnoteMini.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { KnownTarget } from 'styled-components/dist/types';
2+
13
import { Text, TextProps } from './Text';
24

3-
export type FoonoteMiniProps = Omit<TextProps, 'variant'>;
5+
export type FoonoteMiniProps = Omit<TextProps<KnownTarget>, 'variant'>;
46

57
export const FootnoteMini = ({ children, ...props }: FoonoteMiniProps) => (
68
<Text variant="footnoteMini" {...props}>

desktop/packages/mullvad-vpn/src/renderer/lib/components/typography/Label.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Text } from './Text';
22
import { TextProps } from './Text';
33

4-
export type LabelProps = TextProps & React.LabelHTMLAttributes<HTMLLabelElement>;
4+
export type LabelProps = TextProps<'label'>;
55

66
export const Label = ({ children, ...props }: LabelProps) => {
77
return (

desktop/packages/mullvad-vpn/src/renderer/lib/components/typography/LabelTiny.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { KnownTarget } from 'styled-components/dist/types';
2+
13
import { Text, TextProps } from './Text';
2-
export type LabelTinyProps = Omit<TextProps, 'variant'>;
4+
export type LabelTinyProps = Omit<TextProps<KnownTarget>, 'variant'>;
35

46
export const LabelTiny = ({ children, ...props }: LabelTinyProps) => (
57
<Text variant="labelTiny" {...props}>

desktop/packages/mullvad-vpn/src/renderer/lib/components/typography/Link.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import { RoutePath } from '../../routes';
77
import { buttonReset } from '../../styles';
88
import { Text, TextProps } from './Text';
99

10-
export interface LinkProps extends TextProps, Omit<React.HtmlHTMLAttributes<'button'>, 'color'> {
10+
export interface LinkProps extends Omit<TextProps<'button'>, 'color'> {
1111
to: RoutePath;
12+
color?: Colors;
1213
}
1314

1415
const StyledText = styled(Text)<{
@@ -41,7 +42,7 @@ const getHoverColor = (color: Colors | undefined) => {
4142
export const Link = ({ to, children, color, onClick, ...props }: LinkProps) => {
4243
const history = useHistory();
4344
const navigate = useCallback(
44-
(e: React.MouseEvent<'button'>) => {
45+
(e: React.MouseEvent<HTMLButtonElement>) => {
4546
if (onClick) {
4647
onClick(e);
4748
}
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,50 @@
1-
import { createElement, forwardRef } from 'react';
2-
import styled, { WebTarget } from 'styled-components';
1+
import React from 'react';
2+
import styled, { PolymorphicComponentProps } from 'styled-components';
3+
import { KnownTarget } from 'styled-components/dist/types';
34

4-
import { Colors, Typography, typography, TypographyProperties } from '../../foundations';
5+
import { Colors, Typography, typography } from '../../foundations';
56
import { TransientProps } from '../../types';
67

7-
export type TextProps = React.PropsWithChildren<{
8+
type TextBaseProps = {
89
variant?: Typography;
910
color?: Colors;
10-
tag?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | 'p' | 'span';
11-
as?: WebTarget;
12-
style?: React.CSSProperties;
13-
}>;
11+
children?: React.ReactNode;
12+
};
1413

15-
const StyledText = styled(
16-
({ tag = 'span', ...props }: { tag: TextProps['tag'] } & TransientProps<TypographyProperties>) =>
17-
createElement(tag, props),
18-
)((props) => ({
19-
color: 'var(--color)',
20-
fontFamily: props.$fontFamily,
21-
fontWeight: props.$fontWeight,
22-
fontSize: props.$fontSize,
23-
lineHeight: props.$lineHeight,
24-
}));
14+
export type TextProps<T extends KnownTarget = 'span'> = PolymorphicComponentProps<
15+
'web',
16+
TextBaseProps,
17+
T,
18+
T
19+
>;
2520

26-
export const Text = forwardRef(
27-
(
28-
{
29-
tag = 'span',
30-
variant = 'bodySmall',
31-
color = Colors.white,
32-
children,
33-
style,
34-
...props
35-
}: TextProps,
36-
ref,
37-
) => {
38-
const { fontFamily, fontSize, fontWeight, lineHeight } = typography[variant];
39-
return (
40-
<StyledText
41-
ref={ref}
42-
tag={tag}
43-
style={
44-
{
45-
'--color': color,
46-
...style,
47-
} as React.CSSProperties
48-
}
49-
$fontFamily={fontFamily}
50-
$fontWeight={fontWeight}
51-
$fontSize={fontSize}
52-
$lineHeight={lineHeight}
53-
{...props}>
54-
{children}
55-
</StyledText>
56-
);
21+
const StyledText = styled.span<TransientProps<TextBaseProps>>(
22+
({ $variant = 'bodySmall', $color = Colors.white }) => {
23+
const { fontFamily, fontSize, fontWeight, lineHeight } = typography[$variant];
24+
return `
25+
--color: ${$color};
26+
27+
color: var(--color);
28+
font-family: ${fontFamily};
29+
font-size: ${fontSize};
30+
font-weight: ${fontWeight};
31+
line-height: ${lineHeight};
32+
`;
5733
},
5834
);
5935

36+
export const Text = <T extends KnownTarget = 'span'>({
37+
variant,
38+
color,
39+
children,
40+
style,
41+
...props
42+
}: TextProps<T>) => {
43+
return (
44+
<StyledText $variant={variant} $color={color} {...props}>
45+
{children}
46+
</StyledText>
47+
);
48+
};
49+
6050
Text.displayName = 'Text';
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { KnownTarget } from 'styled-components/dist/types';
2+
13
import { Text, TextProps } from './Text';
2-
export type TitleBigProps = Omit<TextProps, 'variant'>;
4+
export type TitleBigProps<T extends KnownTarget> = TextProps<T>;
35

4-
export const TitleBig = ({ children, ...props }: TitleBigProps) => (
5-
<Text variant="titleBig" {...props}>
6-
{children}
7-
</Text>
6+
export const TitleBig = <T extends KnownTarget>({ ...props }: TitleBigProps<T>) => (
7+
<Text variant="titleBig" {...props} />
88
);

desktop/packages/mullvad-vpn/src/renderer/lib/components/typography/TitleLarge.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { KnownTarget } from 'styled-components/dist/types';
2+
13
import { Text, TextProps } from './Text';
2-
export type TitleLargeProps = Omit<TextProps, 'variant'>;
4+
export type TitleLargeProps = Omit<TextProps<KnownTarget>, 'variant'>;
35

46
export const TitleLarge = ({ children, ...props }: TitleLargeProps) => (
57
<Text variant="titleLarge" {...props}>

desktop/packages/mullvad-vpn/src/renderer/lib/components/typography/TitleMedium.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { KnownTarget } from 'styled-components/dist/types';
2+
13
import { Text, TextProps } from './Text';
2-
export type TitleMediumProps = Omit<TextProps, 'variant'>;
4+
export type TitleMediumProps = Omit<TextProps<KnownTarget>, 'variant'>;
35

46
export const TitleMedium = ({ children, ...props }: TitleMediumProps) => (
57
<Text variant="titleMedium" {...props}>

0 commit comments

Comments
 (0)