Skip to content

Commit

Permalink
Remove redundant styles in Section and BreakoutBanner components (#677)
Browse files Browse the repository at this point in the history
* remove redundant styles in section and breakout banner components

* Create plenty-tomatoes-greet.md
  • Loading branch information
rezrah committed Aug 2, 2024
1 parent 7da3471 commit c76c8c8
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-tomatoes-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react-brand": patch
---

Removed redundant styles in default `Section` and `BreakoutBanner` components
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 16 additions & 9 deletions packages/react/src/BreakoutBanner/BreakoutBanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,22 @@ const Root = forwardRef(
[processBackgroundValue],
)

const backgroundStyles = useMemo(
() => ({
...createStyles('background-color', backgroundColor),
...createStyles('background-image-src', backgroundImageSrc),
...createStyles('background-image-position', backgroundImagePosition),
...createStyles('background-image-size', backgroundImageSize),
}),
[backgroundColor, backgroundImageSrc, backgroundImagePosition, backgroundImageSize, createStyles],
)
const backgroundStyles = useMemo(() => {
const allStyles = {}

const addStyle = (property, value) => {
if (value) {
Object.assign(allStyles, createStyles(property, value))
}
}

addStyle('background-color', backgroundColor)
addStyle('background-image-src', backgroundImageSrc)
addStyle('background-image-position', backgroundImagePosition)
addStyle('background-image-size', backgroundImageSize)

return allStyles
}, [backgroundColor, backgroundImageSrc, backgroundImagePosition, backgroundImageSize, createStyles])

const hasHeading = useMemo(
() => React.Children.toArray(children).some(child => React.isValidElement(child) && child.type === _Heading),
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/Section/Section.features.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export const CustomBackgroundColor: StoryFn<typeof Section> = () => {
)
}

export const ResponsivemBackgroundColor: StoryFn<typeof Section> = () => {
export const ResponsiveBackgroundColor: StoryFn<typeof Section> = () => {
return (
<>
<Section backgroundColor={{narrow: 'subtle', regular: '#F8F8Ff', wide: '#FFF8F8'}}>
Expand Down
28 changes: 19 additions & 9 deletions packages/react/src/Section/Section.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ export const Section = forwardRef<HTMLDivElement, PropsWithChildren<SectionProps
[processBackgroundValue],
)

const addStyle = useCallback(
(obj, property, value) => {
if (value) {
Object.assign(obj, createStyles(property, value))
}
},
[createStyles],
)

const paddingBlockStartClass = useMemo(
() => createPaddingClasses(paddingBlockStart, 'paddingBlockStart'),
[paddingBlockStart, createPaddingClasses],
Expand All @@ -141,15 +150,16 @@ export const Section = forwardRef<HTMLDivElement, PropsWithChildren<SectionProps
[paddingBlockEnd, createPaddingClasses],
)

const backgroundStyles = useMemo(
() => ({
...createStyles('background-color', backgroundColor),
...createStyles('background-image-src', backgroundImageSrc),
...createStyles('background-image-position', backgroundImagePosition),
...createStyles('background-image-size', backgroundImageSize),
}),
[backgroundColor, backgroundImageSrc, backgroundImagePosition, backgroundImageSize, createStyles],
)
const backgroundStyles = useMemo(() => {
const allStyles = {}

addStyle(allStyles, 'background-color', backgroundColor)
addStyle(allStyles, 'background-image-src', backgroundImageSrc)
addStyle(allStyles, 'background-image-position', backgroundImagePosition)
addStyle(allStyles, 'background-image-size', backgroundImageSize)

return allStyles
}, [addStyle, backgroundColor, backgroundImageSrc, backgroundImagePosition, backgroundImageSize])

return (
<Component
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/Section/Section.visual.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ test.describe('Visual Comparison: Section', () => {
expect(await page.screenshot()).toMatchSnapshot()
})

test('Section / Responsivem Background Color', async ({page}) => {
test('Section / Responsive Background Color', async ({page}) => {
await page.goto(
'http://localhost:6006/iframe.html?args=&id=components-section-features--responsivem-background-color&viewMode=story',
'http://localhost:6006/iframe.html?args=&id=components-section-features--responsive-background-color&viewMode=story',
)

await page.waitForTimeout(500)
Expand Down

0 comments on commit c76c8c8

Please sign in to comment.