Skip to content

Commit bfca976

Browse files
author
Lucas Araujo
committed
feat: add toggle button group small variant
1 parent bc3bb57 commit bfca976

File tree

5 files changed

+91
-30
lines changed

5 files changed

+91
-30
lines changed

src/design-system/toggle-button-group/toggle-button-group-item.component.tsx

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import cn from 'classnames';
66
import { Text } from '../text';
77

88
import * as cx from './toggle-button-group-item.css';
9+
import { useToggleButtonGroupContext } from './toggle-button-group-root.component';
910

1011
import type { ToggleGroupItemProps } from '@radix-ui/react-toggle-group';
1112

@@ -20,20 +21,27 @@ export const Item = forwardRef<HTMLButtonElement, ToggleButtonGroupItemProps>(
2021
(
2122
{ icon: IconComponent, children, ...props },
2223
forwardReference,
23-
): JSX.Element => (
24-
<ToggleGroup.Item
25-
className={cn(cx.root, cx.rootHandle)}
26-
ref={forwardReference}
27-
{...props}
28-
>
29-
{IconComponent && <IconComponent className={cx.icon} />}
30-
{Boolean(children) && (
31-
<Text.Button weight="$semibold" className={cx.label}>
32-
{children}
33-
</Text.Button>
34-
)}
35-
</ToggleGroup.Item>
36-
),
24+
): JSX.Element => {
25+
const variant = useToggleButtonGroupContext();
26+
27+
return (
28+
<ToggleGroup.Item
29+
className={cn(cx.root, cx.rootHandle, {
30+
[cx.smallVariant]: variant === 'small',
31+
[cx.commonVariant]: variant !== 'small',
32+
})}
33+
ref={forwardReference}
34+
{...props}
35+
>
36+
{IconComponent && <IconComponent className={cx.icon} />}
37+
{Boolean(children) && (
38+
<Text.Button weight="$semibold" className={cx.label}>
39+
{children}
40+
</Text.Button>
41+
)}
42+
</ToggleGroup.Item>
43+
);
44+
},
3745
);
3846

3947
Item.displayName = 'Item';

src/design-system/toggle-button-group/toggle-button-group-item.css.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export const root = style([
99
padding: '$8',
1010
color: '$toggle_button_group_item_label_color',
1111
backgroundColor: '$toggle_button_group_item_bgColor',
12-
borderRadius: '$small',
1312
gap: '$6',
1413
}),
1514
{
@@ -71,3 +70,11 @@ export const icon = sx({
7170
width: '$24',
7271
height: '$24',
7372
});
73+
74+
export const commonVariant = sx({
75+
borderRadius: '$small',
76+
});
77+
78+
export const smallVariant = sx({
79+
borderRadius: '$tiny',
80+
});
Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useCallback } from 'react';
1+
import React, { useCallback, createContext } from 'react';
22

33
import * as ToggleGroup from '@radix-ui/react-toggle-group';
44
import classNames from 'classnames';
@@ -7,6 +7,10 @@ import * as cx from './toggle-button-group-root.css';
77

88
import type { ToggleGroupSingleProps } from '@radix-ui/react-toggle-group';
99

10+
type Variant = 'compact' | 'wide' | 'small';
11+
12+
const ToggleButtonGroupContext = createContext<Variant>('wide');
13+
1014
type ToggleGroupSingleOptionalProps = Pick<
1115
ToggleGroupSingleProps,
1216
'loop' | 'rovingFocus'
@@ -18,10 +22,16 @@ type ToggleGroupSingleRequiredProps = Required<
1822

1923
export type ToggleButtonGroupRootProps = ToggleGroupSingleOptionalProps &
2024
ToggleGroupSingleRequiredProps & {
21-
variant?: 'compact' | 'wide';
25+
variant?: Variant;
2226
disabled?: boolean;
2327
};
2428

29+
export const useToggleButtonGroupContext = (): Variant => {
30+
const context = React.useContext(ToggleButtonGroupContext);
31+
32+
return context;
33+
};
34+
2535
export const Root = ({
2636
children,
2737
onValueChange,
@@ -37,17 +47,21 @@ export const Root = ({
3747
);
3848

3949
return (
40-
<ToggleGroup.Root
41-
className={classNames(cx.root, {
42-
[cx.rootCompact]: variant === 'compact',
43-
[cx.rootDisabled]: disabled,
44-
})}
45-
type="single"
46-
onValueChange={handleValueChange}
47-
disabled={disabled}
48-
{...props}
49-
>
50-
{children}
51-
</ToggleGroup.Root>
50+
<ToggleButtonGroupContext.Provider value={variant}>
51+
<ToggleGroup.Root
52+
className={classNames(cx.root, {
53+
[cx.rootCompact]: variant === 'compact',
54+
[cx.rootSmall]: variant === 'small',
55+
[cx.rootDisabled]: disabled,
56+
[cx.defaultRadius]: variant !== 'small',
57+
})}
58+
type="single"
59+
onValueChange={handleValueChange}
60+
disabled={disabled}
61+
{...props}
62+
>
63+
{children}
64+
</ToggleGroup.Root>
65+
</ToggleButtonGroupContext.Provider>
5266
);
5367
};

src/design-system/toggle-button-group/toggle-button-group-root.css.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { style, sx } from '../../design-tokens';
33
export const root = style([
44
sx({
55
backgroundColor: '$toggle_button_group_bgColor',
6-
borderRadius: '$medium',
76
padding: '$8',
87
gap: '$8',
98
}),
@@ -19,3 +18,18 @@ export const rootCompact = style({
1918
});
2019

2120
export const rootDisabled = style({});
21+
22+
export const defaultRadius = sx({
23+
borderRadius: '$medium',
24+
});
25+
26+
export const rootSmall = style([
27+
sx({
28+
height: '$40',
29+
borderRadius: '$extraSmall',
30+
boxSizing: 'border-box',
31+
}),
32+
{
33+
padding: '5px',
34+
},
35+
]);

src/design-system/toggle-button-group/toggle-button-group-root.stories.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,24 @@ export const Overview = (): JSX.Element => {
238238
</Variants.Cell>
239239
</Variants.Row>
240240
</Variants.Table>
241+
<Variants.Table headers={['(Small) Label']}>
242+
<Variants.Row>
243+
<Variants.Cell>
244+
<ToggleButtonGroup.Root
245+
variant="small"
246+
value={activeVariants}
247+
onValueChange={handleVariantsValueChange}
248+
>
249+
<ToggleButtonGroup.Item value={ToggleGroupItemType.grid}>
250+
Label
251+
</ToggleButtonGroup.Item>
252+
<ToggleButtonGroup.Item value={ToggleGroupItemType.list}>
253+
Label
254+
</ToggleButtonGroup.Item>
255+
</ToggleButtonGroup.Root>
256+
</Variants.Cell>
257+
</Variants.Row>
258+
</Variants.Table>
241259
</Section>
242260
<Divider my="$64" />
243261
<Section title="Main components">

0 commit comments

Comments
 (0)