Friday, November 17, 2017

GitHub markdown style on Visual Studio Code

These days I found out the very useful feature on Visual Studio Code for markdown editing, which renders the MD file on VSCode itself, opening a new tab at right.

However, the rendered markdown styles matches VSCode itself. Since I mainly use markdown on GitHub, I’d like to preview the markdown using GitHub style. So I inspected GitHub and made a quick CSS file to VSCode:

Save the file and add this entry to VSCode user settings, which on Linux looks like this:

"markdown.styles": ["/home/your_name/.config/Code/User/eu-markdown.css"]

The markdown preview should be pretty close to GitHub styling now.

Wednesday, October 18, 2017

React Material-UI: a FAB with an optional tooltip

I’m using Material UI library to have Material Design components on a rather complex React project. Despite being good, this library unfortunately doesn’t provide a tooltip option to the Float Action Button, something I need at the moment.

I found some useful information on this project issue, and after digging into the code of IconButton – which has a tooltip option – and Tooltip itself, I wrote a component that provides a Float Action Button with an optional tooltip:

The icon is ready to be used with Font Awesome project, which is what I’m using. Other minor adjustments I made, like font size, can just be removed too. And my project requirements are web only, so that’s all I tested.

Wednesday, October 11, 2017

Extending native objects in create-react-app

Despite being considered a bad practice by some, I like to extend native objects to add some syntatic sugar. However, when working in a React project with create-react-app, I receive the following ESLint error:

Array prototype is read only, properties should not be added  no-extend-native

Fortunately this warning can be disabled by adding a comment in the file where you extend the native object. Putting it all together, this is what I’ve got:

/*eslint no-extend-native: ["error", { "exceptions": ["Array"] }]*/
Object.defineProperty(Array.prototype, 'last', {
  get: function() {
    return this[this.length - 1];
  }
});
Object.defineProperty(Array.prototype, 'empty', {
  get: function() {
    return this.length === 0;
  }
});

Syntatic sugar added, clear console.

Monday, October 9, 2017

Absolute imports with create-react-app

When developing React using create-react-app with custom-react-scripts, I wanted to use absolute paths for import, and I found this: just adding NODE_PATH=src/ to “.env” file. It worked on Windows, but not on Linux.

After a while, I sorted it out: echo $NODE_PATH told me that my Linux had NODE_PATH being set somewhere. Then, being conservative, I just added unset NODE_PATH to “~/.bashrc” file, and it finally worked.

JavaScript development is a nightmare these days.

Sunday, October 8, 2017

Google Chrome 61 slow, how to fix

After updating to Chrome 61, I instantly noticed that YouTube became slow, and Google Maps – particularly Street View – became so slow it’s unusable. I went back to Firefox Developer 57, but then I missed Chrome’s search capabilities.

I found out that the problem is the HTML5 canvas, and the fix is rather easy: just disable this flag in Chrome flag page:

chrome://flags/#enable-color-correct-rendering

Instantly all the sites became responsive again.

Thursday, September 28, 2017

React App State: React without Redux

Coming from Angular 2, I’m delighted with React simplicity. In large projects, having a global state management seems to be a very good thing – lift the state through the roof! –, so I started to search for libraries to do this.

After trying hard to use and like Redux, the most famous one, I became very frustated on how unnecessarily complicated it is, probably because it started as a framework-agnostic solution that was then ported to React. It’s the complete opposite of React’s simplicity. I also tried MobX, and I also disliked it. But I was convinced that the basic idea was good.

So I ended up writing my own implementation of a Redux alternative: what I would like to have as a state manager for React, the result is React App State.

The idea is to have an AppState instance that receives a set call, analog to setState, and then all components that have subscribed to this instance will receive the updates via props. Even better, my implementation doesn’t resort to context as crutches. Multiple AppState instances can be used in an app, and a component can subscribe to any number of instances.

Wrap the set calls in controller classes, like shown in the example, and you have a scalable solution.

I only found two pitfalls:

  1. If a component subscribes to an instance which has a variable named foo, and the parent of this component tries to pass the same prop as foo="bar", there will be a name collision – what is obvious;
  2. If two AppState instances have a variable foo, and a component subscribes to both instances, there will be a name collision as well.

I think these pitfalls are very manageable, and I can live with them. After all, name collisions also exist in Redux.

React App State is the first package I publish on NPM.

Wednesday, September 13, 2017

Grep and find aliases

Two quick bash functions to speed up name and content search on files. These are to be appended at ~/.bashrc file:
function gr { clear ; grep -rn $1 . ; }
function ff { clear ; find . -name "$1" ; }