Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Show Code Open by default #10430

Closed
kschoi opened this issue Apr 15, 2020 · 14 comments
Closed

docs: Show Code Open by default #10430

kschoi opened this issue Apr 15, 2020 · 14 comments

Comments

@kschoi
Copy link

kschoi commented Apr 15, 2020

Hey team, I noticed the issue #8356 closed.
I'd only like to have an option to customize the story view (especially source code view), too.
Have you got any plan to resolve this issue?

addParameters({ docs: { previewSource: 'open' } })
@SimplyY
Copy link

SimplyY commented Sep 23, 2020

@shilman has new progress?

@shilman shilman added the P1 label Sep 25, 2020
@shilman shilman self-assigned this Sep 25, 2020
@ldeveber
Copy link

This would be lovely, I was just searching the docs on how to do this!

@signal-intrusion
Copy link

🤞 my current workaround is to poll the page for show code buttons and click them. It's crazy janky.

@shilman shilman added this to the 6.1 docs milestone Oct 22, 2020
@ron0115
Copy link

ron0115 commented Oct 30, 2020

🤞 my current workaround is to poll the page for show code buttons and click them. It's crazy janky.

@signal-intrusion How can you solve the problem by this hacking tricks.Could you pls show the code?Thank you

@AshConnolly
Copy link

Any progress on this?

@shilman shilman modified the milestones: 6.1 docs, 6.2 docs Nov 24, 2020
@signal-intrusion
Copy link

signal-intrusion commented Nov 30, 2020

@ron0115 In preview.js I added this:

// this little bit of hacky code opens code samples
window.addEventListener('load', () => {
  let loc = window.location.href
  showCodeSamples()

  window.setInterval(() => {
    let newLoc = window.location.href

    if (newLoc !== loc) {
      loc = newLoc
      showCodeSamples()
    }
  }, 300)
})

function showCodeSamples() {
  try {
    const docs = document.querySelectorAll('.sbdocs')

    ;[].forEach.call(docs, (el) => {
      const buttons = el.querySelectorAll('button')
      const codeButton = [].find.call(buttons, (el) => el.textContent === 'Show code')
      if (codeButton) {
        codeButton.click()
      }
    })
  } catch (e) {
    console.warn(e)
  }
}

@frassinier
Copy link
Contributor

I'm looking for a review, please👆🏻

@shilman
Copy link
Member

shilman commented Apr 30, 2021

Gadzooks!! I just released https://github.com/storybookjs/storybook/releases/tag/v6.3.0-alpha.17 containing PR #14729 that references this issue. Upgrade today to the @next NPM tag to try it out!

npx sb upgrade --prerelease

Closing this issue. Please re-open if you think there's still more to do.

@tklives
Copy link

tklives commented Jun 3, 2021

Leaving this here to hopefully help someone in the future...

If you are just looking to have the code block open for a single story, you just need to add withSource="open" to your <Canvas> tag that wraps the story:

<Canvas withSource="open">
  <Story name="Cool story bro">
    <>
  </Story>
</Canvas>

Hope this helps someone like myself, since this isn't documented anywhere that I could find.

Cheers!

@jared-christensen
Copy link
Contributor

@JUNNNI
Copy link

JUNNNI commented Jan 5, 2023

Based on shilman comment above, for those that are looking on how to implement it:

// .storybook/preview.ts

export const parameters = {
    docs: {
        source: {
            state: 'open',
        },
    }
}

@ryuheechul
Copy link

now with storybook 7, it seems to be changed to following and it works for me:

// .storybook/preview.js

export const parameters = {
  // ...
  docs: {
    canvas: {
      sourceState: 'shown'
    }
  }
};

Couldn't find a corresponding documentation on this though.

@kasperskov-pdk
Copy link

now with storybook 7, it seems to be changed to following and it works for me:

// .storybook/preview.js

export const parameters = {
  // ...
  docs: {
    canvas: {
      sourceState: 'shown'
    }
  }
};

Couldn't find a corresponding documentation on this though.

As I read the source code, your example is on story level, not global in preview.js. For example, the enum for SourceState is still close or open.

export enum SourceState {
  OPEN = 'open',
  CLOSED = 'closed',
  NONE = 'none',
}

But then again the function filtering the states is on story level with the "old" docs.source.state:
const states = stories.map((story) => story.parameters.docs?.source?.state).filter(Boolean);
Readme still refers to docs.source.state See here
I'm so confused.

Anyways neither solution works for me on storybook 7.4.0. Anyone else still having issues?

@jared-christensen
Copy link
Contributor

I found the docs for it. https://storybook.js.org/docs/react/api/doc-block-canvas#sourcestate

abiriadev added a commit to abiriadev/storybook that referenced this issue Apr 18, 2024
For storybook 7 or higher, the code below does not work.

```js
export const parameters = {
  docs: {
    source: {
      state: 'open',
    },
  },
};
```

It should be fixed like below:

```js
export const parameters = {
  docs: {
    canvas: {
      sourceState: 'shown'
    }
  }
};
```

However, this document seems not changed so far. So I updated it.

## References:

- https://storybook.js.org/docs/api/doc-block-canvas#sourcestate
- storybookjs#10430
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests