diff --git a/.changeset/plenty-books-agree.md b/.changeset/plenty-books-agree.md
new file mode 100644
index 00000000000..e893025c849
--- /dev/null
+++ b/.changeset/plenty-books-agree.md
@@ -0,0 +1,5 @@
+---
+"@primer/react": patch
+---
+
+fix for `toggleStyledComponent` utility, When the feature flag is enabled and sx prop is passed in use, Box
diff --git a/packages/react/src/internal/utils/__tests__/toggleStyledComponent.test.tsx b/packages/react/src/internal/utils/__tests__/toggleStyledComponent.test.tsx
new file mode 100644
index 00000000000..6a4206003d0
--- /dev/null
+++ b/packages/react/src/internal/utils/__tests__/toggleStyledComponent.test.tsx
@@ -0,0 +1,49 @@
+import React from 'react'
+import {render} from '@testing-library/react'
+import {FeatureFlags} from '../../../FeatureFlags'
+import {toggleStyledComponent} from '../toggleStyledComponent'
+import styled from 'styled-components'
+
+describe('toggleStyledComponent', () => {
+ test('renders the `as` prop when flag is enabled', () => {
+ const TestComponent = toggleStyledComponent('testFeatureFlag', () =>
)
+ const {container} = render(
+
+
+ ,
+ )
+ expect(container.firstChild).toBeInstanceOf(HTMLButtonElement)
+ })
+
+ test('renders a div as fallback when flag is enabled and no `as` prop is provided', () => {
+ const TestComponent = toggleStyledComponent('testFeatureFlag', () => )
+ const {container} = render(
+
+
+ ,
+ )
+ expect(container.firstChild).toBeInstanceOf(HTMLDivElement)
+ })
+
+ test('renders Box with `as` if `sx` is provided and flag is enabled', () => {
+ const TestComponent = toggleStyledComponent('testFeatureFlag', () => styled.div``)
+ const {container} = render(
+
+
+ ,
+ )
+
+ expect(container.firstChild).toBeInstanceOf(HTMLButtonElement)
+ expect(container.firstChild).toHaveStyle('color: red')
+ })
+
+ test('renders styled component when flag is disabled', () => {
+ const StyledComponent = toggleStyledComponent('testFeatureFlag', styled.div.attrs({['data-styled']: true})``)
+ const {container} = render(
+
+
+ ,
+ )
+ expect(container.firstChild).toHaveAttribute('data-styled')
+ })
+})
diff --git a/packages/react/src/internal/utils/toggleStyledComponent.tsx b/packages/react/src/internal/utils/toggleStyledComponent.tsx
index 4a9bbd0e00a..7b366cb0eb0 100644
--- a/packages/react/src/internal/utils/toggleStyledComponent.tsx
+++ b/packages/react/src/internal/utils/toggleStyledComponent.tsx
@@ -1,9 +1,12 @@
import React from 'react'
import {useFeatureFlag} from '../../FeatureFlags'
+import Box from '../../Box'
+import {defaultSxProp} from '../../utils/defaultSxProp'
type CSSModulesProps = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
as?: string | React.ComponentType
+ sx?: React.CSSProperties
}
/**
@@ -18,12 +21,18 @@ type CSSModulesProps = {
* is disabled
*/
export function toggleStyledComponent(flag: string, Component: React.ComponentType) {
- const Wrapper = React.forwardRef(function Wrapper({as: BaseComponent = 'div', ...rest}, ref) {
+ const Wrapper = React.forwardRef(function Wrapper(
+ {as: BaseComponent = 'div', sx: sxProp = defaultSxProp, ...rest},
+ ref,
+ ) {
const enabled = useFeatureFlag(flag)
if (enabled) {
+ if (sxProp !== defaultSxProp) {
+ return
+ }
return
}
- return
+ return
})
return Wrapper