-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathButtonGroup.tsx
More file actions
73 lines (64 loc) · 1.9 KB
/
ButtonGroup.tsx
File metadata and controls
73 lines (64 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import type { Orientation } from '@react-types/shared';
import type { VariantProps } from 'class-variance-authority';
import type { Ref } from 'react';
import type { ContextValue, GroupProps } from 'react-aria-components';
import { cva } from 'class-variance-authority';
import { createContext } from 'react';
import {
ButtonContext,
composeRenderProps,
Group,
Provider,
ToggleButtonContext,
} from 'react-aria-components';
import styles from './styles/ButtonGroup.module.css';
import { useLPContextProps } from './utils';
const buttonGroupStyles = cva(styles.base, {
variants: {
spacing: {
basic: styles.basic,
compact: styles.compact,
large: styles.large,
},
orientation: {
horizontal: styles.horizontal,
vertical: styles.vertical,
},
},
defaultVariants: {
spacing: 'basic',
},
});
interface ButtonGroupProps extends GroupProps, VariantProps<typeof buttonGroupStyles> {
orientation?: Orientation | null;
ref?: Ref<HTMLDivElement>;
}
const ButtonGroupContext = createContext<ContextValue<ButtonGroupProps, HTMLDivElement>>(null);
const ButtonGroup = ({ ref, ...props }: ButtonGroupProps): React.JSX.Element => {
[props, ref] = useLPContextProps(props, ref, ButtonGroupContext);
const { spacing = 'basic', orientation = 'horizontal' } = props;
return (
<Group
{...props}
ref={ref}
className={composeRenderProps(props.className, (className, renderProps) =>
buttonGroupStyles({ ...renderProps, spacing, orientation, className }),
)}
data-orientation={orientation ?? undefined}
>
{composeRenderProps(props.children, (children, { isDisabled }) => (
<Provider
values={[
[ButtonContext, { isDisabled }],
[ToggleButtonContext, { isDisabled }],
]}
>
{children}
</Provider>
))}
</Group>
);
};
ButtonGroup.displayName = 'ButtonGroup';
export { ButtonGroup, ButtonGroupContext, buttonGroupStyles };
export type { ButtonGroupProps };