Stas Gavrylov

@stasgavrylov

Lead Frontend Engineer @ Rails Reactor

Enhancing your apps
with
CSS Custom Properties

 

CSS-in-JS

Over 50 (!!!) solutions

CSS in NOT
a concise language

const Button = styled.a`
  display: inline-block;
  border-radius: 3px;
  padding: 0.5rem 0;
  margin: 0.5rem 1rem;
  width: 11rem;
  background: transparent;
  color: white;
  border: 2px solid white;

  ${props => props.primary && css`
    background: white;
    color: palevioletred;
  `}
`

Me after printing my
<StyledComponent /> source code

Separation
of logic and presentation

CSS Custom Properties
2012-04-10 First Public Draft
2015-12-03 Candidate Recommendation

CSS Custom Properties 101

:root {
  --app-max-width: 600px;
}

#app {
  --main-color: midnightblue;
  
  padding: calc(50% - var(--app-max-width) / 2);
  background: linear-gradient(
    90deg, 
    var(--main-color) 0%,
    var(--dynamic-color, white) 100%
  );
}

Everything is possible... NOT!

:root {
  --tilted-gradient: linear-gradient(
    calc(var(--tilt-angle) * 1deg),
    black 0%,
    white 100%
  );
}

.tilted {
  --tilt-angle: 45;
  background-image: var(--tilted-gradient);
}

But there's always a solution

*, .withTiltedGradient {
  --tilted-gradient: linear-gradient(
    calc(var(--tilt-angle) * 1deg),
    black 0%,
    white 100%
  );
}

.tilted {
  --tilt-angle: 45;
  background-image: var(--tilted-gradient);
}

Demo Time

Links

Thank You

Enhancing your apps with CSS Custom Properties

By Stas Gavrylov

Enhancing your apps with CSS Custom Properties

A primer on using CSS Custom Properties in React apps

  • 435