88 */
99
1010import { css , Part } from 'lit' ;
11+ import { state } from 'lit/decorators.js' ;
1112import settings from '@carbon/ibmdotcom-utilities/es/utilities/settings/settings.js' ;
1213import StableSelectorMixin from '../../globals/mixins/stable-selector' ;
1314import C4DContentBlock from '../content-block/content-block' ;
1415import styles from './content-block-cards.scss' ;
1516import { carbonElement as customElement } from '@carbon/web-components/es/globals/decorators/carbon-element.js' ;
17+ import MediaQueryMixin , {
18+ MQBreakpoints ,
19+ MQDirs ,
20+ } from '../../component-mixins/media-query/media-query' ;
1621
1722const { prefix, stablePrefix : c4dPrefix } = settings ;
1823
@@ -22,7 +27,15 @@ const { prefix, stablePrefix: c4dPrefix } = settings;
2227 * @element c4d-content-block-cards
2328 */
2429@customElement ( `${ c4dPrefix } -content-block-cards` )
25- class C4DContentBlockCards extends StableSelectorMixin ( C4DContentBlock ) {
30+ class C4DContentBlockCards extends MediaQueryMixin (
31+ StableSelectorMixin ( C4DContentBlock ) ,
32+ {
33+ [ MQBreakpoints . MD ] : MQDirs . MAX ,
34+ }
35+ ) {
36+ @state ( )
37+ private _isMobileOrMedium = this . carbonBreakpoints . md . matches ;
38+
2639 /**
2740 * The CSS class list for the container (grid) node.
2841 */
@@ -35,6 +48,61 @@ class C4DContentBlockCards extends StableSelectorMixin(C4DContentBlock) {
3548 return `${ c4dPrefix } --content-block-cards` ;
3649 }
3750
51+ protected setSameHeightOnMobile ( ) {
52+ requestAnimationFrame ( ( ) => {
53+ let maxHeightHeading = 0 ;
54+ let maxHeightParagraph = 0 ;
55+
56+ const blockCards = this . querySelectorAll ( `${ c4dPrefix } -card-group-item` ) ;
57+
58+ blockCards . forEach ( ( card ) => {
59+ const heading = card . querySelector ( 'c4d-card-heading' ) as HTMLElement ;
60+ const para =
61+ card . querySelector ( 'p' ) ||
62+ card ?. shadowRoot ?. querySelector ( 'div.cds--card__copy' ) ;
63+
64+ let headingHeight = 0 ;
65+ let paraHeight = 0 ;
66+
67+ if ( heading && para ) {
68+ headingHeight = heading ?. getBoundingClientRect ( ) . height ;
69+ paraHeight = para ?. getBoundingClientRect ( ) . height ;
70+ }
71+
72+ maxHeightHeading =
73+ maxHeightHeading > headingHeight ? maxHeightHeading : headingHeight ;
74+
75+ maxHeightParagraph =
76+ maxHeightParagraph > paraHeight ? maxHeightParagraph : paraHeight ;
77+ } ) ;
78+
79+ for ( const card of blockCards ) {
80+ const heading = card . querySelector ( 'c4d-card-heading' ) ;
81+ const para =
82+ card . querySelector ( 'p' ) ||
83+ card ?. shadowRoot ?. querySelector ( 'div.cds--card__copy' ) ;
84+ if ( heading && para ) {
85+ heading . setAttribute (
86+ 'style' ,
87+ `height: ${ maxHeightHeading } px; display: block;`
88+ ) ;
89+ para . setAttribute (
90+ 'style' ,
91+ `height: ${ maxHeightParagraph } px; display: block;`
92+ ) ;
93+ }
94+ }
95+ } ) ;
96+ }
97+
98+ firstUpdated ( ) {
99+ window . addEventListener ( 'load' , ( ) => {
100+ if ( this . _isMobileOrMedium ) {
101+ this . setSameHeightOnMobile ( ) ;
102+ }
103+ } ) ;
104+ }
105+
38106 connectedCallback ( ) {
39107 super . connectedCallback ( ) ;
40108
0 commit comments