Skip to content

Commit

Permalink
feat: set allowed values for grid tag property to limit misuse (#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
melaniebmn committed Mar 14, 2024
1 parent f859d43 commit a686d09
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 5 deletions.
20 changes: 18 additions & 2 deletions packages/web/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,15 @@ export namespace Components {
/**
* Set tag for grid container
*/
"tag"?: string;
"tag"?: | 'article'
| 'aside'
| 'div'
| 'dl'
| 'main'
| 'nav'
| 'ol'
| 'section'
| 'ul';
}
interface GcdsGridCol {
/**
Expand Down Expand Up @@ -2287,7 +2295,15 @@ declare namespace LocalJSX {
/**
* Set tag for grid container
*/
"tag"?: string;
"tag"?: | 'article'
| 'aside'
| 'div'
| 'dl'
| 'main'
| 'nav'
| 'ol'
| 'section'
| 'ul';
}
interface GcdsGridCol {
/**
Expand Down
37 changes: 35 additions & 2 deletions packages/web/src/components/gcds-grid/gcds-grid.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, Element, Host, Prop, h } from '@stencil/core';
import { Component, Element, Host, Watch, Prop, h } from '@stencil/core';

export type ContentValues =
| 'center'
Expand Down Expand Up @@ -53,7 +53,35 @@ export class GcdsGrid {
/**
* Set tag for grid container
*/
@Prop() tag?: string = 'div';
@Prop({ mutable: true }) tag?:
| 'article'
| 'aside'
| 'div'
| 'dl'
| 'main'
| 'nav'
| 'ol'
| 'section'
| 'ul' = 'div';

@Watch('tag')
validateTag(newValue: string) {
const values = [
'article',
'aside',
'div',
'dl',
'main',
'nav',
'ol',
'section',
'ul',
];

if (!values.includes(newValue)) {
this.tag = 'div';
}
}

/**
* If total grid size is less than the size of its grid container,
Expand Down Expand Up @@ -87,6 +115,11 @@ export class GcdsGrid {
*/
@Prop() placeItems?: 'center' | 'end' | 'start' | 'stretch';

componentWillLoad() {
// Validate attributes and set defaults
this.validateTag(this.tag);
}

render() {
const {
alignContent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,18 @@ export default {
},
},
tag: {
control: 'text',
control: { type: 'select' },
options: [
'article',
'aside',
'div',
'dl',
'main',
'nav',
'ol',
'section',
'ul',
],
table: {
type: { summary: 'string' },
defaultValue: { summary: 'div' },
Expand Down Expand Up @@ -323,6 +334,20 @@ Individual.args = {
default: 'This is some example content to display the grid component.',
};

// ------ Grid tag ------

export const Tag = Template.bind({});
Tag.args = {
columns: 'repeat(auto-fit, minmax(100px, 250px))',
columnsDesktop: '',
columnsTablet: '',
container: 'full',
tag: 'article',
default: `<p>This is some example content to display the grid component.</p>
<p>This is some example content to display the grid component.</p>
<p>This is some example content to display the grid component.</p>`,
};

// ------ Grid events & props ------

export const Props = Template.bind({});
Expand Down
6 changes: 6 additions & 0 deletions packages/web/src/components/gcds-grid/stories/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ Use the `columns-desktop` property to define your layout for larger screens **(6

<Canvas of={Grid.ColumnsDesktop} story={{ inline: true }} />

### Tag

Always use the tag in a standard way to maintain accessibility.

<Canvas of={Grid.Tag} story={{ inline: true }} />

## Resources

{/* prettier-ignore */}
Expand Down
18 changes: 18 additions & 0 deletions packages/web/src/components/gcds-grid/test/gcds-grid.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,22 @@ describe('gcds-grid', () => {
</gcds-grid>
`);
});

it('renders - div when passed an invalid tag value', async () => {
const { root } = await newSpecPage({
components: [GcdsGrid],
html: `
<gcds-grid columns="1fr" tag="p" />
`,
});
expect(root).toEqualHtml(`
<gcds-grid columns="1fr" tag="p">
<mock:shadow-root>
<div class="display-grid gcds-grid" style="--gcds-grid-columns: 1fr;">
<slot></slot>
</div>
</mock:shadow-root>
</gcds-grid>
`);
});
});

0 comments on commit a686d09

Please sign in to comment.