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

[v4] [icons] feat: add iconNameToPathsRecordKey utility fn #4955

Merged
merged 2 commits into from
Oct 7, 2021
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
3 changes: 1 addition & 2 deletions packages/core/src/components/icon/icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
import classNames from "classnames";
import * as React from "react";

import { IconName, IconSvgPaths16, IconSvgPaths20 } from "@blueprintjs/icons";
import { IconName, IconSvgPaths16, IconSvgPaths20, iconNameToPathsRecordKey } from "@blueprintjs/icons";

import { AbstractPureComponent2, Classes, DISPLAYNAME_PREFIX, IntentProps, Props, MaybeElement } from "../../common";
import { iconNameToPathsRecordKey } from "./iconUtils";

export { IconName };

Expand Down
18 changes: 18 additions & 0 deletions packages/icons/src/iconCodepoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright 2021 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// icon sets are identical aside from SVG paths, so we just import the info for the 16px set
export { BLUEPRINT_ICONS_16_CODEPOINTS as IconCodepoints } from "./generated/16px/blueprint-icons-16";
12 changes: 9 additions & 3 deletions packages/icons/src/iconNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@
import { snakeCase } from "change-case";

// icon sets are identical aside from SVG paths, so we just import the info for the 16px set
import { BlueprintIcons_16, BlueprintIcons_16Id, BlueprintIcons_16Key } from "./generated/16px/blueprint-icons-16";
import {
BlueprintIcons_16,
BlueprintIcons_16Id as IconName,
BlueprintIcons_16Key,
} from "./generated/16px/blueprint-icons-16";

const IconNamesLegacy: Record<string, BlueprintIcons_16Id> = {};
export type { IconName };

const IconNamesLegacy: Record<string, IconName> = {};

for (const [pascalCaseKey, iconName] of Object.entries(BlueprintIcons_16)) {
const screamingSnakeCaseKey = snakeCase(pascalCaseKey).toUpperCase();
Expand All @@ -30,7 +36,7 @@ for (const [pascalCaseKey, iconName] of Object.entries(BlueprintIcons_16)) {

export const IconNames = {
...BlueprintIcons_16,
...(IconNamesLegacy as Record<ScreamingSnakeCaseIconNames, BlueprintIcons_16Id>),
...(IconNamesLegacy as Record<ScreamingSnakeCaseIconNames, IconName>),
};

type ScreamingSnakeCaseIconNames = Uppercase<StripLeadingUnderscore<CamelToSnake<BlueprintIcons_16Key>>>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,19 @@
* limitations under the License.
*/

import { IconName, IconSvgPaths16 } from "@blueprintjs/icons";
import * as IconSvgPaths16 from "./generated/16px/paths";
import * as IconSvgPaths20 from "./generated/20px/paths";
import type { IconName } from "./iconNames";

export { IconSvgPaths16, IconSvgPaths20 };

/**
* Type safe string literal conversion of snake-case icon names to camelCase icon names,
* useful for indexing into the SVG paths record to extract a single icon's SVG path definition.
*
* N.B. `IconSvgPaths16` and `IconSvgPaths20` are assignable to each other, so it doesn't matter
* which one is used in the return type definition here.
*/
export function iconNameToPathsRecordKey(name: IconName): keyof typeof IconSvgPaths16 {
return kebabCaseToCamelCase(name);
}
Expand Down
13 changes: 3 additions & 10 deletions packages/icons/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@
* limitations under the License.
*/

// icon sets are identical aside from SVG paths, so we just import the info for the 16px set
import {
BlueprintIcons_16Id as IconName,
BLUEPRINT_ICONS_16_CODEPOINTS as IconCodepoints,
} from "./generated/16px/blueprint-icons-16";
import * as IconSvgPaths16 from "./generated/16px/paths";
import * as IconSvgPaths20 from "./generated/20px/paths";
import { IconNames } from "./iconNames";

export { IconCodepoints, IconName, IconNames, IconSvgPaths16, IconSvgPaths20 };
export { IconSvgPaths16, IconSvgPaths20, iconNameToPathsRecordKey } from "./iconSvgPaths";
export { IconCodepoints } from "./iconCodepoints";
export { IconName, IconNames } from "./iconNames";