Skip to content

Commit 9426214

Browse files
authored
refactor: use attributes for widget/section state (#7807)
1 parent 9f35872 commit 9426214

7 files changed

Lines changed: 170 additions & 27 deletions

‎packages/dashboard/src/vaadin-dashboard-helpers.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
export const WRAPPER_LOCAL_NAME = 'vaadin-dashboard-widget-wrapper';
88

9+
// The attributes that should be synchronized from the wrapper to widget/section
10+
export const SYNCHRONIZED_ATTRIBUTES = ['editable', 'dragging'];
11+
912
/**
1013
* Returns the array of items that contains the given item.
1114
* Might be the dashboard items or the items of a section.

‎packages/dashboard/src/vaadin-dashboard-styles.js‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,21 @@ export const hasWidgetWrappers = css`
77
`;
88

99
export const dashboardWidgetAndSectionStyles = css`
10-
/* Placeholder shown while the widget or section is dragged */
11-
:host::before {
12-
content: '';
13-
z-index: 1;
14-
position: absolute;
15-
display: var(--_vaadin-dashboard-item-placeholder-display, none);
16-
inset: 0;
17-
border: 3px dashed black;
18-
border-radius: 5px;
19-
background-color: #fff;
10+
:host {
11+
box-sizing: border-box;
12+
}
13+
14+
:host([dragging]) {
15+
border: 3px dashed black !important;
16+
}
17+
18+
:host([dragging]) * {
19+
visibility: hidden;
20+
}
21+
22+
:host(:not([editable])) #drag-handle,
23+
:host(:not([editable])) #remove-button {
24+
display: none;
2025
}
2126
2227
header {
@@ -26,7 +31,6 @@ export const dashboardWidgetAndSectionStyles = css`
2631
}
2732
2833
#drag-handle {
29-
display: var(--_vaadin-dashboard-widget-actions-display, none);
3034
font-size: 30px;
3135
cursor: grab;
3236
}
@@ -36,7 +40,6 @@ export const dashboardWidgetAndSectionStyles = css`
3640
}
3741
3842
#remove-button {
39-
display: var(--_vaadin-dashboard-widget-actions-display, none);
4043
font-size: 30px;
4144
cursor: pointer;
4245
}

‎packages/dashboard/src/vaadin-dashboard-widget.js‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
1515
import { PolylitMixin } from '@vaadin/component-base/src/polylit-mixin.js';
1616
import { css } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
1717
import { TitleController } from './title-controller.js';
18+
import { SYNCHRONIZED_ATTRIBUTES, WRAPPER_LOCAL_NAME } from './vaadin-dashboard-helpers.js';
1819
import { dashboardWidgetAndSectionStyles } from './vaadin-dashboard-styles.js';
1920

2021
/**
@@ -45,13 +46,16 @@ class DashboardWidget extends ControllerMixin(ElementMixin(PolylitMixin(LitEleme
4546
display: none !important;
4647
}
4748
49+
:host(:not([editable])) #resize-handle {
50+
display: none;
51+
}
52+
4853
#content {
4954
flex: 1;
5055
min-height: 100px;
5156
}
5257
5358
#resize-handle {
54-
display: var(--_vaadin-dashboard-widget-actions-display, none);
5559
position: absolute;
5660
bottom: 0;
5761
inset-inline-end: 0;
@@ -129,6 +133,13 @@ class DashboardWidget extends ControllerMixin(ElementMixin(PolylitMixin(LitEleme
129133
connectedCallback() {
130134
super.connectedCallback();
131135

136+
const wrapper = this.closest(WRAPPER_LOCAL_NAME);
137+
if (wrapper) {
138+
SYNCHRONIZED_ATTRIBUTES.forEach((attr) => {
139+
this.toggleAttribute(attr, wrapper.hasAttribute(attr));
140+
});
141+
}
142+
132143
const undefinedAncestor = this.closest('*:not(:defined)');
133144
if (undefinedAncestor) {
134145
customElements.whenDefined(undefinedAncestor.localName).then(() => queueMicrotask(() => this.__updateTitle()));

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ import { defineCustomElement } from '@vaadin/component-base/src/define.js';
1616
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
1717
import { PolylitMixin } from '@vaadin/component-base/src/polylit-mixin.js';
1818
import { css, ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
19-
import { getElementItem, getItemsArrayOfItem, WRAPPER_LOCAL_NAME } from './vaadin-dashboard-helpers.js';
19+
import {
20+
getElementItem,
21+
getItemsArrayOfItem,
22+
SYNCHRONIZED_ATTRIBUTES,
23+
WRAPPER_LOCAL_NAME,
24+
} from './vaadin-dashboard-helpers.js';
2025
import { DashboardLayoutMixin } from './vaadin-dashboard-layout-mixin.js';
2126
import { DashboardSection } from './vaadin-dashboard-section.js';
2227
import { hasWidgetWrappers } from './vaadin-dashboard-styles.js';
@@ -53,10 +58,6 @@ class Dashboard extends ControllerMixin(DashboardLayoutMixin(ElementMixin(Themab
5358
return [
5459
super.styles,
5560
css`
56-
:host([editable]) {
57-
--_vaadin-dashboard-widget-actions-display: block;
58-
}
59-
6061
#grid[resizing] {
6162
-webkit-user-select: none;
6263
user-select: none;
@@ -98,13 +99,12 @@ class Dashboard extends ControllerMixin(DashboardLayoutMixin(ElementMixin(Themab
9899
*/
99100
editable: {
100101
type: Boolean,
101-
reflectToAttribute: true,
102102
},
103103
};
104104
}
105105

106106
static get observers() {
107-
return ['__itemsOrRendererChanged(items, renderer)'];
107+
return ['__itemsOrRendererChanged(items, renderer, editable)'];
108108
}
109109

110110
constructor() {
@@ -144,6 +144,12 @@ class Dashboard extends ControllerMixin(DashboardLayoutMixin(ElementMixin(Themab
144144
} else {
145145
cell.innerHTML = '';
146146
}
147+
148+
if (cell.firstElementChild) {
149+
SYNCHRONIZED_ATTRIBUTES.forEach((attr) => {
150+
cell.firstElementChild.toggleAttribute(attr, cell.hasAttribute(attr));
151+
});
152+
}
147153
});
148154
}
149155

@@ -182,6 +188,7 @@ class Dashboard extends ControllerMixin(DashboardLayoutMixin(ElementMixin(Themab
182188
}
183189

184190
section.toggleAttribute('highlight', !!this.__widgetReorderController.draggedItem);
191+
SYNCHRONIZED_ATTRIBUTES.forEach((attr) => section.toggleAttribute(attr, wrapper.hasAttribute(attr)));
185192
// Render the subitems
186193
this.__renderItemWrappers(item.items, section);
187194
}
@@ -200,15 +207,14 @@ class Dashboard extends ControllerMixin(DashboardLayoutMixin(ElementMixin(Themab
200207

201208
/** @private */
202209
__updateWrapper(wrapper, item) {
203-
const itemDragged = this.__widgetReorderController.draggedItem === item;
204-
205210
const style = `
206211
${item.colspan ? `--vaadin-dashboard-item-colspan: ${item.colspan};` : ''}
207212
${item.rowspan ? `--vaadin-dashboard-item-rowspan: ${item.rowspan};` : ''}
208-
${itemDragged ? '--_vaadin-dashboard-item-placeholder-display: block;' : ''}
209213
`.trim();
210214

211215
wrapper.setAttribute('style', style);
216+
wrapper.toggleAttribute('editable', !!this.editable);
217+
wrapper.toggleAttribute('dragging', this.__widgetReorderController.draggedItem === item);
212218
}
213219

214220
/** @private */

‎packages/dashboard/test/dashboard-widget-reordering.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,9 @@ describe('dashboard - widget reordering', () => {
553553
fireDragOver(getElementFromCell(dashboard, 0, 0)!, 'top');
554554
await nextFrame();
555555

556+
fireDragEnd(dashboard);
557+
await nextFrame();
558+
556559
// prettier-ignore
557560
expectLayout(dashboard, [
558561
[2, 3],

‎packages/dashboard/test/dashboard.test.ts‎

Lines changed: 116 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
getDraggable,
1111
getElementFromCell,
1212
getRemoveButton,
13+
getResizeHandle,
1314
setGap,
1415
setMaximumColumnWidth,
1516
setMinimumColumnWidth,
@@ -19,11 +20,11 @@ type TestDashboardItem = DashboardItem & { id: string; component?: Element | str
1920

2021
describe('dashboard', () => {
2122
let dashboard: Dashboard<TestDashboardItem>;
22-
const columnWidth = 100;
23+
const columnWidth = 200;
2324

2425
beforeEach(async () => {
2526
dashboard = fixtureSync('<vaadin-dashboard></vaadin-dashboard>');
26-
dashboard.style.width = `${columnWidth * 100}px`;
27+
dashboard.style.width = `${columnWidth * 2}px`;
2728
setMinimumColumnWidth(dashboard, columnWidth);
2829
setMaximumColumnWidth(dashboard, columnWidth);
2930
setGap(dashboard, 0);
@@ -51,7 +52,7 @@ describe('dashboard', () => {
5152
dashboard.items = [...dashboard.items, { id: 'Item 2' }];
5253
await nextFrame();
5354

54-
const newWidget = getElementFromCell(dashboard, 0, 2);
55+
const newWidget = getElementFromCell(dashboard, 1, 0);
5556
expect(newWidget).to.be.ok;
5657
expect(newWidget?.localName).to.equal('vaadin-dashboard-widget');
5758
expect(newWidget).to.have.property('widgetTitle', 'Item 2 title');
@@ -220,6 +221,95 @@ describe('dashboard', () => {
220221
const draggable = getDraggable(widget);
221222
expect(draggable.getBoundingClientRect().height).to.be.above(0);
222223
});
224+
225+
it('should hide remove button by default', () => {
226+
const widget = getElementFromCell(dashboard, 0, 0) as DashboardWidget;
227+
const removeButton = getRemoveButton(widget);
228+
expect(removeButton.getBoundingClientRect().height).to.equal(0);
229+
});
230+
231+
it('should unhide remove button when editable', async () => {
232+
dashboard.editable = true;
233+
await nextFrame();
234+
const widget = getElementFromCell(dashboard, 0, 0) as DashboardWidget;
235+
const removeButton = getRemoveButton(widget);
236+
expect(removeButton.getBoundingClientRect().height).to.be.above(0);
237+
});
238+
239+
it('should hide resize handle by default', () => {
240+
const widget = getElementFromCell(dashboard, 0, 0) as DashboardWidget;
241+
const resizeHandle = getResizeHandle(widget);
242+
expect(resizeHandle.getBoundingClientRect().height).to.equal(0);
243+
});
244+
245+
it('should unhide resize handle when editable', async () => {
246+
dashboard.editable = true;
247+
await nextFrame();
248+
const widget = getElementFromCell(dashboard, 0, 0) as DashboardWidget;
249+
const resizeHandle = getResizeHandle(widget);
250+
expect(resizeHandle.getBoundingClientRect().height).to.be.above(0);
251+
});
252+
253+
it('should unhide resize handle when editable with a lazy renderer', async () => {
254+
// Assign a renderer that initially renders nothing
255+
const syncRenderer = dashboard.renderer!;
256+
dashboard.renderer = (root, _, model) => {
257+
root.textContent = '';
258+
requestAnimationFrame(() => {
259+
syncRenderer(root, _, model);
260+
});
261+
};
262+
await nextFrame();
263+
264+
dashboard.editable = true;
265+
await nextFrame();
266+
const widget = getElementFromCell(dashboard, 0, 0) as DashboardWidget;
267+
const resizeHandle = getResizeHandle(widget);
268+
expect(resizeHandle.getBoundingClientRect().height).to.be.above(0);
269+
});
270+
271+
describe('section', () => {
272+
beforeEach(async () => {
273+
dashboard.items = [
274+
{ id: 'Item 0' },
275+
{ id: 'Item 1' },
276+
{ title: 'Section', items: [{ id: 'Item 2' }, { id: 'Item 3' }] },
277+
];
278+
await nextFrame();
279+
});
280+
281+
it('should hide draggable handle by default', () => {
282+
const widget = getElementFromCell(dashboard, 1, 0)!;
283+
const section = widget.closest('vaadin-dashboard-section') as DashboardSection;
284+
const draggable = getDraggable(section);
285+
expect(draggable.getBoundingClientRect().height).to.equal(0);
286+
});
287+
288+
it('should unhide draggable handle when editable', async () => {
289+
dashboard.editable = true;
290+
await nextFrame();
291+
const widget = getElementFromCell(dashboard, 1, 0)!;
292+
const section = widget.closest('vaadin-dashboard-section') as DashboardSection;
293+
const draggable = getDraggable(section);
294+
expect(draggable.getBoundingClientRect().height).to.be.above(0);
295+
});
296+
297+
it('should hide remove button by default', () => {
298+
const widget = getElementFromCell(dashboard, 1, 0) as DashboardSection;
299+
const section = widget.closest('vaadin-dashboard-section') as DashboardSection;
300+
const removeButton = getRemoveButton(section);
301+
expect(removeButton.getBoundingClientRect().height).to.equal(0);
302+
});
303+
304+
it('should unhide remove button when editable', async () => {
305+
dashboard.editable = true;
306+
await nextFrame();
307+
const widget = getElementFromCell(dashboard, 1, 0) as DashboardSection;
308+
const section = widget.closest('vaadin-dashboard-section') as DashboardSection;
309+
const removeButton = getRemoveButton(section);
310+
expect(removeButton.getBoundingClientRect().height).to.be.above(0);
311+
});
312+
});
223313
});
224314

225315
describe('item components', () => {
@@ -347,5 +437,28 @@ describe('dashboard', () => {
347437
await nextFrame();
348438
expect(document.activeElement).to.equal(getElementFromCell(dashboard, 0, 0)!);
349439
});
440+
441+
it('should unhide resize handle when editable', async () => {
442+
getElementFromCell(dashboard, 0, 0)!.focus();
443+
await nextFrame();
444+
dashboard.editable = true;
445+
await nextFrame();
446+
const widget = getElementFromCell(dashboard, 0, 0) as DashboardWidget;
447+
const resizeHandle = getResizeHandle(widget);
448+
expect(resizeHandle.getBoundingClientRect().height).to.be.above(0);
449+
});
450+
451+
it('should unhide remove button of a section when editable', async () => {
452+
dashboard.items = [{ title: 'Section', items: [{ id: 'Item 0' }] }, { id: 'Item 1' }];
453+
await nextFrame();
454+
getElementFromCell(dashboard, 0, 0)!.focus();
455+
await nextFrame();
456+
dashboard.editable = true;
457+
await nextFrame();
458+
const widget = getElementFromCell(dashboard, 0, 0) as DashboardWidget;
459+
const section = widget.closest('vaadin-dashboard-section') as DashboardSection;
460+
const removeButton = getRemoveButton(section);
461+
expect(removeButton.getBoundingClientRect().height).to.be.above(0);
462+
});
350463
});
351464
});

‎packages/dashboard/test/helpers.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,12 @@ export function fireResizeOver(dragOverTarget: Element, location: 'top' | 'botto
263263
dragOverTarget.dispatchEvent(event);
264264
}
265265

266+
export function getResizeHandle(resizedWidget: Element): Element {
267+
return resizedWidget.shadowRoot!.querySelector('.resize-handle')!;
268+
}
269+
266270
export function fireResizeStart(resizedWidget: Element): void {
267-
let handle = resizedWidget.shadowRoot!.querySelector('.resize-handle');
271+
let handle = getResizeHandle(resizedWidget);
268272
if (!handle) {
269273
handle = resizedWidget;
270274
}

0 commit comments

Comments
 (0)