Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
26 changes: 23 additions & 3 deletions src/components/Icon/Icon.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';

import type {DOMProps, QAProps} from '../types';
import type {CSSProperties, DOMProps, QAProps} from '../types';
import {block} from '../utils/cn';
import {a11yHiddenSvgProps} from '../utils/svg';

Expand Down Expand Up @@ -29,14 +29,26 @@ export interface IconProps extends QAProps, DOMProps {
size?: number | string;
fill?: string;
stroke?: string;
color?: CSSProperties['color'] | string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the point of duplication style functionality? Let's instead add feature from the Text component, which have color property accepting text color name (typeof TEXT_COLORS[number]).

}

const b = block('icon');

export const Icon: React.ForwardRefExoticComponent<IconProps & React.RefAttributes<SVGSVGElement>> &
IconComposition = React.forwardRef<SVGSVGElement, IconProps>(
(
{data, width, height, size, className, style, fill = 'currentColor', stroke = 'none', qa},
{
data,
width,
height,
size,
className,
style,
color,
fill = 'currentColor',
stroke = 'none',
qa,
},
ref,
) => {
// This component supports four different ways to load and use icons:
Expand Down Expand Up @@ -91,13 +103,21 @@ export const Icon: React.ForwardRefExoticComponent<IconProps & React.RefAttribut
}
}

const svgStyle =
color && style?.color === undefined
? {
...style,
color,
}
: style;

const props = {
xmlns: 'http://www.w3.org/2000/svg',
xmlnsXlink: 'http://www.w3.org/1999/xlink',
width: w,
height: h,
className: b(null, className),
style,
style: svgStyle,
fill,
stroke,
'data-qa': qa,
Expand Down
1 change: 1 addition & 0 deletions src/components/Icon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@ import CheckIcon from './check.svg';
| size | Both `width` and `height` SVG attributes | `number` `string` | |
| fill | `fill` SVG attribute | `string` | `"currentColor"` |
| stroke | `stroke` SVG attribute | `string` | `"none"` |
| color | Text color applied to the SVG icon | `string` | |
| className | Custom CSS class for the root element | `string` | |
| style | Custom styles for the root element | `CSSProperties` | |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/components/Icon/__stories__/Icon.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const meta: Meta<typeof Icon> = {
options: Object.keys(icons),
mapping: icons,
},
color: {
control: 'text',
},
style: {
control: 'object',
},
Expand Down
4 changes: 3 additions & 1 deletion src/components/Icon/__tests__/Icon.visual.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {smokeTest, test} from '~playwright/core';
import {createSmokeScenarios} from '../../../stories/tests-factory/create-smoke-scenarios';
import type {IconProps} from '../Icon';

import {sizeCases} from './cases';
import {colorCases, sizeCases, styleCases} from './cases';
import {TestIcon} from './helpersPlaywright';

test.describe('Icon', {tag: '@Icon'}, () => {
Expand All @@ -12,6 +12,8 @@ test.describe('Icon', {tag: '@Icon'}, () => {
{},
{
size: sizeCases,
color: colorCases,
style: styleCases,
},
);

Expand Down
11 changes: 10 additions & 1 deletion src/components/Icon/__tests__/cases.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import type {Cases} from '../../../stories/tests-factory/models';
import type {Cases, CasesWithName} from '../../../stories/tests-factory/models';
import type {IconProps} from '../Icon';

export const sizeCases: Cases<IconProps['size']> = [10, 20, 30];

export const colorCases: Cases<IconProps['color']> = [
'var(--g-color-text-primary)',
'var(--g-color-text-danger)',
];

export const styleCases: CasesWithName<IconProps['style']> = [
['color: var(--g-color-text-positive)', {color: 'var(--g-color-text-positive)'}],
];
Loading