Skip to content

Commit

Permalink
custom theme initial commit + added theme provider
Browse files Browse the repository at this point in the history
  • Loading branch information
ajainvivek committed Nov 22, 2017
1 parent d895df6 commit 9f3ea91
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 26 deletions.
11 changes: 11 additions & 0 deletions docs/Theme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<b>Examples:</b>

```js
const theme = {
bgColorLight: "#000"
};

<ThemeProvider theme={theme}>
<Button>Default Button</Button>
</ThemeProvider>
```
9 changes: 7 additions & 2 deletions src/Button/Button.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { Component } from 'preact';
import PropTypes from 'prop-types';

import { StyledButton } from './styles';

/**
Expand Down Expand Up @@ -51,6 +50,10 @@ class Button extends Component {
clicked: false
};

static contextTypes = {
theme: PropTypes.object,
};

_handleClick = (event) => {
this.setState({
clicked: true
Expand All @@ -74,8 +77,10 @@ class Button extends Component {
render() {
const clicked = this.state.clicked ? 'clicked' : '';
const {badge='', loading=false, className, left, right} = this.props;
const { theme = {} } = this.context
console.log(theme);
return (
<StyledButton {...this.props} onClick={this._handleClick} className={`${clicked} ${loading && 'loading'} ${className}`}>
<StyledButton {...this.props} theme={theme} onClick={this._handleClick} className={`${clicked} ${loading && 'loading'} ${className}`}>
<span className="item-left">
{left}
</span>
Expand Down
39 changes: 19 additions & 20 deletions src/Button/styles.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import styled, { css } from 'styled-components';

import colors from '../theme';
import keyframes from '../keyframes';
import animations from '../Animations';
import theme from '../theme';

export const StyledButton = styled.button`
position: relative;
Expand All @@ -18,10 +17,10 @@ export const StyledButton = styled.button`
font-size: 14px;
line-height: 14px;
padding: 10px 20px;
background: ${colors.bgColorLight};
border: 1px solid ${colors.primaryColor};
background: ${theme.bgColorLight};
border: 1px solid ${theme.primaryColor};
border-radius: 2px;
color: ${colors.linkColor};
color: ${theme.linkColor};
&.clicked:after {
content: '';
Expand All @@ -31,7 +30,7 @@ export const StyledButton = styled.button`
bottom: -1px;
right: -1px;
border-radius: inherit;
border: 0 solid ${colors.primaryColor};
border: 0 solid ${theme.primaryColor};
opacity: 0.4;
animation: ${keyframes.buttonEffect} .4s;
display: block;
Expand All @@ -54,7 +53,7 @@ export const StyledButton = styled.button`
display: inline-block;
font-size: 12px;
${props => props.badge && css`
background: ${props.badge.color ? props.badge.color : colors.primaryColor};
background: ${props.badge.color ? props.badge.color : theme.primaryColor};
padding: ${props.badge.value && props.badge.value.toString().length > 1 ? '3px 8px' : '3px'};
`}
}
Expand All @@ -66,7 +65,7 @@ export const StyledButton = styled.button`
&.loading:after {
animation: ${keyframes.loading} 500ms infinite linear;
border: 2px solid ${colors.primaryColorDark};
border: 2px solid ${theme.primaryColorDark};
border-radius: 50%;
border-right-color: transparent;
border-top-color: transparent;
Expand Down Expand Up @@ -95,29 +94,29 @@ export const StyledButton = styled.button`
`}
${props => props.primary && css`
background: ${colors.primaryColor};
border: 1px solid ${colors.primaryColorDark};
background: ${theme.primaryColor};
border: 1px solid ${theme.primaryColorDark};
border-radius: 2px;
color: ${colors.lightColor};
color: ${theme.lightColor};
&.loading:after {
border: 2px solid ${colors.lightColor};
border-right-color: ${colors.primaryColor};
border-top-color: ${colors.primaryColor};
border: 2px solid ${theme.lightColor};
border-right-color: ${theme.primaryColor};
border-top-color: ${theme.primaryColor};
}
`}
${props => props.secondary && css`
background: ${colors.secondaryColor};
border: 1px solid ${colors.secondaryColorDark};
background: ${theme.secondaryColor};
border: 1px solid ${theme.secondaryColorDark};
border-radius: 2px;
color: ${colors.lightColor};
color: ${theme.lightColor};
&.loading:after {
border: 2px solid ${colors.lightColor};
border-right-color: ${colors.primaryColor};
border-top-color: ${colors.primaryColor};
border: 2px solid ${theme.lightColor};
border-right-color: ${theme.primaryColor};
border-top-color: ${theme.primaryColor};
}
`}
Expand Down
20 changes: 20 additions & 0 deletions src/ThemeProvider/ThemeProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React, { Component } from 'preact';
import PropTypes from 'prop-types';

/**
* Custom theme provider for application
*/
class ThemeProvider extends Component {
static propTypes = {
theme: PropTypes.object.isRequired,
}
getChildContext () {
let { children, ...context } = this.props;
return context;
}
render ({ children }) {
return children && children[0] || null
}
}

export default ThemeProvider;
1 change: 1 addition & 0 deletions src/ThemeProvider/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default from './ThemeProvider';
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export ThemeProvider from './ThemeProvider';
export AppBar from './AppBar';
export Button from './Button';
export Link from './Link';
Expand Down
4 changes: 1 addition & 3 deletions src/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const controlErrorColor = '#e85600';
const codeColor = '#e06870';
const highlightColor = '#ffe9b3';
const notifyBgColor = '#ececec';
const listActiveColor = '#f0f3f5';

export default {
primaryColor,
Expand All @@ -60,7 +59,6 @@ export default {
controlErrorColor,
codeColor,
highlightColor,
notifyBgColor,
listActiveColor
notifyBgColor
};

2 changes: 2 additions & 0 deletions styleguide.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ module.exports = {
'./src/Animate/Animate.js'
];
}
}, {
name: 'Theme', content: './docs/Theme.md'
}],
theme: {
baseBackground: '#fdfdfc',
Expand Down
4 changes: 3 additions & 1 deletion styleguide/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ListSection from './../src/List/ListSection';
import ListItem from './../src/List/ListItem';
import ListFooter from './../src/List/ListFooter';
import Cell from './../src/Layout/Cell';
import ThemeProvider from './../src/ThemeProvider/ThemeProvider';
import lodash from 'lodash';

global.lodash = lodash;
Expand All @@ -19,4 +20,5 @@ global.ListHeader = ListHeader;
global.ListSection = ListSection;
global.ListItem = ListItem;
global.ListFooter = ListFooter;
global.Cell = Cell;
global.Cell = Cell;
global.ThemeProvider = ThemeProvider;

0 comments on commit 9f3ea91

Please sign in to comment.