-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Addon-contexts: Improve Vue integration (#6632)
Addon-contexts: Improve Vue integration
- Loading branch information
Showing
5 changed files
with
178 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
examples/vue-kitchen-sink/src/stories/__snapshots__/addon-contexts.stories.storyshot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Storyshots Addon|Contexts Languages 1`] = ` | ||
<div | ||
style="color: white; background: rgb(23, 63, 95); height: 100vh; padding: 10px;" | ||
> | ||
<div> | ||
|
||
Your locale is unknown, so I say NULL! | ||
|
||
</div> | ||
</div> | ||
`; | ||
|
||
exports[`Storyshots Addon|Contexts Simple CSS Theming 1`] = ` | ||
<div | ||
style="color: white; background: rgb(23, 63, 95); height: 100vh; padding: 10px;" | ||
> | ||
<span> | ||
I'm a children of the injected 'div' (where provides a theming context). | ||
</span> | ||
</div> | ||
`; |
144 changes: 144 additions & 0 deletions
144
examples/vue-kitchen-sink/src/stories/addon-contexts.stories.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import { storiesOf } from '@storybook/vue'; | ||
import { withContexts } from '@storybook/addon-contexts/vue'; | ||
|
||
// Example A: Simple CSS Theming | ||
const topLevelContexts = [ | ||
{ | ||
icon: 'sidebaralt', | ||
title: 'CSS Themes', | ||
components: ['div'], | ||
params: [ | ||
{ | ||
name: 'Desert', | ||
props: { | ||
style: { color: 'brown', background: '#F4A261', height: '100vh', padding: '10px' }, | ||
}, | ||
}, | ||
{ | ||
name: 'Ocean', | ||
props: { | ||
style: { color: 'white', background: '#173F5F', height: '100vh', padding: '10px' }, | ||
}, | ||
default: true, | ||
}, | ||
], | ||
}, | ||
]; | ||
|
||
const storyLevelContexts = [ | ||
{ | ||
title: 'CSS Themes', | ||
params: [ | ||
{ | ||
name: 'Forest', | ||
props: { | ||
style: { color: 'teal', background: '#00b894', height: '100vh', padding: '10px' }, | ||
}, | ||
}, | ||
], | ||
}, | ||
]; | ||
|
||
const stories = storiesOf('Addon|Contexts', module).addDecorator(withContexts(topLevelContexts)); | ||
|
||
stories.add( | ||
'Simple CSS Theming', | ||
() => ({ | ||
template: | ||
"<span>I'm a children of the injected 'div' (where provides a theming context).</span>", | ||
}), | ||
{ | ||
contexts: storyLevelContexts, | ||
} | ||
); | ||
|
||
// Example B: Language (Vue provide/inject API) | ||
const createContext = initialValue => { | ||
const uid = `_${Date.now()}${Math.random()}`; | ||
return { | ||
Provider: { | ||
name: `Context.Provider`, | ||
props: ['value'], | ||
provide() { | ||
return this.value === undefined | ||
? undefined | ||
: { | ||
[uid]: () => this.value, | ||
}; | ||
}, | ||
template: ` | ||
<div> | ||
<slot /> | ||
</div> | ||
`, | ||
}, | ||
Consumer: { | ||
name: `Context.Consumer`, | ||
inject: { | ||
[uid]: { | ||
default: () => () => | ||
initialValue instanceof Object ? { ...initialValue } : { value: initialValue }, | ||
}, | ||
}, | ||
computed: { | ||
value() { | ||
return this[uid](); | ||
}, | ||
}, | ||
template: ` | ||
<div> | ||
<slot v-bind="value" /> | ||
</div> | ||
`, | ||
}, | ||
}; | ||
}; | ||
|
||
const NaiveIntlContext = createContext({ | ||
locale: 'unknown', | ||
greeting: 'NULL', | ||
}); | ||
|
||
stories.add( | ||
'Languages', | ||
() => ({ | ||
components: { 'NaiveIntlContext.Consumer': NaiveIntlContext.Consumer }, | ||
template: ` | ||
<NaiveIntlContext.Consumer v-slot="{ locale, greeting }"> | ||
Your locale is {{ locale }}, so I say {{ greeting }}! | ||
</NaiveIntlContext.Consumer> | ||
`, | ||
}), | ||
{ | ||
contexts: [ | ||
{ | ||
icon: 'globe', | ||
title: 'Languages', | ||
components: [NaiveIntlContext.Provider], | ||
params: [ | ||
{ | ||
name: 'English', | ||
props: { | ||
value: { locale: 'en', greeting: 'Hello' }, | ||
}, | ||
}, | ||
{ | ||
name: 'French', | ||
props: { | ||
value: { locale: 'fr', greeting: 'Bonjour' }, | ||
}, | ||
}, | ||
{ | ||
name: 'Chinese', | ||
props: { | ||
value: { locale: 'cn', greeting: '你好' }, | ||
}, | ||
}, | ||
], | ||
options: { | ||
cancelable: true, | ||
}, | ||
}, | ||
], | ||
} | ||
); |
9bb3ae8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully aliased the URL https://monorepo-od2ej69ae.now.sh to the following aliases.