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

What is the method for including as: :json in your code?

I have a file with the extension .ts, which is part of a Ruby on Rails application. The code in this file looks something like this: export const create = async (params: CreateRequest): Promise<XYZ> => { const response = await request<XYZ> ...

Tips on expanding typings in TypeScript?

In my software library, there exists a map function with the following definitions: function map<T, U>(f: (x: T) => U, a: Array<T>): Array<U> function map<T, U>(f: (x: T) => U, a: Functor<T>): Functor<U> Furtherm ...

Set up variables during instantiation or within the class declaration

Is there a preferred way to initialize class variables in ReactJS with TypeScript - in the constructor or when declaring the variable? Both methods seem to work equally well and result in the same transpiled javascript. export class MyClass extends Reac ...

Issue with TypeScript not detecting exported Firebase Cloud Functions

Dealing With Firebase Cloud Functions Organization I am managing a large number of Firebase Cloud Functions, and in order to keep the code well-structured, I have divided them into separate files for each function category (such as userFunctions, adminFun ...

Angular 6 component experiencing issues with animation functionality

I've implemented a Notification feature using a Notification component that displays notifications at the top of the screen. The goal is to make these notifications fade in and out smoothly. In my NotificationService, there's an array that holds ...

What is the best way to ensure that the base class Resolver finishes before allowing the derived class Resolver to execute?

I have a situation where many of my resolvers (@angular/router Resolve) need to query the same data before executing their route-specific queries. To streamline this process, I want to create a resolver base class that resolves the initial data before the ...

Currently, I am working on developing a to-do task manager using Angular 2. One of the tasks I am tackling involves updating the value

I'm facing an issue with managing to-do tasks. I would like to update the value of an option in a select dropdown when the (change) event is triggered. There are 2 components: //app.component.ts //array object this.dataArr[this.counter] = {id: this ...

Exploring the Power of Vue3 Composables Through an NPM Package

I am in the process of creating an NPM Package to distribute a collection of Vue3 Components. In order to register the components, I have included the following code snippet: import components from "./components/components"; const plugin = { i ...

Looking for a method to efficiently populate an array with values in Typescript

Here is an example of how I work with arrays in my code: var alphas: string[]; alphas = ['abc1', 'abc2', 'abc3']; // (this array can be changed) My modal class looks like this: export class Team { TeamName: string; } To ...

The variable's Ionic value is not being displayed in the HTML

I recently developed a new Ionic application and encountered an issue while attempting to display a variable value in the HTML. Without making any modifications, this is the current state of my page after creating the app. import { IonicModule } from &ap ...

Lack of intellisense support for .ts files in Visual Studio Code

Currently, I am using Visual Studio Code 1.17.2 on Arch Linux to kickstart my work with Node.js/Angular4. To avoid confusion caused by loosely typed code, I have decided to switch to TypeScript for my NodeJS server as well. This is why my main file is name ...

Launching a Material UI Modal nested within a parent component

I have a table displaying various teams. Each row in the table has a menu option that, when clicked, should open either a modal or a dialog box. I want to keep the table, menu functionality, and modals as separate components for better organization. Here&a ...

Typescript encounters issues when assigning declaration as TRUE

Currently, I'm working on a project in Angular 2 and attempting to create TypeScript definitions for it so that it can be exported as a library. I have various services set up that make HTTP requests to components, all structured similarly to the cod ...

Issue: The element p-button (and p-password) is unrecognized

After installing primeng using the command npm install primeng --save, I have version ^12.0.1 of primeng listed in both dependencies and devDependencies in my package.json file. In my angular.json file, I have included the necessary styles: "styles& ...

Nearly every category except for one from "any" (all varieties but one)

In Typescript, is it feasible to specify a type for a variable where the values can be any value except for one (or any other number of values)? For instance: let variable: NOT<any, 'number'> This variable can hold any type of value excep ...

The API functions seamlessly with TypeScript, however, during the transpilation process, it fails to locate the model

I am in the process of developing a straightforward API that is capable of Creating, Reading, and Deleting student information within a postgres database. Interestingly, I have encountered an issue when using ts-node-dev without transpiling the files to J ...

A static factory method within an abstract class

I am currently developing a class system using Typescript. The main structure consists of an abstract class called Component, which includes a static method called create(). This method is utilized on child classes to generate specific instances. abstract ...

Cannot access a Typescript method variable within an inline function

I've encountered an issue with my code involving loading values into the array usageCategory within an inline function. Despite successfully adding values to the array inside the function, I am unable to print them outside it. getAllUsageCategoryElem ...

Encountering the following error message: "Received error: `../node_modules/electron/index.js:1:0 Module not found: Can't resolve 'fs'` while integrating next.js with electron template."

I am utilizing the electron template with next.js, and I am trying to import ipcRenderer in my pages/index.tsx file. Below is the crucial code snippet: ... import { ipcRenderer } from 'electron'; function Home() { useEffect(() => { ip ...

What is the best way to include a non-Typed Angular service in a TypeScript class?

I have a module and service in Angular that were originally developed without TypeScript, like this: MyModule = angular.module('MyModule', ['dependency1', 'dependency2']); MyModule.factory('MyService', ['$other ...