Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading