Skip to content
Open
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Below is a list of the components available in this library. Each component has
- [BBBAccordion](./src/components/Accordion/README.md)
- [BBBAvatar](./src/components/Avatar/README.md)
- [BBButton](./src/components/Button/README.md)
- [BBBButtonGroup](./src/components/ButtonGroup/README.md)
- [BBBCheckbox](./src/components/Checkbox/README.md)
- [BBBDivider](./src/components/Divider/README.md)
- [BBBHint](./src/components/Hint/README.md)
Expand All @@ -17,6 +18,7 @@ Below is a list of the components available in this library. Each component has
- [BBBNavigation](./src/components/Navigation/README.md)
- [BBBScrollArea](./src/components/ScrollArea/README.md)
- [BBBSearch](./src/components/Search/README.md)
- [BBBSegmentedControl](./src/components/SegmentedControl/README.md)
- [BBBSelect](./src/components/Select/README.md)
- [BBBSlider](./src/components/Slider/README.md)
- [BBBSpinner](./src/components/Spinner//README.md)
Expand Down
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
"require": "./dist/components/Button.js",
"default": "./dist/components/Button.js"
},
"./ButtonGroup": {
"types": "./dist/types/components/ButtonGroup/index.d.ts",
"node": "./dist/components/ButtonGroup.js",
"import": "./dist/esm/components/ButtonGroup/index.js",
"require": "./dist/components/ButtonGroup.js",
"default": "./dist/components/ButtonGroup.js"
},
"./Checkbox": {
"types": "./dist/types/components/Checkbox/index.d.ts",
"node": "./dist/components/Checkbox.js",
Expand Down Expand Up @@ -92,6 +99,13 @@
"require": "./dist/components/Search.js",
"default": "./dist/components/Search.js"
},
"./SegmentedControl": {
"types": "./dist/types/components/SegmentedControl/index.d.ts",
"node": "./dist/components/SegmentedControl.js",
"import": "./dist/esm/components/SegmentedControl/index.js",
"require": "./dist/components/SegmentedControl.js",
"default": "./dist/components/SegmentedControl.js"
},
"./Select": {
"types": "./dist/types/components/Select/index.d.ts",
"node": "./dist/components/Select.js",
Expand Down
20 changes: 20 additions & 0 deletions src/components/Button/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,25 @@ import { BBButton } from 'bbb-ui-components-react';
<BBButton label="Share" fullWidth onClick={() => (console.log('clicked'))}/>
```

### Toggle Button

Set `pressed` to make the button a toggle. It renders `aria-pressed`, and while `true` the button keeps a selected fill through hover and press. Leave it unset for a plain button. For a set of options where exactly one is selected, use [`BBBSegmentedControl`](../SegmentedControl/README.md).

```jsx
import { BBButton } from 'bbb-ui-components-react';
import { MdMic, MdMicOff } from 'react-icons/md';

const [muted, setMuted] = useState(false);

<BBButton
layout="circle"
ariaLabel="Mute"
pressed={muted}
icon={muted ? <MdMicOff /> : <MdMic />}
onClick={() => setMuted(!muted)}
/>
```

### Long Labels

`labelOverflow` sets what happens when the label doesn't fit the button's available width, for example a `fullWidth` button in a narrow container or a row of equal-width buttons. It has no visible effect while the label fits.
Expand Down Expand Up @@ -124,6 +143,7 @@ const FooterButton = styled(BBButton)`
| `layout` | `keyof typeof LAYOUTS` | `'default'` | The layout of the button. |
| `type` | `'button' \| 'submit' \| 'reset'` | `'button'` | The native `type` attribute of the `<button>`. Defaults to `'button'` so it never submits an enclosing form unless `'submit'` is passed explicitly. |
| `disabled` | `boolean` | `false` | If `true`, the button will be disabled. |
| `pressed` | `boolean` | | Makes the button a toggle: `true` or `false` renders `aria-pressed`, and `true` applies a selected fill, per `color`, that holds through hover and active and fades, rather than turning grey, when `disabled`. Leave it unset for a plain button. |
| `fullWidth` | `boolean` | `false` | If `true`, the button fills the width of its container, keeping its content centered. Used for 'default' layout. |
| `labelOverflow` | `'nowrap' \| 'wrap' \| 'ellipsis'` | `'nowrap'` | What happens when the label doesn't fit the button's available width: `nowrap` keeps it on one line, widening the button or overflowing it, `wrap` breaks it onto more lines, `ellipsis` truncates it and shows the full label as a tooltip unless `tooltipLabel` is set. Used for 'default' layout. |
| `className` | `string` | | Class name forwarded to the root element (the `<button>`, or the wrapper `<div>` in the 'stacked' layout), so `styled(BBButton)` works. Escape hatch while BBButton is being adopted; prefer native props and open an issue for recurring needs. |
Expand Down
58 changes: 56 additions & 2 deletions src/components/Button/component.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react';
import React, { useState } from 'react';
import styled from 'styled-components';
import type { Meta, StoryObj } from '@storybook/react';
import { fn } from '@storybook/test';
import BBButton from './component';
import {
MdFavorite, MdMoreVert, MdSettings, MdCheckCircle, MdContentCopy,
MdFavorite, MdMoreVert, MdSettings, MdCheckCircle, MdContentCopy, MdMic, MdMicOff,
} from 'react-icons/md';
import {
VARIANT_VALUES,
Expand Down Expand Up @@ -80,6 +80,11 @@ const meta = {
description: 'Disables interaction and applies disabled visuals. Works across all layouts.',
table: { defaultValue: { summary: 'false' } },
},
pressed: {
control: 'boolean',
description: 'Makes the button a toggle: `true` or `false` renders `aria-pressed`, and `true` applies a selected fill, per `color`, that holds through hover and active and fades, rather than turning grey, when `disabled`. Leave it unset for a plain button.',
table: { defaultValue: { summary: 'undefined' } },
},
fullWidth: {
control: 'boolean',
description: `Makes the button fill the width of its container, keeping its content centered. Only used in the 'default' layout.`,
Expand Down Expand Up @@ -403,6 +408,55 @@ export const Disabled: Story = {
},
};

/** Compares each `color` unpressed and pressed, showing that the pressed fill holds on hover, plus a disabled pair where the pressed fill fades instead of turning grey. */
export const Pressed: Story = {
args: {
variant: 'secondary',
},
argTypes: {
pressed: { control: false },
},
render: (args) => (
<div style={{ display: 'flex', flexDirection: 'column', gap: '1rem' }}>
{COLOR_VALUES.map((color) => (
<div key={color} style={{ display: 'flex', gap: '1rem', alignItems: 'center' }}>
<BBButton {...args} color={color} pressed={false} label={`${color} unpressed`} />
<BBButton {...args} color={color} pressed label={`${color} pressed`} />
</div>
))}
<div style={{ display: 'flex', gap: '1rem', alignItems: 'center' }}>
<BBButton {...args} pressed={false} disabled label="disabled unpressed" />
<BBButton {...args} pressed disabled label="disabled pressed" />
</div>
</div>
),
};

function MuteToggle(args: React.ComponentProps<typeof BBButton>) {
const [muted, setMuted] = useState(false);

return (
<BBButton
{...args}
layout="circle"
pressed={muted}
ariaLabel="Mute"
tooltipLabel={muted ? 'Unmute' : 'Mute'}
icon={muted ? <MdMicOff size="1.25rem" /> : <MdMic size="1.25rem" />}
onClick={() => setMuted((current) => !current)}
/>
);
}

/** A standalone toggle: clicking flips `pressed`, and screen readers announce the button as pressed or not pressed. */
export const PressedToggle: Story = {
name: 'Pressed Toggle',
args: {
variant: 'secondary',
},
render: (args) => <MuteToggle {...args} />,
};

/** Shows a `fullWidth` button filling its container, with the label centered. */
export const FullWidth: Story = {
args: {
Expand Down
6 changes: 6 additions & 0 deletions src/components/Button/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function Button(props: ButtonProps): JSX.Element {
layout = DEFAULT_LAYOUT,
type = DEFAULT_TYPE,
disabled = false,
pressed,
children,
showFeedback = false,
feedbackContent = <MdCheckCircle fontSize="small" />,
Expand Down Expand Up @@ -94,6 +95,7 @@ function Button(props: ButtonProps): JSX.Element {
'aria-label'?: string;
'aria-labelledby'?: string;
'aria-describedby'?: string;
'aria-pressed'?: boolean;
} = {};
const generatedId = useId();
const defaultLabelId = `${id || generatedId}-label`;
Expand All @@ -111,6 +113,7 @@ function Button(props: ButtonProps): JSX.Element {
accessibilityProps['aria-labelledby'] = defaultLabelId;
}
if (ariaDescribedBy) accessibilityProps['aria-describedby'] = ariaDescribedBy;
if (pressed !== undefined) accessibilityProps['aria-pressed'] = pressed;

const buttonElement = (() => {
if (props.layout === LAYOUTS.CIRCLE || props.layout === LAYOUTS.SQUARED) {
Expand All @@ -130,6 +133,7 @@ function Button(props: ButtonProps): JSX.Element {
$variant={variant}
$size={size}
$layout={layout}
$pressed={pressed}
disabled={disabled}
>
{isFeedbackVisible ? (
Expand Down Expand Up @@ -166,6 +170,7 @@ function Button(props: ButtonProps): JSX.Element {
$variant={variant}
$size={size}
$layout={layout}
$pressed={pressed}
disabled={disabled}
>
{!hideHelperIcon && (
Expand Down Expand Up @@ -220,6 +225,7 @@ function Button(props: ButtonProps): JSX.Element {
$layout={layout}
$fullWidth={fullWidth}
$labelOverflow={labelOverflow}
$pressed={pressed}
disabled={disabled}
>
{isFeedbackVisible ? (
Expand Down
25 changes: 24 additions & 1 deletion src/components/Button/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import {
import {
spacingSmall, spacingSmallMedium, spacingMedium, spacingXLarge, spacingXXLarge,
} from '../../stylesheets/sizing';
import { CSSColorPropertiesType, CSSSizePropertiesType, EssentialColorProperties } from './type';
import {
CSSColorPropertiesType, CSSPressedColorPropertiesType, CSSSizePropertiesType, EssentialColorProperties,
} from './type';

const VARIANTS = {
PRIMARY: 'primary',
Expand Down Expand Up @@ -208,6 +210,26 @@ const CSS_COLOR_PROPERTIES_DISABLED: EssentialColorProperties = {
outline: colorNeutral3,
}

// Shared by every variant: a pressed toggle must read as selected next to any
// variant, so the fill comes from the color alone.
const CSS_COLOR_PROPERTIES_PRESSED: CSSPressedColorPropertiesType = {
default: {
color: colorWhite,
background: colorHoverDark,
border: colorHoverDark,
},
danger: {
color: colorWhite,
background: colorErrorDark,
border: colorErrorDark,
},
neutral: {
color: colorWhite,
background: colorHoverDark,
border: colorHoverDark,
},
};

const CSS_SIZE_PROPERTIES: CSSSizePropertiesType = {
sm: {
padding: `${spacingSmall} ${spacingSmallMedium}`,
Expand Down Expand Up @@ -243,5 +265,6 @@ export {
DEFAULT_TYPE,
CSS_COLOR_PROPERTIES,
CSS_COLOR_PROPERTIES_DISABLED,
CSS_COLOR_PROPERTIES_PRESSED,
CSS_SIZE_PROPERTIES,
};
31 changes: 30 additions & 1 deletion src/components/Button/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import styled, { css } from 'styled-components';
import {
CSS_COLOR_PROPERTIES,
CSS_COLOR_PROPERTIES_DISABLED,
CSS_COLOR_PROPERTIES_PRESSED,
CSS_SIZE_PROPERTIES,
DEFAULT_COLOR,
DEFAULT_SIZE,
Expand Down Expand Up @@ -80,8 +81,36 @@ const commonButtonStyles = css<StyledButtonProps>`
text-decoration: underline;
`}

${({ disabled }) => {
${({ $pressed, $color }) => {
if (!$pressed) return null;
const { color, background, border } = CSS_COLOR_PROPERTIES_PRESSED[$color || DEFAULT_COLOR];

return css`
& span {
color: ${color};
}
color: ${color};
background-color: ${background};
border: 1px solid ${border};

&&:hover:not(:has(> [data-is-aux-icon]:hover)),
&&:active {
color: ${color};
background-color: ${background};
border: 1px solid ${border};
}
`;
}}

${({ disabled, $pressed }) => {
if (!disabled) return null;
if ($pressed) {
return css`
pointer-events: none;
cursor: not-allowed;
opacity: 0.5;
`;
}
const { color, background, border, outline } = CSS_COLOR_PROPERTIES_DISABLED;

return css`
Expand Down
14 changes: 14 additions & 0 deletions src/components/Button/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ export type CSSColorPropertiesType = {
};
};

export type PressedColorProperties = {
color: string;
background: string;
border: string;
};

export type CSSPressedColorPropertiesType = {
[key: string]: PressedColorProperties;
};

export type SizeProperties = {
padding: string;
iconBoxSize: string;
Expand Down Expand Up @@ -64,6 +74,7 @@ export interface StyledButtonProps {
$layout?: LayoutType;
$fullWidth?: boolean;
$labelOverflow?: LabelOverflowType;
$pressed?: boolean;
disabled: boolean;
}

Expand Down Expand Up @@ -117,6 +128,9 @@ type BaseButtonProps = {
/** Disables interaction and applies disabled styling. @default false */
disabled?: boolean;

/** Makes the button a toggle: `true` or `false` renders `aria-pressed`, and `true` applies a selected fill, per `color`, that holds through hover and active and fades, rather than turning grey, when `disabled`. Leave it unset for a plain button. */
pressed?: boolean;

/** Additional content rendered inside the button, after the label (only used in the `default` layout). */
children?: React.ReactNode;

Expand Down
54 changes: 54 additions & 0 deletions src/components/ButtonGroup/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# BBBButtonGroup

The `BBBButtonGroup` component lays out a set of buttons in one row of equal-width buttons, and stacks them at full width when the group gets too narrow. With `footer`, it renders a divider above the buttons, which is the arrangement of a side panel's footer.

It only handles the spacing and arrangement inside the group: where the group sits in the panel, such as being pushed to the bottom, is up to the panel.

![Demo](assets/example.png)

## Usage Examples

### Side panel footer

`footer` adds the divider and stacks the buttons once the group is narrower than `stackBelow`.

```jsx
import { BBBButtonGroup, BBButton } from 'bbb-ui-components-react';

<BBBButtonGroup footer>
<BBButton variant="secondary" label="Send message" onClick={handleSendMessage} />
<BBButton color="danger" label="Finish" onClick={handleFinish} />
</BBBButtonGroup>
```

### Stacking when a label doesn't fit

```jsx
import { BBBButtonGroup, BBButton } from 'bbb-ui-components-react';

<BBBButtonGroup stack="overflow">
<BBButton variant="secondary" label="Cancel" onClick={handleCancel} />
<BBButton label="Finish all breakout rooms" onClick={handleFinish} />
</BBBButtonGroup>
```

## Stacking

`stack` decides when the buttons leave their row. Stacking is all or nothing: the group never splits the buttons across rows.

- `never`: the buttons always share one row. A button whose label doesn't fit grows wider than its share.
- `always`: the buttons are always stacked at full width.
- `width`: the buttons stack once the group is narrower than `stackBelow`. This is pure CSS.
- `overflow`: the buttons stack once the widest button no longer fits its share of the row. The group measures its buttons with a `ResizeObserver`, so it follows translated labels without a hand-tuned width.

## Props

| Property | Type | Default | Description |
| ------------ | ----------------------------------------------- | ----------------------------------------- | ----------- |
| `children` | `React.ReactNode` | | Buttons to lay out, usually `BBButton`s; each one becomes a slot of the group. |
| `stack` | `'never' \| 'always' \| 'width' \| 'overflow'` | `'never'`, or `'width'` when `footer` is set | When the buttons leave their single row of equal-width buttons for a stack of full-width ones: `never`, `always`, `width` once the group is narrower than `stackBelow`, or `overflow` once any button no longer fits its share of the row. |
| `stackBelow` | `string` | `'20.75rem'` | Group width, as a CSS length, below which `stack="width"` stacks the buttons. |
| `footer` | `boolean` | `false` | Renders a divider above the buttons, for the footer of a side panel. |
| `id` | `string` | | HTML `id` for the group element. |
| `dataTest` | `string` | | Value for the group's `data-test` attribute. |
| `className` | `string` | | Class name forwarded to the group element, so `styled(BBBButtonGroup)` works. |
Binary file added src/components/ButtonGroup/assets/example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading