-
Notifications
You must be signed in to change notification settings - Fork 0
/
Context.stories.tsx
50 lines (40 loc) · 1.13 KB
/
Context.stories.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/** @jsxImportSource ../src */
import { createContext } from 'react';
import { Meta, Story } from '@storybook/react';
import {inject, r, watchEffect} from '../src';
const ColorContext = createContext('red');
interface Props {
color: string;
}
function App(props: Props) {
watchEffect(() => {
console.log(`New color: "${props.color}"`);
});
return r(() => (
<div>
<h1>Current color is "{props.color}"</h1>
<ColorContext.Provider value={props.color}>
<Text />
</ColorContext.Provider>
</div>
));
}
function Text() {
const color = inject(ColorContext);
return r(() => <p style={{ color: color.current }}>Hello World!</p>);
}
const meta: Meta<Props> = {
title: 'Static Context',
component: App,
parameters: {
controls: { expanded: true },
},
};
export default meta;
const Template: Story<Props> = args => <App {...args} />;
// By passing using the Args format for exported stories, you can control the props for a component for reuse in a test
// https://storybook.js.org/docs/react/workflows/unit-testing
export const Default = Template.bind({});
Default.args = {
color: 'blue',
};