Skip to content

Commit 0421dfc

Browse files
HDS-4532 Move fallback values for CSS properties from JS to CSS
1 parent 49d2c3a commit 0421dfc

File tree

6 files changed

+35
-18
lines changed

6 files changed

+35
-18
lines changed

packages/components/src/components/hds/layout/grid/index.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,10 @@ export default class HdsLayoutGrid extends Component<HdsLayoutGridSignature> {
8787
'--hds-layout-grid-column-min-width'?: string;
8888
} = {};
8989

90-
// Note: "Unitless 0" <length>s aren’t supported in math functions so we use 0px as the default col min width
91-
// https://drafts.csswg.org/css-values/#calc-type-checking
92-
inlineStyles['--hds-layout-grid-column-min-width'] =
93-
this.args.columnMinWidth ?? '0px';
94-
90+
if (this.args.columnMinWidth) {
91+
inlineStyles['--hds-layout-grid-column-min-width'] =
92+
this.args.columnMinWidth;
93+
}
9594
return inlineStyles;
9695
}
9796

packages/components/src/components/hds/layout/grid/item.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ export default class HdsLayoutFlexItem extends Component<HdsLayoutGridItemSignat
3030
'--hds-layout-grid-row-span'?: string;
3131
} = {};
3232

33-
inlineStyles['--hds-layout-grid-column-span'] = this.args.colSpan ?? '1';
34-
inlineStyles['--hds-layout-grid-row-span'] = this.args.rowSpan ?? '1';
33+
if (this.args.colSpan) {
34+
inlineStyles['--hds-layout-grid-column-span'] = this.args.colSpan;
35+
}
36+
if (this.args.rowSpan) {
37+
inlineStyles['--hds-layout-grid-row-span'] = this.args.rowSpan;
38+
}
3539

3640
return inlineStyles;
3741
}

packages/components/src/styles/components/layout/grid.scss

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
auto-fit,
1919
minmax(
2020
calc(
21-
var(--hds-layout-grid-column-min-width) - var(--hds-layout-grid-gap, var(--hds-layout-grid-column-gap, 0px)) ),
21+
var(--hds-layout-grid-column-min-width, 0px) - var(--hds-layout-grid-gap, var(--hds-layout-grid-column-gap, 0px)) ),
2222
1fr
2323
)
2424
);
@@ -76,6 +76,6 @@
7676
// LAYOUT > GRID > ITEM
7777

7878
.hds-layout-grid-item {
79-
grid-row-start: span var(--hds-layout-grid-row-span);
80-
grid-column-start: span var(--hds-layout-grid-column-span);
79+
grid-row-start: span var(--hds-layout-grid-row-span, 1);
80+
grid-column-start: span var(--hds-layout-grid-column-span, 1);
8181
}

showcase/app/templates/layouts/grid.hbs

+11
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@
4343
</Hds::Layout::Grid>
4444
</Shw::Outliner>
4545
</SG.Item>
46+
47+
<SG.Item @label="40em min width columns">
48+
<Shw::Outliner>
49+
<Hds::Layout::Grid @gap="48" @columnMinWidth="40em">
50+
<Shw::Placeholder @text="#1" @height="24" @background="#e4c5f3" />
51+
<Shw::Placeholder @text="#2" @height="24" @background="#e5ffd2" />
52+
<Shw::Placeholder @text="#3" @height="24" @background="#d2f4ff" />
53+
<Shw::Placeholder @text="#4" @height="24" @background="#fff8d2" />
54+
</Hds::Layout::Grid>
55+
</Shw::Outliner>
56+
</SG.Item>
4657
</Shw::Grid>
4758

4859
<Shw::Divider @level={{2}} />

showcase/tests/integration/components/hds/layout/grid/index-test.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ module('Integration | Component | hds/layout/grid/index', function (hooks) {
4040

4141
// COLUMN MIN WIDTH
4242

43-
test('it should render a default min-width of 0px if @columnMinWidth is not declared', async function (assert) {
43+
// Note: A fallback value of 0px is set in the CSS for the `--hds-layout-grid-column-min-width` custom property
44+
test('if the @columnMinWidth prop is not declared, --hds-layout-grid-column-min-width should be unset', async function (assert) {
4445
await render(hbs`<Hds::Layout::Grid id="test-layout-grid" />`);
4546
assert
4647
.dom('#test-layout-grid')
47-
.hasStyle({ '--hds-layout-grid-column-min-width': '0px' });
48+
.doesNotHaveStyle('--hds-layout-grid-column-min-width');
4849
});
4950

5051
test('it should render the correct min-width if the @columnMinWidth prop is declared', async function (assert) {

showcase/tests/integration/components/hds/layout/grid/item-test.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ module('Integration | Component | hds/layout/grid/item', function (hooks) {
5050

5151
// COL SPAN
5252

53-
test('it should render a default column span of 1 if @colSpan is not declared', async function (assert) {
54-
await render(hbs`<Hds::Layout::Grid::Item id="test-layout-grid" />`);
53+
// Note: A fallback value of 1 is set in the CSS for the `--hds-layout-grid-column-span` custom property
54+
test('if the @colSpan prop is not declared, --hds-layout-grid-column-span should be unset', async function (assert) {
55+
await render(hbs`<Hds::Layout::Grid::Item id="test-layout-grid-item" />`);
5556
assert
56-
.dom('#test-layout-grid')
57-
.hasStyle({ '--hds-layout-grid-column-span': '1' });
57+
.dom('#test-layout-grid-item')
58+
.doesNotHaveStyle('--hds-layout-grid-column-span');
5859
});
5960

6061
test('it should render the correct column span if the @colSpan prop is declared', async function (assert) {
@@ -68,11 +69,12 @@ module('Integration | Component | hds/layout/grid/item', function (hooks) {
6869

6970
// ROW SPAN
7071

71-
test('it should render a default row span of 1 if @rowSpan is not declared', async function (assert) {
72+
// Note: A fallback value of 1 is set in the CSS for the `--hds-layout-grid-row-span` custom property
73+
test('if the @rowSpan prop is not declared, --hds-layout-grid-row-span should be unset', async function (assert) {
7274
await render(hbs`<Hds::Layout::Grid::Item id="test-layout-grid" />`);
7375
assert
7476
.dom('#test-layout-grid')
75-
.hasStyle({ '--hds-layout-grid-row-span': '1' });
77+
.doesNotHaveStyle('--hds-layout-grid-row-span');
7678
});
7779

7880
test('it should render the correct row span if the @rowSpan prop is declared', async function (assert) {

0 commit comments

Comments
 (0)