Skip to content

Commit 5f7bd0b

Browse files
authored
bugFix: When the feature flag is enabled and sx prop is passed in use, Box (#5068)
* When the feature flag is enabled and sx prop is passed in use, Box * Create plenty-books-agree.md * Adding tests * Add as button back
1 parent 702ba5c commit 5f7bd0b

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

.changeset/plenty-books-agree.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@primer/react": patch
3+
---
4+
5+
fix for `toggleStyledComponent` utility, When the feature flag is enabled and sx prop is passed in use, Box
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import React from 'react'
2+
import {render} from '@testing-library/react'
3+
import {FeatureFlags} from '../../../FeatureFlags'
4+
import {toggleStyledComponent} from '../toggleStyledComponent'
5+
import styled from 'styled-components'
6+
7+
describe('toggleStyledComponent', () => {
8+
test('renders the `as` prop when flag is enabled', () => {
9+
const TestComponent = toggleStyledComponent('testFeatureFlag', () => <div />)
10+
const {container} = render(
11+
<FeatureFlags flags={{testFeatureFlag: true}}>
12+
<TestComponent as="button" />
13+
</FeatureFlags>,
14+
)
15+
expect(container.firstChild).toBeInstanceOf(HTMLButtonElement)
16+
})
17+
18+
test('renders a div as fallback when flag is enabled and no `as` prop is provided', () => {
19+
const TestComponent = toggleStyledComponent('testFeatureFlag', () => <div />)
20+
const {container} = render(
21+
<FeatureFlags flags={{testFeatureFlag: true}}>
22+
<TestComponent />
23+
</FeatureFlags>,
24+
)
25+
expect(container.firstChild).toBeInstanceOf(HTMLDivElement)
26+
})
27+
28+
test('renders Box with `as` if `sx` is provided and flag is enabled', () => {
29+
const TestComponent = toggleStyledComponent('testFeatureFlag', () => styled.div``)
30+
const {container} = render(
31+
<FeatureFlags flags={{testFeatureFlag: true}}>
32+
<TestComponent as="button" sx={{color: 'red'}} />
33+
</FeatureFlags>,
34+
)
35+
36+
expect(container.firstChild).toBeInstanceOf(HTMLButtonElement)
37+
expect(container.firstChild).toHaveStyle('color: red')
38+
})
39+
40+
test('renders styled component when flag is disabled', () => {
41+
const StyledComponent = toggleStyledComponent('testFeatureFlag', styled.div.attrs({['data-styled']: true})``)
42+
const {container} = render(
43+
<FeatureFlags flags={{testFeatureFlag: false}}>
44+
<StyledComponent />
45+
</FeatureFlags>,
46+
)
47+
expect(container.firstChild).toHaveAttribute('data-styled')
48+
})
49+
})

packages/react/src/internal/utils/toggleStyledComponent.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import React from 'react'
22
import {useFeatureFlag} from '../../FeatureFlags'
3+
import Box from '../../Box'
4+
import {defaultSxProp} from '../../utils/defaultSxProp'
35

46
type CSSModulesProps = {
57
// eslint-disable-next-line @typescript-eslint/no-explicit-any
68
as?: string | React.ComponentType<any>
9+
sx?: React.CSSProperties
710
}
811

912
/**
@@ -18,12 +21,18 @@ type CSSModulesProps = {
1821
* is disabled
1922
*/
2023
export function toggleStyledComponent<T, P extends CSSModulesProps>(flag: string, Component: React.ComponentType<P>) {
21-
const Wrapper = React.forwardRef<T, P>(function Wrapper({as: BaseComponent = 'div', ...rest}, ref) {
24+
const Wrapper = React.forwardRef<T, P>(function Wrapper(
25+
{as: BaseComponent = 'div', sx: sxProp = defaultSxProp, ...rest},
26+
ref,
27+
) {
2228
const enabled = useFeatureFlag(flag)
2329
if (enabled) {
30+
if (sxProp !== defaultSxProp) {
31+
return <Box as={BaseComponent} {...rest} sx={sxProp} ref={ref} />
32+
}
2433
return <BaseComponent {...rest} ref={ref} />
2534
}
26-
return <Component as={BaseComponent} {...(rest as P)} ref={ref} />
35+
return <Component as={BaseComponent} {...(rest as P)} sx={sxProp} ref={ref} />
2736
})
2837

2938
return Wrapper

0 commit comments

Comments
 (0)