|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright (c) 2018 - 2024 Vaadin Ltd. |
| 4 | + * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/ |
| 5 | + */ |
| 6 | +import { html, LitElement } from 'lit'; |
| 7 | +import { buttonStyles } from '@vaadin/button/src/vaadin-button-base.js'; |
| 8 | +import { ButtonMixin } from '@vaadin/button/src/vaadin-button-mixin.js'; |
| 9 | +import { defineCustomElement } from '@vaadin/component-base/src/define.js'; |
| 10 | +import { DirMixin } from '@vaadin/component-base/src/dir-mixin.js'; |
| 11 | +import { isEmptyTextNode } from '@vaadin/component-base/src/dom-utils.js'; |
| 12 | +import { PolylitMixin } from '@vaadin/component-base/src/polylit-mixin.js'; |
| 13 | +import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js'; |
| 14 | +import { drawerToggle } from './vaadin-drawer-toggle-styles.js'; |
| 15 | + |
| 16 | +/** |
| 17 | + * LitElement based version of `<vaadin-drawer-toggle>` web component. |
| 18 | + * |
| 19 | + * ## Disclaimer |
| 20 | + * |
| 21 | + * This component is an experiment and not yet a part of Vaadin platform. |
| 22 | + * There is no ETA regarding specific Vaadin version where it'll land. |
| 23 | + * Feel free to try this code in your apps as per Apache 2.0 license. |
| 24 | + */ |
| 25 | +class DrawerToggle extends ButtonMixin(DirMixin(ThemableMixin(PolylitMixin(LitElement)))) { |
| 26 | + static get is() { |
| 27 | + return 'vaadin-drawer-toggle'; |
| 28 | + } |
| 29 | + |
| 30 | + static get styles() { |
| 31 | + return [buttonStyles, drawerToggle]; |
| 32 | + } |
| 33 | + |
| 34 | + static get properties() { |
| 35 | + return { |
| 36 | + ariaLabel: { |
| 37 | + type: String, |
| 38 | + value: 'Toggle navigation panel', |
| 39 | + reflectToAttribute: true, |
| 40 | + sync: true, |
| 41 | + }, |
| 42 | + |
| 43 | + /** @private */ |
| 44 | + _showFallbackIcon: { |
| 45 | + type: Boolean, |
| 46 | + value: false, |
| 47 | + }, |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + constructor() { |
| 52 | + super(); |
| 53 | + |
| 54 | + this.addEventListener('click', () => { |
| 55 | + this.dispatchEvent(new CustomEvent('drawer-toggle-click', { bubbles: true, composed: true })); |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + /** @protected */ |
| 60 | + render() { |
| 61 | + return html` |
| 62 | + <slot id="slot" @slotchange="${this._toggleFallbackIcon}"> |
| 63 | + <div part="icon"></div> |
| 64 | + </slot> |
| 65 | + <div part="icon" ?hidden="${!this._showFallbackIcon}"></div> |
| 66 | + `; |
| 67 | + } |
| 68 | + |
| 69 | + /** @protected */ |
| 70 | + ready() { |
| 71 | + super.ready(); |
| 72 | + |
| 73 | + this._toggleFallbackIcon(); |
| 74 | + } |
| 75 | + |
| 76 | + /** @private */ |
| 77 | + _toggleFallbackIcon() { |
| 78 | + const nodes = this.$.slot.assignedNodes(); |
| 79 | + |
| 80 | + // Show fallback icon if there are 1-2 empty text nodes assigned to the default slot. |
| 81 | + this._showFallbackIcon = nodes.length > 0 && nodes.every((node) => isEmptyTextNode(node)); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +defineCustomElement(DrawerToggle); |
| 86 | + |
| 87 | +export { DrawerToggle }; |
0 commit comments