import { storiesOf, html, withKnobs } from '@open-wc/demoing-storybook'; import { css, render, LitElement } from '@lion/core'; import '@lion/icon/lion-icon.js'; import '@lion/button/lion-button.js'; import { withBottomSheetConfig, withDropdownConfig, withModalDialogConfig, OverlayMixin, } from '../index.js'; function renderOffline(litHtmlTemplate) { const offlineRenderContainer = document.createElement('div'); render(litHtmlTemplate, offlineRenderContainer); return offlineRenderContainer.firstElementChild; } // Currently toggling while opened doesn't work (see OverlayController) /* let toggledPlacement = 'top'; const togglePlacement = popupController => { const placements = [ 'top-end', 'top', 'top-start', 'right-end', 'right', 'right-start', 'bottom-start', 'bottom', 'bottom-end', 'left-start', 'left', 'left-end', ]; toggledPlacement = placements[(placements.indexOf(toggledPlacement) + 1) % placements.length]; popupController.updatePopperConfig({ togglePlacement }); }; */ const overlayDemoStyle = css` .demo-box { width: 200px; background-color: white; border-radius: 2px; border: 1px solid grey; padding: 8px; } .demo-box_placements { display: flex; flex-direction: column; width: 173px; margin: 0 auto; margin-top: 68px; } lion-demo-overlay { padding: 10px; } .close-button { color: black; font-size: 28px; line-height: 28px; } .demo-box__column { display: flex; flex-direction: column; } .overlay { display: block; position: absolute; font-size: 16px; color: white; background-color: black; border-radius: 4px; padding: 8px; } .demo-popup { padding: 10px; border: 1px solid black; } @media (max-width: 480px) { .overlay { display: none; } } `; customElements.define( 'lion-demo-overlay', class extends OverlayMixin(LitElement) { constructor() { super(); this.closeEventName = 'demo-overlay-close'; } // eslint-disable-next-line class-methods-use-this _defineOverlayConfig() { return { placementMode: 'global', // have to set a default }; } _setupOpenCloseListeners() { this.__close = () => { this.opened = false; }; this.__toggle = () => { this.opened = !this.opened; }; this._overlayCtrl.invokerNode.addEventListener('click', this.__toggle); } _teardownOpenCloseListeners() { this._overlayCtrl.invokerNode.removeEventListener('click', this.__toggle); } render() { return html` `; } }, ); storiesOf('Overlay System | Overlay as a WC', module) .addDecorator(withKnobs) .add( 'Default', () => html`

Important note: For placementMode: 'global', your slot="content" gets moved to global overlay container. After initialization it is no longer a child of lion-demo-overlay

To close your overlay from some action performed inside the content slot, fire a close event.

For the overlay to close, it will need to bubble to the content slot (use bubbles: true. Also composed: true if it needs to traverse shadow boundaries)

The demo below demonstrates this

Overlay
Hello! You can close this notification here: e.target.dispatchEvent(new Event('demo-overlay-close', { bubbles: true }))} >⨯
`, ) .add('Global placement configuration', () => { const overlay = placement => html` Overlay ${placement}
Hello! You can close this notification here: e.target.dispatchEvent(new Event('demo-overlay-close', { bubbles: true }))} >⨯
`; return html`
${overlay('center')} ${overlay('top-left')} ${overlay('top-right')} ${overlay('bottom-left')} ${overlay('bottom-right')}
`; }) .add( 'Local placementMode', () => html`
Overlay
Hello! You can close this notification here: e.target.dispatchEvent(new Event('demo-overlay-close', { bubbles: true }))} >⨯
`, ) .add( 'Override the popper config', () => html`
The API is aligned with Popper.js, visit their documentation for more information: Popper.js Docs
United Kingdom
`, ) .add('Switch overlays configuration', () => { const overlay = renderOffline(html` Overlay
Hello! You can close this notification here: e.target.dispatchEvent(new Event('demo-overlay-close', { bubbles: true }))} >⨯
`); return html`
${overlay}
`; }) .add('On hover', () => { const popup = renderOffline(html` { popup.opened = true; }} @mouseleave=${() => { popup.opened = false; }} >UK
United Kingdom
`); return html`
In the beautiful ${popup} the weather is nice.
`; }) .add('On an input', () => { const popup = renderOffline(html`
United Kingdom
e.stopImmediatePropagation()} @focusout=${() => { popup.opened = false; }} @focusin=${() => { popup.opened = true; }} />
`); return html`
${popup}
`; }); /* .add('Toggle placement with knobs', () => { const overlay = (placementMode = 'global') => html` Overlay
Hello! You can close this notification here: e.target.dispatchEvent(new Event('demo-overlay-close', { bubbles: true }))} >⨯
`; return html`

Local

${overlay('local')}

Global

${overlay()}
`; }) */