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

import type {ColorTextBaseProps} from '../Text/colorText/colorText';
import {colorText} from '../Text/colorText/colorText';
import type {DOMProps, QAProps} from '../types';
import {block} from '../utils/cn';
import {a11yHiddenSvgProps} from '../utils/svg';
Expand All @@ -22,7 +24,7 @@ interface IconComposition {
prefix?: string;
}

export interface IconProps extends QAProps, DOMProps {
export interface IconProps extends QAProps, DOMProps, ColorTextBaseProps {
data: IconData;
width?: number | string;
height?: number | string;
Expand All @@ -36,7 +38,18 @@ 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,12 +104,17 @@ export const Icon: React.ForwardRefExoticComponent<IconProps & React.RefAttribut
}
}

const hasStyleColor = style?.color !== undefined;

const svgClassName =
color && !hasStyleColor ? colorText({color}, b(null, className)) : b(null, className);

const props = {
xmlns: 'http://www.w3.org/2000/svg',
xmlnsXlink: 'http://www.w3.org/1999/xlink',
width: w,
height: h,
className: b(null, className),
className: svgClassName,
style,
fill,
stroke,
Expand Down
21 changes: 11 additions & 10 deletions src/components/Icon/README-ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ import CheckIcon from './check.svg';

## Свойства

| Имя | Описание | Тип | Значение по умолчанию |
| :-------- | :--------------------------------------------- | :---------------: | :-------------------: |
| data | Источник SVG-иконки. | `IconData` | |
| width | Атрибут `width` для SVG. | `number` `string` | |
| height | Атрибут `height` для SVG. | `number` `string` | |
| size | Атрибуты `width` и `height` для SVG. | `number` `string` | |
| fill | Атрибут `fill` для SVG. | `string` | `"currentColor"` |
| stroke | Атрибут `stroke` для SVG. | `string` | `"none"` |
| className | Пользовательский CSS-класс корневого элемента. | `string` | |
| style | Пользовательские стили для корневого элемента. | `CSSProperties` | |
| Имя | Описание | Тип | Значение по умолчанию |
| :-------- | :--------------------------------------------- | :----------------------------------------------------------: | :-------------------: |
| data | Источник SVG-иконки. | `IconData` | |
| width | Атрибут `width` для SVG. | `number` `string` | |
| height | Атрибут `height` для SVG. | `number` `string` | |
| size | Атрибуты `width` и `height` для SVG. | `number` `string` | |
| fill | Атрибут `fill` для SVG. | `string` | `"currentColor"` |
| stroke | Атрибут `stroke` для SVG. | `string` | `"none"` |
| color | Цвет текста иконки. | `string` (см. значения в разделе **Цвет** компонента `Text`) | |
| className | Пользовательский CSS-класс корневого элемента. | `string` | |
| style | Пользовательские стили для корневого элемента. | `CSSProperties` | |
21 changes: 11 additions & 10 deletions src/components/Icon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ import CheckIcon from './check.svg';

## Properties

| Name | Description | Type | Default |
| :-------- | :--------------------------------------- | :---------------: | :--------------: |
| data | Source of SVG icon | `IconData` | |
| width | `width` SVG attribute | `number` `string` | |
| height | `height` SVG attribute | `number` `string` | |
| size | Both `width` and `height` SVG attributes | `number` `string` | |
| fill | `fill` SVG attribute | `string` | `"currentColor"` |
| stroke | `stroke` SVG attribute | `string` | `"none"` |
| className | Custom CSS class for the root element | `string` | |
| style | Custom styles for the root element | `CSSProperties` | |
| Name | Description | Type | Default |
| :-------- | :--------------------------------------- | :----------------------------------------------------------: | :--------------: |
| data | Source of SVG icon | `IconData` | |
| width | `width` SVG attribute | `number` `string` | |
| height | `height` SVG attribute | `number` `string` | |
| 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` (see the values in the **Color** section of `Text`) | |
| 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.
5 changes: 5 additions & 0 deletions src/components/Icon/__stories__/Icon.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Envelope, Gear, Rocket} from '@gravity-ui/icons';
import type {Meta, StoryObj} from '@storybook/react-webpack5';

import {Showcase} from '../../../demo/Showcase';
import {TEXT_COLORS} from '../../Text/colorText/colorText';
import {Icon} from '../Icon';

const icons = {Gear, Envelope, Rocket};
Expand All @@ -14,6 +15,10 @@ const meta: Meta<typeof Icon> = {
options: Object.keys(icons),
mapping: icons,
},
color: {
control: 'select',
options: TEXT_COLORS,
},
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
8 changes: 7 additions & 1 deletion src/components/Icon/__tests__/cases.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
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']> = ['primary', 'danger'];

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