Skip to content

Commit

Permalink
feat(Banner): add support for custom icons when variant="upsell" (#4838)
Browse files Browse the repository at this point in the history
* Update Banner.tsx

* feat(Banner): add support for custom icons when variant="upsell"

* chore: add changeset

* test(Banner): update test for info and upsell custom icons

---------

Co-authored-by: Josh Black <[email protected]>
  • Loading branch information
joshblack and joshblack authored Aug 13, 2024
1 parent 529e1a3 commit 7a3b55d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-tables-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': minor
---

Add support for custom icons when a Banner is variant="upsell"
4 changes: 2 additions & 2 deletions packages/react/src/Banner/Banner.docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
},
{
"name": "hideTitle",
"type": "boolean",
"type": "boolean",
"description": "Whether to hide the title visually."
},
{
"name": "icon",
"type": "React.ReactNode",
"description": "Provide an icon for the banner"
"description": "Provide a custom icon for the Banner. This is only available when `variant` is `info` or `upsell`"
},
{
"name": "onDismiss",
Expand Down
13 changes: 13 additions & 0 deletions packages/react/src/Banner/Banner.features.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react'
import {CopilotIcon} from '@primer/octicons-react'
import {action} from '@storybook/addon-actions'
import type {Meta} from '@storybook/react'
import {Banner} from '../Banner'
Expand Down Expand Up @@ -202,3 +203,15 @@ export const WithActions = () => {
/>
)
}

export const CustomIcon = () => {
return (
<Banner
title="Upsell"
description="An example banner with a custom icon"
icon={<CopilotIcon />}
onDismiss={action('onDismiss')}
variant="upsell"
/>
)
}
8 changes: 4 additions & 4 deletions packages/react/src/Banner/Banner.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,22 +160,22 @@ describe('Banner', () => {
expect(container.firstChild).toHaveAttribute('data-testid', 'test')
})

it('should support a custom icon only for info variants', () => {
it('should support a custom icon for info and upsell variants', () => {
const CustomIcon = jest.fn(() => <svg data-testid="icon" aria-hidden="true" />)
const {rerender} = render(
<Banner title="test" description="test-description" variant="info" icon={<CustomIcon />} />,
)
expect(screen.getByTestId('icon')).toBeInTheDocument()

rerender(<Banner title="test" description="test-description" variant="upsell" icon={<CustomIcon />} />)
expect(screen.getByTestId('icon')).toBeInTheDocument()

rerender(<Banner title="test" description="test-description" variant="critical" icon={<CustomIcon />} />)
expect(screen.queryByTestId('icon')).toBe(null)

rerender(<Banner title="test" description="test-description" variant="success" icon={<CustomIcon />} />)
expect(screen.queryByTestId('icon')).toBe(null)

rerender(<Banner title="test" description="test-description" variant="upsell" icon={<CustomIcon />} />)
expect(screen.queryByTestId('icon')).toBe(null)

rerender(<Banner title="test" description="test-description" variant="warning" icon={<CustomIcon />} />)
expect(screen.queryByTestId('icon')).toBe(null)
})
Expand Down
6 changes: 3 additions & 3 deletions packages/react/src/Banner/Banner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ export type BannerProps = React.ComponentPropsWithoutRef<'section'> & {
hideTitle?: boolean

/**
* Provide an icon for the banner.
* Note: Only `variant="info"` banners should use custom icons
* Provide a custom icon for the Banner. This is only available when `variant` is `info` or `upsell`
*/
icon?: React.ReactNode

Expand Down Expand Up @@ -99,6 +98,7 @@ export const Banner = React.forwardRef<HTMLElement, BannerProps>(function Banner
const hasActions = primaryAction || secondaryAction
const bannerRef = React.useRef<HTMLElement>(null)
const ref = useMergedRefs(forwardRef, bannerRef)
const supportsCustomIcon = variant === 'info' || variant === 'upsell'

if (__DEV__) {
// This hook is called consistently depending on the environment
Expand Down Expand Up @@ -134,7 +134,7 @@ export const Banner = React.forwardRef<HTMLElement, BannerProps>(function Banner
ref={ref}
>
<style>{BannerContainerQuery}</style>
<div className="BannerIcon">{icon && variant === 'info' ? icon : iconForVariant[variant]}</div>
<div className="BannerIcon">{icon && supportsCustomIcon ? icon : iconForVariant[variant]}</div>
<div className="BannerContainer">
<div className="BannerContent">
{title ? (
Expand Down

0 comments on commit 7a3b55d

Please sign in to comment.