Skip to content

Commit

Permalink
fix(conten-group-heading): render h3 tag to light DOM (#12074)
Browse files Browse the repository at this point in the history
### Related Ticket(s)
[JIRA](https://jsw.ibm.com/browse/ADCMS-5810)

### Description

Google analytics tools can't pickup on heading elements in shadowroots. We've been asked to adjust content-group-heading elements.
This PR addresses the issue by rendering an h3 element to the light DOM.


### Changelog
the content-group-heading component will now wrap the slotted markup in and <h3> and write the result to the light DOM for SEO purposes
  • Loading branch information
marcelojcs authored Oct 25, 2024
1 parent e221b3c commit cf153d0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@

display: block;
margin-block-end: $spacing-07;

::slotted(h3) {
// stylelint-disable-next-line declaration-no-important
font-size: inherit !important;
}
}

:host(#{$c4d-prefix}-content-group-heading),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* LICENSE file in the root directory of this source tree.
*/

import { LitElement, html } from 'lit';
import { LitElement, html, render } from 'lit';
import { property } from 'lit/decorators.js';
import settings from '@carbon/ibmdotcom-utilities/es/utilities/settings/settings.js';
import StableSelectorMixin from '../../globals/mixins/stable-selector';
Expand All @@ -20,6 +20,7 @@ const { stablePrefix: c4dPrefix } = settings;
* The heading content in content group.
*
* @element c4d-content-group-heading
* @csspart heading - The Heading. Usage: `c4d-content-group-heading::part(heading)`
*/
@customElement(`${c4dPrefix}-content-group-heading`)
class C4DContentGroupHeading extends StableSelectorMixin(LitElement) {
Expand All @@ -29,6 +30,17 @@ class C4DContentGroupHeading extends StableSelectorMixin(LitElement) {
@property({ reflect: true })
slot = 'heading';

/**
* Render the heading tag into the light DOM of this component.
*/
protected _renderHeading() {
const template = document.createElement('template');
template.innerHTML = `<h3>${this.innerHTML.trim()}</h3>`;
this.innerHTML = '';
const heading = template.content.firstChild;
render(html`${heading}`, this);
}

connectedCallback() {
if (!this.hasAttribute('role')) {
this.setAttribute('role', 'heading');
Expand All @@ -39,8 +51,12 @@ class C4DContentGroupHeading extends StableSelectorMixin(LitElement) {
super.connectedCallback();
}

firstUpdated() {
this._renderHeading();
}

render() {
return html` <slot></slot> `;
return html`<slot></slot>`;
}

static get stableSelector() {
Expand Down

0 comments on commit cf153d0

Please sign in to comment.