Skip to content

Commit

Permalink
move the title element inside the shadow root
Browse files Browse the repository at this point in the history
  • Loading branch information
tomivirkki committed Oct 3, 2024
1 parent 844d4cf commit ab4e01b
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 186 deletions.
76 changes: 0 additions & 76 deletions packages/dashboard/src/title-controller.js

This file was deleted.

26 changes: 2 additions & 24 deletions packages/dashboard/src/vaadin-dashboard-section.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ 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 { css, ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
import { TitleController } from './title-controller.js';
import { DashboardItemMixin } from './vaadin-dashboard-item-mixin.js';
import { getDefaultI18n } from './vaadin-dashboard-item-mixin.js';
import { hasWidgetWrappers } from './vaadin-dashboard-styles.js';
Expand Down Expand Up @@ -181,7 +180,6 @@ class DashboardSection extends DashboardItemMixin(
sectionTitle: {
type: String,
value: '',
observer: '__onSectionTitleChanged',
},
};
}
Expand All @@ -194,7 +192,7 @@ class DashboardSection extends DashboardItemMixin(
<div id="focustrap">
<header part="header">
${this.__renderDragHandle()}
<slot name="title" id="title" @slotchange="${this.__onTitleSlotChange}"></slot>
<h2 id="title" part="title">${this.sectionTitle}</h2>
${this.__renderRemoveButton()}
</header>
</div>
Expand All @@ -203,30 +201,10 @@ class DashboardSection extends DashboardItemMixin(
`;
}

constructor() {
super();
this.__titleController = new TitleController(this);
this.__titleController.addEventListener('slot-content-changed', (event) => {
const { node } = event.target;
if (node) {
this.setAttribute('aria-labelledby', node.id);
}
});
}

/** @protected */
ready() {
super.ready();
this.addController(this.__titleController);

if (!this.hasAttribute('role')) {
this.setAttribute('role', 'section');
}
}

/** @private */
__onSectionTitleChanged(sectionTitle) {
this.__titleController.setTitle(sectionTitle);
this.role ??= 'section';
}
}

Expand Down
31 changes: 12 additions & 19 deletions packages/dashboard/src/vaadin-dashboard-widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ 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 { css, ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
import { TitleController } from './title-controller.js';
import { SYNCHRONIZED_ATTRIBUTES, WRAPPER_LOCAL_NAME } from './vaadin-dashboard-helpers.js';
import { DashboardItemMixin } from './vaadin-dashboard-item-mixin.js';
import { getDefaultI18n } from './vaadin-dashboard-item-mixin.js';
Expand Down Expand Up @@ -200,6 +199,12 @@ class DashboardWidget extends DashboardItemMixin(
value: '',
observer: '__onWidgetTitleChanged',
},

/* @private */
__nestedHeadingLevel: {
type: Boolean,
value: false,
},
};
}

Expand All @@ -211,7 +216,9 @@ class DashboardWidget extends DashboardItemMixin(
<div id="focustrap">
<header part="header">
${this.__renderDragHandle()}
<slot name="title" id="title" @slotchange="${this.__onTitleSlotChange}"></slot>
${this.__nestedHeadingLevel
? html`<h3 id="title" part="title" .hidden=${!this.widgetTitle}>${this.widgetTitle}</h3>`
: html`<h2 id="title" part="title" .hidden=${!this.widgetTitle}>${this.widgetTitle}</h2>`}
<slot name="header-content"></slot>
${this.__renderRemoveButton()}
</header>
Expand All @@ -225,17 +232,6 @@ class DashboardWidget extends DashboardItemMixin(
`;
}

constructor() {
super();
this.__titleController = new TitleController(this);
this.__titleController.addEventListener('slot-content-changed', (event) => {
const { node } = event.target;
if (node) {
this.setAttribute('aria-labelledby', node.id);
}
});
}

/** @protected */
connectedCallback() {
super.connectedCallback();
Expand All @@ -259,11 +255,7 @@ class DashboardWidget extends DashboardItemMixin(
/** @protected */
ready() {
super.ready();
this.addController(this.__titleController);

if (!this.hasAttribute('role')) {
this.setAttribute('role', 'article');
}
this.role ??= 'article';
}

/** @private */
Expand All @@ -273,7 +265,8 @@ class DashboardWidget extends DashboardItemMixin(

/** @private */
__updateTitle() {
this.__titleController.setTitle(this.widgetTitle);
const titleLevel = getComputedStyle(this).getPropertyValue('--_vaadin-dashboard-title-level');
this.__nestedHeadingLevel = titleLevel === '3';
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/dashboard/test/dashboard-layout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getColumnWidths,
getRowHeights,
getScrollingContainer,
getTitleElement,
onceResized,
setColspan,
setMaximumColumnCount,
Expand Down Expand Up @@ -511,7 +512,7 @@ describe('dashboard layout', () => {
});

it('should not use minimum row height for section header row', async () => {
const title = section.querySelector<HTMLHeadingElement>('[slot="title"]')!;
const title = getTitleElement(section);
title.style.height = '100%';

const titleHeight = title.offsetHeight;
Expand Down
23 changes: 3 additions & 20 deletions packages/dashboard/test/dashboard-section.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getMoveBackwardButton,
getMoveForwardButton,
getRemoveButton,
getTitleElement,
} from './helpers.js';

describe('dashboard section', () => {
Expand Down Expand Up @@ -40,28 +41,10 @@ describe('dashboard section', () => {
expect(section.getAttribute('role')).to.eql('region');
});

it('should add title id to aria-labelledby attribute when using property', async () => {
section.sectionTitle = 'Custom title';
await nextFrame();
const title = section.querySelector('[slot="title"]');
expect(section.getAttribute('aria-labelledby')).equal(title?.id);
});

it('should add title id to aria-labelledby attribute when using slot', async () => {
const title = document.createElement('div');
title.id = 'custom-title';
title.slot = 'title';
title.textContent = 'Custom title';
section.appendChild(title);

await nextFrame();
expect(section.getAttribute('aria-labelledby')).equal(title?.id);
});

it('should have text content for the title', async () => {
section.sectionTitle = 'Custom title';
await nextFrame();
const title = section.querySelector('[slot="title"]');
const title = getTitleElement(section);
expect(title?.textContent).equal('Custom title');
});
});
Expand Down Expand Up @@ -102,7 +85,7 @@ describe('dashboard section', () => {
section.sectionTitle = null;
await nextFrame();

const title = section.querySelector('[slot="title"]');
const title = getTitleElement(section);
expect(title?.textContent).to.eql('');
});
});
Expand Down
Loading

0 comments on commit ab4e01b

Please sign in to comment.