I want to create an expandable side menu which is toggled by clicking the hamburger menu inside "MobileNav". I'm having issues selecting the elements since they're inside seperate documents. How would I go about doing this?
The idea is to add and remove the "hidden" class to hide and show the expanded menu.
App
import React from "react";
import ExpandedNav from "./components/ExpandedNav";
import MobileNav from "./components/MobileNav";
import Navbar from "./components/Navbar";
import "./styles/styles.css";
function App() {
return (
<div className="app-wrapper">
{/* Mobile Nav */}
<MobileNav />
<ExpandedNav />
{/* Main Nav */}
<Navbar />
{/* Routing */}
</div>
);
}
export default App;
Expanded Nav
import React from "react";
function ExpandedNav() {
return <div className="expanded-nav" id="expanded-nav"></div>;
}
export default ExpandedNav;
Mobile Nav
import React from "react";
import { AiOutlineMenu } from "react-icons/ai";
function MobileNav() {
return (
<div className="app-mobile-nav">
<div className="mobile-nav-brand">
<span>Chicken</span>
<div className="mobile-nav-divider" />
<span>Little</span>
</div>
<AiOutlineMenu className="nav-icon" id="mobile-hb-nav" />
</div>
);
}
export default MobileNav;
Expanded Nav SCSS
.expanded-nav {
display: flex;
position: absolute;
top: 0;
right: 0;
bottom: 0;
margin: 15px 20px 15px 0px;
width: 300px;
border-radius: $border-radius;
box-shadow: $box-shadow;
border: $thin-light-border;
background-color: $block-color;
}
.hidden {
display: none; /* responds to click event */
}
There is a better way then toggling CSS classes. Add state in the App file and then forward function for changing state via props to the MobileNav component. Based on the state show or hide expanded component. Something like this:
import React from "react";
import ExpandedNav from "./components/ExpandedNav";
import MobileNav from "./components/MobileNav";
import Navbar from "./components/Navbar";
import "./styles/styles.css";
function App() {
const [isVisible, setIsVisible] = useState(false);
return (
<div className="app-wrapper">
{/* Mobile Nav */}
<MobileNav toggleNav={() => setIsVisible(!visible)}/>
{isVisible && <ExpandedNav />}
{/* Main Nav */}
<Navbar />
{/* Routing */}
</div>
);
}
export default App;
import React from "react";
import { AiOutlineMenu } from "react-icons/ai";
function MobileNav({toggleNav}) {
return (
// Add onClick function to the element that you need, this is only
// for example
<div className="app-mobile-nav" onClick={toggleNav}>
<div className="mobile-nav-brand">
<span>Chicken</span>
<div className="mobile-nav-divider" />
<span>Little</span>
</div>
<AiOutlineMenu className="nav-icon" id="mobile-hb-nav" />
</div>
);
}
export default MobileNav;
Related
This question already exists:
How to Change footer value inside antd layout when i click on button inside any route based component in reactjs
Closed 1 year ago.
I have used antd Layout with footer for Reactjs application. How to change value of footer when I click on button inside numbers component. I have used antd layout like this
https://ant.design/components/layout/
Code For Above Functionality Like:
When i click on button how to display Input value on Footer Label.
Mainly Two component Like :
WebSiteLayout.js (Footer inside them)
NumberList.js (Button and Input value inside them)
How to pass value from NumberList.js component to WebSiteLayout.js
WebSiteLayout.js
import React from "react";
import { withRouter, Switch, Link } from "react-router-dom";
import NumbersList from "../pages/Numbers/NumbersList";
import AuthenticatedRoute from '../../AuthenticatedRoute';
import './WebLayout.css';
import '../../../../assets/css/app.css';
import { Layout, Button, Menu, Popover, Spin, Select, Checkbox, Row, Col, Form, notification } from 'antd';
const { Header, Sider, Content, Footer } = Layout;
const { SubMenu } = Menu;
import {
PhoneOutlined,
} from '#ant-design/icons';
const WebSiteLayout = ({ location, row }) => {
return (
<Layout>
<Sider trigger={null} collapsible collapsed={collapsed}>
<div className="topbar1">
<div className="topbar-left">
</div>
</div>
<Menu theme="dark" mode="inline">
<Menu.Item key="/numbers" icon={<PhoneOutlined style={{ fontSize: '18px' }} />} title="Numbers" >
<span>Numbers</span>
<Link to="/numbers" />
</Menu.Item>
</Menu>
</Sider>
<Layout className="site-layout" hasSider={true} style={{ height: "100vh" }}>
<Content
className="site-layout-background"
style={{
padding: 24,
height: '80px',
minHeight: 280,
}}
>
<Switch>
<AuthenticatedRoute path='/numbers' component={NumbersList} />
</Switch>
<Footer style={{ position: "fixed", bottom: "0px", right: "0px", background: "#c2c2c2", padding: "15px 25px" }}>
Display Numbers Field Value
</Footer>
</Content>
</Layout>
</Layout>
);
};
export default withRouter(WebSiteLayout)
NumberList.js
import React, {Component} from 'react';
import 'antd/dist/antd.css';
import { withRouter } from "react-router";
import { Button, Input } from 'antd';
class NumbersList extends Component {
constructor(props) {
super(props);
this.state = {
value:''
}
}
sendValueToFooter = () => {
console.log(this.state.value);
}
render() {
return (
<div class="container-fluid">
<h4 class="page-title float-left">Numbers</h4>
<Input placeholder="Basic usage" value={this.state.value} onChange={(e)=>{this.setState({value : e.target.value})}}/>
<br></br>
<Button type="primary" onClick={this.sendValueToFooter}>
Click Here To Send Value On Footer
</Button>
</div>
)
}
}
export default withRouter(NumbersList)
When I click on button how to display Input value on Footer Label.
I have the following structure
<button>
<OtherComponent />
</button>
OtherComponent just gives a < span /> with an icon, with its own styles.
I want to pass a style to button to override OtherComponent's styles, eg. set the margin to 0. I've tried (doesn't work)
<button style={{ 'span': { margin: 0 }}}>
<OtherComponent />
</button>
Inline style only affects the current element that you are applying to
If you want to style a child element then you should select it from a CSS file.
So in your parent component you would do:
import React from "react";
import { OtherComponent } from "./components/OtherComponent";
import "./styles.css";
export default function App() {
return (
<div className="App">
<OtherComponent />
</div>
);
}
And then in styles.css file:
.App span {
margin: 0;
}
Complete Example
A simple way is to add div/Button in backend and override style like style={{ marginLeft: "100px" }} with double curle braces.
Parent Component
import "./styles.css";
import OtherComponent from "./OtherComponent";
export default function App() {
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<button style={{ marginLeft: "100px" }}>
<OtherComponent />
</button>
<h2>Start editing to see some magic happen!</h2>
</div>
);
}
Child Component
import React from "react";
import "./buttonStyle.css";
class OtherComponent extends React.Component {
render() {
return <button className="button">Style Button</button>;
}
}
export default OtherComponent;
CodeSandBox Live Demo
i'm new to react and im trying to add render a list of items from an external SidebarData.js file (in the same root /components/..)
i'm not sure why my map function is not returning anything.
i get a list of elements thats correct, but the item.title and item.path seem not to render...
I feel there's a problem with the props.
I tried to write just
render(){
<h1>{SubmenuData[1].title}</h1>
}
and it works fine, but when i try to map on the full array, it doesn't seem to render anything. it renders the correct number of elements, but the title and path are not returning...
Here's my two components : Sidebar (Main one)
import React from 'react'
import styled from 'styled-components'
import { SidebarData } from './SidebarData'
import Submenu from './Submenu'
const Nav = styled.div`
background: #f5f5f5;
color: #7d7d7d;
display:flex;
justify-content:flex-start;
height:100%;
width:15%;
`
const Sidebar = () => {
return (
<>
<Nav>
{SidebarData.map((item, index)=>{
return <Submenu item={item} key={item.index} />
})}
</Nav>
</>
)
}
export default Sidebar (Where i think there's a problem)
and Submenu
import React, { Component } from 'react'
import styled from 'styled-components'
import { Link } from "react-router-dom"
const SidebarLink = styled(Link)`
display: flex;
color: #404040;
`
const SidebarLabel = styled.span`
color:#000;
`
const Submenu = (item)=>{
return (
<SidebarLink to={item.path} >
<SidebarLabel>{item.title}</SidebarLabel>
</SidebarLink>
)
}
export default Submenu
Your style of receiving props is mistake i guess. Destructure the props like:
const Submenu = ({item})=>{
return (
<SidebarLink to={item.path} >
<SidebarLabel>{item.title}</SidebarLabel>
</SidebarLink>
)
}
export default Submenu
this is my component code
import React from 'react';
import Aux from '../../hoc/Aux';
import classes from './Layout.css';
const layout = (props) => (
<Aux>
<div>toolbar, sideshow, backdrop</div>
<main className = {classes.Content}>
{props.children}
</main>
</Aux>
);
export default layout;
in this {props.children} just the <p> Hi </p> and this is my css file
.Content{
margin: 2% auto;
}
thi
why the style of the classes.Content not working?
I'm trying to learn Styled Components - It's great, and everything works awesome, but I can't get an <a> to be styled
This is my styled-component from StyledCSS.js:
...............
export const HeadLink = styled.a`
color: pink,
text-decoration: none,
`;
...............
Here is my react component
import React from 'react';
import {
HeadCon,
HeadLink,
Container,
Branding,
Nav,
UL,
LI,
} from './StyledCSS';
function Header() {
return (
<HeadCon colorBG="#35424a">
<Container>
<Branding>
<h1>Acme Web Design</h1>
</Branding>
<Nav>
<UL>
<LI>
<HeadLink href="index.html">About</HeadLink>
</LI>
<LI>
<HeadLink href="index.html">Contact</HeadLink>
</LI>
<LI>
<HeadLink href="index.html">Weather</HeadLink>
</LI>
</UL>
</Nav>
</Container>
</HeadCon>
);
}
export default Header;
You should be using semi-colons inside the styled object—not commas. Here is a working example.
import React from "react";
import "./styles.css";
import { HeadLink } from "./HeadLink";
export default function App() {
return (
<div className="App">
<HeadLink href="index.html">About</HeadLink>
</div>
);
}
Headlinks.js
import styled from "styled-components";
export const HeadLink = styled.a`
color: pink;
text-decoration: none;
`;
CodeSandbox