Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add secondary variant to CardTitle and CardHeader #1001

Merged
merged 1 commit into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/components/layout/CardHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type ComponentProps = {
* Make the header take the full width of the Card, with a full-width border
*/
fullWidth?: boolean;

variant?: 'primary' | 'secondary';
};

type HTMLAttributes = JSX.HTMLAttributes<HTMLElement>;
Expand All @@ -39,6 +41,7 @@ const CardHeader = function CardHeader({
fullWidth = false,
onClose,
title,
variant = 'primary',

...htmlAttributes
}: CardHeaderProps) {
Expand All @@ -48,12 +51,20 @@ const CardHeader = function CardHeader({
{...htmlAttributes}
className={classnames(
'flex items-center gap-x-2 border-b py-2',
{ 'mx-3': !fullWidth, 'px-3': fullWidth },
{
'bg-slate-0 border-slate-5': variant === 'secondary',
'mx-3': !fullWidth && variant === 'primary',
'px-3': fullWidth || variant === 'secondary',
},
classes
)}
ref={downcastRef(elementRef)}
>
{title && <CardTitle>{title}</CardTitle>}
{title && (
<CardTitle classes="grow" variant={variant}>
{title}
</CardTitle>
)}
{children}
{onClose && (
<IconButton
Expand Down
24 changes: 22 additions & 2 deletions src/components/layout/CardTitle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ import type { JSX } from 'preact';
import type { PresentationalProps } from '../../types';
import { downcastRef } from '../../util/typing';

type ComponentProps = {
/** Wrap the title with this HTML heading tag */
tagName?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realized that we lost something in the most recent round of Card and Panel implementations: we aren't using heading elements. For HTML semantics and accessibility it is best if we do. This prop allows a consumer to use a different heading level than default (h1) if so desired.


variant?: 'primary' | 'secondary';
};

export type CardTitleProps = PresentationalProps &
ComponentProps &
JSX.HTMLAttributes<HTMLElement>;

/**
Expand All @@ -15,16 +23,28 @@ const CardTitle = function CardTitle({
classes,
elementRef,

tagName = 'h1',
variant = 'primary',

...htmlAttributes
}: CardTitleProps) {
const WrapperElement = tagName;
return (
<div
data-component="CardTitle"
{...htmlAttributes}
className={classnames('grow text-lg text-brand font-semibold', classes)}
className={classnames(
{
'text-lg text-brand font-semibold': variant === 'primary',
'text-xl text-slate-7 font-normal': variant === 'secondary',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the text supposed to be bigger on secondary variant? Is that intended?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it actually is!

},
classes
)}
ref={downcastRef(elementRef)}
>
{children}
<WrapperElement data-testid="card-title-heading">
{children}
</WrapperElement>
</div>
);
};
Expand Down
2 changes: 1 addition & 1 deletion src/components/layout/test/CardHeader-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('CardHeader', () => {

it('renders a close button if `onClose` set', () => {
const onClose = sinon.stub();
const wrapper = createComponent(CardHeader, { onClose });
const wrapper = createComponent(CardHeader, { title: 'My title', onClose });

wrapper.find('button').simulate('click');
assert.calledOnce(onClose);
Expand Down
24 changes: 24 additions & 0 deletions src/components/layout/test/CardTitle-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
import { mount } from 'enzyme';

import { testPresentationalComponent } from '../../test/common-tests';
import CardTitle from '../CardTitle';

const createComponent = (Component, props = {}) => {
return mount(<Component {...props}>This is child content</Component>);
};

describe('CardTitle', () => {
testPresentationalComponent(CardTitle);

describe('heading element tagName', () => {
it('should wrap title in an `h1` by default', () => {
const wrapper = createComponent(CardTitle);
const headingEl = wrapper
.find('[data-testid="card-title-heading"]')
.getDOMNode();
assert.equal(headingEl.tagName, 'H1');
});

it('should allow designation of other heading tags', () => {
const wrapper = createComponent(CardTitle, { tagName: 'h3' });
const headingEl = wrapper
.find('[data-testid="card-title-heading"]')
.getDOMNode();
assert.equal(headingEl.tagName, 'H3');
});
});
});
89 changes: 89 additions & 0 deletions src/pattern-library/components/patterns/layout/CardPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,39 @@ export default function CardPage() {
</Card>
</Library.Demo>
</Library.Example>

<Library.Example title="variant">
<Library.Demo title="variant='secondary' (default)" withSource>
<Card>
<CardHeader
variant="primary"
title="Primary variant"
onClose={() => {}}
/>
<CardContent>
<p>
This {"Card's"} <code>CardHeader</code> is styled with the{' '}
<code>primary</code> (default) variant.
</p>
</CardContent>
</Card>
</Library.Demo>
<Library.Demo title="variant='secondary'" withSource>
<Card>
<CardHeader
variant="secondary"
onClose={() => {}}
title="Secondary variant"
/>
<CardContent>
<p>
This {"Card's"} <code>CardHeader</code> is styled with the{' '}
<code>secondary</code> variant.
</p>
</CardContent>
</Card>
</Library.Demo>
</Library.Example>
</Library.Pattern>
</Library.Section>

Expand Down Expand Up @@ -306,6 +339,62 @@ export default function CardPage() {
</Card>
</Library.Demo>
</Library.Pattern>
<Library.Pattern title="Props">
<Library.Example title="tagName">
<p>
{' '}
The <code>
tagName: {"'h1' | 'h2' | 'h3' | 'h4' | 'h5'"}
</code>{' '}
prop (default <code>{"'h1'"}</code>) determines which HTML heading
element will wrap the rendered content.
</p>
<Library.Demo title="tagName='h3'" withSource>
<Card>
<CardHeader>
<EditIcon />
<CardTitle tagName="h3">Card title</CardTitle>
</CardHeader>
<CardContent>
<p>
Setting a different heading level (HTML tag) in a{' '}
<code>CardTitle</code>.
</p>
</CardContent>
</Card>
</Library.Demo>
</Library.Example>

<Library.Example title="variant">
<Library.Demo title="variant='primary'" withSource>
<Card>
<CardHeader>
<CardTitle variant="primary">Card title</CardTitle>
</CardHeader>
<CardContent>
<p>
This <code>CardTitle</code> uses the default{' '}
<code>{"'primary'"}</code> variant.
</p>
</CardContent>
</Card>
</Library.Demo>

<Library.Demo title="variant='secondary'" withSource>
<Card>
<CardHeader>
<CardTitle variant="secondary">Card title</CardTitle>
</CardHeader>
<CardContent>
<p>
This <code>CardTitle</code> uses the
<code>{"'secondary'"}</code> variant.
</p>
</CardContent>
</Card>
</Library.Demo>
</Library.Example>
</Library.Pattern>
</Library.Section>

<Library.Section
Expand Down