Skip to content

Conversation

ia319
Copy link
Member

@ia319 ia319 commented Oct 11, 2025

Closes #32683

What I did

Added usePrimaryStory hook to centralize primary story selection.

Refactored Primary.tsx to use usePrimaryStory instead of selecting stories[0].

Refactored Controls.tsx to call usePrimaryStory when no specific story is provided via of.

Checklist for Contributors

Testing

The changes in this PR are covered in the following automated tests:

  • stories
  • unit tests
  • integration tests
  • end-to-end tests

Manual testing

Translation:

Before modification:

Write a sample sandbox:

const meta = preview.meta({
  title: 'Example/Button',
  component: Button,
  parameters: {
    layout: 'centered',
  },
  tags: ['autodocs'], // **whether this line exists or not makes no difference**
  argTypes: {
    backgroundColor: { control: 'color' },
  },
  args: { onClick: fn() },
});

export const Primary = meta.story({
  args: {
    primary: true,
    label: 'utton',
  },
  tags: ["!autodocs"]
});

export const Secondary = meta.story({
  args: {
    label: 'Bllutton',
    size: 'medium',
  },
  tags: ["autodocs"] // **It works the same even without this line.**
});
Screenshot 2025-10-12 011351

From the logs, we can see that the handling of tags in
code/core/src/preview-api/modules/store/csf/prepareStory.ts is correct.
The issue lies in
code/addons/docs/src/blocks/blocks/Primary.tsx,
where const primaryStory = context.componentStoriesFromCSFFile(csfFile)[0];
always selects the first story directly.

After modification:
Screenshot 2025-10-12 014005
Primary.tsx and Controls.tsx now correctly retrieve the stories with the autodocs tag.

Documentation

  • Add or update documentation reflecting your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Checklist for Maintainers

  • When this PR is ready for testing, make sure to add ci:normal, ci:merged or ci:daily GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found in code/lib/cli-storybook/src/sandbox-templates.ts

  • Make sure this PR contains one of the labels below:

    Available labels
    • bug: Internal changes that fixes incorrect behavior.
    • maintenance: User-facing maintenance tasks.
    • dependencies: Upgrading (sometimes downgrading) dependencies.
    • build: Internal-facing build tooling & test updates. Will not show up in release changelog.
    • cleanup: Minor cleanup style change. Will not show up in release changelog.
    • documentation: Documentation only changes. Will not show up in release changelog.
    • feature request: Introducing a new feature.
    • BREAKING CHANGE: Changes that break compatibility in some way with current major version.
    • other: Changes that don't fit in the above categories.

🦋 Canary release

This PR does not have a canary release associated. You can request a canary release of this pull request by mentioning the @storybookjs/core team here.

core team members can create a canary release here or locally with gh workflow run --repo storybookjs/storybook publish.yml --field pr=<PR_NUMBER>

Summary by CodeRabbit

  • New Features

    • Docs pages now auto-select a primary story when available (based on autodocs).
    • Controls more reliably determine and display the current story.
  • Bug Fixes

    • Prevents crashes when no story is available; components now render nothing gracefully.
  • Refactor

    • Primary display simplified: no external props required and story selection is internal.
    • Streamlined story-resolution logic across docs blocks for consistency.

- add usePrimaryStory hook to centralize primary story selection
- refactor Primary, Controls to use the hook instead of stories[0]
- ensure !autodocs stories are correctly hidden in all doc blocks
Copy link
Contributor

coderabbitai bot commented Oct 11, 2025

📝 Walkthrough

Walkthrough

Refactors Controls and Primary to resolve the current story via a new usePrimaryStory hook instead of context.resolveOf or props. Adds usePrimaryStory to select the first autodocs-tagged story from DocsContext.componentStories(). Components early-return null when no story is found.

Changes

Cohort / File(s) Summary
Docs blocks: Controls and Primary refactor
code/addons/docs/src/blocks/blocks/Controls.tsx, code/addons/docs/src/blocks/blocks/Primary.tsx
Controls: stop using context.resolveOf(...); use usePrimaryStory() and return null if no story. Primary: remove props/validation, switch from FC<PrimaryProps> to FC and obtain primary story via usePrimaryStory(); render DocsStory only when available.
New hook: primary story resolution
code/addons/docs/src/blocks/blocks/usePrimaryStory.ts
Add usePrimaryStory() hook: reads DocsContext, calls componentStories(), and returns the first story tagged autodocs (returns `PreparedStory

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor User
  participant DocsPage
  participant Primary
  participant Controls
  participant usePrimaryStory as usePrimaryStory()
  participant DocsContext

  User->>DocsPage: open docs
  DocsPage->>Primary: render
  DocsPage->>Controls: render

  rect rgba(230,245,255,0.5)
    Primary->>usePrimaryStory: request primary story
    Controls->>usePrimaryStory: request primary story
    usePrimaryStory->>DocsContext: componentStories()
    DocsContext-->>usePrimaryStory: [stories]
    usePrimaryStory-->>Primary: PreparedStory | undefined
    usePrimaryStory-->>Controls: PreparedStory | undefined
  end

  alt Story found
    Primary->>DocsPage: render DocsStory(primary)
    Controls->>DocsPage: render controls(primary)
  else No story
    Primary-->>DocsPage: return null
    Controls-->>DocsPage: return null
  end

  note right of usePrimaryStory: selects first story with tag "autodocs"
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4da52f8 and ef59364.

📒 Files selected for processing (1)
  • code/addons/docs/src/blocks/blocks/usePrimaryStory.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • code/addons/docs/src/blocks/blocks/usePrimaryStory.ts

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
code/addons/docs/src/blocks/blocks/usePrimaryStory.ts (1)

7-10: Consider clarifying the doc comment.

The comment states "filters out stories with the '!autodocs' tag", which is technically correct but could be more direct. Consider rephrasing to explicitly state what the hook returns.

Apply this diff to clarify the documentation:

 /**
- * A hook to get the primary story for the current component It filters out stories with the
- * '!autodocs' tag.
+ * A hook to get the primary story for the current component. Returns the first story
+ * that includes the 'autodocs' tag (excluding stories tagged with '!autodocs').
  */
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e9a2317 and 4da52f8.

📒 Files selected for processing (3)
  • code/addons/docs/src/blocks/blocks/Controls.tsx (2 hunks)
  • code/addons/docs/src/blocks/blocks/Primary.tsx (1 hunks)
  • code/addons/docs/src/blocks/blocks/usePrimaryStory.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{ts,tsx,js,jsx}

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Adhere to ESLint and Prettier rules across all JS/TS source files

Files:

  • code/addons/docs/src/blocks/blocks/Controls.tsx
  • code/addons/docs/src/blocks/blocks/Primary.tsx
  • code/addons/docs/src/blocks/blocks/usePrimaryStory.ts
**/*.{ts,tsx}

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Fix type errors and prefer precise typings instead of using any or suppressions, consistent with strict mode

Files:

  • code/addons/docs/src/blocks/blocks/Controls.tsx
  • code/addons/docs/src/blocks/blocks/Primary.tsx
  • code/addons/docs/src/blocks/blocks/usePrimaryStory.ts
🧬 Code graph analysis (3)
code/addons/docs/src/blocks/blocks/Controls.tsx (1)
code/addons/docs/src/blocks/blocks/usePrimaryStory.ts (1)
  • usePrimaryStory (11-15)
code/addons/docs/src/blocks/blocks/Primary.tsx (1)
code/addons/docs/src/blocks/blocks/usePrimaryStory.ts (1)
  • usePrimaryStory (11-15)
code/addons/docs/src/blocks/blocks/usePrimaryStory.ts (1)
code/core/src/types/modules/story.ts (1)
  • PreparedStory (100-116)
🔇 Additional comments (2)
code/addons/docs/src/blocks/blocks/Primary.tsx (1)

7-13: LGTM! Clean refactor that centralizes story resolution.

The component simplification looks good:

  • Removes props-based complexity
  • Delegates story selection to usePrimaryStory hook
  • Properly handles the case when no autodocs-tagged story exists
  • Maintains the same rendering behavior for the primary story

This change aligns with the PR objective of fixing the !autodocs tag behavior for the first story.

code/addons/docs/src/blocks/blocks/Controls.tsx (1)

43-50: Approve Controls fallback logic

  • Uses usePrimaryStory when of prop is omitted (ensuring <Controls /> in DocsPage and MDX renders correctly).
  • Gracefully returns null when no story is available.
  • Aligns with the Primary component’s approach and resolves the autodocs visibility issue.

Comment on lines +11 to +15
export const usePrimaryStory = (): PreparedStory | undefined => {
const context = useContext(DocsContext);
const stories = context.componentStories();
return stories.find((story) => story.tags.includes('autodocs'));
};
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion | 🟠 Major

🧩 Analysis chain

LGTM! The hook logic correctly addresses the bug.

The implementation correctly selects the first story with the 'autodocs' tag, which excludes stories marked with '!autodocs'. This should fix the issue where stories tagged with '!autodocs' were appearing as the primary preview when positioned first.

Verify the fix resolves the reported issue by testing against the reproduction case:


🏁 Script executed:

#!/bin/bash
# Description: Search for test files or stories that might cover the !autodocs tag behavior

# Look for existing tests related to autodocs tags
rg -n --type=ts --type=tsx -C3 '\!autodocs|autodocs.*tag'

# Find stories or test files that might need verification
fd -e story -e test.ts -e test.tsx | head -20

Length of output: 1067


Add unit tests for '!autodocs' exclusion
No existing tests cover scenarios where a story is tagged !autodocs. Add tests (e.g., code/addons/docs/src/blocks/blocks/usePrimaryStory.test.ts) to verify that:

  • Stories with the 'autodocs' tag are selected.
  • Stories with the '!autodocs' tag are excluded.

@ia319 ia319 marked this pull request as draft October 11, 2025 12:10
@ia319 ia319 marked this pull request as ready for review October 11, 2025 18:02
@Sidnioulz Sidnioulz changed the title fix(docs): fix !autodocs stories showing in primary preview Bug: fix !autodocs stories showing in primary preview Oct 13, 2025
Copy link

nx-cloud bot commented Oct 15, 2025

View your CI Pipeline Execution ↗ for commit ef59364

Command Status Duration Result
nx run-many -t build --parallel=3 ✅ Succeeded 44s View ↗

☁️ Nx Cloud last updated this comment at 2025-10-15 08:12:54 UTC

@ia319
Copy link
Member Author

ia319 commented Oct 15, 2025

After pull from upstream and rebase, I ran yarn test and yarn vitest run addons/vitest/src/node/test-manager.test.ts locally. The tests passed, and I did not encounter this error. I don’t know how to proceed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: !autodocs does not hide the preview of the story in the documentation if it is the first story

3 participants