-
-
Notifications
You must be signed in to change notification settings - Fork 232
/
ui-text.tsx
172 lines (154 loc) Β· 4.84 KB
/
ui-text.tsx
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
/** @jsx jsx */
import { Interpolation, jsx } from '@emotion/core';
import { omit, pick } from '@remirror/core-helpers';
import { KeyOfThemeVariant } from '@remirror/core-types';
import { useRemirrorTheme } from '@remirror/ui';
import {
ColorProperty,
FontFamilyProperty,
FontSizeProperty,
FontWeightProperty,
LetterSpacingProperty,
TextAlignProperty,
} from 'csstype';
import mergeProps from 'merge-props';
import { FC, forwardRef, ReactElement, ReactNode } from 'react';
type MergeProp = typeof mergeProps;
interface InjectedTextProps {
className?: string;
children: ReactNode;
css: Interpolation;
}
interface RenderPropsParams {
props: InjectedTextProps;
mergeProps: MergeProp;
}
export interface BaseTextProps {
render: (params: RenderPropsParams) => ReactElement;
children?: ReactNode;
variant?: KeyOfThemeVariant<'remirror:text'>;
className?: string;
color?: ColorProperty;
textAlign?: TextAlignProperty;
letterSpacing?: LetterSpacingProperty<number | string>;
fontWeight?: FontWeightProperty;
fontFamily?: FontFamilyProperty;
fontSize?: FontSizeProperty<string | number>;
}
const keys = ['textAlign', 'letterSpacing', 'fontWeight', 'fontFamily', 'fontSize', 'children'] as const;
const pickTextProps = <GProps extends TextProps>(props: GProps) => pick(props, keys as any);
const omitTextProps = <GProps extends TextProps>(props: GProps) => omit(props, keys as any);
export const BaseText: FC<BaseTextProps> = ({
children,
render,
variant = 'p',
className,
color,
textAlign,
letterSpacing,
fontFamily,
fontWeight,
fontSize,
}) => {
const { sxx } = useRemirrorTheme();
const css = sxx(
{ variant: `remirror:text.${variant}` },
{ color, textAlign, letterSpacing, fontFamily, fontWeight, fontSize },
);
return render({ props: { children, css, className }, mergeProps });
};
export interface TextProps extends Omit<BaseTextProps, 'render' | 'variant'> {}
export const H1 = forwardRef<HTMLHeadingElement, TextProps & JSX.IntrinsicElements['h1']>((props, ref) => {
return (
<BaseText
variant='h1'
{...pickTextProps(props)}
render={params => <h1 {...params.mergeProps(params.props, omitTextProps(props))} ref={ref} />}
/>
);
});
H1.displayName = 'H1';
export const H2 = forwardRef<HTMLHeadingElement, TextProps & JSX.IntrinsicElements['h2']>((props, ref) => {
return (
<BaseText
variant='h2'
{...pickTextProps(props)}
render={params => <h2 {...params.mergeProps(params.props, omitTextProps(props))} ref={ref} />}
/>
);
});
H2.displayName = 'H2';
export const H3 = forwardRef<HTMLHeadingElement, TextProps & JSX.IntrinsicElements['h3']>((props, ref) => {
return (
<BaseText
variant='h3'
{...pickTextProps(props)}
render={params => <h3 {...params.mergeProps(params.props, omitTextProps(props))} ref={ref} />}
/>
);
});
H3.displayName = 'H3';
export const H4 = forwardRef<HTMLHeadingElement, TextProps & JSX.IntrinsicElements['h4']>((props, ref) => {
return (
<BaseText
variant='h4'
{...pickTextProps(props)}
render={params => <h4 {...params.mergeProps(params.props, omitTextProps(props))} ref={ref} />}
/>
);
});
H4.displayName = 'H4';
export const H5 = forwardRef<HTMLHeadingElement, TextProps & JSX.IntrinsicElements['h5']>((props, ref) => {
return (
<BaseText
variant='h5'
{...pickTextProps(props)}
render={params => <h5 {...params.mergeProps(params.props, omitTextProps(props))} ref={ref} />}
/>
);
});
H5.displayName = 'H5';
export const H6 = forwardRef<HTMLHeadingElement, TextProps & JSX.IntrinsicElements['h6']>((props, ref) => {
return (
<BaseText
variant='h6'
{...pickTextProps(props)}
render={params => <h6 {...params.mergeProps(params.props, omitTextProps(props))} ref={ref} />}
/>
);
});
H6.displayName = 'H6';
export const Text = forwardRef<HTMLParagraphElement, TextProps & JSX.IntrinsicElements['p']>((props, ref) => {
return (
<BaseText
variant='body'
{...pickTextProps(props)}
render={params => <p {...params.mergeProps(params.props, omitTextProps(props))} ref={ref} />}
/>
);
});
Text.displayName = 'Text';
export const Label = forwardRef<HTMLLabelElement, TextProps & JSX.IntrinsicElements['label']>(
(props, ref) => {
return (
<BaseText
variant='label'
{...pickTextProps(props)}
render={params => <label {...params.mergeProps(params.props, omitTextProps(props))} ref={ref} />}
/>
);
},
);
Label.displayName = 'Label';
export const Caption = forwardRef<HTMLSpanElement, TextProps & JSX.IntrinsicElements['span']>(
(props, ref) => {
return (
<BaseText
variant='caption'
{...pickTextProps(props)}
render={params => <span {...params.mergeProps(params.props, omitTextProps(props))} ref={ref} />}
/>
);
},
);
Caption.displayName = 'Caption';