Skip to content

Commit b390974

Browse files
Merge pull request #28 from steven-mercatante/refactor-theming
Refactor theming
2 parents a8530e4 + a1e5452 commit b390974

18 files changed

+240
-166
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@merc/react-timeline",
3-
"version": "0.1.36",
3+
"version": "0.1.40",
44
"description": "An opinionated (but highly customizable) timeline component",
55
"main": "dist/index.cjs.js",
66
"module": "dist/index.esm.js",

src/components/Button.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
import PropTypes from 'prop-types';
22
import styled from 'styled-components';
33

4-
const Button = styled.button(props => {
5-
return props.theme.button;
6-
});
4+
const Button = styled.button`
5+
&& {
6+
font-size: ${props => props.theme.button.fontSize};
7+
background-color: ${props => props.theme.button.backgroundColor};
8+
border-radius: ${props => props.theme.button.borderRadius};
9+
padding: ${props => props.theme.button.padding};
10+
color: ${props => props.theme.button.color};
11+
margin: ${props => props.theme.button.margin};
12+
border: ${props => props.theme.button.border};
13+
text-decoration: ${props => props.theme.button.textDecoration};
14+
cursor: ${props => props.theme.button.cursor};
15+
}
16+
`;
717

818
Button.propTypes = {
919
children: PropTypes.node.isRequired,

src/components/Events.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@ import LAYOUTS from '../constants/layouts';
33

44
const Events = styled.div.attrs(_ => ({
55
className: 'events',
6-
}))(props => {
7-
const defaults = {
8-
[`&.${LAYOUTS.INLINE_EVENTS_INLINE_DATE}`]: {
9-
paddingLeft: '0px',
10-
},
11-
};
6+
}))`
7+
&& {
8+
padding: ${props => props.theme.events.padding};
129
13-
const style = { ...defaults, ...props.theme.events };
14-
15-
return style;
16-
});
10+
&.${LAYOUTS.INLINE_EVENTS_INLINE_DATE} {
11+
padding-left: 0px;
12+
}
13+
}
14+
`;
1715

1816
export default Events;

src/components/Theming.stories.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ export const customThemeExample = () => {
4949
</div>
5050
</TextEvent>
5151
<ImageEvent
52+
date="10/04/19"
5253
src="https://images.unsplash.com/photo-1559291001-693fb9166cba?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=934&q=80"
5354
alt="jellyfish"
5455
credit="Photo by [Vino Li](https://unsplash.com/@vinomamba24)"
5556
/>
5657
<ImageEvent
58+
date="10/05/19"
5759
src="https://images.unsplash.com/photo-1555901879-245ec81f706c?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=934&q=80"
5860
alt="blue octopus"
5961
credit="Photo by [Adam Muise](https://unsplash.com/@muiseadam)"

src/components/Theming.stories.mdx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ Check out the example below in which we customize some of the elements.
1818
import React from 'react';
1919
import { Timeline, Events, TextEvent, themes, createTheme } from '@merc/react-timeline';
2020

21+
/**
22+
* `createTheme` is a helper function that takes two arguments:
23+
* - a base theme to extend from
24+
* - the new theme properties
25+
* It uses deep copying behind the scenes to create a new theme object with
26+
* all of the required properties.
27+
*/
2128
const customTheme = createTheme(themes.default, {
2229
card: {
2330
backgroundColor: '#efefef',

src/components/Timeline.js

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,49 @@ const OverflowWrapper = styled.div`
1212
overflow: auto;
1313
`;
1414

15-
const Container = styled.div(props => {
16-
const defaults = {
17-
display: 'flex',
18-
flexDirection: 'column',
19-
position: 'relative',
15+
const Container = styled.div`
16+
position: relative;
17+
display: flex;
18+
flex-direction: column;
19+
line-height: normal;
2020
21-
'*, *:before, *:after': {
22-
boxSizing: 'border-box',
23-
},
24-
25-
// renders the vertical line
26-
'::after': { ...props.theme.timelineTrack },
21+
*,
22+
*:before,
23+
*:after {
24+
box-sizing: border-box;
25+
}
2726
28-
[`&.${LAYOUTS.INLINE_EVENTS}`]: {
29-
'::after': { left: '130px' },
30-
},
27+
font-size: ${props => props.theme.timeline.fontSize};
28+
font-family: ${props => props.theme.timeline.fontFamily};
29+
background-color: ${props => props.theme.timeline.backgroundColor};
3130
32-
[`&.${LAYOUTS.INLINE_EVENTS_INLINE_DATE}`]: {
33-
'::after': { left: '29px' },
34-
},
31+
::after {
32+
position: absolute;
33+
left: ${props => props.theme.timelineTrack.left};
34+
width: ${props => props.theme.timelineTrack.width};
35+
height: ${props => props.theme.timelineTrack.height};
36+
background-color: ${props => props.theme.timelineTrack.backgroundColor};
37+
content: ${props => props.theme.timelineTrack.content};
38+
}
3539
36-
'@media (max-width: 768px)': {
37-
'::after': { left: '29px !important' },
38-
},
39-
};
40+
&.${LAYOUTS.INLINE_EVENTS} {
41+
::after {
42+
left: 130px;
43+
}
44+
}
4045
41-
const style = { ...defaults, ...props.theme.timeline };
46+
&.${LAYOUTS.INLINE_EVENTS_INLINE_DATE} {
47+
::after {
48+
left: 29px;
49+
}
50+
}
4251
43-
return style;
44-
});
52+
@media (max-width: 768px) {
53+
::after {
54+
left: 29px !important;
55+
}
56+
}
57+
`;
4558

4659
const _opts = {
4760
layout: LAYOUTS.ALTERNATE_EVENTS, // see LAYOUTS constant

src/components/UrlButton.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3-
import styled from 'styled-components';
4-
5-
const UrlButtonContainer = styled.a(props => props.theme.urlButton);
63

74
export default function UrlButton({ href, target, children }) {
85
const extraProps = {};
@@ -11,9 +8,9 @@ export default function UrlButton({ href, target, children }) {
118
}
129

1310
return (
14-
<UrlButtonContainer href={href} className="url-button" {...extraProps}>
11+
<a href={href} className="url-button" {...extraProps}>
1512
{children}
16-
</UrlButtonContainer>
13+
</a>
1714
);
1815
}
1916

src/components/atoms/ImageAtom.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ import React from 'react';
22
import PropTypes from 'prop-types';
33
import styled from 'styled-components';
44

5-
const Image = styled.img(props => {
6-
return props.theme.imageAtom;
7-
});
5+
const Image = styled.img`
6+
&& {
7+
overflow: hidden;
8+
object-fit: ${props => props.theme.imageAtom.objectFit};
9+
width: ${props => props.theme.imageAtom.width};
10+
max-height: ${props => props.theme.imageAtom.maxHeight};
11+
}
12+
`;
813

914
export default function ImageAtom({ src, alt }) {
1015
return <Image src={src} alt={alt} />;

src/components/atoms/ImageAtom.test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@ import React from 'react';
22
import { render, cleanup } from '@testing-library/react';
33
import '@testing-library/jest-dom/extend-expect';
44
import ImageAtom from './ImageAtom';
5+
import { ThemeProvider } from 'styled-components';
6+
import themes from '../../themes';
57

68
afterEach(cleanup);
79

810
test('ImageAtom renders an img tag', () => {
9-
const { container } = render(<ImageAtom src="test.png" alt="test image" />);
11+
const { container } = render(
12+
<ThemeProvider theme={themes.default}>
13+
<ImageAtom src="test.png" alt="test image" />
14+
</ThemeProvider>
15+
);
1016

1117
expect(container.firstChild).toHaveAttribute('src', 'test.png');
1218
expect(container.firstChild).toHaveAttribute('alt', 'test image');

0 commit comments

Comments
 (0)