How can I customize a Vue component slot in Storybook 8.0.6 using Vue 3.4 and Typescript to display various subcomponents within a story?

Incorporating a variety of sub-components into my Vue 3 component based on context is proving to be a challenge. Utilizing slots seems to be the solution in Vue 3, but I'm struggling to make it work within Storybook 8, which I'm using to showcase the functionality to stakeholders.

My setup was functional in Storybook 7, but after upgrading to Storybook 8 with enhanced 'First-class Vue support', things have stopped working as they did before. I clearly need guidance on how to navigate this migration and adjust my practices accordingly.

Considering my limited experience with both Vue and Storybook, it's possible that I'm overlooking some fundamental aspect of the process.

If someone could direct me to a Storybook example demonstrating how to implement dynamic sub-components within a component slot, it would greatly assist me in resolving this issue.

Below is the code snippet for my Vue component (everything functions except for the slots, implying that my props, setup, and overall Storybook configuration are correct, I believe.)

<template>
  <div class="alert" :id="props.identifier" :class="[alertStyleClass, alertWidthClass, alertLayoutClass]">
    <Icon v-if="props.icon" class="alert__icon" :name="props.icon" />

    <div class="alert__main" v-if="slots.main && !props.message">
      <slot name="main"> </slot>
    </div>
    <div class="alert__main" v-else v-html="props.message"></div>

    <div class="alert__actions" v-if="slots.actions">
      <slot name="actions"> </slot>
    </div>
  </div>
</template>

Additionally, here is an exemplary story utilizing this component (the conventional direct props are operational, and the subcomponent 'CheckboxComponent' appears fine on its own). This syntax was effective in Storybook 7, so I'm unsure about the changes or adjustments required to make it work in Storybook 8.

export const CheckboxNotification: Story = {
  args: {
    identifier: '',
    style: AlertStyle.Information
  },
  parameters: {
    slots: {
      main: {
        description: 'Main slot',
        components: { CheckboxComponent },
        template: `<CheckboxComponent label="Label" identifier="AlertCheckbox" value="true" name="AlertCB" />`
      }
    }
  }
}

My attempt involved populating the main slot with a literal string. Although I didn't anticipate any results, at least the content appeared in the intended location without being compiled into the necessary subcomponent.

export const CheckboxNotification: Story = {
  args: {
    identifier: '',
    style: AlertStyle.Information,
    main: `<CheckboxComponent label="Label" identifier="AlertCheckbox" value="true" name="AlertCB" />`
  }
}

This excerpt lacks many crucial elements such as prop definitions, complete setup code, etc. If there are any key details I've omitted that could aid in diagnosing this issue, please notify me. All I know is that I'm feeling frustrated and unable to find assistance - it seems like a critical problem, leaving me baffled by my inability to solve it. However, I firmly believe that all functionalities should be achievable through story code without altering my Vue component to suit Storybook.

Answer №1

I have made some changes to my Story export by utilizing the render function within Storybook to insert the subcomponent. Additionally, I am importing both the component and subcomponent in a slightly different manner.

export const CheckboxNotification: Story = {
    args: {
        identifier: '',
        style: AlertStyle.Information
    },
    render: (args) => ({
    components: { AlertComponent, CheckboxComponent },
    setup() {
        return { args }
    },
    template: `
        <AlertComponent v-bind="args">
            <template #main>
                <CheckboxComponent label="Label" identifier="AlertCheckbox" value="true" name="AlertCB" />
            </template>
       </AlertComponent>
    `
  })
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Can the tiles in a grid-list be organized in a specific order?

I am facing an issue with a class named 'scenario' that has properties such as 'id', 'name', and 'number' among others. In the HTML, scenarios are displayed in this format: <mat-grid-list [cols]="breakpoint" r ...

Tips for generating a new method using an existing one

Searching for the best practice to inform the ts compiler that a method will be generated at runtime. interface Todo { /* ... */ } export class TodoModel { todos: Todo[] = []; constructor() { //... } bindTodoListChanged(callback : (todos: Todo[]) ...

The issue arises when TypeScript is unable to accurately infer the type as not being undefined following a type guard condition using

The following code snippet illustrates that in the else statement, it is evident that b cannot be undefined (a||b returns truthy and since a is falsy, b must be truthy). Why does Typescript show the error 'b' is possibly 'undefined', a ...

What is preventing me from including an additional parameter in a function in TypeScript?

I am currently developing a task management application. I am facing an issue while attempting to incorporate the event and items.id into a button function for actions like delete, edit, or mark as completed. While this functionality works smoothly in pla ...

Creating new Angular2 Observables - Subscribing to undefined developments

Is it a scoping issue or a problem with how my observables are set up? Can you assist me in figuring it out? To explain my logic: My goal is to first check if the data is available locally before making an http.get request for it. I want to return the loc ...

Ways to deduce or implement intellisense for parameter types in overloaded functions

Currently, I am developing an Electron application where numerous events are being passed around with specific listeners assigned to each event. One example is BrowserWindow.on: Electron.BrowserWindow.on(event, listener) The event can take on values such ...

What potential drawbacks come with utilizing the OOP approach in both JavaScript and React?

While working on an internal project, I found myself creating a base system and implementing a custom form structure using TypeScript with an OOP approach. class Form extends React.Component {} abstract class FormElement extends React.Component<{valid ...

Differences between ng build --prod and ng --build aot in Angular 7

Recently, I successfully built an Angular 7 application using the command ng build --prod. However, I am now facing a dilemma regarding ng build --aot versus ng build --prod. Our application is currently deployed on ..., and although it runs successfully ...

Avoiding repetitive logic in both parent and child components in Angular 8

In my parent controller, I have common requests and I also read router params for those requests. However, for the child components, I have different requests but still need to extract the same parameters from the router - resulting in duplicate code. For ...

Why is it advantageous to use Observable as the type for Angular 5 component variables?

Being a beginner in Angular 6, I have been exploring the process of http mentioned in this link: https://angular.io/tutorial/toh-pt6#create-herosearchcomponent One thing that caught my attention was that the heroes array type is set to Observable in the ...

What is the best way to refresh or reload a child component in Angular?

I have a transaction.component.html file that displays the app-deal-partners component. Every time the delete function is triggered, I want to refresh and reload the child component, which is the app-deal-partners component. I need to reload <app-deal- ...

What could be causing the Properties Array to come back as undefined?

When attempting to add an item to an array stored in the properties, I am encountering an error: "Cannot read properties of undefined (reading 'value')." Within the props, the following interfaces are defined: ILinkItemProps.ts export interface ...

Efficiently Combining Objects with Rxjs

I have a specific object structure that I am working with: const myObject = { info: [ { id: 'F', pronouns: 'hers' }, { id: 'M', pronouns: 'his'} ], items:[ { name: 'John', age: 35, ...

"Enhance your PrimeVue Tree component with interactive action buttons placed on every TreeNode

Details: Using Vue 3.3.6 in composition API script setup style Utilizing PrimeVue 3.37.0 and PrimeFlex 3.3.1 Implemented with Typescript Objective: To create a tree structure with checkboxes that are selectable, along with action buttons on each TreeNod ...

The search for 'Renderer2' in '@angular/core' did not yield any results

After successfully installing Angular Material in my Angular Project by following the instructions provided in the Material documentation, I encountered some issues. Specifically, when attempting to launch the application with 'npm start', I star ...

Exploring methods for interacting with and controlling structural directives in e2e testing

Background: My goal is to permutation all potential configurations of an Angular2 screen for a specified route and capture screenshots using Protractor from the following link: http://www.protractortest.org/#/debugging. Problem: I am struggling to figure ...

What causes the error message "No exported member 'ɵɵFactoryDeclaration' in @angular/core/core" to appear during project compilation?

I am facing an issue where the global Angular CLI version is 13.0.1 and the local version in my project is 10.2.3. Despite making changes to some components (without touching package.json), I encountered an error during the build step of my bitbucket pipel ...

Adjusting ngClass dynamically as values change

Currently, I am facing a situation where I want to dynamically add a class to my view using ngClass based on changes in the output value. The output value is dependent on the response received from an API, and I am fetching data from the endpoint every sec ...

Encountering a "undefined response" issue within an Angular

I am encountering an issue when trying to fetch data from a REST API. Upon logging the response, I am getting an undefined value. How can I resolve this? I have confirmed that the API is sending data by checking my network tab in the developer tool. getPro ...

Translate array into object with correct data types (type-specific method)

Welcome In our project, we have implemented attributes support where each attribute acts as a class. These attributes include information on type, optionality, and name. Instead of creating an interface for every entity, my goal is to automate this proces ...