How to change specific elements style in storybook?

Member

by samara , in category: Javascript , 9 months ago

How to change specific elements style in storybook?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by denis , 9 months ago

@samara 

To change specific elements' styles in Storybook, you can do the following:

  1. Use the "addDecorator" function to apply a global style to all stories in your Storybook:
1
2
3
4
5
6
7
import { addDecorator } from '@storybook/react';

addDecorator(story => (
  <div style={{ fontFamily: 'Arial', color: 'blue' }}>
    {story()}
  </div>
));


  1. Use the "parameters" object to apply specific styles to individual stories:
1
2
3
4
5
export const Button = () => <button style={{ backgroundColor: 'red', color: 'white' }}>Click me</button>;

Button.parameters = {
  backgrounds: { default: 'dark' }
};


  1. Use the "withProps" function to pass custom styles as props to your components:
1
2
3
4
5
import { withProps } from '@storybook/addon-docs/blocks';

export const Button = withProps({
  style: { backgroundColor: 'green', color: 'white' }
})(() => <button>Click me</button>);


By using these methods, you can easily change the styles of specific elements in Storybook and customize the appearance of your UI components.