Skip to content

Commit c519b06

Browse files
authored
feat(logo-grid): add logoCount and logoRatio props (#8905)
### Related Ticket(s) Closes #8848 ### Description Adds `logoCount` and `logoRatio` props to `DDSLogoGrid` `logoCount` takes an integer to use as a number of columns for the grid. The stylesheet will constrain that value to between 2 and 4, inclusively. `logoRatio` takes a string matching the format `<integer><separator><integer>` and sets a custom CSS property in an inline style to use in logo-grid-item elements as an aspect ratio. ### Changelog **New** - Added `logoCount` and `logoRatio` props to `DDSLogoGrid`
1 parent bfb3343 commit c519b06

File tree

8 files changed

+134
-15
lines changed

8 files changed

+134
-15
lines changed

packages/styles/scss/components/logo-grid/_logo-grid.scss

+26-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright IBM Corp. 2016, 2021
2+
* Copyright IBM Corp. 2016, 2022
33
*
44
* This source code is licensed under the Apache-2.0 license found in the
55
* LICENSE file in the root directory of this source tree.
@@ -36,7 +36,7 @@
3636
}
3737

3838
.#{$prefix}--content-block__cta {
39-
margin-top: $layout-01;
39+
margin-top: $spacing-09;
4040

4141
@include carbon--make-col-ready;
4242
@include carbon--make-col(1, 1);
@@ -97,25 +97,47 @@
9797

9898
.#{$prefix}--logo-grid__row {
9999
display: grid;
100-
grid-template-columns: 1fr;
101100
margin-left: calc(-1 * #{$carbon--grid-gutter} / 2);
102101
margin-right: calc(-1 * #{$carbon--grid-gutter} / 2);
102+
grid-template-columns: repeat(2, 1fr);
103103

104104
@include carbon--breakpoint('md') {
105-
grid-template-columns: repeat(2, 1fr);
105+
&.#{$prefix}--logo-grid__4-columns {
106+
grid-template-columns: repeat(4, 1fr);
107+
}
106108
}
107109

108110
@include carbon--breakpoint('lg') {
109111
grid-template-columns: repeat(3, 1fr);
112+
113+
&.#{$prefix}--logo-grid__4-columns {
114+
grid-template-columns: repeat(4, 1fr);
115+
}
110116
}
111117
}
112118

113119
.#{$prefix}--logo-grid__col,
114120
:host(#{$dds-prefix}-logo-grid-item) {
121+
/* stylelint-disable */
122+
aspect-ratio: var(--logo-ratio, '16/9');
123+
/* stylelint-enable */
115124
padding-left: $carbon--grid-gutter--condensed / 2;
116125
padding-right: $carbon--grid-gutter--condensed / 2;
117126
}
118127

128+
:host(#{$dds-prefix}-logo-grid-item) {
129+
display: flex;
130+
align-items: stretch;
131+
justify-content: center;
132+
133+
picture,
134+
img {
135+
width: 100%;
136+
height: 100%;
137+
object-fit: contain;
138+
}
139+
}
140+
119141
.#{$prefix}--logo-grid__heading {
120142
@include carbon--type-style('expressive-heading-05', true);
121143

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright IBM Corp. 2016, 2022
3+
*
4+
* This source code is licensed under the Apache-2.0 license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
export { default as parseAspectRatio } from './parseAspectRatio';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright IBM Corp. 2016, 2022
3+
*
4+
* This source code is licensed under the Apache-2.0 license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
/**
9+
* Utility to parse aspect ratios into width & height values
10+
*
11+
* @example
12+
* import {parseAspectRatio} from '@carbon/ibmdotcom-utilities';
13+
*
14+
* const [width, height] = parseAspectRatio('16x9');
15+
*
16+
* @param {string} aspectRatioString string in format <integer><separator><integer>
17+
* @returns {Array} [widthInt, heightInt]
18+
*/
19+
20+
function parseAspectRatio(aspectRatioString) {
21+
if (typeof aspectRatioString !== 'string') {
22+
throw new TypeError(
23+
`parseAspectRatio expected a string but was given a ${typeof aspectRatioString}`
24+
);
25+
}
26+
27+
const parsed = aspectRatioString.split(/[^0-9]{1}/).filter(i => i);
28+
29+
if (parsed.length !== 2) {
30+
throw new RangeError(
31+
`Parsed aspect ratio should have a length of 2, but had ${parsed.length} instead`
32+
);
33+
}
34+
35+
return parsed;
36+
}
37+
38+
export default parseAspectRatio;

packages/web-components/examples/codesandbox/components/logo-grid/cdn.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<div class="bx--grid">
2929
<div class="bx--row">
3030
<div class="bx--col-sm-4 bx--col-lg-12">
31-
<dds-logo-grid>
31+
<dds-logo-grid logo-count="3" logo-ratio="2:1">
3232
<dds-content-block-heading>
3333
This is a heading
3434
</dds-content-block-heading>

packages/web-components/examples/codesandbox/components/logo-grid/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<div class="bx--grid">
2828
<div class="bx--row">
2929
<div class="bx--col-sm-4 bx--col-lg-12">
30-
<dds-logo-grid>
30+
<dds-logo-grid logo-count="3" logo-ratio="2:1">
3131
<dds-content-block-heading>
3232
This is a heading
3333
</dds-content-block-heading>

packages/web-components/src/components/logo-grid/__stories__/logo-grid.stories.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license
33
*
4-
* Copyright IBM Corp. 2020, 2021
4+
* Copyright IBM Corp. 2020, 2022
55
*
66
* This source code is licensed under the Apache-2.0 license found in the
77
* LICENSE file in the root directory of this source tree.
@@ -12,17 +12,18 @@ import '../../content-block/content-block-heading';
1212
import '../logo-grid-item';
1313
import '../logo-grid-link';
1414
import '../../card/card-heading';
15-
import { boolean, text } from '@storybook/addon-knobs';
15+
import { boolean, text, select } from '@storybook/addon-knobs';
1616
import { html } from 'lit-element';
1717
import ArrowRight20 from 'carbon-web-components/es/icons/arrow--right/20.js';
1818
import logos from './data/logos.js';
1919
import readme from './README.stories.mdx';
2020
import textNullable from '../../../../.storybook/knob-text-nullable';
2121

2222
export const Default = ({ parameters }) => {
23-
const { heading, logosGroup, hideBorder, showCta, ctaCopy, ctaHref } = parameters?.props?.LogoGrid ?? {};
23+
const { heading, logoCount, logoRatio, logosGroup, hideBorder, showCta, ctaCopy, ctaHref } = parameters?.props?.LogoGrid ?? {};
24+
2425
return html`
25-
<dds-logo-grid ?hide-border="${hideBorder}">
26+
<dds-logo-grid ?hide-border="${hideBorder}" logo-count="${logoCount}" logo-ratio="${logoRatio}">
2627
<dds-content-block-heading>
2728
${heading}
2829
</dds-content-block-heading>
@@ -65,6 +66,13 @@ export default {
6566
knobs: {
6667
LogoGrid: ({ groupId }) => ({
6768
heading: textNullable('Heading (heading)', 'Our customers', groupId),
69+
logoCount: select('Column count (logoCount)', { 'Default (3)': '3', '4': '4' }, '3', groupId),
70+
logoRatio: select(
71+
'Logo aspect ratio (logoRatio)',
72+
{ 'Default (4:3)': '4:3', '16:9': '16:9', '2:1': '2:1' },
73+
'4:3',
74+
groupId
75+
),
6876
logosGroup: logos,
6977
hideBorder: boolean('Hide border (hideBorder): Hide the bottom border', false, groupId),
7078
showCta: boolean('Display CTA:', false, groupId),
@@ -76,6 +84,8 @@ export default {
7684
default: {
7785
LogoGrid: {
7886
heading: 'Our customers',
87+
logoCount: '3',
88+
logoRatio: '4:3',
7989
logosGroup: logos,
8090
hideBorder: false,
8191
showCta: false,

packages/web-components/src/components/logo-grid/logo-grid.ts

+45-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
/**
22
* @license
33
*
4-
* Copyright IBM Corp. 2020, 2021
4+
* Copyright IBM Corp. 2020, 2022
55
*
66
* This source code is licensed under the Apache-2.0 license found in the
77
* LICENSE file in the root directory of this source tree.
88
*/
99
import { css, customElement, html, property, TemplateResult } from 'lit-element';
10+
import { classMap } from 'lit-html/directives/class-map';
1011
import settings from 'carbon-components/es/globals/js/settings.js';
11-
import ddsSettings from '@carbon/ibmdotcom-utilities/es/utilities/settings/settings';
12+
import ddsSettings from '../../internal/vendor/@carbon/ibmdotcom-utilities/utilities/settings/settings';
13+
import parseAspectRatio from '../../internal/vendor/@carbon/ibmdotcom-utilities/utilities/parseAspectRatio/parseAspectRatio';
1214
import DDSContentBlock from '../content-block/content-block';
1315
import '../horizontal-rule/horizontal-rule';
1416
import '../content-block/content-block-heading';
@@ -26,10 +28,16 @@ const { stablePrefix: ddsPrefix } = ddsSettings;
2628
@customElement(`${ddsPrefix}-logo-grid`)
2729
class DDSLogoGrid extends StableSelectorMixin(DDSContentBlock) {
2830
protected _renderInnerBody() {
29-
const { _hasContent: hasContent, _hasMedia: hasMedia } = this;
31+
const { _hasContent: hasContent, _hasMedia: hasMedia, logoCount } = this;
32+
33+
const rowClasses = {
34+
[`${prefix}--logo-grid__row`]: true,
35+
[`${prefix}--logo-grid__${logoCount}-columns`]: logoCount,
36+
};
37+
3038
return html`
3139
<div ?hidden="${!hasContent && !hasMedia}" class="${prefix}--content-block__children ${prefix}--content-layout__body">
32-
<div class="${prefix}--logo-grid__row">
40+
<div class="${classMap(rowClasses)}">
3341
${this._renderContent()}${this._renderMedia()}
3442
</div>
3543
</div>
@@ -53,6 +61,39 @@ class DDSLogoGrid extends StableSelectorMixin(DDSContentBlock) {
5361
@property({ attribute: 'hide-border', reflect: true, type: Boolean })
5462
hideBorder = false;
5563

64+
/**
65+
* Integer value that determines the number of columns in the grid.
66+
*
67+
* While any non-zero integer will work, styles will constrain values to between
68+
* two and four.
69+
*/
70+
@property({ attribute: 'logo-count', reflect: true })
71+
logoCount?;
72+
73+
/**
74+
* Aspect ratio of grid cells.
75+
*
76+
* Values should match the format `<integer><divider-character><integer>`.
77+
* Any non-digit character can work as a divider.
78+
*/
79+
@property({ attribute: 'logo-ratio', reflect: true })
80+
logoRatio?;
81+
82+
updated(changedProperties) {
83+
const { logoRatio } = this;
84+
if (changedProperties.has('logoRatio')) {
85+
if (logoRatio) {
86+
const ratioSplit = parseAspectRatio(logoRatio);
87+
if (ratioSplit.length === 2) {
88+
const [w, h] = ratioSplit;
89+
this.style.setProperty('--logo-ratio', `${w}/${h}`);
90+
}
91+
} else {
92+
this.style.removeProperty('--logo-ratio');
93+
}
94+
}
95+
}
96+
5697
render() {
5798
return html`
5899
<div class="${prefix}--content-layout--logo-grid">

packages/web-components/tests/snapshots/dds-logo-grid.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
class="bx--content-block__children bx--content-layout__body"
1717
hidden=""
1818
>
19-
<div class="bx--logo-grid__row">
19+
<div class="bx--logo-grid__row bx--logo-grid__undefined-columns">
2020
<slot>
2121
</slot>
2222
<slot name="media">

0 commit comments

Comments
 (0)