Logo

dev-resources.site

for different kinds of informations.

How to add custom fonts and viewports in storybook?

Published at
12/6/2024
Categories
storybook
react
custom
font
Author
vishesh-tiwari
Categories
4 categories in total
storybook
open
react
open
custom
open
font
open
Author
14 person written this
vishesh-tiwari
open
How to add custom fonts and viewports in storybook?

Hi devs,

In this article, we will discuss how to add and customize our Storybook dashboard with:

  • Custom font
  • Custom viewport

Step-by-Step Guide

Step 1: Setting Up Custom Viewports

To add custom viewports, you need to define them in your Storybook configuration. This allows you to simulate different screen sizes and resolutions. Here’s how you can do it:

Define Custom Viewports: Create a customViewports object with your desired viewports. Each viewport should have a name and styles for width and height.


const customViewports = {
  Mac: {
    name: 'Mac',
    styles: {
      width: '1728px',
      height: '868px',
    },
  },
  Windows: {
    name: 'Windows',
    styles: {
      width: '1280px',
      height: '559px',
    },
  },
  'Business Desktop': {
    name: 'Business Desktop',
    styles: {
      width: '3840px',
      height: '2160px',
    },
  },
  'Minimized Windows': {
    name: 'Minimized Windows',
    styles: {
      width: '1024px',
      height: '768px',
    },
  },
};
Enter fullscreen mode Exit fullscreen mode
  • Add Viewports to Parameters: Integrate the customViewports into your Storybook’s parameters.

const parameters = {
  viewport: {
    viewports: { ...customViewports },
  },
};
Enter fullscreen mode Exit fullscreen mode

Step 2: Customizing Fonts

  • To customize fonts in your Storybook, you can define global types and use a theme provider. This allows you to switch between different fonts easily.

Define Fonts: Create a Font object with your font options.

const Font = {
  FELLIX: 'fellix',
  ROBOTO: 'roboto',
};

Enter fullscreen mode Exit fullscreen mode
  • Set Up Global Types: Define globalTypes to manage the font settings. This includes a description, default value, and toolbar configuration.
const globalTypes = {
  font: {
    description: 'Global font for components',
    defaultValue: Font.ROBOTO,
    toolbar: {
      title: 'Fonts',
      items: [
        { value: Font.FELLIX, title: 'Fellix' },
        { value: Font.ROBOTO, title: 'Roboto' },
      ],
      dynamicTitle: true,
    },
  },
};
Enter fullscreen mode Exit fullscreen mode
  • Create a Theme Provider: Use a theme provider to apply the selected font globally. This involves creating a withThemeProvider decorator.

const withThemeProvider = (Story, storyContext) => {
  const { globals } = storyContext;
  const currentFont = globals.font;

  return (
    <ThemeProvider theme={currentTheme} font={currentFont}>
      <GlobalStyles />
      <Story />
    </ThemeProvider>
  );
};
Enter fullscreen mode Exit fullscreen mode
  • Add the Decorator: Finally, add the withThemeProvider decorator to your Storybook configuration.
export const decorators = [withThemeProvider];
Enter fullscreen mode Exit fullscreen mode

Conclusion

By following these steps, you can easily add and customize your Storybook dashboard with custom fonts and viewports. This will help you create a more tailored and visually appealing development environment.

Please do not forget export variables -

export { decorators, globalTypes, parameters };
Enter fullscreen mode Exit fullscreen mode

Happy coding! 🚀

Buy Me A Coffee

custom Article's
30 articles in total
Favicon
Custom Website Development - A Complete Guide for Small Businesses
Favicon
What Are the Steps Involved in Developing a Custom AI Solution?
Favicon
How Will Custom EMR EHR Software Reshape the Healthcare Horizon?
Favicon
The Ultimate Guide to Custom T-Shirts: Unique, Creative, and Personalized
Favicon
Custom Bottle Neckers for Sale Wholesale and Printed Options
Favicon
Order Custom Bottle Neckers Wholesale for Packaging Needs
Favicon
Custom CBD Boxes Wholesale with Logo for Premium Branding
Favicon
How to Build a Secure and Profitable DeFi Staking Platform
Favicon
Power of Custom Product Packaging: Enhancing Brand Experience and Driving Sales
Favicon
How to add custom fonts and viewports in storybook?
Favicon
How to choose the right service app for your needs | Optimity Logics
Favicon
Vue Vs Angular: Which One Should You Choose for Your Project? | Optimity Logics
Favicon
Transform Your Branding with 3M Printable Vinyl Wraps in Deltona, FL
Favicon
This Single-Line Command Reduced Our Database Object Management Efforts
Favicon
Custom Website Design Benefits
Favicon
Elevate Gift Giving With Custom Pillow Boxes For Every Occasion
Favicon
Creating a Custom 404 Not Found Page in Next.js 14
Favicon
How Cloud-Native Development is Redefining Custom Software Solutions?
Favicon
Boost Your Sales with a Custom Virtual Store: Here’s How
Favicon
DOM Selection with Custom Function
Favicon
When it comes to interior design in Dubai
Favicon
Building a Custom Flutter Widget from Scratch
Favicon
The Ultimate Guide to Choosing the Right Custom Sunglasses Manufacturer for Your Brand
Favicon
Laravel 11 Custom Component File Structure
Favicon
Secure Your Online World for Under $6: Why You Need a Personal VPN
Favicon
Streamline Your Django Workflow: A Guide to Creating Custom Management Commands
Favicon
5 Software Development Tips to Improve Your Product Growth
Favicon
Desenhos complexos com CustomPaint
Favicon
Desenhe o que quiser com Custom Paint no Flutter
Favicon
How to create a custom VSCode theme

Featured ones: