How to fit React Modal size children? - css

I am trying to load a dynamic login page with React Modal. Is there a way to make React Modal resize to the size of its child elements?
//App.js
import "./styles/app.scss";
import Layout from './options/Layout'
import Login from './Components/Login'
import { useState } from "react";
import Modal from 'react-modal'
function App() {
const [OpenModal, setOpenModal] = useState(false)
return (
<div id="app" className="App">
<Layout>
<button onClick = {() => setOpenModal(true)}>open modal</button>
<Modal isOpen = {OpenModal}>
<Login/>
</Modal>
</Layout>
</div>
);
}
export default App;
//login.scss
.login {
display: flex;
justify-content: center;
align-items: center;
width: 300px;
height: 280px;
margin: 0 auto;
background-color: #5c8fc2;
border-radius: 30px;
}
.login_top {
display: flex;
align-items: center;
justify-content: center;
margin-top: -40px;
margin-bottom: 20px;
color: whitesmoke;
}
.login_register_container {
border: none;
outline: none;
border-radius: 50%;
}
button {
margin-right: 12px;
margin-top: 30px;
border: none;
border-radius: 4px;
cursor: pointer;
}
I want to dynamically manage the size according to the child element.
I've tried several methods, but without success. How to fit modal's component to child element???

If you use the developer tools you can see that the content grows because they have applied some styles to the modal.
position: absolute;
inset:40
inset is same as
top: 40px;
left: 40px;
right: 40px;
bottom: 40px;
If you want your content to be exactly the same size as your content, you can remove the right and bottom from the styles.
<ReactModal
isOpen={true}
contentLabel="Minimal Modal Example"
className="Modal"
>
<div>
<div style={{ height: '500px' }}>Some content</div>
</div>
<button onClick={() => {}}>Close Modal</button>
</ReactModal>
CSS:
.Modal {
position: absolute;
top: 40px;
left: 40px;
background-color: papayawhip;
}
You will lose equal spacing on all sides of the modal if you do this though.
Here is an example. https://stackblitz.com/edit/react-vxe3cn

Related

Why does Google's inspector mobile tool render content different than my iphone

I have a pretty simple website, which renders a background image, a header, a footer, and some body text.
When I view the site from Google Dev Tools mobile inspector. The text sits above the footer as expected. When I view the site from an actual iphone the text is shown at the very bottom of the page (behind the footer).
App.js
return (
<div className={style.app}>
<Header setNav={setNav} nav={nav} />
<Routes>
<Route
exact
path="/"
element={
<Mobile
setNav={setNav}
nav={nav}
menuList={menuList}
setFilter={setFilter}
/>
}
/>
<Route
exact
path="/directions"
element={
<Directions
setNav={setNav}
nav={nav}
filter={filter}
setFilter={setFilter}
menuList={menuList}
/>
}
/>
</Routes>
<MobileNavOneButton setNav={setNav} setFilter={setFilter} />
</div>
);
the css for style.app is:
.app {
background-image: url("../CompanyImages/Port507BackgroundImage.jpg");
background-repeat: no-repeat;
background-attachment: fixed;
position: fixed;
height: 100vh;
width: 100vw;
max-height: 100vh;
max-width: 100vw;
background-size: cover;
z-index: -999;
}
Mobile.js
import React from "react";
import style from "./Mobile.module.css";
const Mobile = () => {
return (
<div className={style.app}>
<div className={style.mainContent}>
<div className={style.address}>128 W 2nd St, Winona, MN 55987</div>
</div>
</div>
);
};
export default Mobile;
css for mobile.js
html,
body {
margin: 0;
/* height: 100% */
}
.app {
height: calc(100vh - 8rem);
width: 100vw;
background-size: cover;
display: flex;
justify-content: center;
margin-top: 5rem;
}
.mainContent {
text-align: center;
color: white;
width: 100%;
display: flex;
align-self: flex-end;
margin-bottom: 1.5rem;
margin-left: 1rem;
}
Footer.js
import React from "react";
import { useNavigate } from "react-router-dom";
import style from "./MobileNavOneButton.module.css";
import HomeIcon from "#mui/icons-material/Home";
import ExploreIcon from "#mui/icons-material/Explore";
const MobileNavOneButton = ({ setNav }) => {
const navigate = useNavigate();
return (
<div className={style.NavBar}>
<div
className={style.NavButton}
onClick={() => {
navigate("../directions", { replace: true });
setNav(false);
}}
>
<ExploreIcon style={{ fontSize: "1.25rem", color: "#000000" }} />
<div className={style.buttonText}>Directions</div>
</div>
</div>
);
};
export default MobileNavOneButton;
css for the footer
.buttonText {
font-size: 0.75rem;
font-family: Montserrat-Light;
text-decoration: none;
color: black;
}
.NavBar {
width: 100%;
float: none;
display: flex;
height: 3rem;
overflow: hidden;
position: fixed;
bottom: 0;
z-index: 999;
background-color: rgba(150, 150, 150, 0.8);
}
.NavButton {
align-items: center;
justify-content: center;
/* border: 1px solid black; */
/* background-color: rgba(150, 150, 150, 0.8); */
border-radius: 0;
float: left;
display: flex;
flex-direction: column;
font-size: 1rem;
font-family: Montserrat-Light;
text-align: center;
width: 100%;
text-decoration: none;
color: #222222;
}
.NavButton:focus {
outline: none !important;
outline-offset: none !important;
}
The website can be seen here. Note: you will be redirected if viewing from a browser (need to be in Dev Tools mobile)
Website

When using a media query, target tag does not disappear

Hi I think this is very simple question so I apologize in advance for not being able to resolve it myself. I'm trying to make tag disappear by media query when width of window becomes less than 1200px. But it doesn't disappear. I think it's a matter of inheritance. I'd appreciate if you let me know how to solve this.
this is NaviSearch.jsx file. I want to make top tag disappear
import React from "react";
import "../navi.css";
import pencil from "./images/pencil.png";
import { Link } from "react-router-dom";
function NaviRecent (props) {
return (
<Link to="/login" style={{textDecoration : 'none'}}>
<button id="NaviRecent">
<img src={pencil} id="NaviRecentImage"/>
<span id="NaviRecentText">최근강의</span>
</button>
</Link>
);
}
export default NaviRecent;
and this file is css file. I only brought what seemed relevant
#Navi {
position: sticky;
background-color: #fff;
width: 100%;
height: 64px;
top: -1px;
z-index: 5;
box-shadow: 0 2px 4px 0 hsl(0deg 0% 81% / 50%);
width: 1263.330;
height: 64px;
display: flex;
align-items: center;
justify-content: space-between;
}
#NaviRecent {
border: none;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
position: relative;
right: 20px;
width: 100px;
height: 30px;
padding: 8px;
background: #00c471;
color: #fff;
border-radius: 4px;
}
#NaviHiddenLogo {
display: none;
}
#NaviRecentText {
box-sizing: border-box;
color: white;
cursor: pointer;
font-family: Pretendard,-apple-system,BlinkMacSystemFont,system-ui,Roboto,Helvetica Neue,Segoe UI,Apple SD Gothic Neo,Noto Sans KR,Malgun Gothic,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,sans-serif;
font-size: 16px;
font-weight: 600;
margin-left: 4px;
}
#NaviRecentImage {
width: 15px;
height: 15px;
}
#media screen and (max-width: 1020px) {
#NaviRecent {
display: none;
}
}
you should wrap into a tag like this :
import React from "react";
import "../navi.css";
import pencil from "./images/pencil.png";
import { BrowserRouter as Router, Link } from "react-router-dom";
function NaviRecent(props) {
return (
<Router>
<Link to="/login" style={{ textDecoration: "none" }}>
<button id="NaviRecent">
<img src={pencil} id="NaviRecentImage"/>
<span id="NaviRecentText">최근강의</span>
</button>
</Link>
</Router>
);
}
export default NaviRecent;
Be carefull, in your css file max-width is 1020px and not 1200px as you mentioned in your statement.
Hope it will help.

react.js text div wont scale with my page but the image div will

I am doing this website for my full stack web development course but I am having a bit of trouble with my css. I am wondering how I can get the text Div to scale with the page.
the div looks like this zoomed in,
and it looks like this zoomed out,
the text div always wants to stay the same size.
the css is
.waitingMainDiv {
width: 70%;
display: flex;
justify-content: center;
align-items: center;
margin: auto;
padding-top: 80px;
padding-bottom: 80px;
}
.waitingLeftDiv {
width: 100%;
height: auto;
}
.waitingLeftDivImage {
width: 100%;
height: auto;
}
.waitingRightDiv {
width: 100%;
height: auto;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.waitingTextDiv {
width: 85%;
color: grey;
}
.waitingTextDiv h2 {
font-family: nunito;
font-weight: 700;
font-size: 2.3em;
margin: 4%;
}
.waitingTextDiv h3 {
font-family: nunito;
margin: 30px;
}
.waitingTextDiv p {
font-family: nunito;
margin: 30px;
font-size: 1.2em;
}
.waitingButtonDiv {
display: flex;
justify-content: flex-start;
width: 85%;
margin-left: 50px;
}
.waitingButtonDiv button {
width: 180px;
border-radius: 8px;
box-shadow: 0px 2px 5px rgb(0, 0, 0, 0.4);
font-family: nunito;
font-weight: 800;
}
.waitingEnquireButton {
border: 2px solid #43c0f6;
background-color: white;
color: #707070;
margin-right: 100px;
}
.waitingSignUpButton {
border: 2px solid #f91c85;
background-color: #f91c85;
color: white;
}
and the react.js is
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js">
import teacherPicture from "../../../../img/teacherPicture.png";
import React, { useState } from "react";
import "./Waiting.css";
import NavModal from "../../../Nav/NavModal";
export default function Waiting() {
const [open, setOpen] = useState(false);
const handleOpen = () => {
setOpen(true);
};
const handleClose = () => {
setOpen(false);
};
return (
<div className="waitingMainContainer">
<div className="waitingMainDiv">
<div className="waitingLeftDiv">
<img className="waitingLeftDivImage" src={teacherPicture} alt="" />
</div>
<div className="waitingRightDiv">
<div className="waitingTextDiv">
<h2>What are you waiting for?</h2>
<h3>Start teaching Digital Technologies today.</h3>
<p>
If you need more information, we are happy to answer any questions
you may have.
</p>
</div>
<div className="waitingButtonDiv">
<button className="waitingEnquireButton">ENQUIRE NOW</button>
<button
onClick={() => handleOpen()}
className="waitingSignUpButton"
>
SIGN UP
</button>
<NavModal open={open} handleClose={handleClose} />
</div>
</div>
</div>
</div>
);
}
</script>
Thankyou to those who review my code, its definitely a simple fix but im driving myself crazy over it.
Give your main div a width, not %;
.waitingMainDiv {
width: 70vw; //changed
display: flex;
justify-content: center;
align-items: center;
margin: auto;
padding-top: 80px;
padding-bottom: 80px;
}

Some CSS Class selectors don't work in React

I'm working on some basic styling for my personal project, and for some reason, most of the class selectors in my css file Header.css works, except headerOptionCount. Earlier, some other classes weren't working, but putting Header.css in a styles directory seemed to help, but now it is happening again. When changing margin-left and margin-right of headerOptionCount, nothing changes. When inspecting the element in dev tools, the styles dont show up at all.
Header.css:
.header {
height: 60px;
display: flex;
align-items: center;
background-color: #131921;
position: sticky;
top: 0;
z-index: 100;
}
.headerLogo {
width: 60px;
object-fit: contain;
margin: 0 10px;
}
.headerSearch {
display: flex;
flex: 1;
align-items: center;
border-radius: 24px;
}
.headerSearchInput {
width: 100%;
height: 12px;
padding: 10px;
border: none;
}
.headerSearchIcon {
padding: 5px;
height: 22px !important;
background-color: tomato;
}
.headerOptionLineOne {
font-size: 10px;
}
.headerOptionLineTwo {
font-size: 13px;
font-weight: 800;
}
.headerOptionBasket {
display: flex;
align-items: center;
color: white;
}
.headerOptionCount {
margin-left: 10px;
margin-right: 10px;
}
.headerNav {
display: flex;
justify-content: space-evenly;
}
.headerOption {
display: flex;
flex-direction: column;
margin-left: 10px;
margin-right: 10px;
color: white;
}
Here is my Header component that imports this css file.
Header.js:
import React from "react";
import logo from "../zipshopIcon.png";
import { Search, ShoppingCart } from "#material-ui/icons";
import "../styles/Header.css";
const Header = () => {
return (
<div className="header">
<img className="headerLogo" src={logo} alt="Logo" />
<div className="headerSearch">
<input className="headerSearchInput" type="text" />
{/* Logo */}
<Search className="headerSearchIcon" />
</div>
<div className="headerNav">
<div className="headerOption">
<span className="headerOptionLineOne">Hello Guest</span>
<span className="headerOptionLineTwo">Sign In</span>
</div>
<div className="headerOption">
<span className="headerOptionLineOne">Returns</span>
<span className="headerOptionLineTwo">& Orders</span>
</div>
<div className="headerOption">
<span className="headerOptionLineOne">Your</span>
<span className="headerOptionLineTwo">Prime</span>
</div>
<div className="headerOptionBasket">
<ShoppingCart />
<span className="headerOptionTwo headerBasketCount">0</span>
</div>
</div>
</div>
);
};
export default Header;
The import directory is definitely correct since the other styles work. I've tried using css modules, but it didnt fix anything. Should i try using scss? it just baffles me why class selectors cant even work when imported as css files. Any advice would be appreciated, thanks!
None of your elements in the js have the classname headerOptionCount, you need to give the element you want to have those styles the classname for it to work.

changing CSS of element on page scroll

I have a fixed header component as the two pics below illustrate. The current styling that I have is fine for when the page loads and nothing has changed. However, I want the header to have a visible border-bottom of say 1px solid black as soon as the user starts to scroll down the page. So in this case, this styling change would apply to the second pic. How can I accomplish this?
header on page load:
header on scroll:
Header.js:
const Header = props => {
return (
<header className="header-container">
<div className="logo-container">
<img className="white-logo" src={logo} alt="Food Truck TrackR logo white" />
</div>
<section className="header-section-one">
<div className="location-sub-div">
<i class="fas fa-map-marker-alt"></i>
<h3>User Location</h3>
</div>
<div className="order-sub-div">
<i class="fas fa-store"></i>
<h3>Order now</h3>
</div>
</section>
<section className="header-section-two">
<NavLink to="/dine/search" className="search-sub-div">
<i class="fas fa-search search-icon"></i>
<h3>Search</h3>
</NavLink>
<div className="acct-sub-div">
<i class="fas fa-user acct-icon"></i>
<h3>Account</h3>
</div>
</section>
</header>
)
}
Header.scss:
.header-container {
width: 100%;
height: 9vh;
display: flex;
align-items: center;
color: black;
background: white;
font-size: 0.6rem;
padding-left: 4%;
padding-right: 4%;
position: fixed;
top: 0;
// border-bottom: 1px solid black;
z-index: 99;
}
.logo-container {
width: 20%;
margin-right: 6%;
.white-logo {
width: 100%;
}
}
.header-section-one {
width: 32%;
display: flex;
margin-right: 25%;
justify-content: space-evenly;
.location-sub-div {
display: flex;
h3 {
width: 100%;
white-space: nowrap;
}
}
.order-sub-div {
display: flex;
h3 {
white-space: nowrap;
}
}
}
.header-section-two {
width: 32%;
display: flex;
justify-content: space-evenly;
.search-sub-div {
display: flex;
.search-icon {
margin-right: 1%;
}
}
.acct-sub-div {
display: flex;
.acct-icon {
margin-right: 1%;
}
}
}
i {
margin-right: 1% !important;
}
This neat CSS trick might help you with the problem:
html:not([data-scroll='0']) {
.header-container {
width: 100%;
height: 9vh;
display: flex;
align-items: center;
color: black;
background: white;
font-size: 0.6rem;
padding-left: 4%;
padding-right: 4%;
position: fixed;
top: 0;
border-bottom: 1px solid black;
z-index: 99;
}
}
Here is a, link further explaining the solution: https://css-tricks.com/styling-based-on-scroll-position/
Good luck.
You can add an event listener to the document, and when a scroll event is fired, you can check to see what the vertical scroll position (scrollTop) of the document is, and conditionally show a border based on that value.
Here's an example:
import React, { useEffect, useState } from "react";
const Header = () => {
// Store a bool that determines if the border is visible
const [isBorderVisible, setIsBorderVisible] = useState(false);
useEffect(() => {
// Define a function that is called when the scroll event fires
const handleScroll = e => {
const scrollTop = e.target.documentElement.scrollTop;
if (scrollTop > 200) {
setIsBorderVisible(true);
} else {
setIsBorderVisible(false);
}
};
// Add the event listener inside a useEffect
if (document) {
document.addEventListener("scroll", handleScroll);
}
// Remove the event listener on unmount
return () => {
if (document) {
document.removeEventListener("scroll", handleScroll);
}
};
}, [setIsBorderVisible]);
return (
<div
style={{
position: "fixed",
top: 0,
left: 0,
right: 0,
height: "100px",
background: "hotpink",
// Conditionally style the border
borderBottom: isBorderVisible ? "2px solid #000" : "0"
}}
/>
);
};
So we have a useEffect, that adds an event listener to the document, listening for the scroll event. Whenever a scroll occurs, the handleScroll function (also defined inside the useEffect) fires.
In this function, we get the scrollTop value, which is the number of pixels that have been scrolled from the top of the document.
In the example, we are setting the state value isBorderVisible to true once we have a scrollTop greater than 200 pixels, but this can be anything you want.
In the header's style, we conditionally set a border, based on the state value of isBorderVisible.

Resources