1
- import styled from 'styled-components'
2
- import React from 'react'
3
- import sx from '../sx'
4
- import type { ComponentProps } from '../utils/types'
1
+ import React , { type PropsWithChildren } from 'react'
2
+ import { type SxProp } from '../sx'
5
3
import classes from './ButtonGroup.module.css'
6
- import { toggleStyledComponent } from '../internal/utils/toggleStyledComponent'
7
4
import { clsx } from 'clsx'
8
- import { useFeatureFlag } from '../FeatureFlags'
9
5
import { FocusKeys , useFocusZone } from '../hooks/useFocusZone'
10
6
import { useProvidedRefOrCreate } from '../hooks'
11
7
import type { ForwardRefComponent as PolymorphicForwardRefComponent } from '../utils/polymorphic'
8
+ import Box from '../Box'
9
+ import { defaultSxProp } from '../utils/defaultSxProp'
12
10
13
- const StyledButtonGroup = toggleStyledComponent (
14
- 'primer_react_css_modules_ga' ,
15
- 'div' ,
16
- styled . div `
17
- display: inline-flex;
18
- vertical-align: middle;
19
- isolation: isolate;
20
-
21
- & > *:not([data-loading-wrapper]) {
22
- /* stylelint-disable-next-line primer/spacing */
23
- margin-inline-end: -1px;
24
- position: relative;
25
-
26
- /* reset border-radius */
27
- button,
28
- a {
29
- border-radius: 0;
30
- }
31
-
32
- &:first-child {
33
- button,
34
- a {
35
- border-top-left-radius: var(--borderRadius-medium);
36
- border-bottom-left-radius: var(--borderRadius-medium);
37
- }
38
- }
39
-
40
- &:last-child {
41
- button,
42
- a {
43
- border-top-right-radius: var(--borderRadius-medium);
44
- border-bottom-right-radius: var(--borderRadius-medium);
45
- }
46
- }
47
-
48
- &:focus,
49
- &:active,
50
- &:hover {
51
- z-index: 1;
52
- }
53
- }
54
-
55
- /* this is a workaround until portal based tooltips are fully removed from dotcom */
56
- &:has(div:last-child:empty) {
57
- button,
58
- a {
59
- border-radius: var(--borderRadius-medium);
60
- }
61
- }
62
-
63
- /* if child is loading button */
64
- & > *[data-loading-wrapper] {
65
- /* stylelint-disable-next-line primer/spacing */
66
- margin-inline-end: -1px;
67
- position: relative;
68
- /* reset border-radius */
69
- button,
70
- a {
71
- border-radius: 0;
72
- }
73
-
74
- &:focus,
75
- &:active,
76
- &:hover {
77
- z-index: 1;
78
- }
79
- &:first-child {
80
- button,
81
- a {
82
- border-top-left-radius: var(--borderRadius-medium);
83
- border-bottom-left-radius: var(--borderRadius-medium);
84
- }
85
- }
86
-
87
- &:last-child {
88
- button,
89
- a {
90
- border-top-right-radius: var(--borderRadius-medium);
91
- border-bottom-right-radius: var(--borderRadius-medium);
92
- }
93
- }
94
- }
95
-
96
- ${ sx } ;
97
- ` ,
98
- )
99
-
100
- export type ButtonGroupProps = ComponentProps < typeof StyledButtonGroup >
11
+ export type ButtonGroupProps = {
12
+ /** The role of the group */
13
+ role ?: string
14
+ /** className passed in for styling */
15
+ className ?: string
16
+ } & PropsWithChildren &
17
+ SxProp
101
18
102
19
const ButtonGroup = React . forwardRef < HTMLElement , ButtonGroupProps > ( function ButtonGroup (
103
- { children, className, role, ...rest } ,
20
+ { children, className, role, sx , ...rest } ,
104
21
forwardRef ,
105
22
) {
106
- const enabled = useFeatureFlag ( 'primer_react_css_modules_ga' )
107
23
const buttons = React . Children . map ( children , ( child , index ) => < div key = { index } > { child } </ div > )
108
24
const buttonRef = useProvidedRefOrCreate ( forwardRef as React . RefObject < HTMLDivElement > )
109
25
@@ -114,17 +30,18 @@ const ButtonGroup = React.forwardRef<HTMLElement, ButtonGroupProps>(function But
114
30
focusOutBehavior : 'wrap' ,
115
31
} )
116
32
33
+ if ( sx !== defaultSxProp ) {
34
+ return (
35
+ < Box as = "div" className = { clsx ( className , classes . ButtonGroup ) } role = { role } { ...rest } sx = { sx } ref = { buttonRef } >
36
+ { buttons }
37
+ </ Box >
38
+ )
39
+ }
40
+
117
41
return (
118
- < StyledButtonGroup
119
- ref = { buttonRef }
120
- className = { clsx ( className , {
121
- [ classes . ButtonGroup ] : enabled ,
122
- } ) }
123
- role = { role }
124
- { ...rest }
125
- >
42
+ < div ref = { buttonRef } className = { clsx ( className , classes . ButtonGroup ) } role = { role } { ...rest } >
126
43
{ buttons }
127
- </ StyledButtonGroup >
44
+ </ div >
128
45
)
129
46
} ) as PolymorphicForwardRefComponent < 'div' , ButtonGroupProps >
130
47
0 commit comments