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

Vue: Improve generated code snippets #27194

Merged
merged 30 commits into from
Jul 21, 2024

Conversation

larsrickert
Copy link
Contributor

@larsrickert larsrickert commented May 18, 2024

relates to #26691

What I did

I created a new improved Vue source code generator that has several improvements compared to the current one:

  • use shorthand for truthy booleans: my-prop instead of :my-prop="true"
  • Support double quoted strings like my-prop='"I am double quoted"', useful if e.g. passing SVG icon content as props
  • use correct source code for arrays (:my-props="['a', 'b']"), currently they are generated as object (:my-prop="{0:'a',1: 'b'}") which is incorrect
  • do not render "undefined / null" values
  • support for BigInt in props and slots
  • support for Symbol in props
  • do not add unnecessary <template #default> for default slot without bindings
  • improved source code generate when using slots with bindings
  • support for <script lang="ts" setup> (automatically move arrays and objects there)
  • support for v-models
  • support for slots that use VNodes / custom Vue components

Here is an example which compares the most important features of this PR:

Old:

<template>
  <SourceCode
    foo="Example string"
    :bar="42"
    :array="{0:'A',1:'B',2:'C'}"
    :object="{a:'Test A',b:42}"
    model-value="Model value"
  >
    <template #default>Default slot content</template>
    <template #namedSlots="{foo}">
      {{["Plain text", h("div", {
      style: "color:red"
    }, ["Div child", h("span", foo)])]}}
    </template>
  </SourceCode>
</template>

New:

<script lang="ts" setup>
import { ref } from "vue";

const array = ["A","B","C"];

const modelValue = ref("Model value");

const object = {"a":"Test A","b":42};
</script>

<template>
  <SourceCode
    :array="array"
    :bar="42"
    foo="Example string"
    v-model="modelValue"
    :object="object"
  >
    Default slot content

    <template #namedSlots="{ foo }">
      Plain text
      <div style="color:red">
        Div child
        <span>{{ foo }}</span>
      </div>
    </template>
  </SourceCode>
</template>

As reference, here is the Story source for the above example:

export const Default = {
  args: {
    foo: 'Example string',
    bar: 42,
    array: ['A', 'B', 'C'],
    object: {
      a: 'Test A',
      b: 42,
    },
    modelValue: 'Model value',
    default: 'Default slot content',
    namedSlots: ({ foo }) => [
      'Plain text',
      h('div', { style: 'color:red' }, ['Div child', h('span', foo)]),
    ],
  },
} satisfies Story;

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

  1. Run a sandbox for template, e.g. yarn task --task sandbox --start-from auto --template vue3-vite/default-ts
  2. Open Storybook in your browser
  3. Access http://localhost:6006/?path=/docs/stories-renderers-vue3-vue3-vite-default-ts-sourcecode--docs
  4. Click on "Show code" to see the generated source code

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/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 canary-release-pr.yml --field pr=<PR_NUMBER>

Copy link

nx-cloud bot commented May 18, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 22938b9. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 2 targets

Sent with 💌 from NxCloud.

larsrickert added a commit to SchwarzIT/onyx that referenced this pull request May 23, 2024
Relates to #1078, #414

- simplified some Stories
- refactor oynx icon import code transformer to work globally so we
don't need to add it to every component that uses icons
- temporarily copy over improved source code generation until it is
released in Storybook itself (see
storybookjs/storybook#27194)
Copy link
Contributor

@chakAs3 chakAs3 left a comment

Choose a reason for hiding this comment

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

Great job, @larsrickert! I had some old TODOs regarding slots because the generated code wasn't handling edge cases properly.

code/renderers/vue3/src/entry-preview-docs.ts Outdated Show resolved Hide resolved
@kasperpeulen kasperpeulen added the patch:yes Bugfix & documentation PR that need to be picked to main branch label May 30, 2024
@kasperpeulen
Copy link
Contributor

@larsrickert Should be next indeed. I added patch:yes, so that it will automatically patched back to main as well.

Let me now when you are finished with this!

@larsrickert larsrickert changed the base branch from main to next June 8, 2024 10:21
@larsrickert larsrickert changed the base branch from next to main June 8, 2024 10:21
@larsrickert larsrickert force-pushed the larsrickert/improve-vue-source-code branch from 25eda56 to 4428320 Compare June 8, 2024 12:28
@larsrickert larsrickert changed the base branch from main to next June 8, 2024 12:28
@larsrickert
Copy link
Contributor Author

@chakAs3 Thanks a lot for the positive feedback! I also think that this is a very advanced feature for the Vue renderer 👍 Should we perhaps write a blog post that showcases the new features? I could collaborate with @joevaugh4n on this

@kasperpeulen Merge conflicts are resolved, feel free to release it whenever it fits your schedule :)

@chakAs3
Copy link
Contributor

chakAs3 commented Jul 10, 2024

@larsrickert Personally, I still find it one of the most—if not the most—attractive features in Storybook, and a blog post about it would certainly highlight its appeal

However there still some conflicts

@kasperpeulen
Copy link
Contributor

@larsrickert At this moment @shilman is managing the blog.

💯 Let's do a blog about this.

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.

🎉

@kasperpeulen
Copy link
Contributor

@larsrickert Could you check the CI failures?

@kasperpeulen kasperpeulen merged commit 6356195 into next Jul 21, 2024
49 of 52 checks passed
@kasperpeulen kasperpeulen deleted the larsrickert/improve-vue-source-code branch July 21, 2024 15:55
storybook-bot pushed a commit that referenced this pull request Jul 21, 2024
…urce-code

Vue: Improve generated code snippets
(cherry picked from commit 6356195)
@shilman shilman removed the patch:yes Bugfix & documentation PR that need to be picked to main branch label Jul 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

6 participants