Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core/presentation): add Icon component w/ inline SVG (#8056)
* feat(core/presentation): add Icon component w/ inline SVG * refactor(core/managed): add explicit size strings to IIconProps Co-Authored-By: Chris Thielen <christopherthielen@users.noreply.github.com> Co-authored-by: Chris Thielen <christopherthielen@users.noreply.github.com>
- Loading branch information
1 parent
15e47a6
commit 113c4cd
Showing
8 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React, { memo } from 'react'; | ||
|
||
import { iconsByName } from './iconsByName'; | ||
|
||
export type IconNames = keyof typeof iconsByName; | ||
|
||
export type IIconProps = { | ||
name: IconNames; | ||
appearance?: 'light' | 'neutral' | 'dark'; | ||
size?: 'extraSmall' | 'small' | 'medium' | 'large' | 'extraLarge' | string; | ||
color?: string; | ||
className?: string; | ||
}; | ||
|
||
const DEFAULT_SIZE = 'small'; | ||
const DEFAULT_APPEARANCE = 'neutral'; | ||
|
||
const pxDimensionsBySize: { [size: string]: string } = { | ||
extraSmall: '16px', | ||
small: '20px', | ||
medium: '24px', | ||
large: '32px', | ||
extraLarge: '40px', | ||
}; | ||
|
||
const colorsByAppearance = { | ||
light: 'var(--color-white)', | ||
neutral: 'rgba(0, 0, 0, 0.5)', | ||
dark: 'var(--color-black)', | ||
}; | ||
|
||
const throwInvalidIconError = (name: string) => { | ||
throw new Error(`No icon with the name ${name} exists`); | ||
}; | ||
|
||
export const Icon = memo(({ name, appearance, size, color, className }: IIconProps) => { | ||
const Component = iconsByName[name]; | ||
|
||
if (!Component) { | ||
throwInvalidIconError(name); | ||
} | ||
|
||
const width = pxDimensionsBySize[size] || size || pxDimensionsBySize[DEFAULT_SIZE]; | ||
const fill = color ? `var(--color-${color})` : colorsByAppearance[appearance || DEFAULT_APPEARANCE]; | ||
|
||
return <Component className={className} style={{ width, fill }} />; | ||
}); |
9 changes: 9 additions & 0 deletions
9
app/scripts/modules/core/src/presentation/icons/iconsByName.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ReactComponent as artifact } from './vectors/artifact.svg'; | ||
import { ReactComponent as close } from './vectors/close.svg'; | ||
import { ReactComponent as environment } from './vectors/environment.svg'; | ||
|
||
export const iconsByName = { | ||
artifact, | ||
close, | ||
environment, | ||
} as const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './Icon'; |
14 changes: 14 additions & 0 deletions
14
app/scripts/modules/core/src/presentation/icons/vectors/artifact.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions
12
app/scripts/modules/core/src/presentation/icons/vectors/close.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions
38
app/scripts/modules/core/src/presentation/icons/vectors/environment.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters