4008063323.net

Creating a Responsive Navbar with Shards React in Your App

Written on

Introduction to Shards React

Shards React is an effective UI library that simplifies the process of integrating various components into your React application. In this guide, we will explore how to utilize this library to enhance your application's navigation.

Using the Nav Component

The Nav component enables the creation of navigation elements. Here's a simple example of how to set it up:

import React from "react";

import { Nav, NavItem, NavLink } from "shards-react";

import "bootstrap/dist/css/bootstrap.min.css";

import "shards-ui/dist/css/shards.min.css";

export default function App() {

return (

<Nav>

<NavItem>

<NavLink active>Active</NavLink>

</NavItem>

<NavItem>

<NavLink>Link</NavLink>

</NavItem>

<NavItem>

<NavLink>Link</NavLink>

</NavItem>

<NavItem>

<NavLink disabled>Disabled Link</NavLink>

</NavItem>

</Nav>

);

}

To configure your links as tabs, add the tabs prop:

<Nav tabs>

...

</Nav>

For a pill-style navigation, utilize the pills prop:

<Nav pills>

...

</Nav>

If you want the navbar to span the entire width of the screen, incorporate the fill prop:

<Nav fill>

...

</Nav>

To create evenly spaced navigation items, use the justified prop:

<Nav justified>

...

</Nav>

Implementing the Navbar Component

To integrate a navbar into your application, you can use the Navbar component. Here's an example:

import React, { useState } from "react";

import { faSearch } from "@fortawesome/free-solid-svg-icons";

import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";

import {

Navbar,

NavbarToggler,

NavbarBrand,

Nav,

NavItem,

NavLink,

Dropdown,

DropdownToggle,

DropdownMenu,

DropdownItem,

InputGroup,

InputGroupAddon,

InputGroupText,

FormInput,

Collapse

} from "shards-react";

import "bootstrap/dist/css/bootstrap.min.css";

import "shards-ui/dist/css/shards.min.css";

export default function App() {

const [dropdownOpen, setDropdownOpen] = useState(false);

const [collapseOpen, setCollapseOpen] = useState(false);

const toggleNavbar = () => {

setCollapseOpen(prev => !prev);

};

const toggleDropdown = () => {

setDropdownOpen(prev => !prev);

};

return (

<Navbar>

<NavbarBrand>Shards React</NavbarBrand>

<NavbarToggler onClick={toggleNavbar} />

<Collapse open={collapseOpen} navbar>

<Nav navbar>

<NavItem>

<NavLink active>Active</NavLink>

</NavItem>

<NavItem>

<NavLink disabled>Disabled</NavLink>

</NavItem>

<NavItem>

<Dropdown open={dropdownOpen} toggle={toggleDropdown}>

<DropdownToggle caret>Dropdown</DropdownToggle>

<DropdownMenu>

<DropdownItem>Action</DropdownItem>

<DropdownItem>Another action</DropdownItem>

<DropdownItem>Something else here</DropdownItem>

</DropdownMenu>

</Dropdown>

</NavItem>

</Nav>

<InputGroup>

<InputGroupAddon type="prepend">

<InputGroupText><FontAwesomeIcon icon={faSearch} /></InputGroupText>

</InputGroupAddon>

<FormInput placeholder="Search" />

</InputGroup>

</Collapse>

</Navbar>

);

}

In this setup, the Collapse component is included in the Navbar to make it responsive at the small breakpoint. The navbar prop must be added to both the Collapse and Nav components to ensure they fit correctly within the navbar layout. The Collapse will expand at medium breakpoints or larger, while the InputGroup remains visible across all screen sizes.

Conclusion

In conclusion, Shards React provides a straightforward way to implement navigation bars in your React applications, making your UI more intuitive and user-friendly.

Watch this tutorial on creating a responsive navbar in React using React Router. This beginner-friendly guide will help you understand how to effectively implement navigation in your applications.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Maximizing Productivity: Achieve More in Less Time

Discover effective strategies to enhance productivity and manage your time efficiently.

The Intrinsic Intelligence of Humanity: A Journey Through AI

Explore the dual nature of human creativity and ambition as it reflects in the evolution of artificial intelligence and our collective future.

Exploring the Intricacies of Consciousness: A Philosophical Inquiry

Delve into the complexities of consciousness with insights from David Chalmers and contemporary philosophical discussions.

The Definitive Guide to Mastering Self-Discipline

Discover practical strategies to enhance your self-discipline for achieving personal goals and gaining freedom in your life.

# Unlocking Success on Medium: Your Guide to MPP

Discover effective strategies for thriving in the Medium Partner Program and building your writing audience.

Understanding Water Scarcity and Megadroughts in the American West

An exploration of the impact of megadroughts on water resources and civilization in the American West.

# Understanding the Phenomenon of Quiet Quitting and Its Impacts

Explore the concept of quiet quitting, its implications, and personal insights on overcoming self-doubt and achieving self-acceptance.

Navigating Life Beyond 35: Essential Truths Uncovered

Discover crucial insights that no one shares about life after 35, covering relationships, regrets, and personal growth.