-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ButtonGroup component and refactor Inline Alert (#446)
* component scaffold * fleshing out component * add alignment prop and styles * add built-in buttonGroup slot to inline alert * clean up vite and storybook examples * add default layout behaviour * storybook docs and examples * storybook docs and examples * typo * aria-label support * docs clarification * fix aria-labelledby declaration * storybook docs cleanup * use small buttons in alert example
- Loading branch information
1 parent
144c083
commit 040e2b7
Showing
15 changed files
with
266 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
packages/react-components/src/components/ButtonGroup/ButtonGroup.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
.bcds-ButtonGroup { | ||
display: flex; | ||
gap: var(--layout-margin-small); | ||
flex-grow: 1; | ||
} | ||
|
||
.bcds-ButtonGroup.horizontal { | ||
flex-direction: row; | ||
} | ||
|
||
.bcds-ButtonGroup.vertical { | ||
flex-direction: column; | ||
} | ||
|
||
.bcds-ButtonGroup.start { | ||
justify-content: flex-start; | ||
} | ||
|
||
.bcds-ButtonGroup.center { | ||
justify-content: center; | ||
} | ||
|
||
.bcds-ButtonGroup.end { | ||
justify-content: flex-end; | ||
} |
29 changes: 29 additions & 0 deletions
29
packages/react-components/src/components/ButtonGroup/ButtonGroup.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import "./ButtonGroup.css"; | ||
|
||
export interface ButtonGroupProps extends React.PropsWithChildren { | ||
/* Sets layout of button group */ | ||
orientation?: "horizontal" | "vertical"; | ||
/* Sets alignment of button group */ | ||
alignment?: "start" | "center" | "end"; | ||
/* Semantic label for button group */ | ||
ariaLabel?: string | undefined; | ||
} | ||
|
||
export default function ButtonGroup({ | ||
orientation = "horizontal", | ||
alignment = "start", | ||
ariaLabel, | ||
children, | ||
...props | ||
}: ButtonGroupProps) { | ||
return ( | ||
<div | ||
className={`bcds-ButtonGroup ${orientation} ${alignment}`} | ||
role="group" | ||
aria-label={ariaLabel} | ||
{...props} | ||
> | ||
{children} | ||
</div> | ||
); | ||
} |
2 changes: 2 additions & 0 deletions
2
packages/react-components/src/components/ButtonGroup/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default } from "./ButtonGroup"; | ||
export type { ButtonGroupProps } from "./ButtonGroup"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
packages/react-components/src/pages/ButtonGroup/ButtonGroup.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Button, ButtonGroup } from "@/components"; | ||
|
||
export default function ButtonGroupPage() { | ||
return ( | ||
<> | ||
<h2>ButtonGroup</h2> | ||
<ButtonGroup> | ||
<Button variant="primary">Button 1</Button> | ||
<Button variant="secondary">Button 2</Button> | ||
<Button variant="tertiary">Button 3</Button> | ||
<Button variant="link">Button 4</Button> | ||
</ButtonGroup> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import ButtonGroupPage from "./ButtonGroup"; | ||
|
||
export default ButtonGroupPage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
{/* ButtonGroup.mdx */} | ||
|
||
import { | ||
Canvas, | ||
Controls, | ||
Meta, | ||
Primary, | ||
Source, | ||
Story, | ||
Subtitle, | ||
} from "@storybook/blocks"; | ||
|
||
import * as ButtonGroupStories from "./ButtonGroup.stories"; | ||
import * as InlineAlertStories from "./InlineAlert.stories"; | ||
|
||
<Meta of={ButtonGroupStories} /> | ||
|
||
# ButtonGroup | ||
|
||
<Subtitle>Layout component for buttons</Subtitle> | ||
|
||
<Source | ||
code={`import { ButtonGroup } from "@bcgov/design-system-react-components";`} | ||
language="typescript" | ||
/> | ||
|
||
## Usage and resources | ||
|
||
ButtonGroup is a utility component intended for use when multiple buttons need to be rendered alongside one another. It contains basic styling to ensure proper spacing between the buttons. | ||
|
||
ButtonGroup renders a `<div>` with the [ARIA "group" role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/group_role) and a user-defined `aria-label`. It expects an array of [buttons](/docs/components-button-button--docs), passed via the `children` prop. | ||
|
||
### Example | ||
|
||
ButtonGroup is used to populate buttons within the [Inline Alert component](/docs/components-inline-alert-inline-alert--docs): | ||
|
||
<Canvas of={InlineAlertStories.AlertWithButtons} /> | ||
|
||
## Controls | ||
|
||
<Canvas of={ButtonGroupStories.ButtonGroupTemplate} /> | ||
<Controls of={ButtonGroupStories.ButtonGroupTemplate} /> | ||
|
||
## Layout | ||
|
||
By default, ButtonGroup renders buttons in a horizontal row, starting from the left edge. | ||
|
||
### Orientation | ||
|
||
Use the `orientation` prop to toggle the orientation of the button group between `horizontal` and `vertical` layouts: | ||
|
||
<Canvas of={ButtonGroupStories.VerticalButtonGroup} /> | ||
|
||
### Alignment | ||
|
||
Use the `alignment` prop to set the alignment of the buttons within the group: | ||
|
||
- `start` (left or top, depending on orientation) | ||
- `center` | ||
- `end` (right or bottom, depending on orientation) | ||
|
||
<Canvas of={ButtonGroupStories.CenteredButtonGroup} /> |
70 changes: 70 additions & 0 deletions
70
packages/react-components/src/stories/ButtonGroup.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
|
||
import { Button, ButtonGroup } from "../components"; | ||
import { ButtonGroupProps } from "@/components/ButtonGroup"; | ||
|
||
const meta = { | ||
title: "Components/Button/ButtonGroup", | ||
component: ButtonGroup, | ||
parameters: { | ||
layout: "centered", | ||
}, | ||
argTypes: { | ||
orientation: { | ||
options: ["horizontal", "vertical"], | ||
control: { type: "radio" }, | ||
description: "Layout of button group as a whole", | ||
}, | ||
alignment: { | ||
options: ["start", "center", "end"], | ||
control: { type: "radio" }, | ||
description: "Alignment of buttons within group", | ||
}, | ||
ariaLabel: { | ||
control: { type: "text" }, | ||
description: "Semantic label for the button group", | ||
}, | ||
}, | ||
} satisfies Meta<typeof ButtonGroup>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const ButtonGroupTemplate: Story = { | ||
args: { | ||
alignment: "start", | ||
orientation: "horizontal", | ||
ariaLabel: "A group of buttons", | ||
children: [ | ||
<Button variant="primary">Button 1</Button>, | ||
<Button variant="secondary">Button 2</Button>, | ||
<Button variant="secondary">Button 3</Button>, | ||
], | ||
}, | ||
render: ({ ...args }: ButtonGroupProps) => <ButtonGroup {...args} />, | ||
}; | ||
|
||
export const VerticalButtonGroup: Story = { | ||
...ButtonGroupTemplate, | ||
args: { | ||
orientation: "vertical", | ||
children: [ | ||
<Button variant="primary">Button 1</Button>, | ||
<Button variant="secondary">Button 2</Button>, | ||
<Button variant="secondary">Button 3</Button>, | ||
], | ||
}, | ||
}; | ||
|
||
export const CenteredButtonGroup: Story = { | ||
...ButtonGroupTemplate, | ||
args: { | ||
orientation: "horizontal", | ||
alignment: "center", | ||
children: [ | ||
<Button variant="primary">Button 1</Button>, | ||
<Button variant="secondary">Button 2</Button>, | ||
<Button variant="secondary">Button 3</Button>, | ||
], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.