@@ -6,12 +6,14 @@ import { getFontStyle } from '../../textStyles';
6
6
7
7
export type HeadlineSize = 'small' | 'medium' ;
8
8
9
+ export type HeadlineLevel = keyof typeof styledHeadlines ;
10
+
9
11
export interface HeadlineProps extends StandardProps {
10
12
/**
11
13
* Represent 5 levels of headings (1-5)
12
14
* Default is 3
13
15
*/
14
- level ?: 1 | 2 | 3 | 4 | 5 ;
16
+ level ?: HeadlineLevel ;
15
17
/**
16
18
* When specified headline will have muted text color
17
19
*/
@@ -24,67 +26,55 @@ export interface HeadlineProps extends StandardProps {
24
26
25
27
export interface StyledHeadlineProps {
26
28
size : HeadlineSize ;
27
- level : number ;
29
+ level : HeadlineLevel ;
28
30
theme ?: any ;
29
31
subheader ?: boolean ;
30
32
}
31
33
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
+ ) ;
79
45
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
+ } ;
82
71
83
72
/**
84
73
* Headline component with styles for all headline levels.
85
74
*/
86
75
export const Headline : React . SFC < HeadlineProps > = ( { level = 3 , children, ...rest } ) => {
87
- const StyledHeadline = getStyledHeadline ( level ) ;
76
+ const StyledHeadline = styledHeadlines [ level ] || styledHeadlines [ 3 ] ;
77
+
88
78
return (
89
79
< StyledHeadline level = { level } { ...rest } >
90
80
{ children }
0 commit comments