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

[core] Add Icon title prop #2182

Merged
merged 1 commit into from
Feb 25, 2018
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
12 changes: 10 additions & 2 deletions packages/core/src/components/icon/icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ export interface IIconProps extends IIntentProps, IProps {

/** CSS style properties. */
style?: React.CSSProperties;

/**
* Description string.
* Browsers usually render this as a tooltip on hover, whereas screen
* readers will use it for aural feedback.
* By default, this is set to the icon's name for accessibility.
*/
title?: string | false | null;
}

export class Icon extends React.PureComponent<IIconProps & React.SVGAttributes<SVGElement>> {
Expand All @@ -54,7 +62,7 @@ export class Icon extends React.PureComponent<IIconProps & React.SVGAttributes<S
public static readonly SIZE_LARGE = 20;

public render() {
const { className, icon, iconSize = Icon.SIZE_STANDARD, intent, ...svgProps } = this.props;
const { className, icon, iconSize = Icon.SIZE_STANDARD, intent, title = icon, ...svgProps } = this.props;
if (icon == null) {
return null;
} else if (typeof icon !== "string") {
Expand All @@ -79,7 +87,7 @@ export class Icon extends React.PureComponent<IIconProps & React.SVGAttributes<S
height={iconSize}
viewBox={viewBox}
>
<title>{icon}</title>
{title ? <title>{title}</title> : null}
{paths}
</svg>
);
Expand Down
10 changes: 10 additions & 0 deletions packages/core/test/icon/iconTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ describe("<Icon>", () => {
assert.isTrue(icon.isEmptyRender());
});

it("title sets content of <title> element", () => {
const icon = shallow(<Icon icon="airplane" title="bird" />);
assert.equal(icon.find("title").text(), "bird");
});

it("title defaults to icon name", () => {
const icon = shallow(<Icon icon="airplane" />);
assert.equal(icon.find("title").text(), "airplane");
});

/** Asserts that rendered icon has given className. */
function assertIcon(icon: React.ReactElement<IIconProps>, iconName: IconName) {
assert.strictEqual(shallow(icon).text(), iconName);
Expand Down