Replies: 3 comments 2 replies
-
I'm also looking for a way to provide value to a single component. But you could still provide a global value to all components using the setup function in import { setup } from "@storybook/vue3";
import "../src/style.css";
setup((app) => {
app.provide("key", "provided value");
}); The other way I could think of is to create a custom wrapper that provide value to the child components as in this. |
Beta Was this translation helpful? Give feedback.
-
Using storybook v8 (I'm guessing this is available in v7 as well, but I didn't check), I was able to provide data via
|
Beta Was this translation helpful? Give feedback.
-
I created a decorator for this which allows you to create specific stories with different values injected/provided via a story's Add this to your preview.js file: const preview = {
decorators: [
(story, { args, parameters }) => {
const valueMap = new Map();
const newArgs = { ...args };
parameters.inject?.forEach((attribute) => {
valueMap.set(attribute, args[attribute]);
delete newArgs[attribute];
});
const WrapperComponent = defineComponent({
setup() {
valueMap.forEach((value, key) => {
provide(
key,
computed(() => value),
);
});
},
template: "<slot />",
});
story({ args: newArgs });
return {
components: { story, WrapperComponent },
template: "<WrapperComponent><story /></WrapperComponent>",
};
},
],
};
export default preview; In your stories you can add: const meta = {
title: "Editor/Home/Home",
component: Home,
args: {
yourInjectedRef: 'and it's value', // provide what you'd like your injected value to be
},
parameters: {
inject: ["yourInjectedRef"], // define what needs to be injected
}, I'm currently trying to make all of it type safe but thats pretty tricky. |
Beta Was this translation helpful? Give feedback.
-
Hiya,
How do I provide data for a component trying to inject the data?
https://vuejs.org/guide/components/provide-inject.html
Thanks,
Nicolaj
Beta Was this translation helpful? Give feedback.
All reactions