Skip to content

Commit 74adca6

Browse files
authored
refactor!: simplify internal structure to make styling easier (#3108)
1 parent b9926f0 commit 74adca6

10 files changed

Lines changed: 379 additions & 435 deletions

‎packages/crud/src/vaadin-crud-dialog.js‎

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,23 @@
44
* This program is available under Commercial Vaadin Developer License 4.0, available at https://vaadin.com/license/cvdl-4.0.
55
*/
66
import { html } from '@polymer/polymer/lib/utils/html-tag.js';
7-
import { Dialog } from '@vaadin/dialog/src/vaadin-dialog.js';
7+
import { Dialog, DialogOverlay } from '@vaadin/dialog/src/vaadin-dialog.js';
88
import { css, registerStyles } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
99

1010
registerStyles(
11-
'vaadin-dialog-overlay',
11+
'vaadin-crud-dialog-overlay',
1212
css`
13+
[part='overlay'] {
14+
max-width: 54em;
15+
min-width: 20em;
16+
}
17+
18+
[part='content'] {
19+
display: flex;
20+
flex-direction: column;
21+
padding: 0;
22+
}
23+
1324
[part='scroller'] {
1425
display: flex;
1526
flex-direction: column;
@@ -26,6 +37,8 @@ registerStyles(
2637
{ moduleId: 'vaadin-crud-dialog-overlay-styles' }
2738
);
2839

40+
let memoizedTemplate;
41+
2942
const editorTemplate = html`
3043
<div part="scroller" role="group" aria-labelledby="header">
3144
<div part="header" id="header">
@@ -41,16 +54,49 @@ const editorTemplate = html`
4154
</div>
4255
`;
4356

57+
class CrudDialogOverlay extends DialogOverlay {
58+
static get is() {
59+
return 'vaadin-crud-dialog-overlay';
60+
}
61+
62+
static get template() {
63+
if (!memoizedTemplate) {
64+
memoizedTemplate = super.template.cloneNode(true);
65+
const contentPart = memoizedTemplate.content.querySelector('[part="content"]');
66+
const defaultSlot = contentPart.querySelector('slot:not([name])');
67+
contentPart.removeChild(defaultSlot);
68+
contentPart.appendChild(editorTemplate.content.cloneNode(true));
69+
}
70+
return memoizedTemplate;
71+
}
72+
}
73+
74+
customElements.define('vaadin-crud-dialog-overlay', CrudDialogOverlay);
75+
4476
class CrudDialog extends Dialog {
45-
ready() {
46-
super.ready();
47-
48-
this.renderer = (root) => {
49-
if (!root.__dialogInitialized) {
50-
root.__dialogInitialized = true;
51-
root.$.content.appendChild(editorTemplate.content.cloneNode(true));
52-
}
53-
};
77+
/**
78+
* Override template to provide custom overlay tag name.
79+
*/
80+
static get template() {
81+
return html`
82+
<style>
83+
:host {
84+
display: none;
85+
}
86+
</style>
87+
88+
<vaadin-crud-dialog-overlay
89+
id="overlay"
90+
on-opened-changed="_onOverlayOpened"
91+
on-mousedown="_bringOverlayToFront"
92+
on-touchstart="_bringOverlayToFront"
93+
theme$="[[theme]]"
94+
modeless="[[modeless]]"
95+
with-backdrop="[[!modeless]]"
96+
resizable$="[[resizable]]"
97+
focus-trap
98+
></vaadin-crud-dialog-overlay>
99+
`;
54100
}
55101
}
56102

‎packages/crud/src/vaadin-crud.js‎

Lines changed: 142 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import '@vaadin/button/src/vaadin-button.js';
88
import '@vaadin/dialog/src/vaadin-dialog.js';
99
import '@vaadin/confirm-dialog/src/vaadin-confirm-dialog.js';
1010
import '@vaadin/vaadin-license-checker/vaadin-license-checker.js';
11-
import './vaadin-dialog-layout.js';
11+
import './vaadin-crud-dialog.js';
1212
import './vaadin-crud-grid.js';
1313
import './vaadin-crud-form.js';
1414
import { FlattenedNodesObserver } from '@polymer/polymer/lib/utils/flattened-nodes-observer.js';
@@ -176,7 +176,8 @@ class Crud extends SlotMixin(ElementMixin(ThemableMixin(PolymerElement))) {
176176
height: 100%;
177177
}
178178
179-
:host([hidden]) {
179+
:host([hidden]),
180+
[hidden] {
180181
display: none !important;
181182
}
182183
@@ -199,6 +200,39 @@ class Crud extends SlotMixin(ElementMixin(ThemableMixin(PolymerElement))) {
199200
:host([editor-position='bottom']) #container {
200201
flex-direction: column;
201202
}
203+
204+
[part='editor'] {
205+
z-index: 1;
206+
display: flex;
207+
flex-direction: column;
208+
height: 100%;
209+
}
210+
211+
:host(:not([editor-position=''])[editor-opened]) [part='editor']:not([mobile]) {
212+
flex: 1 0 100%;
213+
}
214+
215+
:host([editor-position='bottom'][editor-opened]) [part='editor']:not([mobile]) {
216+
max-height: var(--vaadin-crud-editor-max-height);
217+
}
218+
219+
:host([editor-position='aside'][editor-opened]) [part='editor']:not([mobile]) {
220+
min-width: 300px;
221+
max-width: var(--vaadin-crud-editor-max-width);
222+
}
223+
224+
[part='scroller'] {
225+
display: flex;
226+
flex-direction: column;
227+
overflow: auto;
228+
flex: auto;
229+
}
230+
231+
[part='footer'] {
232+
display: flex;
233+
flex: none;
234+
flex-direction: row-reverse;
235+
}
202236
</style>
203237
204238
<div id="container">
@@ -222,25 +256,38 @@ class Crud extends SlotMixin(ElementMixin(ThemableMixin(PolymerElement))) {
222256
</slot>
223257
</div>
224258
</div>
225-
<vaadin-dialog-layout
226-
id="dialog"
227-
theme$="[[theme]]"
228-
form="[[_form]]"
229-
no-close-on-outside-click="[[__isDirty]]"
230-
no-close-on-esc="[[__isDirty]]"
231-
opened="{{editorOpened}}"
232-
editor-position="{{editorPosition}}"
233-
mobile="[[__mobile]]"
234-
theme="crud"
259+
260+
<div
261+
id="editor"
262+
part="editor"
263+
hidden$="[[__computeEditorHidden(editorOpened, __mobile, editorPosition)]]"
264+
mobile$="[[__mobile]]"
235265
>
236-
<slot name="header" slot="header"></slot>
237-
<slot name="form" slot="form"></slot>
238-
<slot name="save-button" slot="save-button"></slot>
239-
<slot name="cancel-button" slot="cancel-button"></slot>
240-
<slot name="delete-button" slot="delete-button"></slot>
241-
</vaadin-dialog-layout>
266+
<div part="scroller" id="scroller" role="group" aria-labelledby="header">
267+
<div part="header" id="header">
268+
<slot name="header"></slot>
269+
</div>
270+
<slot name="form"></slot>
271+
</div>
272+
273+
<div part="footer" role="toolbar">
274+
<slot name="save-button"></slot>
275+
<slot name="cancel-button"></slot>
276+
<slot name="delete-button"></slot>
277+
</div>
278+
</div>
242279
</div>
243280
281+
<vaadin-crud-dialog
282+
id="dialog"
283+
opened="[[__computeDialogOpened(editorOpened, __mobile, editorPosition)]]"
284+
aria-label="[[__editorAriaLabel]]"
285+
no-close-on-outside-click="[[__isDirty]]"
286+
no-close-on-esc="[[__isDirty]]"
287+
theme$="[[theme]]"
288+
on-opened-changed="__onDialogOpened"
289+
></vaadin-crud-dialog>
290+
244291
<vaadin-confirm-dialog
245292
theme$="[[theme]]"
246293
id="confirmCancel"
@@ -252,6 +299,7 @@ class Crud extends SlotMixin(ElementMixin(ThemableMixin(PolymerElement))) {
252299
message="[[i18n.confirm.cancel.content]]"
253300
confirm-theme="primary"
254301
></vaadin-confirm-dialog>
302+
255303
<vaadin-confirm-dialog
256304
theme$="[[theme]]"
257305
id="confirmDelete"
@@ -436,6 +484,7 @@ class Crud extends SlotMixin(ElementMixin(ThemableMixin(PolymerElement))) {
436484
*/
437485
editorOpened: {
438486
type: Boolean,
487+
reflectToAttribute: true,
439488
notify: true,
440489
observer: '__onOpenedChanged'
441490
},
@@ -532,6 +581,9 @@ class Crud extends SlotMixin(ElementMixin(ThemableMixin(PolymerElement))) {
532581
}
533582
},
534583

584+
/** @private */
585+
__editorAriaLabel: String,
586+
535587
/** @private */
536588
__isDirty: Boolean,
537589

@@ -628,8 +680,8 @@ class Crud extends SlotMixin(ElementMixin(ThemableMixin(PolymerElement))) {
628680
this.__gridSizeListener = this.__onGridSizeChanges.bind(this);
629681
this.__boundEditOnClickListener = this.__editOnClickListener.bind(this);
630682
this._grid = this.$.grid;
631-
this.$.dialog.$.dialog.$.overlay.addEventListener('vaadin-overlay-outside-click', this.__cancelBound);
632-
this.$.dialog.$.dialog.$.overlay.addEventListener('vaadin-overlay-escape-press', this.__cancelBound);
683+
this.$.dialog.$.overlay.addEventListener('vaadin-overlay-outside-click', this.__cancelBound);
684+
this.$.dialog.$.overlay.addEventListener('vaadin-overlay-escape-press', this.__cancelBound);
633685
// Initialize the default buttons
634686
this.__propagateHostAttributes();
635687
}
@@ -691,12 +743,23 @@ class Crud extends SlotMixin(ElementMixin(ThemableMixin(PolymerElement))) {
691743
this.__onFormChange(this._form);
692744
}
693745

746+
if (opened) {
747+
this.__ensureChildren();
748+
}
749+
694750
this.__toggleToolbar();
751+
752+
// Make sure to reset scroll position
753+
this.$.scroller.scrollTop = 0;
695754
}
696755

697756
/** @private */
698-
__mobileChanged() {
699-
this.__toggleToolbar();
757+
__mobileChanged(mobile, oldMobile) {
758+
if (mobile || oldMobile) {
759+
this.__toggleToolbar();
760+
761+
this.__ensureChildren();
762+
}
700763
}
701764

702765
/** @private */
@@ -707,6 +770,62 @@ class Crud extends SlotMixin(ElementMixin(ThemableMixin(PolymerElement))) {
707770
}
708771
}
709772

773+
/** @private */
774+
__moveChildNodes(target) {
775+
const nodes = [this._headerNode, this._form, this._saveButton, this._cancelButton, this._deleteButton];
776+
if (!nodes.every((node) => node instanceof HTMLElement)) {
777+
return;
778+
}
779+
780+
// Teleport header node, form, and the buttons to corresponding slots.
781+
// NOTE: order in which buttons are moved matches the order of slots.
782+
nodes.forEach((node) => {
783+
target.appendChild(node);
784+
});
785+
786+
// Wait to set label until slotted element has been moved.
787+
setTimeout(() => {
788+
this.__dialogAriaLabel = this._headerNode.textContent.trim();
789+
});
790+
}
791+
792+
/** @private */
793+
__shouldOpenDialog(isMobile, editorPosition) {
794+
return editorPosition === '' || isMobile;
795+
}
796+
797+
/** @private */
798+
__ensureChildren() {
799+
if (this.__shouldOpenDialog(this.__mobile, this.editorPosition)) {
800+
// Move form to dialog
801+
this.__moveChildNodes(this.$.dialog.$.overlay);
802+
} else {
803+
// Move form to crud
804+
this.__moveChildNodes(this);
805+
}
806+
}
807+
808+
/** @private */
809+
__computeDialogOpened(opened, isMobile, editorPosition) {
810+
// Only open dialog when editorPosition is "" or mobile is set
811+
return this.__shouldOpenDialog(isMobile, editorPosition) ? opened : false;
812+
}
813+
814+
/** @private */
815+
__computeEditorHidden(opened, mobile, editorPosition) {
816+
// Only show editor when editorPosition is "bottom" or "aside"
817+
if (['aside', 'bottom'].includes(editorPosition) && !mobile) {
818+
return !opened;
819+
}
820+
821+
return true;
822+
}
823+
824+
/** @private */
825+
__onDialogOpened(event) {
826+
this.editorOpened = event.detail.value;
827+
}
828+
710829
/** @private */
711830
__onDomChange(addedNodes) {
712831
// TODO: restore default button when a corresponding slotted button is removed.
@@ -729,10 +848,8 @@ class Crud extends SlotMixin(ElementMixin(ThemableMixin(PolymerElement))) {
729848
} else if (slotAttributeValue.indexOf('button') >= 0) {
730849
const [button] = slotAttributeValue.split('-');
731850
this[`_${button}Button`] = node;
732-
this.$.dialog[`${button}Button`] = node;
733851
} else if (slotAttributeValue == 'header') {
734852
this._headerNode = node;
735-
this.$.dialog.header = node;
736853
}
737854
});
738855
}
@@ -811,9 +928,7 @@ class Crud extends SlotMixin(ElementMixin(ThemableMixin(PolymerElement))) {
811928
currentButton.parentElement.removeChild(currentButton);
812929
}
813930

814-
if (slottedButton.parentElement === this) {
815-
slottedButton.addEventListener('click', clickListener);
816-
}
931+
slottedButton.addEventListener('click', clickListener);
817932
}
818933

819934
/** @private */

0 commit comments

Comments
 (0)