1
- import React , { useCallback } from 'react' ;
1
+ import React , { useCallback , createContext } from 'react' ;
2
2
3
3
import * as ToggleGroup from '@radix-ui/react-toggle-group' ;
4
4
import classNames from 'classnames' ;
@@ -7,6 +7,10 @@ import * as cx from './toggle-button-group-root.css';
7
7
8
8
import type { ToggleGroupSingleProps } from '@radix-ui/react-toggle-group' ;
9
9
10
+ type Variant = 'compact' | 'wide' | 'small' ;
11
+
12
+ const ToggleButtonGroupContext = createContext < Variant > ( 'wide' ) ;
13
+
10
14
type ToggleGroupSingleOptionalProps = Pick <
11
15
ToggleGroupSingleProps ,
12
16
'loop' | 'rovingFocus'
@@ -18,10 +22,16 @@ type ToggleGroupSingleRequiredProps = Required<
18
22
19
23
export type ToggleButtonGroupRootProps = ToggleGroupSingleOptionalProps &
20
24
ToggleGroupSingleRequiredProps & {
21
- variant ?: 'compact' | 'wide' ;
25
+ variant ?: Variant ;
22
26
disabled ?: boolean ;
23
27
} ;
24
28
29
+ export const useToggleButtonGroupContext = ( ) : Variant => {
30
+ const context = React . useContext ( ToggleButtonGroupContext ) ;
31
+
32
+ return context ;
33
+ } ;
34
+
25
35
export const Root = ( {
26
36
children,
27
37
onValueChange,
@@ -37,17 +47,21 @@ export const Root = ({
37
47
) ;
38
48
39
49
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 >
52
66
) ;
53
67
} ;
0 commit comments