Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 6 additions & 29 deletions .storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import type { StorybookConfig } from '@storybook-vue/nuxt'

const config = {
stories: ['../app/**/*.stories.@(js|ts)'],
addons: ['@storybook/addon-a11y', '@storybook/addon-docs', '@storybook/addon-themes'],
addons: [
'@storybook/addon-a11y',
'@storybook/addon-docs',
'@storybook/addon-themes',
'storybook-i18n',
],
framework: '@storybook-vue/nuxt',
staticDirs: ['./.public'],
features: {
Expand All @@ -19,34 +24,6 @@ const config = {
}
},
})
// Replace the built-in vue-docgen plugin with a fault-tolerant version.
// vue-docgen-api can crash on components that import types from other
// .vue files (it tries to parse the SFC with @babel/parser as plain TS).
// This wrapper catches those errors so the build doesn't fail.
const docgenPlugin = config.plugins?.find(
(p): p is Extract<typeof p, { name: string }> =>
!!p && typeof p === 'object' && 'name' in p && p.name === 'storybook:vue-docgen-plugin',
)

if (docgenPlugin && 'transform' in docgenPlugin) {
const hook = docgenPlugin.transform
// Vite plugin hooks can be a function or an object with a `handler` property
const originalFn = typeof hook === 'function' ? hook : hook?.handler
if (originalFn) {
const wrapped = async function (this: unknown, ...args: unknown[]) {
try {
return await originalFn.apply(this, args)
} catch {
return undefined
}
}
if (typeof hook === 'function') {
docgenPlugin.transform = wrapped as typeof hook
} else if (hook) {
hook.handler = wrapped as typeof hook.handler
}
}
}

return config
},
Expand Down
9 changes: 2 additions & 7 deletions .storybook/manager.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { addons } from 'storybook/manager-api'
import { create } from 'storybook/theming'

const npmxTheme = create({
brandTitle: 'npmx Storybook',
brandImage: '/npmx-storybook.svg',
})
import npmxDark from './theme'

addons.setConfig({
theme: npmxTheme,
theme: npmxDark,
})
6 changes: 6 additions & 0 deletions .storybook/preview-head.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<style>
/* Override docs story canvas background to match npmx theme */
.docs-story {
background-color: var(--bg, oklch(0.171 0 0)) !important;
}
</style>
Comment on lines +1 to +6
Copy link
Contributor

@coderabbitai coderabbitai bot Mar 6, 2026

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

The docs canvas background never follows the selected theme.

Line 4 depends on --bg, but Storybook never sets that variable in the provided setup: .storybook/preview.ts:62-82 only updates --accent-color, and .storybook/theme.ts:1-12 sets appContentBg for the manager UI rather than a CSS custom property. As a result, this always falls back to the dark value, so Light theme docs still render on a dark canvas. Please either initialise --bg in Storybook or switch this rule to a selector that keys off data-theme directly.

Copy link
Contributor Author

@cylewaitforit cylewaitforit Mar 6, 2026

Choose a reason for hiding this comment

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

The backgrounds in the autodocs pages look correct with this when viewing the light versions of components and removing this makes them not display correctly. Would love to find a way to fix this in the storybook theme overrides instead of needing this preview-head file but at this time have been unsuccessful.

Copy link
Contributor

Choose a reason for hiding this comment

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

@cylewaitforit, thanks for the context! This is actually a well-known Storybook limitation. The dark theme is applied correctly in the canvas view but not in the Docs tab — the DOM inside Docs has extra wrappers and inline styles that override the background and theme styles. This is a tracked open bug (#30928) in Storybook, so your preview-head.html workaround is reasonable and pragmatic.

For a cleaner alternative (when you want to revisit), the closest officially-documented approach is:

  1. Use parameters.docs.theme with an appContentBg value. Storybook Docs uses the same theme system as Storybook's UI but is themed independently from the main UI. The default theme for Docs is always the "light" theme, regardless of the main UI theme. You can pass a custom theme specifically to docs in .storybook/preview.ts:

    import { npmxDark } from './theme';
    // in preview parameters:
    docs: {
      theme: npmxDark, // applies appContentBg to the docs page wrapper
    }

    However, this styles the docs page background, not the individual .docs-story canvas iframes — which is why preview-head.html is still needed for the story canvas itself.

  2. Acknowledge the !important risk: Since preview-head.html uses !important to override .docs-story, it will prevent any future per-story background customisation. If you later need story-specific backgrounds, you'd need to revisit this rule.

Given the upstream bug, the current preview-head.html approach is a valid and widely-used workaround. I'm withdrawing my original concern — the fix is intentional and appropriate for the current state of Storybook.

(ミ^ ◡ ^ミ) ✦ noted for future reviews!


✏️ Learnings added
Learnt from: cylewaitforit
Repo: npmx-dev/npmx.dev PR: 1964
File: .storybook/preview-head.html:1-6
Timestamp: 2026-03-06T17:44:59.085Z
Learning: In the npmx.dev project, the `.storybook/preview-head.html` file sets `background-color` on `.docs-story` via the `--bg` CSS custom property to fix the Autodocs canvas background. This is an intentional workaround for a known Storybook bug (storybookjs/storybook#30928) where `addon-themes` and `parameters.docs.theme` do not apply to the individual story canvas iframes in the Autodocs tab. Removing this file causes the autodocs pages to not display correctly.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.


🧠 Learnings used
Learnt from: alexdln
Repo: npmx-dev/npmx.dev PR: 1042
File: app/assets/main.css:120-129
Timestamp: 2026-02-06T11:09:31.529Z
Learning: In the npmx.dev project's app/assets/main.css, the `:root[data-theme='light'][data-bg-theme='black']` variant intentionally only overrides `--bg-subtle` and reuses the default light theme values for `--bg`, `--bg-muted`, and `--bg-elevated`. This is by design because the default light theme is mostly neutral, so selective override is sufficient for the black theme variant.

55 changes: 30 additions & 25 deletions .storybook/preview.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { Preview } from '@storybook-vue/nuxt'
import { withThemeByDataAttribute } from '@storybook/addon-themes'
import { addons } from 'storybook/preview-api'
import { currentLocales } from '../config/i18n'
import { fn } from 'storybook/test'
import { ACCENT_COLORS } from '../shared/utils/constants'
import npmxDark from './theme'

// related: https://github.com/npmx-dev/npmx.dev/blob/1431d24be555bca5e1ae6264434d49ca15173c43/test/nuxt/setup.ts#L12-L26
// Stub Nuxt specific globals
Expand All @@ -17,6 +19,14 @@ globalThis['__NUXT_COLOR_MODE__'] ??= {
// @ts-expect-error - dynamic global name
globalThis.defineOgImageComponent = fn()

// Subscribe to locale changes from storybook-i18n addon (once, outside decorator)
let currentI18nInstance: any = null
addons.getChannel().on('LOCALE_CHANGED', (newLocale: string) => {
if (currentI18nInstance) {
currentI18nInstance.setLocale(newLocale)
}
})

const preview: Preview = {
parameters: {
controls: {
Expand All @@ -25,25 +35,22 @@ const preview: Preview = {
date: /Date$/i,
},
},
docs: {
theme: npmxDark,
},
},
initialGlobals: {
locale: 'en-US',
locales: currentLocales.reduce(
(acc, locale) => {
acc[locale.code] = locale.name
return acc
},
{} as Record<string, string>,
),
},
// Provides toolbars to switch things like theming and language
globalTypes: {
locale: {
name: 'Locale',
description: 'UI language',
defaultValue: 'en-US',
toolbar: {
icon: 'globe',
dynamicTitle: true,
items: [
// English is at the top so it's easier to reset to it
{ value: 'en-US', title: 'English (US)' },
...currentLocales
.filter(locale => locale.code !== 'en-US')
.map(locale => ({ value: locale.code, title: locale.name })),
],
},
},
accentColor: {
name: 'Accent Color',
description: 'Accent color',
Expand All @@ -70,9 +77,9 @@ const preview: Preview = {
attributeName: 'data-theme',
}),
(story, context) => {
const { locale, accentColor } = context.globals as {
locale: string
const { accentColor, locale } = context.globals as {
accentColor?: string
locale?: string
}

// Set accent color from globals
Expand All @@ -84,14 +91,12 @@ const preview: Preview = {

return {
template: '<story />',
// Set locale from globals
created() {
if (this.$i18n) {
this.$i18n.setLocale(locale)
}
},
updated() {
if (this.$i18n) {
// Store i18n instance for LOCALE_CHANGED events
currentI18nInstance = this.$i18n

// Set initial locale when component is created
if (locale && this.$i18n) {
this.$i18n.setLocale(locale)
}
},
Expand Down
13 changes: 13 additions & 0 deletions .storybook/theme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { create } from 'storybook/theming/create'

const npmxDark = create({
base: 'dark',

brandTitle: 'npmx Storybook',
brandImage: '/npmx-storybook.svg',

// UI
appContentBg: '#101010', // oklch(0.171 0 0)
})

export default npmxDark
60 changes: 0 additions & 60 deletions app/components/Button/Base.stories.ts

This file was deleted.

7 changes: 7 additions & 0 deletions app/components/Button/Base.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<script setup lang="ts">
import type { IconClass } from '~/types'

/**
* A base button component that supports multiple variants, sizes, and states as well as icons and keyboard shortcuts.
*/
defineOptions({
name: 'ButtonBase',
})

const props = withDefaults(
defineProps<{
disabled?: boolean
Expand Down
116 changes: 116 additions & 0 deletions app/components/Button/ButtonBase.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import type { Meta, StoryObj } from '@storybook-vue/nuxt'
import ButtonBase from './Base.vue'

const meta = {
component: ButtonBase,
parameters: {
docs: {
source: {
type: 'dynamic',
transform: (code: string) =>
code.replace(/<Base\b/g, '<ButtonBase').replace(/<\/Base>/g, '</ButtonBase>'),
},
},
},
tags: ['autodocs'],
} satisfies Meta<typeof ButtonBase>

export default meta
type Story = StoryObj<typeof meta>

export const Default: Story = {
args: {
default: 'Button Text',
},
}
Comment on lines +21 to +25
Copy link
Contributor

@coderabbitai coderabbitai bot Mar 6, 2026

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Default story may not render slot content without a render function.

All other stories use explicit render functions with templates to handle slot content, but Default relies on args.default without a render function. In Vue Storybook, this pattern typically requires either a render function or specific argTypes configuration to pass slot content properly.

Consider adding a render function for consistency and to ensure the button text displays:

Proposed fix
 export const Default: Story = {
-  args: {
-    default: 'Button Text',
-  },
+  render: args => ({
+    components: { ButtonBase },
+    setup() {
+      return { args }
+    },
+    template: `<ButtonBase v-bind="args">Button Text</ButtonBase>`,
+  }),
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export const Default: Story = {
args: {
default: 'Button Text',
},
}
export const Default: Story = {
render: args => ({
components: { ButtonBase },
setup() {
return { args }
},
template: `<ButtonBase v-bind="args">Button Text</ButtonBase>`,
}),
}

Copy link
Contributor Author

@cylewaitforit cylewaitforit Mar 6, 2026

Choose a reason for hiding this comment

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

Keeping the default story with the default render allow to someone viewing the storybook to change the story to alternative text in place of Button Text. The other stories use the template render in order for the internationalization to work.

Copy link
Contributor

Choose a reason for hiding this comment

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

@cylewaitforit, that makes perfect sense! Using args.default without a render function is the correct approach here — it exposes the slot content as a Storybook control, allowing users to interactively edit the button text directly from the controls panel. The render function approach in the other stories is needed for i18n to work, but would prevent that interactive editing for the Default story. Thanks for the clarification!

ʕ •`ᴥ´• ʔ


✏️ Learnings added
Learnt from: cylewaitforit
Repo: npmx-dev/npmx.dev PR: 1964
File: app/components/Button/ButtonBase.stories.ts:21-25
Timestamp: 2026-03-06T18:16:31.538Z
Learning: In the npmx.dev project's ButtonBase.stories.ts (app/components/Button/ButtonBase.stories.ts), the `Default` story intentionally uses `args.default` without a render function so that Storybook users can interactively edit the slot content (button text) via the controls panel. Other stories (e.g. Primary, Secondary, etc.) use explicit render functions with templates specifically to support storybook-i18n internationalization. Do not flag the `Default` story's lack of a render function as an issue.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.


🧠 Learnings used
Learnt from: userquin
Repo: npmx-dev/npmx.dev PR: 1335
File: app/components/Compare/FacetSelector.vue:72-78
Timestamp: 2026-02-10T15:47:40.710Z
Learning: In the npmx.dev project, the `ButtonBase` component (app/components/ButtonBase.vue or similar) provides default `border border-border` styles. When styling ButtonBase instances, avoid adding duplicate `border` classes as this triggers the HTML validator error `no-dup-class` and fails CI.

Learnt from: cylewaitforit
Repo: npmx-dev/npmx.dev PR: 1964
File: .storybook/preview-head.html:1-6
Timestamp: 2026-03-06T17:44:59.085Z
Learning: In the npmx.dev project, the `.storybook/preview-head.html` file sets `background-color` on `.docs-story` via the `--bg` CSS custom property to fix the Autodocs canvas background. This is an intentional workaround for a known Storybook bug (storybookjs/storybook#30928) where `addon-themes` and `parameters.docs.theme` do not apply to the individual story canvas iframes in the Autodocs tab. Removing this file causes the autodocs pages to not display correctly.


export const Primary: Story = {
args: {
variant: 'primary',
},
render: args => ({
components: { ButtonBase },
setup() {
return { args }
},
template: `<ButtonBase v-bind="args">{{ $t("nav.settings") }}</ButtonBase>`,
}),
}

export const Secondary: Story = {
args: {
variant: 'secondary',
},
render: args => ({
components: { ButtonBase },
setup() {
return { args }
},
template: `<ButtonBase v-bind="args">{{ $t("nav.settings") }}</ButtonBase>`,
}),
}

export const Small: Story = {
args: {
size: 'small',
},
render: args => ({
components: { ButtonBase },
setup() {
return { args }
},
template: `<ButtonBase v-bind="args">{{ $t("nav.settings") }}</ButtonBase>`,
}),
}

export const Disabled: Story = {
args: {
disabled: true,
},
render: args => ({
components: { ButtonBase },
setup() {
return { args }
},
template: `<ButtonBase v-bind="args">{{ $t("nav.settings") }}</ButtonBase>`,
}),
}

export const WithIcon: Story = {
args: {
classicon: 'i-lucide:search',
},
render: args => ({
components: { ButtonBase },
setup() {
return { args }
},
template: `<ButtonBase v-bind="args">{{ $t("search.button") }}</ButtonBase>`,
}),
}

export const WithKeyboardShortcut: Story = {
args: {
ariaKeyshortcuts: '/',
},
render: args => ({
components: { ButtonBase },
setup() {
return { args }
},
template: `<ButtonBase v-bind="args">{{ $t("search.button") }}</ButtonBase>`,
}),
}

export const Block: Story = {
args: {
block: true,
},
render: args => ({
components: { ButtonBase },
setup() {
return { args }
},
template: `<ButtonBase v-bind="args">{{ $t("nav.settings") }}</ButtonBase>`,
}),
}
Loading
Loading