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

[core] feat(Card): new "compact" prop #6432

Merged
merged 5 commits into from
Oct 6, 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
10 changes: 7 additions & 3 deletions packages/core/src/components/card-list/card-list.scss
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@
}
}

&.#{$ns}-compact > .#{$ns}-card {
min-height: $card-list-item-min-height-compact;
padding: $card-list-item-padding-compact;
&.#{$ns}-compact {
padding: 0;
adidahiya marked this conversation as resolved.
Show resolved Hide resolved

> .#{$ns}-card {
min-height: $card-list-item-min-height-compact;
padding: $card-list-item-padding-compact;
}
}

.#{$ns}-dark & {
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/components/card-list/cardList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export interface CardListProps extends Props, HTMLDivProps, React.RefAttributes<
/**
* Whether this component should use compact styles with reduced visual padding.
*
* Note that this prop affects styling for all Cards within this CardList and you do not need to set the
* `compact` prop individually on those child Cards.
*
* @default false
*/
compact?: boolean;
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/components/card/_card.scss
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ Styleguide card
}
}

.#{$ns}-card.#{$ns}-compact {
padding: $card-padding-compact;
}

.#{$ns}-card.#{$ns}-interactive {
&:hover {
box-shadow: $pt-elevation-shadow-3;
Expand Down
11 changes: 9 additions & 2 deletions packages/core/src/components/card/card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ export interface CardProps extends Props, HTMLDivProps, React.RefAttributes<HTML
*/
interactive?: boolean;

/**
* Whether this component should use compact styles with reduced visual padding.
*
* @default false
*/
compact?: boolean;

/**
* Callback invoked when the card is clicked.
* Recommended when `interactive` is `true`.
Expand All @@ -54,10 +61,10 @@ export interface CardProps extends Props, HTMLDivProps, React.RefAttributes<HTML
* @see https://blueprintjs.com/docs/#core/components/card
*/
export const Card: React.FC<CardProps> = React.forwardRef((props, ref) => {
const { className, elevation, interactive, ...htmlProps } = props;
const { className, elevation, interactive, compact, ...htmlProps } = props;
const classes = classNames(
Classes.CARD,
{ [Classes.INTERACTIVE]: interactive },
{ [Classes.INTERACTIVE]: interactive, [Classes.COMPACT]: compact },
Classes.elevationClass(elevation!),
className,
);
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/components/control-card/_control-card.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

.#{$ns}-switch-control-card {
// need a lot of specificity here to override control styles
> .#{$ns}-switch.#{$ns}-control.#{$ns}-align-right {
.#{$ns}-switch.#{$ns}-control.#{$ns}-align-right {
align-items: center;
display: flex;
flex-direction: row-reverse;
Expand All @@ -39,4 +39,8 @@
margin: 0;
}
}

&.#{$ns}-compact .#{$ns}-switch.#{$ns}-control.#{$ns}-align-right {
padding: $card-padding-compact;
}
}
1 change: 0 additions & 1 deletion packages/core/src/components/control-card/switchCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@ export type SwitchCardProps = Omit<ControlCardProps, "controlKind">;
export const SwitchCard: React.FC<SwitchCardProps> = React.forwardRef((props, ref) => {
return <ControlCard controlKind="switch" ref={ref} {...props} />;
});
SwitchCard.defaultProps = {};
SwitchCard.displayName = `${DISPLAYNAME_PREFIX}.SwitchCard`;
7 changes: 6 additions & 1 deletion packages/core/src/components/section/_section.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ $section-card-padding-compact: $pt-grid-size * 1.5 !default;

.#{$ns}-section {
overflow: hidden;
padding: 0;
width: 100%;

&,
&.#{$ns}-compact {
// override Card compact styles here
padding: 0;
}

&-header {
align-items: center;
border-bottom: 1px solid $pt-divider-black;
Expand Down
41 changes: 23 additions & 18 deletions packages/docs-app/src/examples/core-examples/cardExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,21 @@ import { Button, Card, Classes, Elevation, H5, Label, Slider, Switch } from "@bl
import { Example, ExampleProps } from "@blueprintjs/docs-theme";

export interface CardExampleState {
compact: boolean;
elevation: Elevation;
interactive: boolean;
}

export class CardExample extends React.PureComponent<ExampleProps, CardExampleState> {
public state: CardExampleState = {
compact: false,
elevation: 0,
interactive: false,
};

public render() {
const options = (
<>
<H5>Props</H5>
<Switch checked={this.state.interactive} label="Interactive" onChange={this.handleInteractiveChange} />
<Label>
Elevation
<Slider
max={4}
showTrackFill={false}
value={this.state.elevation}
onChange={this.handleElevationChange}
handleHtmlProps={{ "aria-label": "card elevation" }}
/>
</Label>
</>
);

return (
<Example options={options} {...this.props}>
<Example options={this.renderOptions()} {...this.props}>
<Card {...this.state}>
<H5>Analytical applications</H5>
<p>
Expand All @@ -62,7 +47,27 @@ export class CardExample extends React.PureComponent<ExampleProps, CardExampleSt
);
}

private renderOptions = () => (
<>
<H5>Props</H5>
<Switch checked={this.state.interactive} label="Interactive" onChange={this.handleInteractiveChange} />
<Switch checked={this.state.compact} label="Compact" onChange={this.handleCompactChange} />
<Label>
Elevation
<Slider
max={4}
showTrackFill={false}
value={this.state.elevation}
onChange={this.handleElevationChange}
handleHtmlProps={{ "aria-label": "card elevation" }}
/>
</Label>
</>
);

private handleElevationChange = (elevation: Elevation) => this.setState({ elevation });

private handleInteractiveChange = () => this.setState({ interactive: !this.state.interactive });

private handleCompactChange = () => this.setState({ compact: !this.state.compact });
}
25 changes: 10 additions & 15 deletions packages/docs-app/src/examples/core-examples/switchCardExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,41 @@

import * as React from "react";

import { H5, Switch, SwitchCard } from "@blueprintjs/core";
import { H5, Switch, SwitchCard, SwitchCardProps } from "@blueprintjs/core";
import { Example, ExampleProps, handleBooleanChange } from "@blueprintjs/docs-theme";

interface SwitchCardExampleState {
// TODO: add compact option
// compact: boolean;
disabled: boolean;
}
type SwitchCardExampleState = Pick<SwitchCardProps, "compact" | "disabled">;

export class SwitchCardExample extends React.PureComponent<ExampleProps, SwitchCardExampleState> {
public state: SwitchCardExampleState = {
// TODO: add compact option
// compact: false,
compact: false,
disabled: false,
};

public render() {
const { disabled } = this.state;
const sharedProps = { disabled };

return (
<Example options={this.renderOptions()} {...this.props}>
<div className="docs-control-card-grid">
<SwitchCard {...sharedProps}>Wifi</SwitchCard>
<SwitchCard {...sharedProps}>Bluetooth</SwitchCard>
<SwitchCard {...sharedProps}>NFC</SwitchCard>
<SwitchCard {...this.state}>Wifi</SwitchCard>
<SwitchCard {...this.state}>Bluetooth</SwitchCard>
<SwitchCard {...this.state}>NFC</SwitchCard>
</div>
</Example>
);
}

private renderOptions() {
const { disabled } = this.state;
const { compact, disabled } = this.state;
return (
<>
<H5>Props</H5>
<Switch checked={compact} label="Compact" onChange={this.toggleCompact} />
<Switch checked={disabled} label="Disabled" onChange={this.toggleDisabled} />
</>
);
}

private toggleCompact = handleBooleanChange(compact => this.setState({ compact }));

private toggleDisabled = handleBooleanChange(disabled => this.setState({ disabled }));
}