June 14, 2019

Writing css on ReasonReact with oCaml

Recently I was working quite a bit on ReasonML / ReasonReact projects, and we moved from writing old approach of writing external CSS to inline-css with bs-emotion (using oCaml syntax directly).

Its really easy to use css or sass / scss into ReasonReact components, you can import them the same way you import with from JS files (commonJs), you declared that you want to use raw js and required the style file with commonjs

[%%raw {|require from "./style.scss"|}];

Make sure that you have style-loader, sass-loader in your webpack if you are loading sass/scss.

Usually we declare a style per component and a shared global css, lets say you have a <button></button>, and we import button.scss, at the beginning this works fine but when project gets bigger and bigger we realised more and more problems, one of the major problem is that many classes can be shared among other components or some classes are no longer required for this component but global css are too big since some projects does not required all the classes in global css, also we thought since we are writing ReasonML or even oCaml code, why not having a same language across all code base. And so we looked into some inline-css modules that are binded to ReasonML, such as bs-css bs-emotion and quickly we choose bs-emotion because of the oCaml syntax and now its cool even backed-end guys can understand that we are writing in css.

Lets take a look at how we can use bs-emotion in our ReasonML/ReasonReact projects, lets install bs-emotion:

yarn add @ahrefs/bs-emotion

Include the bs-emotion in your bsconfig.json

"bs-dependencies": [
  "@ahrefs/bs-emotion"
]

lets create a Style.ml (create a oCaml file) and open Emotion and declare a variable which will be used later in your Reason file. using css method which is a list of css methods, below you can see I create a variable called container and add property display with value block and font-size property with value 14px.

let button = [%css [ width (`px 14) ]] 

Back to your Reason file you can use this style by adding it to your element className.

module Css = ComponentStyles;

let component = ReasonReact.statelessComponent(__MODULE__);

let make = _ => {
  ...component,
  render: _ => <button className=Css.button> ... </button>,
};

The benefits here is that if you declared a variable and compiler will warn you or even alerts you if you did not use it anywhere, and thats really cool, and it forces us to use variables instead of nesting css too.

You can do nested css in bs-emotion and you can even use ppx, extensions that helps you to ease your complex oCaml syntax as example below.

let button = [%css [ 
  width (`px 14) ; 
  hover [
    width: (`px 18) ; 
  ]
 ]] 

You can see that you can nested styles inside your container by using select method which take in a string (the css selectors), there are lots of other helpers methods such as hover, before, after, firstChild, lastChild.... and its really cool.

Top 6 Most Recommended Developer Books

The Pragmatic Programmer

by Dave Thomas, Andy Hunt

Check out this book on Amazon)}

The Pragmatic Programmer is one of those rare tech audiobooks you'll listen, re-listen, and listen to again over the years. Whether you're new to the field or an experienced practitioner, you'll come away with fresh insights each and every time. Dave Thomas and Andy Hunt wrote the first edition of this influential book in 1999 to help their clients create better software and rediscover the joy of coding. These lessons have helped a generation of programmers examine the very essence of software development, independent of any particular language, framework, or methodology, and the Pragmatic philosophy has spawned hundreds of books, screencasts, and audio books, as well as thousands of careers and success stories. Now, 20 years later, this new edition re-examines what it means to be a modern programmer. Topics range from personal responsibility and career development to architectural techniques for keeping your code flexible and easy to adapt and reuse.

Published: 2019

Genre: Programming

Cover of The Pragmatic Programmer

The Pragmatic Programmer

Cover of Clean Code: A Handbook of Agile Software Craftsmanship

Clean Code: A Handbook of Agile Software Craftsmanship

Cover of Working Effectively with Legacy Code

Working Effectively with Legacy Code

Cover of Introduction to Algorithms

Introduction to Algorithms

Cover of Eloquent JavaScript

Eloquent JavaScript

Cover of The Road to React

The Road to React