Skip to content

Commit d5de0f5

Browse files
authored
Make dynamically created Headline subcomponents static (#389)
1 parent 88f1e43 commit d5de0f5

File tree

4 files changed

+48
-54
lines changed

4 files changed

+48
-54
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Precise UI Changelog
22

3+
## 2.1.17
4+
5+
- Make dynamically created Headline subcomponents static
6+
37
## 2.1.16
48

59
- Fix some typings for React 18 compatibility.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "precise-ui",
3-
"version": "2.1.16",
3+
"version": "2.1.17",
44
"description": "Precise UI React component library powered by Styled Components.",
55
"keywords": [
66
"react",

src/components/Headline/Headline.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import { mount, mount } from 'enzyme';
2+
import { mount } from 'enzyme';
33
import { Headline } from './';
44
import { light } from '../../themes';
55

src/components/Headline/index.tsx

+42-52
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import { getFontStyle } from '../../textStyles';
66

77
export type HeadlineSize = 'small' | 'medium';
88

9+
export type HeadlineLevel = keyof typeof styledHeadlines;
10+
911
export interface HeadlineProps extends StandardProps {
1012
/**
1113
* Represent 5 levels of headings (1-5)
1214
* Default is 3
1315
*/
14-
level?: 1 | 2 | 3 | 4 | 5;
16+
level?: HeadlineLevel;
1517
/**
1618
* When specified headline will have muted text color
1719
*/
@@ -24,67 +26,55 @@ export interface HeadlineProps extends StandardProps {
2426

2527
export interface StyledHeadlineProps {
2628
size: HeadlineSize;
27-
level: number;
29+
level: HeadlineLevel;
2830
theme?: any;
2931
subheader?: boolean;
3032
}
3133

32-
interface HeadlineCache {
33-
[key: string]: any;
34-
}
35-
36-
const Headlines: HeadlineCache = {};
37-
38-
function getComponentName(level: number) {
39-
return `h${level >= 1 && level <= 5 ? level : 3}`;
40-
}
41-
42-
function getHeadlineStyle(level: StyledHeadlineProps['level']) {
43-
switch (level) {
44-
case 1:
45-
return getFontStyle({ size: 'xxxLarge', weight: 'light' });
46-
case 2:
47-
return getFontStyle({ size: 'xxLarge', weight: 'light' });
48-
case 3:
49-
return getFontStyle({ size: 'xLarge', weight: 'medium' });
50-
case 4:
51-
return getFontStyle({ size: 'large', weight: 'regular' });
52-
case 5:
53-
return getFontStyle({ size: 'medium', weight: 'medium' });
54-
default:
55-
return '';
56-
}
57-
}
58-
59-
function getStyledHeadline(level: number) {
60-
const component = getComponentName(level);
61-
const Headline = Headlines[component];
62-
63-
if (!Headline) {
64-
const NewHeadline = styled(component as 'h1')<StyledHeadlineProps>(
65-
themed<StyledHeadlineProps>(
66-
props => css`
67-
${getHeadlineStyle(props.level)}
68-
69-
margin: 0;
70-
padding: ${props.theme.headingsPadding || `0 ${distance.small} 0 0`};
71-
font-family: ${props.theme.fontFamily || 'inherit'};
72-
color: ${props.subheader ? props.theme.text5 : 'inherit'};
73-
`,
74-
),
75-
);
76-
Headlines[component] = NewHeadline;
77-
return NewHeadline;
78-
}
34+
/**
35+
* A common style for all headline levels.
36+
*/
37+
const baseStyle = themed<StyledHeadlineProps>(
38+
props => css`
39+
margin: 0;
40+
padding: ${props.theme.headingsPadding || `0 ${distance.small} 0 0`};
41+
font-family: ${props.theme.fontFamily || 'inherit'};
42+
color: ${props.subheader ? props.theme.text5 : 'inherit'};
43+
`,
44+
);
7945

80-
return Headline;
81-
}
46+
/**
47+
* A map of styled components for each headline level.
48+
*/
49+
const styledHeadlines = {
50+
1: styled.h1`
51+
${getFontStyle({ size: 'xxxLarge', weight: 'light' })}
52+
${baseStyle}
53+
`,
54+
2: styled.h2`
55+
${getFontStyle({ size: 'xxLarge', weight: 'light' })}
56+
${baseStyle}
57+
`,
58+
3: styled.h3`
59+
${getFontStyle({ size: 'xLarge', weight: 'medium' })}
60+
${baseStyle}
61+
`,
62+
4: styled.h4`
63+
${getFontStyle({ size: 'large', weight: 'regular' })}
64+
${baseStyle}
65+
`,
66+
5: styled.h5`
67+
${getFontStyle({ size: 'medium', weight: 'medium' })}
68+
${baseStyle}
69+
`,
70+
};
8271

8372
/**
8473
* Headline component with styles for all headline levels.
8574
*/
8675
export const Headline: React.SFC<HeadlineProps> = ({ level = 3, children, ...rest }) => {
87-
const StyledHeadline = getStyledHeadline(level);
76+
const StyledHeadline = styledHeadlines[level] || styledHeadlines[3];
77+
8878
return (
8979
<StyledHeadline level={level} {...rest}>
9080
{children}

0 commit comments

Comments
 (0)