-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Addon-contexts: Improve Vue integration #6632
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
71bce16
FIX addon-contexts: allow `props` to map on VueComponent contexts
74f0f7c
ADD addon-contexts: example for vue-kitchen-sink
cefcc2e
ADD addon-contexts: fix typos
3e6e3e8
ADD addon-contexts: correct the function name in Vue
c1bcdb1
ADD addon-contexts: snapshot files
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, | ||
}, | ||
}, | ||
], | ||
} | ||
); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think the
createContext
in React is really well-designed, so I'm also thinking to make this util component a standalone package or as a helper in the addon. I have google and look into NPM registry, few examples or ideal implementation (vue2.6 changed something so they seems a bit outdated)...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.
I have published the latest implementation for this util component: vue-create-context with some minor improvements. But I think we can just merge this PR since it is not super long and working well (or maybe just copy past the latest source code from my repo)?!