lion/packages/localize/stories/message.stories.js
Thomas Allmer 89b835a799 feat: improved storybook demos
Co-authored-by: Joren Broekema <Joren.Broekema@ing.com>
Co-authored-by: Alex Ghiu <Alex.Ghiu@ing.com>
Co-authored-by: Thijs Louisse <Thijs.Louisse@ing.com>
2020-01-13 13:58:03 +01:00

61 lines
1.5 KiB
JavaScript

import { LitElement, render } from '@lion/core';
import { html } from '@open-wc/demoing-storybook';
import { localize, LocalizeMixin } from '../index.js';
import '@lion/helpers/sb-locale-switcher.js';
export default {
title: 'Localize/System/_internals',
};
export const asFunction = () => {
const wrapper = document.createElement('div');
let message = 'Loading...';
function update() {
message = localize.msg('lit-html-example:body');
render(
html`
<p>${message}</p>
<sb-locale-switcher></sb-locale-switcher>
`,
wrapper,
);
}
localize
.loadNamespace({
'lit-html-example': locale => import(`./translations/${locale}.js`),
})
.then(() => {
update();
});
localize.addEventListener('localeChanged', () => {
localize.loadingComplete.then(() => update());
});
return wrapper;
};
export const webComponent = () => {
class MessageExample extends LocalizeMixin(LitElement) {
static get localizeNamespaces() {
return [
{ 'lit-html-example': locale => import(`./translations/${locale}.js`) },
...super.localizeNamespaces,
];
}
render() {
return html`
<div aria-live="polite">
<p>${localize.msg('lit-html-example:body')}</p>
</div>
`;
}
}
if (!customElements.get('message-example')) {
customElements.define('message-example', MessageExample);
}
return html`
<message-example></message-example>
<sb-locale-switcher></sb-locale-switcher>
`;
};