Tips for adjusting the icon size within the <IconContext.Provider> dynamically

Currently, I am using the 'IconContext.Provider' tag to style my icons from the 'react-icons' library. Are there any solutions available to dynamically change the size of the icon based on the size of my media?

Is using the global stylesheet the only option for achieving this? I am hesitant to do so as I am new to NextJs and I want to avoid any potential clashes with other styles. This is why I have decided to separate them into modules.

Thank you.

NavBar.Module.Css

@import url("https://fontlibrary.org//face/metropolis");

.siteTitleWrapper {
  box-sizing: border-box;
  display: table-cell;
  vertical-align: middle;
}

.siteTitle {
  font-family: "MetropolisRegular";
  font-weight: 600;
  font-style: normal;
  font-size: 20px;
  letter-spacing: 2px;
  text-transform: uppercase;
  color: #000;
  margin: 0;
  padding-top: 0;
  padding-bottom: 0;
  line-height: 1em;
  white-space: nowrap;
}

.headerInner {
  padding: 20px 0;
  display: table;
  width: 100%;
}

.mainNavigation {
  text-align: right;
  position: relative;
  z-index: 1000;
  display: block;
}

.NavItemsWrapper {
  display: inline-flex;
  align-items: center;
  overflow: hidden;
  cursor: pointer;
}

.NavItems {
  font: "MetropolisRegular";
  font-weight: 600;
  font-style: normal;
  font-size: 13px;
  letter-spacing: 2px;
  text-transform: uppercase;
  text-decoration: none;
}

@media only screen and (max-width: 830px) {
  .mainNavigation {
    -webkit-font-smoothing: subpixel-antialiased;
    display: inline-flexbox;
    position: fixed;
    bottom: 64px;
    left: 0;
    height: auto;
    width: 100%;
    background-color: #f7f7f7;
    overflow: hidden;
    text-align: center;
    vertical-align: top;
  }

  .NavItems {
    display: none;
  }

}

NavBar.tsx

import Link from 'next/link'
import styles from '../styles/components/NavBar.module.css'
import {HiHome} from 'react-icons/hi'
import {FaBloggerB} from 'react-icons/fa'
import {AiOutlineFundProjectionScreen} from 'react-icons/ai'
import {BsPersonBadge} from 'react-icons/bs'
import {IconContext} from 'react-icons'
import { IconType } from 'react-icons/lib'

interface navItemsTypes {
    icon: IconType,
    category: string,
    link: string
}

const navItems : navItemsTypes[] = [
    {icon: HiHome, category: 'Home', link: '/'},
    {icon: AiOutlineFundProjectionScreen, category: 'Projects', link: '/projects'},
    {icon: FaBloggerB, category: 'Blogs', link: '/blogs'},
    {icon: BsPersonBadge, category: 'About', link: '/about'},
]


const NavBar = () => {
    return (
    <div>
        <nav className={styles.headerInner}>
            <div className={styles.siteTitleWrapper}>
            <h1 id="site-title" className={styles.siteTitle} ><Link href='/'>Hong Sheng Yang</Link></h1>
            </div>
            
            <div className={styles.mainNavigation}>
                <IconContext.Provider value={{ size: '1em', style:{marginLeft:'3em',marginRight:'0.5em', color:'#000'}}}>
                    {navItems.map((item) => (
                        <div className={styles.NavItemsWrapper}>
                            <Link href={item.link}>
                                <span style={{display:'flex'}}>
                                    <item.icon/>
                                    <p className={styles.NavItems}>
                                        {item.category}
                                    </p>
                                </span>
                            </Link>
                        </div>
                    ))}

                </IconContext.Provider>             
            </div>
        </nav>   
    </div>
    )
}

export default NavBar

Answer №1

I installed the package "npm i react-responsive" and added the following import statement to my code

import { useMediaQuery } from 'react-responsive'

In addition, I defined the sizes for mobile and desktop like this

const phoneViewNav = {
size: '1em',

}

const DesktopViewNav = {
    size: '1.5em',
}

Finally, I utilized a media query to determine the screen size and applied the appropriate styles

const NavBar = () => {
    
    const isDesktopOrLaptop = useMediaQuery({maxWidth:830})
    const iconSize = isDesktopOrLaptop ? phoneViewNav : DesktopViewNav
    const router = useRouter();
    
    return (
    <div id="nav">
    ....
        <IconContext.Provider value={{ size: iconSize.size, style:{color:"#000"}}}> ...
    </div> )}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

information not visible on the webpage

Recently, I made an alarming discovery on my website. I have implemented two different types of menus - one for the front page (designated as .navbar-inverse) and another for all other pages (.navbar-default). The front page menu is static while the other ...

Two divs positioned to the right on top of each other

I want to align two divs on the right side of their parent div, one above the other. Here's a visual representation: div#top |-------------------------------------||------------| |div#parent ||div#menu | | ...

Bring to life the div that has been clicked

There are three divs labeled as "left", "active", and "right" in my setup: Whenever the left div is clicked, it animates to become active and the previously active one moves into its place. I want this same functionality to apply to the right div as well. ...

"Enhanced interactivity: Hover effects and selection states on an image map

Hello there, I need assistance with my code. Here it is: <img id="body_image" usemap="#body_map" src="assets/images/body.jpg" alt=""> <map name="body_map"> <area shape="poly" alt="d" href="#body_chart" name="ad" coords="153, 153, 145, 1 ...

Adding a border to my image that extends to the edges of the page

.overlay{ position:absolute; top:0; left:0; height:100%; width:100%; background:url(hex-shape.png) no-repeat center; z-index:99999; } I have created an overlay background image that covers the entire page. I want the area surrounding the ove ...

Is feature recognition possible in a jQuery plugin?

Can I integrate a tool similar to Modernizr into my plugin? I want to be able to test for specific CSS3 properties without starting from scratch. ...

Pressing the menu button will trigger a function that halts after the third click

As I attempted to implement the functionality of this left menu bar, I encountered a roadblock. The click event on the menu button seems to work fine initially - the left bar appears with the first click and closes as expected with the second click. Howeve ...

Header and footer that remain in place while the content scrolls smoothly

I'm currently working on designing a webpage that includes three separate divs: a header, a footer, and a content area. These divs are intended to fill up the entire screen space available. The header and footer remain consistent in size and structur ...

Exploring Angular 4.0: How to Loop through Numerous Input Fields

I am looking to loop through several input fields that are defined in two different ways: <input placeholder="Name" name="name" value="x.y"> <input placeholder="Description" name="description" value"x.z"> <!-- And more fields --> or lik ...

I'm curious as to why window.opener is null in a popup when utilizing Google OAuth, and what is the most effective way to transfer the access token to the parent window

In my React application, I am working with Google OAuth and implementing it through a popup. The setup involves using Passport with my own custom backend. I start by supplying the initial URL to the popup window, which is the entry point on my backend that ...

Develop an event listener in a React Component that watches for changes

I have a React component and I am looking to implement an "onChange" event that can be detected by another component in the application. const FirstComponent = () => { // Implement functionality // How can I create an event here that can be detec ...

How to center elements using Bootstrap 4 and flexbox styling

Just starting out in web development. I currently have the following HTML: <section class="boxes"> <div class="container-fluid"> <div class="row"> <div class="col-12 col-lg-4 box1&q ...

Utilizing Material-UI's MaterialTable Component for Custom Sorting and Actions

Here is the code I'm working with: let materialTableRef = React.createRef<MaterialTable<RowData>>(); <MaterialTable title="" columns={getTableDataColumns()} d ...

Error encountered while trying to access `cookies.js` file in the `axios` module located at `../node_modules/axios/lib/helpers/`. The specific

Every time I attempt to retrieve data using axios.get(url), an error is thrown. Despite trying numerous solutions, nothing seems to work and this issue has persisted for quite some time. Any assistance that can be provided would be greatly appreciated. Er ...

Implementing a design aesthetic to installed React components

After downloading a react multiselect component created by an individual, I installed it using npm install .... and incorporated the component in my react code with <MultiSelect .../>. The dropdown checkbox 'menu' is designed to allow users ...

Flexbox divs that adapt to different screen sizes

Hello everyone, I need help with a school project where I have to make blocks responsive and change their layout based on screen width. Specifically, above 1024px they should be in 2 horizontal rows, and below 1024px in 2 vertical rows, always spelling out ...

Learn how to utilize extra classes such as 'fa-fw' in React Font Awesome

When using React-FontAwesome, I imported Font Awesome in the following way: <FontAwesomeIcon icon={['far', calendar]}/>. Now, if I want to add spacing using the class "fa-fw", how can this be accomplished? For more information and examples ...

Difficulty in Rendering Data with Next Js

I'm currently working on integrating data from an API by following the steps outlined in the Next documentation. Unfortunately, I am experiencing difficulties as the data is not rendering. This project marks my first venture into React, so I may be ov ...

The font remains the same despite the <Div style=> tag

I have a script that loads external HTML files, but I am facing an issue with changing the font to Arial. The script is as follows: <script type="text/javascript"> $(document).ready(function(){ $("#page1").click(function(){ ...

What is the Process of Rendering CSS/HTML in Web Browsers?

I have a simple question that I haven't been able to find an answer for despite my efforts on Google and Wikipedia. Perhaps I'm not wording it correctly. My query is regarding how the combination of CSS and HTML is displayed on-screen and in a b ...