Design Converter
Education
Last updated on Mar 26, 2025
•15 mins read
Last updated on Nov 26, 2023
•15 mins read
In modern web development, icons are essential for enhancing user experience by providing intuitive visual cues. React Octicon, a collection of SVG icons from GitHub's Primer Octicons library, offers a simple and versatile solution for React applications. These handcrafted icons ensure scalability and clarity across all screen sizes while maintaining consistency with GitHub’s design language.
This blog explores React Octicon’s features, benefits, and implementation, helping both beginners and experienced developers integrate high-quality icons into their React projects.
Icons are more than decorative elements—they serve as a visual language that enhances user experience by conveying actions, concepts, and navigation cues efficiently. In React applications, maintaining scalability and consistency across devices is crucial, and React Octicon provides a versatile solution.
React Octicon, part of GitHub’s Primer design system, offers a lightweight, scalable, and accessible set of SVG icons. Each icon is a React component, making integration seamless while ensuring optimized performance by allowing developers to include only the icons they need, keeping bundle sizes small—especially beneficial for mobile users.
To start using React Octicon, you need to install the library using npm or yarn.
Staying up-to-date with the latest version of React Octicon is crucial to leverage new features and improvements. The Primer Octicons React ecosystem is continuously evolving, with GitHub regularly releasing updates that introduce new icons, enhance accessibility, and optimize performance.
To check for the latest version and update your package, you can run:
1// Using npm 2npm update @primer/octicons-react 3 4// Using yarn 5yarn upgrade @primer/octicons-react 6
By keeping your React Octicon library up-to-date, you ensure you have access to the latest version with all the newest icons and features.
The size prop is a powerful feature of React Octicon components that allows you to control the dimensions of your icons. This prop accepts either a numeric value representing pixels or a string value corresponding to one of the standard sizes provided by the library.
Here's how you can use the size prop to set an icon's size:
1 2<MarkGithubIcon size="large" ariaLabel="GitHub" /> 3
In this example, the MarkGithubIcon is rendered with a large size, which the React Octicon library predefines. This ensures the icon is displayed appropriately for better visibility and alignment with other UI elements.
Each octicon icon has multiple variants to suit design contexts and user interface requirements. Whether you need a bold version for emphasis or a simple outline for a more subdued presence, React Octicon provides you with options.
To choose a variant, you select the appropriate octicon icon from the library. For example, if you need a bold version of the AlertIcon, you would import it like so:
1import { AlertIcon } from '@primer/octicons-react'; 2
This flexibility allows you to maintain design consistency while also having the freedom to highlight certain elements when necessary.
The svg element is the foundation of every icon in the React Octicon library. It allows the icons to scale without losing quality, making them ideal for responsive design. Understanding how the SVG element works is key to effectively using React Octicon in your projects.
When you render an Octicon component, you are rendering an SVG element that can be styled and manipulated like any other HTML element. This means you can apply CSS styles, add animations, and even interact with it using JavaScript.
Here's an example of styling an SVG icon with CSS:
1.my-icon { 2 fill: #555; 3 transition: fill 0.3s ease; 4} 5 6.my-icon:hover { 7 fill: #000; 8} 9
1<AlertIcon className="my-icon" size="medium" ariaLabel="Alert" /> 2
In this example, the AlertIcon will change color when hovered over, thanks to the CSS styles applied to the svg element.
Creating a scalable set of icons means ensuring your icons can adapt to different sizes and resolutions without losing their visual integrity. React Octicon makes this easy by providing vector-based icons and thus inherently scalable.
To create a consistent and scalable icon set, you can define a set of sizes you use throughout your application. React Octicon's size prop makes it easy to apply these sizes to your icons:
1<AlertIcon size="small" ariaLabel="Alert" /> 2<AlertIcon size="medium" ariaLabel="Alert" /> 3<AlertIcon size="large" ariaLabel="Alert" /> 4
Using a consistent set of sizes ensures that your icons look harmonious across your application and maintain their clarity at any resolution.
Accessibility is a critical aspect of modern web development, and icons play a significant role in making interfaces accessible. The aria label prop in React Octicon components allows you to provide an accessible name for icons, which is essential for users who rely on screen readers.
For example, to give an icon an accessible name, you would use the aria label prop like this:
1<SearchIcon ariaLabel="Search" /> 2
This simple addition can greatly enhance the experience for users with visual impairments by providing meaningful context to what might otherwise be an ambiguous visual cue.
When you incorporate React Octicon into your application, it's essential to understand the default behavior of these components. By default, React Octicon icons inherit their parent elements' color and font size, which helps them seamlessly integrate into your design without additional configuration.
However, you can override this default behavior using props like fill and size to customize the icons as needed. React Octicon components are designed to be flexible, and passes props directly to the SVG element, allowing you to adjust styles and attributes according to your application's requirements:
1<AlertIcon fill="#f00" size={24} ariaLabel="Error" /> 2
In this example, the AlertIcon is set to a red fill color and a size of 24 pixels, overriding the default styles.
The fill prop is a powerful way to style your React Octicon icons with color. This prop takes a string value representing a CSS color, a named color, a hex code, or an RGB value. Using the fill prop, you can ensure that your icons match your application's color scheme or stand out when necessary.
Here's how to use the fill prop to set an icon's color:
1<ArchiveIcon fill="currentColor" ariaLabel="Archive" /> 2
In this example, the ArchiveIcon uses the currentColor value, which means it will inherit the color from its parent element, maintaining consistency with the surrounding text or elements.
Properly importing icons is crucial for maintaining a clean and efficient codebase. The usage import pattern in React Octicon allows you to import only the icons you need, which helps to reduce the overall size of your application bundle.
To import an icon, you specify the icon's name in curly braces after the import keyword:
1import { KebabHorizontalIcon } from '@primer/octicons-react'; 2
This pattern ensures you're not loading unnecessary icons, keeping your application's load time minimal.
The title element and prop provide a way to add descriptive text to your SVG icons, which can be displayed as a tooltip on hover. This enhances the user experience by providing additional context and improves accessibility by giving screen reader users more information about the icon's function.
Adding accessibility information through the title prop ensures that your icons are not only visually appealing but also inclusive, making them understandable to all users, including those using assistive technologies.
Here's how to add a title to an icon:
1<ShieldIcon title="Protected" ariaLabel="Protected" /> 2
In this example, when users hover over the ShieldIcon, they will see a tooltip with the text "Protected," offering more insight into the icon's meaning.
Choosing the correct icon for your UI component is essential for creating an intuitive interface. With React Octicon, you have access to a wide range of icons for various purposes. When selecting a specific icon, consider the action or idea it represents and ensure it aligns with the component's function.
For instance, if you're adding an icon to a delete button, you might choose the TrashIcon:
1import { TrashIcon } from '@primer/octicons-react'; 2 3const DeleteButton = ({ onDelete }) => ( 4 <button onClick={onDelete}> 5 <TrashIcon ariaLabel="Delete" /> 6 </button> 7); 8 9export default DeleteButton;
This specific icon clearly communicates the action to be performed when the button clicks.
Creating reusable components is a fundamental practice in React, and utilizing the export default function is a common approach. This pattern allows you to define an icon component that can be effortlessly imported and utilized throughout your application. By exporting the icon component as default, you streamline the import process, making it easy to integrate icons seamlessly.
For example:
1import React from 'react'; 2import { HeartIcon } from '@primer/octicons-react'; 3 4export default function Icon({ size = 'medium', fill = 'currentColor', ariaLabel }) { 5 return <HeartIcon size={size} fill={fill} ariaLabel={ariaLabel} />; 6}
In this example, the Icon component encapsulates the HeartIcon, allowing you to customize the size, fill color, and accessibility label using props. This approach simplifies the integration of icons, ensuring they are both versatile and easy to maintain across your React application.
The fill prop is designed to take a string value that specifies the icon's color. This allows for precise color customization, ensuring your icons fit perfectly within your application's design language.
For example, to set an icon's color to blue, you would pass a hex color value to the fill prop:
1<SyncIcon fill="#007bff" ariaLabel="Sync" /> 2
This customization capability allows you to maintain a consistent look and feel across your application while also enabling you to highlight certain icons when necessary.
When developing web applications, it's essential to consider browser compatibility to ensure your icons look consistent across all platforms. React Octicon icons are SVG-based, which generally have good support across modern browsers, including Microsoft Edge. However, when it comes to older browsers like Internet Explorer, there may be some limitations or additional steps needed to ensure compatibility.
For instance, you might need to include a polyfill or use a fallback method for certain SVG features not supported in Internet Explorer. It's always a good practice to test your icons in the browsers your audience uses most frequently.
SVGs can be used for more than just icons; they can also serve as decorative elements to enhance the visual appeal of your application. When using an SVG for purely decorative purposes, it's essential to ensure that it does not convey any meaningful content that would be missed by users with accessibility needs.
In such cases, you can use the aria-hidden attribute to hide the decorative SVG from screen readers:
1<svg aria-hidden="true"> 2 {/* SVG markup for decorative graphic */} 3</svg> 4
This approach keeps your application accessible while still allowing for creative visual design.
When working with individual icons in React, you may want to apply inline styles for quick, component-specific styling. React Octicon components accept a style prop that lets you use CSS properties directly on the icon. One of the most useful properties is the verticalAlign prop, which ensures your icons vertical align perfectly with surrounding text:
1<AlertIcon style={{ color: 'red', verticalAlign: 'text-bottom' }} ariaLabel="Alert" /> 2
This method is useful for one-off customizations, but for more extensive styling, it's recommended to use CSS classes for better separation of concerns and maintainability.
Sometimes, an icon must stand out as the only child within a component, such as a unique plus icon for adding a new item. In these cases, it's essential to ensure that the icon is both visually distinct and accessible:
1<PlusIcon size="large" fill="green" ariaLabel="Add new item" /> 2
Giving the icon a larger size and a distinct color makes it clear that it serves a special purpose in the interface.
Sometimes, you may need to assign unique identifiers to your icons, especially when dealing with SVGs that include title or desc elements for accessibility. Adding accessibility information is crucial in these cases, and the id prop allows you to set a unique ID on the SVG element:
1<CheckIcon id="check-icon-1" ariaLabel="Completed" /> 2
This id prop can reference the icon elsewhere in your application or provide additional accessibility features, such as aria-labelledby.
The octicon component is reusable and encapsulates an icon from the React Octicon library. By creating reusable icon components, you can ensure consistency and reduce duplication in your codebase:
1const SaveIconComponent = () => <SaveIcon ariaLabel="Save" />; 2 3export default SaveIconComponent; 4
This octicon component can then be imported and used throughout your application wherever a save action is available.
Proper vertical alignment of icons with text is crucial for a polished look. React Octicon components have a verticalAlign prop that allows you to adjust the icon's alignment relative to the surrounding text:
1<ArrowDownIcon verticalAlign="text-bottom" ariaLabel="Expand" /> 2
With the verticalAlign prop, you can ensure that your icons align perfectly with the text's baseline (text-bottom) or top (text-top), depending on your design needs.
The aria-labelledby prop is used to associate an icon with a descriptive element by ID, providing additional context for users of assistive technologies. This is particularly useful when an icon has a complex meaning or is part of a larger interactive component:
1<> 2 <span id="settings-description">Settings</span> 3 <GearIcon aria-labelledby="settings-description" /> 4</> 5
In this example, the GearIcon is linked to the descriptive text "Settings," which helps users understand the icon's purpose.
When dealing with dynamic icon sets where icons may be generated or changed based on user interaction, it's important to manage id values carefully to avoid duplicates. You can use unique identifiers or dynamic generation techniques to ensure that each svg element has a unique ID:
1<StarIcon id={`star-icon-${uniqueId}`} ariaLabel="Starred" /> 2
In this snippet, uniqueId is a variable that would be dynamically generated for each instance of the StarIcon, ensuring that each icon has a unique id prop.
To integrate React Octicon into your project, start by importing React. This setup is essential for creating icon components that seamlessly incorporate Octicons into your application:
1import React from 'react';
This step enables you to leverage React Octicon's capabilities to add icons efficiently.
Enhance accessibility by ensuring icons are keyboard-navigable using the tabIndex prop:
1<SearchIcon tabIndex="0" ariaLabel="Search" />
Setting tabIndex="0" includes the icon in the keyboard navigation flow, making it accessible to all users.
Use aria-labelledby to link an icon with a visible label, providing clear context for screen readers:
1<> 2 <span id="search-label">Search:</span> 3 <SearchIcon aria-labelledby="search-label" /> 4</>
This association aids visually impaired users in understanding the icon's function.
Assign meaningful names to icons with the ariaLabel prop, aiding assistive technologies:
1<PlusIcon ariaLabel="Add new item" />
This ensures that the icon's purpose is clear to all users.
React Octicon components pass props directly to the <svg> element, allowing for extensive customization:
1<MuteIcon className="mute-icon" ariaLabel="Muted" {...props} />
This flexibility enables you to style and adjust functionality as needed.
Control icon size using the size prop for better visibility and emphasis:
1<FlameIcon size="large" ariaLabel="Trending" />
Larger icons are ideal for key UI elements like trending indicators.
Use the verticalAlign prop to manage icon alignment with surrounding text:
1<ArrowUpIcon verticalAlign="middle" ariaLabel="Scroll to top" />
This ensures that icons align properly with adjacent UI elements.
Incorporate Octicons into your UI by using them as standard icon components to render Octicons seamlessly within your application:
1const NavigationMenu = () => ( 2 <nav> 3 <HomeIcon ariaLabel="Home" /> 4 <NotificationIcon ariaLabel="Notifications" /> 5 <SettingsIcon ariaLabel="Settings" /> 6 </nav> 7); 8 9export default NavigationMenu;
Each icon includes an ariaLabel for accessibility, making the menu inclusive for all users.
To keep your application lightweight, import only the icons you need:
1import { ZapIcon } from '@primer/octicons-react';
In conclusion, React Octicon offers a powerful prand flexible way to incorporate icons into your React applications. With its focus on performance, accessibility, and ease of use, React Octicon helps you create user interfaces that are both beautiful and inclusive.
By following the best practices outlined in this blog, such as using the aria label for accessibility, controlling icon size with the size prop, and ensuring proper vertical alignment, you can enhance the user experience of your application. Additionally, importing only the icons you need allows you to keep your application's resulting bundle size small, which is crucial for performance.
Whether you're building a new project or looking to improve an existing one, React Octicon is invaluable in your development toolkit. Embrace the power of these handcrafted icons and take your user interfaces to the next level.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.