| 1 |
# @patternfly/react-icons |
| 2 |
|
| 3 |
PatternFly Icons as React Components. |
| 4 |
|
| 5 |
## Usage |
| 6 |
|
| 7 |
```tsx |
| 8 |
import RhMicronsCloseIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-close-icon'; |
| 9 |
|
| 10 |
const closeIcon = <RhMicronsCloseIcon />; |
| 11 |
``` |
| 12 |
|
| 13 |
For a list of the available icons please refer to the [PatternFly react docs](https://pf-react-staging.patternfly.org/icons) |
| 14 |
|
| 15 |
## Styling icons |
| 16 |
|
| 17 |
All icons from @patternfly/react-icons have the HTML class `pf-v6-svg` applied and CSS styles applied via @patternfly/react-styles. |
| 18 |
|
| 19 |
If not using @patternfly/react-icons in conjunction with @patternfly/react-styles, then the following generic styles will need to be applied to the icons: `height="1em", style="vertical-align: -0.125em;" width="1em"` |
| 20 |
|
| 21 |
If using @patternfly/react-icons in conjunction with @patternfly/react-core, icons can be further styled by wrapping an icon from |
| 22 |
@patternfly/react-icons in a PatternFly icon component. |
| 23 |
|
| 24 |
## Adding icons |
| 25 |
|
| 26 |
Icons for this package are generated from the `@fortawesome/free-solid-svg-icons` package. |
| 27 |
|
| 28 |
If you have some custom icon defined by an SVG path the best way to add such icon to this repository is to add its path definition in the [customIcons.mjs](./scripts/icons/customIcons.mjs) file. |
| 29 |
|
| 30 |
```JS |
| 31 |
export default { |
| 32 |
// ... other icon defintions |
| 33 |
bigPlus: {width: 1024, height: 1024, svgPathData: 'M2 1 h1 v1 h1 v1 h-1 v1 h-1 v-1 h-1 v-1 h1 z'} |
| 34 |
} |
| 35 |
``` |
| 36 |
|
| 37 |
## Tree shaking |
| 38 |
|
| 39 |
Ensure optimization.sideEffects is set to true within your Webpack config: |
| 40 |
|
| 41 |
```JS |
| 42 |
optimization: { |
| 43 |
sideEffects: true |
| 44 |
} |
| 45 |
``` |
| 46 |
|
| 47 |
Use ESM module imports to enable tree shaking with no additional setup required. |
| 48 |
|
| 49 |
```JS |
| 50 |
import RhMicronsCloseIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-close-icon'; |
| 51 |
``` |
| 52 |
|
| 53 |
To enable tree shaking with named imports for CJS modules, utilize [babel-plugin-transform-imports](https://www.npmjs.com/package/babel-plugin-transform-imports) and update a babel.config.js file to utilize the plugin: |
| 54 |
|
| 55 |
```JS |
| 56 |
module.exports = { |
| 57 |
presets: ["@babel/preset-env", "@babel/preset-react"], |
| 58 |
plugins: [ |
| 59 |
[ |
| 60 |
"transform-imports", |
| 61 |
{ |
| 62 |
"@patternfly/react-icons": { |
| 63 |
transform: (importName, matches) => `@patternfly/react-icons/dist/js/icons/${importName.split(/(?=[A-Z])/).join('-').toLowerCase()}`, |
| 64 |
preventFullImport: true |
| 65 |
} |
| 66 |
} |
| 67 |
] |
| 68 |
] |
| 69 |
} |
| 70 |
``` |
| 71 |
|
| 72 |
## Static SVGs |
| 73 |
|
| 74 |
All icons are also available as static SVG files in `@patternfly/react-icons/dist/static`. The static SVGs include all the same attributes as the React components (viewBox, class names, etc.) to ensure visual consistency. |
| 75 |
|
| 76 |
Static SVGs are useful when you need to: |
| 77 |
- Use icons in non-React contexts, such as static HTML |
| 78 |
- Reference icons via URL or file path |
| 79 |
|
| 80 |
### Usage |
| 81 |
|
| 82 |
You can import or reference static SVG files directly: |
| 83 |
|
| 84 |
```jsx |
| 85 |
// In HTML |
| 86 |
<img src="/icons/static/rh-microns-close-icon.svg" alt="Close" /> |
| 87 |
|
| 88 |
// In CSS |
| 89 |
.close-icon { |
| 90 |
background-image: url('/icons/static/rh-microns-close-icon.svg'); |
| 91 |
} |
| 92 |
|
| 93 |
// Direct file path |
| 94 |
import RhMicronsCloseIcon from '@patternfly/react-icons/dist/static/rh-microns-close-icon.svg'; |
| 95 |
``` |