Skip to content

Commit

Permalink
feat: add gap support to dashboard (#7660)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomivirkki authored Aug 16, 2024
1 parent e0a2f54 commit 3c451c6
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 4 deletions.
3 changes: 1 addition & 2 deletions dev/dashboard-layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@
border-radius: 4px;
padding: 1em;
text-align: center;
margin: 0.5em;
box-sizing: border-box;
height: 100px;
}

vaadin-dashboard-layout {
--vaadin-dashboard-col-min-width: 300px;
--vaadin-dashboard-col-max-width: 500px;
--vaadin-dashboard-gap: 20px;
}
</style>
</head>
Expand Down
2 changes: 2 additions & 0 deletions packages/dashboard/src/vaadin-dashboard-layout-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export const DashboardLayoutMixin = (superClass) =>
auto-fill,
minmax(var(--_vaadin-dashboard-col-min-width), var(--_vaadin-dashboard-col-max-width))
);
gap: var(--vaadin-dashboard-gap, 1rem);
}
::slotted(*) {
Expand Down
37 changes: 37 additions & 0 deletions packages/dashboard/test/dashboard-layout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,41 @@ describe('dashboard layout', () => {
]);
});
});

describe('gap', () => {
it('should have a default gap', () => {
// Clear the gap used in the tests
setGap(dashboard, undefined);
// Increase the width of the dashboard to fit two items and a gap
dashboard.style.width = `${columnWidth * 2 + remValue}px`;

const { right: item0Right } = childElements[0].getBoundingClientRect();
const { left: item1Left } = childElements[1].getBoundingClientRect();
// Expect the items to have a gap of 1rem
expect(item1Left - item0Right).to.eql(remValue);
});

it('should have a custom gap between items horizontally', () => {
const customGap = 10;
setGap(dashboard, customGap);
// Increase the width of the dashboard to fit two items and a gap
dashboard.style.width = `${columnWidth * 2 + customGap}px`;

const { right: item0Right } = childElements[0].getBoundingClientRect();
const { left: item1Left } = childElements[1].getBoundingClientRect();
// Expect the items to have a gap of 10px
expect(item1Left - item0Right).to.eql(customGap);
});

it('should have a custom gap between items vertically', () => {
const customGap = 10;
setGap(dashboard, customGap);
dashboard.style.width = `${columnWidth}px`;

const { bottom: item0Bottom } = childElements[0].getBoundingClientRect();
const { top: item1Top } = childElements[1].getBoundingClientRect();
// Expect the items to have a gap of 10px
expect(item1Top - item0Bottom).to.eql(customGap);
});
});
});
4 changes: 2 additions & 2 deletions packages/dashboard/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ export function setColspan(element: HTMLElement, colspan?: number): void {
/**
* Sets the gap between the cells of the dashboard.
*/
export function setGap(dashboard: HTMLElement, gap: number): void {
dashboard.style.gap = `${gap}px`;
export function setGap(dashboard: HTMLElement, gap?: number): void {
dashboard.style.setProperty('--vaadin-dashboard-gap', gap !== undefined ? `${gap}px` : null);
}
11 changes: 11 additions & 0 deletions packages/dashboard/theme/lumo/vaadin-dashboard-layout-styles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { css, registerStyles } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';

export const dashboardLayoutStyles = css`
:host {
--vaadin-dashboard-gap: var(--lumo-space-m);
}
`;

registerStyles('vaadin-dashboard-layout', [dashboardLayoutStyles], {
moduleId: 'lumo-dashboard-layout',
});
1 change: 1 addition & 0 deletions packages/dashboard/theme/lumo/vaadin-dashboard-layout.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import './vaadin-dashboard-layout-styles.js';
import '../../src/vaadin-dashboard-layout.js';
6 changes: 6 additions & 0 deletions packages/dashboard/theme/lumo/vaadin-dashboard-styles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { registerStyles } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
import { dashboardLayoutStyles } from './vaadin-dashboard-layout-styles.js';

registerStyles('vaadin-dashboard', [dashboardLayoutStyles], {
moduleId: 'lumo-dashboard',
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { css, registerStyles } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';

export const dashboardLayoutStyles = css`
:host {
--vaadin-dashboard-gap: 1rem;
}
`;

registerStyles('vaadin-dashboard-layout', [dashboardLayoutStyles], {
moduleId: 'material-dashboard-layout',
});
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import './vaadin-dashboard-layout-styles.js';
import '../../src/vaadin-dashboard-layout.js';
6 changes: 6 additions & 0 deletions packages/dashboard/theme/material/vaadin-dashboard-styles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { registerStyles } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
import { dashboardLayoutStyles } from './vaadin-dashboard-layout-styles.js';

registerStyles('vaadin-dashboard', [dashboardLayoutStyles], {
moduleId: 'material-dashboard',
});

0 comments on commit 3c451c6

Please sign in to comment.