Skip to content

Instantly share code, notes, and snippets.

@amazingandyyy
Last active June 22, 2018 17:29
Show Gist options
  • Save amazingandyyy/f65ed8d40fde74355f8585ce0d77845b to your computer and use it in GitHub Desktop.
Save amazingandyyy/f65ed8d40fde74355f8585ce0d77845b to your computer and use it in GitHub Desktop.
import React from 'react';
import 'style.scss';
const NavButton = (props) => <div className='container'><div classname={`nav-btn ${props.active}`}><i>Click Me</i></div><div>
export default NavButton;
$brand-color: red;
.nav-btn {
border: 1px solid black;
border-radius: 5px;
background: white;
&.true {
background: $brand-color;
}
}
.container {
...
}
@iamthesiz
Copy link

iamthesiz commented Jun 20, 2018

Styled Components

NavButton.js

import React from 'react';
import styled, { css } from 'styled-components';

export const NavButton = ({ children, ...props }) => <Container><Button {...props}><i>{children}</i></Button></Container>;

const Container = styled.div`
    ... 
`;


const Button = styled.div`
  border: 1px solid black;
  border-radius: 5px;
  background: white;
  ${props => props.active && css`
    background: ${props.theme.brandColor};
  `}
`;

Usage:

render() {
  return (
    <NavButton active={true}>Cool</NavButton>
  )
}

@amazingandyyy
Copy link
Author

amazingandyyy commented Jun 20, 2018

<div className={`nav-btn ${props.active}`}></div>

{props.avtive?<div className='nav-btn true'></div>:<div className='navbar-btn'></div>}

<div className={props.active ? 'nav-btn true': 'navbar-btn'}></div>

@iamthesiz
Copy link

iamthesiz commented Jun 20, 2018

CSS-Modules

NavButton.js

import React from 'react';
import cx from 'classnames';
import s from './styles.css';

const NavButton = ({ active, className, ...props }) => <div className={cx(className, s.nbtn, active && s.active)} {...props} />

styles.css

@value brand-color: red;

.nbtn {
  border: 1px solid black;
  border-radius: 5px;
  background: white;
}
.active {
  background: $brand-color;
}

@iamthesiz
Copy link

iamthesiz commented Jun 20, 2018

JSS

NavButton.js

import React from 'react';
import cx from 'classnames';
import { withStyles } from '@material-ui/core';
import styles from './styles.js';

const NavButton = withStyles(styles)(({ active, classes, className, ...props }) => (
  <div className={cx(className, classes.nbtn, { [classes.active]: active })} {...props} />
))

styles.js

const styles = (theme) => ({
  active: {
    background: theme.brandColor,
  },
  nbtn: {
    border: '1px solid black',
    borderRadius: 5,
    background: 'white',
  }
});
export default styles;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment