Skip to content

Commit a3f7219

Browse files
author
Nikolai Lopin
authored
Support responsive size values for Headline (#196)
1 parent f2a7e20 commit a3f7219

File tree

2 files changed

+46
-45
lines changed

2 files changed

+46
-45
lines changed

src/components/Headline/Headline.tsx

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
22
import { ComponentPropsWithoutRef } from 'react';
3-
import styled, { css } from 'styled-components';
4-
import { compose, margin, MarginProps, textAlign, TextAlignProps } from 'styled-system';
3+
import styled from 'styled-components';
4+
import { compose, margin, system, MarginProps, textAlign, TextAlignProps, ResponsiveValue } from 'styled-system';
55
import { Colors } from '../../essentials';
66
import { theme } from '../../essentials/theme';
77
import { get } from '../../utils/themeGet';
@@ -18,7 +18,7 @@ interface HeadlineProps extends ComponentPropsWithoutRef<'h1'>, MarginProps, Tex
1818
/**
1919
* Set the style of the headline
2020
*/
21-
size?: 'xxl' | 'xl' | 'l' | 'm' | 's' | 'xs';
21+
size?: ResponsiveValue<'xxl' | 'xl' | 'l' | 'm' | 's' | 'xs'>;
2222
}
2323

2424
const DEFAULT_HEADLINE_SIZE = {
@@ -30,54 +30,41 @@ const DEFAULT_HEADLINE_SIZE = {
3030
h6: 'xs'
3131
} as const;
3232

33-
function determineFontSize(props: HeadlineProps) {
34-
const h1Styles = css`
35-
font-size: ${get('fontSizes.7')};
36-
line-height: 3.75rem;
37-
`;
38-
39-
const size = props.size ?? DEFAULT_HEADLINE_SIZE[props.as];
40-
switch (size) {
41-
case 'xxl':
42-
return h1Styles;
43-
case 'xl':
44-
return css`
45-
font-size: ${get('fontSizes.5')};
46-
line-height: 2.5rem;
47-
`;
48-
case 'l':
49-
return css`
50-
font-size: ${get('fontSizes.4')};
51-
line-height: 2rem;
52-
`;
53-
case 'm':
54-
return css`
55-
font-size: ${get('fontSizes.2')};
56-
line-height: 1.375rem;
57-
`;
58-
case 's':
59-
return css`
60-
font-size: ${get('fontSizes.1')};
61-
line-height: 1.25rem;
62-
`;
63-
case 'xs':
64-
return css`
65-
font-size: ${get('fontSizes.0')};
66-
line-height: 1.125rem;
67-
`;
68-
default:
69-
return h1Styles;
33+
const parser = system({
34+
fontSize: {
35+
property: 'fontSize',
36+
defaultScale: {
37+
xs: '0.75rem',
38+
s: '0.875rem',
39+
m: '1rem',
40+
l: '1.5rem',
41+
xl: '2rem',
42+
xxl: '3rem'
43+
}
44+
},
45+
lh: {
46+
property: 'lineHeight',
47+
defaultScale: {
48+
xs: '1.125rem',
49+
s: '1.25rem',
50+
m: '1.375rem',
51+
l: '2rem',
52+
xl: '2.5rem',
53+
xxl: '3.75rem'
54+
}
7055
}
71-
}
56+
});
57+
58+
const getSize = ({ as = 'h1', size }: HeadlineProps): ResponsiveValue<'xxl' | 'xl' | 'l' | 'm' | 's' | 'xs'> =>
59+
size || DEFAULT_HEADLINE_SIZE[as];
7260

7361
const Headline: React.FC<HeadlineProps> = styled.h1.attrs({ theme })<HeadlineProps>`
7462
color: ${p => (p.inverted ? Colors.WHITE : Colors.AUTHENTIC_BLUE_900)};
7563
font-family: ${get('fonts.normal')};
7664
font-weight: ${get('fontWeights.bold')};
7765
margin: 0;
7866
79-
${determineFontSize}
80-
67+
${props => parser({ fontSize: getSize(props), lh: getSize(props), ...props })}
8168
${compose(margin, textAlign)}
8269
`;
8370

src/components/Headline/docs/Headline.mdx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ the semantics while following the design.
2828

2929
<HeadlinePropsTable />
3030

31-
3231
## Default headlines
3332

3433
<ItemWrapper>
@@ -60,9 +59,24 @@ the semantics while following the design.
6059
</Headline>
6160
</ItemWrapper>
6261

63-
## Examples
62+
## Responsive size
63+
64+
The `size` property supports [responsive values](https://styled-system.com/responsive-styles/). Pass an array or an object
65+
and the design system use corresponding values for different viewport sizes.
66+
67+
For example, the following snippet uses `s` size for mobile and `xl` size bigger screens:
68+
69+
```jsx
70+
71+
<Headline as="h2" size={{_: 's', medium: 'xl'}}>Small on mobile, big on bigger screens</Headline>
72+
73+
```
74+
75+
## Playground
76+
6477

6578
<Playground>
6679
<Headline as="h3">default headline h3</Headline>
6780
<Headline as="h1" size="xs">The smallest h1</Headline>
81+
<Headline as="h4" size={{_: 'xs', medium: 'm'}}>Responsive headline</Headline>
6882
</Playground>

0 commit comments

Comments
 (0)