Skip to content

Commit

Permalink
feat: Allow an icon to display with an inverse color (#1787)
Browse files Browse the repository at this point in the history
Co-authored-by: Aram <[email protected]>
  • Loading branch information
VincentSmedinga and alimpens authored Dec 16, 2024
1 parent 0192404 commit 1f5d166
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 14 deletions.
4 changes: 4 additions & 0 deletions packages/css/src/components/icon/icon.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
fill: currentColor;
}

.ams-icon--inverse-color {
color: var(--ams-icon-inverse-color);
}

.ams-icon--square {
aspect-ratio: 1 / 1;
justify-content: center;
Expand Down
8 changes: 8 additions & 0 deletions packages/react/src/Icon/Icon.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ describe('Icon', () => {
expect(icon).toHaveClass('ams-icon--square')
})

it('renders the inverse color class', () => {
const { container } = render(<Icon svg={AlertIcon} inverseColor />)

const icon = container.querySelector('span:only-child')

expect(icon).toHaveClass('ams-icon--inverse-color')
})

it('renders an additional class name', () => {
const { container } = render(<Icon svg={AlertIcon} className="large" />)

Expand Down
8 changes: 7 additions & 1 deletion packages/react/src/Icon/Icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { forwardRef } from 'react'
import type { ForwardedRef, HTMLAttributes } from 'react'

export type IconProps = {
/** Changes the icon colour for readability on a dark background. */
inverseColor?: boolean
/** The size of the icon. Corresponds with the text levels. */
size?: 'level-3' | 'level-4' | 'level-5' | 'level-6'
/** Whether the icon container should be made square. */
Expand All @@ -19,11 +21,15 @@ export type IconProps = {
} & HTMLAttributes<HTMLSpanElement>

export const Icon = forwardRef(
({ className, size = 'level-3', square, svg, ...restProps }: IconProps, ref: ForwardedRef<HTMLElement>) => (
(
{ className, inverseColor, size = 'level-3', square, svg, ...restProps }: IconProps,
ref: ForwardedRef<HTMLElement>,
) => (
<span
ref={ref}
className={clsx(
'ams-icon',
inverseColor && 'ams-icon--inverse-color',
size === 'level-3' && 'ams-icon--size-3',
size === 'level-4' && 'ams-icon--size-4',
size === 'level-5' && 'ams-icon--size-5',
Expand Down
3 changes: 3 additions & 0 deletions proprietary/tokens/src/components/ams/icon.tokens.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"ams": {
"icon": {
"inverse": {
"color": { "value": "{ams.brand.color.neutral.0}" }
},
"size-3": {
"font-size": { "value": "{ams.text.level.3.font-size}" },
"line-height": { "value": "{ams.text.level.3.line-height}" }
Expand Down
7 changes: 7 additions & 0 deletions storybook/src/components/Icon/Icon.docs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,10 @@ Sometimes it’s useful for the icon to take up square space, for example, with
### Level 6

<Canvas of={IconStories.Level6} />

### Inverse colour

Set the `inverseColor` prop if the Icon sits on a dark background.
This ensures the colour of the icon provides enough contrast.

<Canvas of={IconStories.InverseColour} />
24 changes: 11 additions & 13 deletions storybook/src/components/Icon/Icon.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import { Meta, StoryObj } from '@storybook/react'
const meta = {
title: 'Components/Media/Icon',
component: Icon,
args: {
inverseColor: false,
svg: Icons.EmailIcon,
},
argTypes: {
svg: {
control: {
Expand All @@ -26,11 +30,7 @@ export default meta

type Story = StoryObj<typeof meta>

export const Default: Story = {
args: {
svg: Icons.EmailIcon,
},
}
export const Default: Story = {}

export const WithText: Story = {
decorators: [
Expand All @@ -46,42 +46,40 @@ export const WithText: Story = {
<Heading size="level-3">Inline text</Heading>
</>
),
args: {
svg: Icons.EmailIcon,
},
}

export const Square: Story = {
args: {
svg: Icons.EmailIcon,
square: true,
},
}

export const Level3: Story = {
args: {
svg: Icons.EmailIcon,
size: 'level-3',
},
}

export const Level4: Story = {
args: {
svg: Icons.EmailIcon,
size: 'level-4',
},
}

export const Level5: Story = {
args: {
svg: Icons.EmailIcon,
size: 'level-5',
},
}

export const Level6: Story = {
args: {
svg: Icons.EmailIcon,
size: 'level-6',
},
}

export const InverseColour: Story = {
args: {
inverseColor: true,
},
}

0 comments on commit 1f5d166

Please sign in to comment.