Skip to content

Conversation

@calebeby
Copy link
Member

@calebeby calebeby commented Jun 7, 2021

Overview

Closes #1004 - @tylersticka is it fine to close that issue or should we leave it open until we manually go through and update whichever stories need manual source code snippets?

Screenshots

Screen Shot 2021-06-07 at 2 07 18 PM

Testing

Different cases to test the "show code" functionality:

@changeset-bot
Copy link

changeset-bot bot commented Jun 7, 2021

⚠️ No Changeset found

Latest commit: edf2ab0

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@calebeby calebeby marked this pull request as ready for review June 7, 2021 21:16
@calebeby calebeby requested review from a team and tylersticka June 7, 2021 21:16
Paul-Hebert
Paul-Hebert previously approved these changes Jun 7, 2021
Copy link
Contributor

@Paul-Hebert Paul-Hebert left a comment

Choose a reason for hiding this comment

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

This is really clever @calebeby !

I assume it's a known short-coming that many of the code previews are loading a Storybook documentation-specific /demo/ twig partial? (I'm not sure what we could do about that besides providing a custom code sample for those stories so I don't think that should be a blocker.)

LGTM!

// the arguments and input path are stored in the window.__twig_inputs__ variable.
// __twig_inputs__ is a map between the output HTML and and objects with the arguments and input paths
// Here, since we have the rendered HTML, we can look up what the arguments and path were
// that correspond to that output
Copy link
Contributor

Choose a reason for hiding this comment

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

This is really interesting! Clever!

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks! This is the second approach, the first approach was to have the twig function return an object like this:

{
  toString() {
    return '...html...'
  },
  path: '...',
  args: { ... }
}

which I think is a nicer solution, and it didn't have the limitation of not working for the stories which use useEffect / other hooks. But then I realized it breaks for the canvas page, because the canvas page has a typeof check and it doesn't allow non-strings (even though an object with a toString method can be implicitly coerced into a string)

Object.keys(args).length > 0
? ` with ${JSON.stringify(args, null, 2)}`
: '';
return `{% include '${path}'${argsString} only %}`;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there ever a use case where we wouldn't want to use only? 🤔

If so, we might want to add a param to control this.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it is best practice for includes to avoid implicitly passing through variables. For our docs, I put the only there so that if someone copy-pastes it it will probably "do the right thing", and they have to opt-in to the implicit passing through behavior. I can't think of any of our demos where someone using our patterns would always or usually want to have the implicit passing through behavior.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah that makes sense 👍

Copy link
Member

Choose a reason for hiding this comment

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

I think that makes sense. We can always revisit later, and this is probably a safer starting point than leaving it off.

@calebeby
Copy link
Member Author

calebeby commented Jun 7, 2021

I assume it's a known short-coming that many of the code previews are loading a Storybook documentation-specific /demo/ twig partial? (I'm not sure what we could do about that besides providing a custom code sample for those stories so I don't think that should be a blocker.)

Yeah I talked through this with Tyler and it sounds like it is fine that it shows the include for the demo, and we can override the story source in those cases

@tylersticka
Copy link
Member

is it fine to close that issue or should we leave it open until we manually go through and update whichever stories need manual source code snippets?

@calebeby I think it's fine to close that issue, this satisfies the intent for sure

Copy link
Member

@tylersticka tylersticka left a comment

Choose a reason for hiding this comment

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

Deferring final approval to @cloudfour/dev, this is a great step forward!

@calebeby calebeby changed the title Generate source code previews automatically in most cases Generate source code previews Jun 7, 2021
@calebeby calebeby merged commit bca7ca8 into v-next Jun 7, 2021
@calebeby calebeby deleted the source-code-previews branch June 7, 2021 22:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve source code previews in Storybook

5 participants