npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@onewelcome/storybook-addon-basestyling

v1.0.2

Published

To have easy configuration of our CSS variables.

Downloads

57

Readme

Storybook BaseStyling addon

This addon is written so that end-users can easily change global color variables without having to do into the browser' devtools.

Technical explanation

The addon works by exporting the global decorator withBaseStyling inside of src/preset/preview.ts. This decorator export will be plugged into the react-lib-component Storybook instance by adding it to the addons array in main.js file inside of /.storybook/main.js.

The withBaseStyling decorator creates a global variable called baseStyling. On initial render of the withBaseStyling decorator, the .basestyling-wrapper element with all the style variables hasn't loaded yet, so we use a setTimeout, which waits 1ms, to make sure everything is loaded.

After everything is loaded we parse all the style variables that are added to the .basestyling-wrapper element.

This means, that this package WILL BREAK when we add anything else to the style attribute of the .basestyling-wrapper element.

Currently, there's no other way to easily grab all the CSS variables dynamically. We also cannot import the object from our BaseStyling component, because Storybook will complain that you're trying to access something outside of this addon. We would have to duplicate all the properties and keep updating this package whenever we add a new variable. I wanted it to be dynamic, so for now we're parsing the .basestyling-wrapper element's style attribute within the Story's iframe.

After we've parsed all the CSS properties into an object, we update the baseStyling global (which works just as state). This is when the addon Panel will get filled. If you go into addon/src/Panel.tsx, you'll see we import the same useGlobals hook.

No, importing it in PanelContent does not work, because it's not a direct child that gets rendered inside manager.ts perhaps this will change in future versions of Storybook.

We therefor let PanelContent.tsx accept two props:

  • All the actual baseStyling props
  • An updateProperties function

Inside the PanelContent.tsx file we simply have a React state variable that keeps track of all of our table rows. Whenever a property value changes, we execute the renderContent function to update all of our table rows. We only trigger a complete update of our sessionStorage variable if an input fires the onBlur event (for performance' sake).

As soon as an onBlur event gets fired, we fire the propertyChanged function that was passed as a prop to PanelContent.tsx. If you go back to Panel.tsx you see it actually updates the baseStyling global variable.

Going back to withBaseStyling.ts you'll see we have a useEffect which listens to any changes made to the baseStyling global. If anything changes, we fire the updateCSSPropertiesOnHTMLElement function, and pass this baseStyling global.

We convert the object back to a valid style tag value, JSON.stringify it, and set it as sessionStorage. After that, we fire a custom event called updated-styling. The file that actually listens to this event is /.storybook/preview.js.

There's a Preview component which has a properties state. This state variable gets used to force the inner Story to rerender whenever you change anything inside the BaseStyling addon. This is needed, because otherwise the story will always be one step behind in rendering the correct values passed to the BaseStyling addon.

For example the initial value is yellow. If you change the value to red, the sessionStorage variable will be updated, but the actual story still rendered with the previous sessionStorage. So imagine you change the red to blue now, it'll render with the red variable intact. This makes sure the Story and BaseStyling addon are synced up properly.

Development

Development for this plugin is quite easy. Either use yarn or npm for the following commands.

Installation

Go into the addon folder and type:

npm install

Active development

For a storybook instance execute:

npm run start

To build and watch the addon execute:

npm run build:watch

This will rebuild the entire addon whenever you make changes to any files in the src directory. You will have to completely refresh our the storybook page. There is no hot reloading.

Build for production

npm run build