/* eslint-disable no-underscore-dangle, class-methods-use-this */ import { storiesOf, html } from '@open-wc/storybook'; import { css } from '@lion/core'; import { LionLitElement } from '@lion/core/src/LionLitElement.js'; import { overlays } from '../src/overlays.js'; import { GlobalOverlayController } from '../src/GlobalOverlayController'; const globalOverlayDemoStyle = css` .demo-overlay { background-color: white; position: fixed; top: 20px; left: 20px; width: 200px; border: 1px solid blue; } .demo-overlay--2 { left: 240px; } .demo-overlay--toast { left: initial; right: 20px; } `; storiesOf('Overlay System|Global/Global Overlay', module) .add('Default', () => { const overlayCtrl = overlays.add( new GlobalOverlayController({ contentTemplate: () => html`

Simple overlay

`, }), ); return html` Anchor 1 Anchor 2 ${Array(50).fill( html`

Lorem ipsum

`, )} `; }) .add('Option "preventsScroll"', () => { const overlayCtrl = overlays.add( new GlobalOverlayController({ preventsScroll: true, contentTemplate: () => html`

Scrolling the body is blocked

`, }), ); return html` ${Array(50).fill( html`

Lorem ipsum

`, )} `; }) .add('Option "hasBackdrop"', () => { const overlayCtrl = overlays.add( new GlobalOverlayController({ hasBackdrop: true, contentTemplate: () => html`

There is a backdrop

`, }), ); return html` `; }) .add('Option "trapsKeyboardFocus"', () => { const overlayCtrl = overlays.add( new GlobalOverlayController({ trapsKeyboardFocus: true, contentTemplate: () => html`

Tab key is trapped within the overlay

Anchor
Tabindex
Contenteditable
`, }), ); return html` Anchor 1 Anchor 2 `; }) .add('Option "trapsKeyboardFocus" (multiple)', () => { const overlayCtrl2 = overlays.add( new GlobalOverlayController({ trapsKeyboardFocus: true, contentTemplate: () => html`

Overlay 2. Tab key is trapped within the overlay

`, }), ); const overlayCtrl1 = overlays.add( new GlobalOverlayController({ trapsKeyboardFocus: true, contentTemplate: () => html`

Overlay 1. Tab key is trapped within the overlay

`, }), ); return html` `; }) .add('Option "isBlocking"', () => { const blockingOverlayCtrl = overlays.add( new GlobalOverlayController({ isBlocking: true, contentTemplate: () => html`

Hides other overlays

`, }), ); const normalOverlayCtrl = overlays.add( new GlobalOverlayController({ contentTemplate: () => html`

Normal overlay

`, }), ); return html` `; }) .add('Sync', () => { const overlayCtrl = overlays.add( new GlobalOverlayController({ contentTemplate: data => html`

${data.title}

`, }), ); return html` `; }) .add('Toast', () => { let counter = 0; function openInfo() { const toastCtrl = overlays.add( new GlobalOverlayController({ contentTemplate: data => html`
Title ${data.counter}

Lorem ipsum ${data.counter}

`, }), ); toastCtrl.sync({ isShown: true, data: { counter }, }); counter += 1; setTimeout(() => { toastCtrl.hide(); counter -= 1; }, 2000); } return html`

Very naive toast implementation

It does not handle adding new while toasts are getting hidden

`; }) .add('In web components', () => { class EditUsernameOverlay extends LionLitElement { static get properties() { return { username: { type: String }, }; } static get styles() { return css` :host { position: fixed; left: 20px; top: 20px; display: block; width: 300px; padding: 24px; background-color: white; border: 1px solid blue; } .close-button { position: absolute; top: 8px; right: 8px; } `; } render() { return html`
`; } _onUsernameEdited() { this.dispatchEvent( new CustomEvent('edit-username-submitted', { detail: this.$id('usernameInput').value, }), ); } _onClose() { this.dispatchEvent(new CustomEvent('edit-username-closed')); } } if (!customElements.get('edit-username-overlay')) { customElements.define('edit-username-overlay', EditUsernameOverlay); } class MyComponent extends LionLitElement { static get properties() { return { username: { type: String }, _editingUsername: { type: Boolean }, }; } constructor() { super(); this.username = 'Steve'; this._editingUsername = false; } disconnectedCallback() { super.disconnectedCallback(); this._editOverlay.hide(); } render() { return html`

Your username is: ${this.username}

`; } firstUpdated() { this._editOverlay = overlays.add( new GlobalOverlayController({ focusElementAfterHide: this.shadowRoot.querySelector('button'), contentTemplate: data => html` `, }), ); } updated() { this._editOverlay.sync({ isShown: this._editingUsername, data: { username: this.username }, }); } _onEditSubmitted(e) { this.username = e.detail; this._editingUsername = false; } _onEditClosed() { this._editingUsername = false; } _onStartEditUsername() { this._editingUsername = true; } } if (!customElements.get('my-component')) { customElements.define('my-component', MyComponent); } return html` `; });