Adding Custom Font to Storybook

Most of the design systems have custom fonts. Let’s see how we can bring them to Storybook. Once the font is in, we can start using it in canvas by adding CSS. My example storybook repo is here.

First of all, we need to add a font folder and add all the font files as well as the css file with font-face declarations.

Then, we need to move this folder into the storybook-static folder (where the built artifacts go). In order to do this, we can write a custom webpack config file and add it.storybook folder. We use copy-webpack-plugin to move static assets here. This custom webpack.config.js file will be called during storybook build. Make sure that you have the correct input and output path.

Once this is added, you can see the font folder appearing in the storybook-static folder after successful build.

To use font in the Canvas, we need to add font to preview-head.html. Canvas is an iframe which can be customised by editing the files prefixed as preview-* (preview-head.html, preview-body.html, preview.js and so on). In this case, we add a custom style, in preview-head.html as below. If the component is already using the same font family, adding @font-face will enable font for that component. Alternatively, we can create a class to access the font and add the class to html elements.

Front-End
TypeScript: type aliases to check type equality

This post is to analyse how the type equality check by using type aliases proposed by Matt Pocock in his twitter post. These type aliases allow us to elegantly express type equality checks in unit tests. All we need to do is to pass the output and expected types in …

Front-End
Fixing it.only type error in Jest

If you are getting a type error with it.only in Jest, it could be due to incorrect TypeScript typings or incompatible versions of Jest and TypeScript. To resolve this issue, you can try the following steps: Make sure you have the latest versions of Jest and its TypeScript typings installed …

Front-End
yup conditional validation example

Here’s an example of a Yup validation logic where the first input field is optional but, if filled, it must contain only alphabetic characters, and the second input field is required: import * as Yup from “yup”; const validationSchema = Yup.object().shape({ firstField: Yup.string().matches(/^[A-Za-z]*$/, { message: “First field must contain only …