Skip to content

Commit

Permalink
experiment: add LitElement based version of vaadin-confirm-dialog (#6539
Browse files Browse the repository at this point in the history
)
  • Loading branch information
web-padawan authored Sep 28, 2023
1 parent 281d178 commit 5f447ca
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/confirm-dialog/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
"type": "module",
"files": [
"src",
"!src/vaadin-lit-confirm-dialog-overlay.js",
"!src/vaadin-lit-confirm-dialog.d.ts",
"!src/vaadin-lit-confirm-dialog.js",
"theme",
"vaadin-*.d.ts",
"vaadin-*.js",
Expand Down
9 changes: 9 additions & 0 deletions packages/confirm-dialog/src/vaadin-confirm-dialog-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,15 @@ export const ConfirmDialogMixin = (superClass) =>
value: () => [],
},

/**
* A reference to the overlay element.
* @private
*/
_overlayElement: {
type: Object,
sync: true,
},

/**
* A reference to the "Reject" button which will be teleported to the overlay.
* @private
Expand Down
121 changes: 121 additions & 0 deletions packages/confirm-dialog/src/vaadin-lit-confirm-dialog-overlay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/**
* @license
* Copyright (c) 2018 - 2023 Vaadin Ltd.
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
import { css, html, LitElement } from 'lit';
import { ifDefined } from 'lit/directives/if-defined.js';
import { defineCustomElement } from '@vaadin/component-base/src/define.js';
import { DirMixin } from '@vaadin/component-base/src/dir-mixin.js';
import { OverlayClassMixin } from '@vaadin/component-base/src/overlay-class-mixin.js';
import { PolylitMixin } from '@vaadin/component-base/src/polylit-mixin.js';
import { DialogBaseMixin } from '@vaadin/dialog/src/vaadin-dialog-base-mixin.js';
import { dialogOverlay } from '@vaadin/dialog/src/vaadin-dialog-styles.js';
import { OverlayMixin } from '@vaadin/overlay/src/vaadin-overlay-mixin.js';
import { overlayStyles } from '@vaadin/overlay/src/vaadin-overlay-styles.js';
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
import { ThemePropertyMixin } from '@vaadin/vaadin-themable-mixin/vaadin-theme-property-mixin.js';
import { ConfirmDialogBaseMixin } from './vaadin-confirm-dialog-base-mixin.js';
import { confirmDialogOverlay } from './vaadin-confirm-dialog-overlay-styles.js';

/**
* An element used internally by `<vaadin-confirm-dialog>`. Not intended to be used separately.
*
* @customElement
* @extends HTMLElement
* @mixes DirMixin
* @mixes OverlayMixin
* @mixes ThemableMixin
* @private
*/
class ConfirmDialogOverlay extends OverlayMixin(DirMixin(ThemableMixin(PolylitMixin(LitElement)))) {
static get is() {
return 'vaadin-confirm-dialog-overlay';
}

static get styles() {
return [overlayStyles, dialogOverlay, confirmDialogOverlay];
}

/** @protected */
render() {
return html`
<div part="backdrop" id="backdrop" ?hidden="${!this.withBackdrop}"></div>
<div part="overlay" id="overlay" tabindex="0">
<section id="resizerContainer" class="resizer-container">
<header part="header"><slot name="header"></slot></header>
<div part="content" id="content">
<div part="message"><slot></slot></div>
</div>
<footer part="footer" role="toolbar">
<div part="cancel-button">
<slot name="cancel-button"></slot>
</div>
<div part="reject-button">
<slot name="reject-button"></slot>
</div>
<div part="confirm-button">
<slot name="confirm-button"></slot>
</div>
</footer>
</section>
</div>
`;
}

/**
* @protected
* @override
*/
ready() {
super.ready();

// ConfirmDialog has header and footer but does not use renderers
this.setAttribute('has-header', '');
this.setAttribute('has-footer', '');
}
}

defineCustomElement(ConfirmDialogOverlay);

/**
* An element used internally by `<vaadin-confirm-dialog>`. Not intended to be used separately.
* @private
*/
class ConfirmDialogDialog extends ConfirmDialogBaseMixin(
DialogBaseMixin(OverlayClassMixin(ThemePropertyMixin(PolylitMixin(LitElement)))),
) {
static get is() {
return 'vaadin-confirm-dialog-dialog';
}

static get styles() {
return css`
:host {
display: none;
}
`;
}

/** @protected */
render() {
return html`
<vaadin-confirm-dialog-overlay
id="overlay"
.owner="${this}"
.opened="${this.opened}"
@opened-changed="${this._onOverlayOpened}"
@mousedown="${this._bringOverlayToFront}"
@touchstart="${this._bringOverlayToFront}"
theme="${ifDefined(this._theme)}"
.modeless="${this.modeless}"
.withBackdrop="${!this.modeless}"
?resizable="${this.resizable}"
restore-focus-on-close
focus-trap
></vaadin-confirm-dialog-overlay>
`;
}
}

defineCustomElement(ConfirmDialogDialog);
6 changes: 6 additions & 0 deletions packages/confirm-dialog/src/vaadin-lit-confirm-dialog.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @license
* Copyright (c) 2018 - 2023 Vaadin Ltd.
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
export * from './vaadin-confirm-dialog.js';
89 changes: 89 additions & 0 deletions packages/confirm-dialog/src/vaadin-lit-confirm-dialog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* @license
* Copyright (c) 2018 - 2023 Vaadin Ltd.
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
import '@vaadin/button/src/vaadin-lit-button.js';
import './vaadin-lit-confirm-dialog-overlay.js';
import { css, html, LitElement } from 'lit';
import { ifDefined } from 'lit/directives/if-defined.js';
import { defineCustomElement } from '@vaadin/component-base/src/define.js';
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
import { PolylitMixin } from '@vaadin/component-base/src/polylit-mixin.js';
import { ThemePropertyMixin } from '@vaadin/vaadin-themable-mixin/vaadin-theme-property-mixin.js';
import { ConfirmDialogMixin } from './vaadin-confirm-dialog-mixin.js';

/**
* LitElement based version of `<vaadin-confirm-dialog>` web component.
*
* ## Disclaimer
*
* This component is an experiment not intended for publishing to npm.
* There is no ETA regarding specific Vaadin version where it'll land.
* Feel free to try this code in your apps as per Apache 2.0 license.
*
* @extends HTMLElement
* @mixes ConfirmDialogMixin
* @mixes ElementMixin
* @mixes ThemePropertyMixin
*/
class ConfirmDialog extends ConfirmDialogMixin(ElementMixin(ThemePropertyMixin(PolylitMixin(LitElement)))) {
static get is() {
return 'vaadin-confirm-dialog';
}

static get styles() {
return css`
:host,
[hidden] {
display: none !important;
}
`;
}

/** @protected */
render() {
return html`
<vaadin-confirm-dialog-dialog
id="dialog"
.opened="${this.opened}"
.overlayClass="${this.overlayClass}"
aria-label="${this.header || 'confirmation'}"
theme="${ifDefined(this._theme)}"
no-close-on-outside-click
.noCloseOnEsc="${this.noCloseOnEsc}"
.contentHeight="${this._contentHeight}"
.contentWidth="${this._contentWidth}"
@opened-changed=${this._onOpenedChanged}"
></vaadin-confirm-dialog-dialog>
<div hidden>
<slot name="header"></slot>
<slot></slot>
<slot name="cancel-button"></slot>
<slot name="reject-button"></slot>
<slot name="confirm-button"></slot>
</div>
`;
}

/** @protected */
async ready() {
super.ready();

await this.$.dialog.updateComplete;

this._overlayElement = this.$.dialog.$.overlay;

this._initOverlay(this._overlayElement);
}

/** @private */
_onOpenedChanged(event) {
this.opened = event.detail.value;
}
}

defineCustomElement(ConfirmDialog);

export { ConfirmDialog };
3 changes: 3 additions & 0 deletions packages/confirm-dialog/test/confirm-dialog-lit.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import './not-animated-styles.js';
import '../src/vaadin-lit-confirm-dialog.js';
import './confirm-dialog.common.js';
3 changes: 3 additions & 0 deletions packages/confirm-dialog/test/confirm-dialog-polymer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import './not-animated-styles.js';
import '../src/vaadin-confirm-dialog.js';
import './confirm-dialog.common.js';
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { expect } from '@esm-bundle/chai';
import { aTimeout, esc, fixtureSync, nextFrame, nextRender, oneEvent } from '@vaadin/testing-helpers';
import { aTimeout, esc, fixtureSync, nextFrame, nextRender, nextUpdate, oneEvent } from '@vaadin/testing-helpers';
import sinon from 'sinon';
import './not-animated-styles.js';
import '../vaadin-confirm-dialog.js';
import { getDeepActiveElement } from '@vaadin/a11y-base/src/focus-utils.js';

describe('vaadin-confirm-dialog', () => {
Expand Down Expand Up @@ -183,6 +181,7 @@ describe('vaadin-confirm-dialog', () => {
await nextRender();
overlay = confirm.$.dialog.$.overlay;
messageSlot = overlay.shadowRoot.querySelector('[part="message"] > slot');
await nextUpdate(overlay);
});

it('should set the message text content using message property', () => {
Expand Down

0 comments on commit 5f447ca

Please sign in to comment.