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

@ima/storybook-integration

v19.6.1

Published

Storybook integration addon for IMA.js applications.

Downloads

2

Readme


Init Storybook

First init storybook inside your application using official installation guide. Skip babel init (installing dependencies and creating .babelrc) when asked, we replace babel with SWC.

npx storybook@latest init

Installation

npm install @ima/storybook-integration -D

Usage

Register addon in config in .storybook/main.js. Optionally you can set custom language, for loading language files, or set CLI plugins to skip (some plugins might not work well with Storybook, such as minimizers/scramblers).

const config = {
  // ...
  addons: [
    {
      name: "@ima/storybook-integration",
      options: {
        language: 'cs',
        skipPlugins: ['ScrambleCssPlugin']
      }
    }
  ],
  // ....
};

export default config;

Overriding boot config, $IMA global object and PageState

You can easily override boot config functions, global window.$IMA and set page state using parameters.ima:

export const Story = {
  parameters: {
    ima: {
      state: { posts: [] }, // IMA PageState
      $IMA: { $Root: '' }, // window.$IMA object
      initBindApp: (...args) => {},
      initRoutes: (...args) => {},
      initServicesApp: (...args) => {},
      initSettings: (...args) => {
        return {
          prod: {
            links: {
              tutorial: 'https://google.com',
            },
          },
        };
      },
    },
  },
};

Where settings are deeply merged with the ones from app settings function. You can use this on per-story basis or define global overrides.

Overriding boot config using initializers

Since parameters are deeply merged across storybook stories and configurations, if you want to create some global overrides and still be able to override certain things on story-basis, you can use initializers instead.

Initializers are functions that are called after boot config is created, but before the story boot config params. This allows you to register multiple initializers, with certain priority and make sure that all of them are called before story boot config is created.

Register your initializers in preview.ts file:

import { registerImaInitializer } from '@ima/storybook-integration';

registerImaInitializer(storybookArgs => {
  return {
    initServicesApp: (ns, oc) => {
      if (storybookArgs.parameters.ima.fireRouteEvents) {
        oc.get('$Dispatcher').fire(RouterEvents.BEFORE_HANDLE_ROUTE, {});
        oc.get('$Dispatcher').fire(RouterEvents.AFTER_HANDLE_ROUTE, {});
      }
    },
  };
}, 100); // Execution priority

isStorybook helper

You can use this helper to check if you are within storybook environment. This is useful for example when you want to render something only in storybook.

import { isStorybook } from '@ima/storybook-integration/helpers';


export function Header() {
  return (
    <div>
      {isStorybook() ? (
        <div>Rendered only in storybook</div>
      ) : (
        <div>Rendered only in app</div>
      )}
    </div>
  );
};

This pattern should be used only as last resort, you should use storybook native features like args or decorators when possible.

Decorators & other utilities

The package also exports some additional utilities and decorates you can use in your stories. All are available from default export.

import { withPageContext } from '@ima/storybook-integration';

export const Story = {
  decorators: [withPageContext],
};
  • withPageContext - adds pageContext to your story. It is used already as root decorator when using ima storybook-integration. So this is usefull only in niche cases.

TypeScript support

Add following to your tsconfig.json. Since we are not importing anything from this packaged (in default state), the types would not be loaded automatically without following option.

{
  "compilerOptions": {
    "types": ["./node_modules/@ima/storybook-integration/dist/index.d.ts"],
  }
}