Skip to content

Comments

feat: Add CSF Next compatibility for definePreview addons pattern#404

Merged
valentinpalkovic merged 4 commits intochromaui:nextfrom
jthrilly:feat/csf-next-compatibility
Feb 6, 2026
Merged

feat: Add CSF Next compatibility for definePreview addons pattern#404
valentinpalkovic merged 4 commits intochromaui:nextfrom
jthrilly:feat/csf-next-compatibility

Conversation

@jthrilly
Copy link
Contributor

@jthrilly jthrilly commented Jan 19, 2026

Below was generated via Claude code, but based on my own research into the cause of this issue: #400

I manually reviewed the change and also tested it locally to verify. Please let me know if there's more you'd like me to do.

Summary

This PR adds compatibility with Storybook's CSF Next pattern where addons are imported and invoked as functions in the preview configuration.

Problem

In CSF Next, the preview file uses definePreview() and addons are expected to be functions that return PreviewAddon objects:

import addonChromatic from '@chromatic-com/storybook';
import { definePreview } from '@storybook/your-framework';

export default definePreview({
  addons: [addonChromatic()],  // ❌ Currently fails: {} is not a function
});

The current implementation exports an empty object {}, which causes a runtime error when invoked as a function.

Solution

Update the default export to be a function that returns a proper PreviewAddon via definePreviewAddon({}):

import { definePreviewAddon } from 'storybook/internal/csf';

export default () => definePreviewAddon({});

Since this addon only provides:

  • Server-side preset functionality (build monitoring, static assets)
  • Manager UI (the Visual Tests panel)

...and no preview annotations (decorators, parameters, globals), the preview addon exports an empty configuration object.

Breaking Changes

None. The addon can still be used in main.ts via the string format for preset loading, and now additionally works in the CSF Next definePreview({ addons: [...] }) pattern.

Test plan

  • Build succeeds with yarn build
  • TypeScript type checking passes with yarn typecheck
  • [ X] Manual testing with CSF Next project (Storybook 10.1+)

🤖 Generated with Claude Code

Copilot AI review requested due to automatic review settings January 19, 2026 06:42
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds compatibility with Storybook's CSF Next pattern, enabling the addon to be used as a function in definePreview({ addons: [...] }) configurations. Previously, the addon exported an empty object which caused runtime errors when invoked as a function in CSF Next projects.

Changes:

  • Modified the default export from an empty object to a function returning a PreviewAddon
  • Added comprehensive documentation explaining the addon's purpose and usage pattern
  • Imported definePreviewAddon from storybook/internal/csf to create proper addon structure

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

src/index.ts Outdated
* addons: [addonChromatic()],
* });
*/
export default () => definePreviewAddon({});
Copy link

Copilot AI Jan 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a test to verify that the default export is a function that returns a valid PreviewAddon object. This would ensure the CSF Next compatibility works as intended and prevent regressions. The test could verify that calling the exported function returns an object with the expected structure from definePreviewAddon.

Copilot uses AI. Check for mistakes.
Update the default export to be compatible with Storybook's CSF Next
pattern where addons are imported and invoked as functions in the
preview configuration.

Previously, the package exported an empty object `{}` which would cause
a runtime error when invoked as a function. Now it exports a function
that returns a proper PreviewAddon via `definePreviewAddon({})`.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@jthrilly jthrilly force-pushed the feat/csf-next-compatibility branch from 9c48d02 to ad1436a Compare January 19, 2026 06:50
@valentinpalkovic valentinpalkovic moved this to Empathy Queue (prioritized) in Core Team Projects Feb 6, 2026
@valentinpalkovic valentinpalkovic self-assigned this Feb 6, 2026
@valentinpalkovic valentinpalkovic added the patch Auto: Increment the patch version when merged label Feb 6, 2026
@valentinpalkovic valentinpalkovic moved this from Empathy Queue (prioritized) to In Progress in Core Team Projects Feb 6, 2026
kasperpeulen
kasperpeulen approved these changes Feb 6, 2026
Copy link
Contributor

@kasperpeulen kasperpeulen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Verified locally:

  • yarn build succeeds — produces dist/index.d.ts (3.95 KB) with proper type declarations
  • yarn typecheck passes with no errors
  • Generated .d.ts correctly exports () => PreviewAddon<ChromaticTypes>
  • No breaking changes for existing users (string-based addon loading in main.ts still works)

The core fix is clean and minimal — changing export default {} to export default () => definePreviewAddon<ChromaticTypes>({}) is the right approach since this addon has no preview annotations to inject.

Minor note: the generated declaration file includes the CONFIG_TYPE and LOGLEVEL global declarations from types.ts since tsup bundles everything into one .d.ts. Not a blocker, but worth being aware of.

@valentinpalkovic valentinpalkovic added the release Auto: Create a `latest` release when merged label Feb 6, 2026
@valentinpalkovic valentinpalkovic merged commit 01b63e6 into chromaui:next Feb 6, 2026
1 check failed
@github-project-automation github-project-automation bot moved this from In Progress to Done in Core Team Projects Feb 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

patch Auto: Increment the patch version when merged release Auto: Create a `latest` release when merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants