Edit: CodeSandbox link: https://codesandbox.io/s/strange-dawn-q9zow
I'm creating a React app that I want to have a collapsible sidebar. I'm using the transition collapse utility from React-Bootstrap. Since this is for a sidebar, I'm using dimension="width". I've tried adding a transition to a few elements including the .width class as it says in the React-Bootstrap documentation, but the animation is still really choppy/not smooth. Am I missing a class, or is there something additional I need to do with the CSS?
JSX:
import React, {useState} from 'react';
import Button from 'react-bootstrap/Button';
import Collapse from 'react-bootstrap/Collapse';
import {
BrowserRouter as Router,
Switch,
Route,
Link,
Redirect,
useHistory,
useLocation
} from "react-router-dom";
import '../../custom_styles/sidebar.css';
function Visualizer() {
const [open, setOpen] = useState(false);
return (
<Router>
<div className="container-fluid h-100 p-0">
<div className="row h-100 no-gutters">
<Collapse id="sidebar" dimension="width" in={open}>
<div className="col-4 bg-dark text-white" id="example-collapse-text">
<h2>Palette</h2>
</div>
</Collapse>
<div className="col-8">
<Button
className="float-left ml-n1"
variant="dark"
onClick={() => setOpen(!open)}
aria-controls="example-collapse-text"
aria-expanded={open}
>
<div id="collapse-btn-text">Toggle Sidebar</div>
</Button>
</div>
</div>
</div>
</Router>
)
}
export default Visualizer;
CSS:
#sidebar {
transition: width 2s;
}
.width {
transition: width 2s;
}
.show {
transition: width 2s;
}
You can try to use this css
#sidebar {
transition: width 2s ease;
display: block;
width: 0;
}
#sidebar.yourActiveClass {
width: auto;
}
But first when you click on
<div id="collapse-btn-text">Toggle Sidebar</div>
button you shoule add "yourActiveClass" in
<Collapse id="sidebar" dimension="width" className={true ? "collapse width yourActiveClass" : "collapse width"} in={open}>
true means your Click function if true
using this you can more smooth your section.
Related
How to make the selected div element to be 100% in the inspect element tool in the picture below. I am using display:flex on .dvPageNotFound class and I want to apply height:100% on the div element inside it. If we put display:flex on parent element then all the child elements gets stretched by default, but here I don't get my child element stretched. I don't know why? I am using 12 column grid system same as bootstrap 4 grid Any help would be appreciated.
HERE IS THE CODE -
import React from 'react';
import { Link } from 'react-router-dom';
const PageNotFound = () => {
return (
<>
<div className="dvPageNotFound d-flex">
<div class='col-12'>
<h1 className="heading-lg">Page Not Found</h1>
<p className="my-3">The page you are looking for we coudn't found.</p>
<Link to="/" className="btn btn-black">
Back to Homepage
</Link>
</div>
</div>
</>
);
};
export default PageNotFound;
This worked for me. I added height: 100vh and added m-auto classes which we get from the 12 column grid.
import React from 'react';
import { Link } from 'react-router-dom';
const PageNotFound = () => {
return (
<>
<div className="dvPageNotFound d-flex justify-content-center align-items-center" style={{ height: '100vh' }}>
<div className="m-auto">
<h1 className="heading-lg">Page Not Found</h1>
<p className="my-3">The page you are looking for we coudn't found.</p>
<Link to="/" className="btn btn-black">
Back to Homepage
</Link>
</div>
</div>
</>
);
};
export default PageNotFound;
I have a button with send and on clicking, I want to move it, (this is just for asking) can you tell me why it is not working
This is the jsx
import React from 'react'
import './home.css'
import sound from './sound.mp3'
import video from './A.mp4';
import img1 from './home1.jpeg';
import img2 from './home4.jpeg';
import img3 from './home3.jpeg';
import { useRef } from 'react';
return (
<>
<div className="form" onClick={prevent} >
<form action="" className='contact_form'>
<div className="heading_contact">
Get in touch with me
</div>
<input className="info_contact" placeholder='Your Name' ></input>
<input className="info_contact" placeholder='Your Email' ></input>
<textarea className='info_contact' name="" id="" cols="27" rows="10" placeholder='You message'></textarea>
<button id='delivery' className="submit" >
<i class="fa-solid fa-truck" ></i>
</button>
</form>
<div className='alternates' >
Or contact me through
</div>
</div>
</>
)
}
And this is the CSS for the animation
#delivery:active {
position: relative;
animation-name: delivery;
animation-duration: 15s;
animation-iteration-count: 5;
#keyframes delivery{
from {
margin-right: 0px;
}
to {
margin-right: 100px;
}
}
}
I also tried to bring the keyframes section out of the :active selector but it still won't work.
I have an array of divs containing book names that I generate using a .map function on some fetched data from my database. Some of the book names are too long so I have used the following CSS to hide them:
.book-title{
font-weight: 600;
height: 1.5rem;
max-width: 15rem;
overflow: hidden;
padding-top: 0.7rem;
}
But I would like to make the text visible when user hovers over the div. My current solution applies to all divs generated and displayed by the .map function - I am unsure how to make it just apply to divs where the text is too long.
.book-title:hover{
height: 6rem;
overflow: visible;
}
Is there a proper way to do this with react?
here is the relevant .jsx
const listsForFrontPage = listArray.map((book, id) => {
return (
<Link className="book-link" to={`/books/${book.book_id}`} key={id}>
<div className="book-card">
<div className="book-card-image">
<img
className="cover-image"
src={book.cover_image}
alt="The book cover"
/>
</div>
<div className="book-info">
<div className="book-title">{book.title}</div>
<div className="author-name">{book.name}</div>
</div>
</div>
</Link>
)
})
You need to check if the element is overflowing
add useRef:
const refs = useRef(listArray.map(() => React.createRef()));
then you should create a function that checks if the element is overflowing:
const isOverflown = (el) => {
if(!el) return;
const { clientWidth, clientHeight, scrollWidth, scrollHeight } = el;
return scrollHeight > clientHeight || scrollWidth > clientWidth;
}
and then you need to add class based on isOverflown value:
const listsForFrontPage = listArray.map((book, id) => {
return (
<Link className="book-link" to={`/books/${book.book_id}`} key={id}>
<div className="book-card">
<div className="book-card-image">
<img
className="cover-image"
src={book.cover_image}
alt="The book cover"
/>
</div>
<div className="book-info">
<div ref={refs.current[id]} className={`book-title ${isOverflown(refs.current[id].current) ? 'book-title-overflow' : ''}`}>{book.title}</div>
<div className="author-name">{book.name}</div>
</div>
</div>
</Link>
)
})
don't forget to add your css:
.book-title-overflow:hover{
height: 6rem;
overflow: visible;
}
I have a very simple div in my footer:
import React from "react";
import { useHistory } from "react-router-dom";
import style from "./DesktopFooter.module.css";
const DesktopFooter = () => {
const history = useHistory();
return (
<div className={style.container}>
<div className={style.footerNav} id="phoneNumber">
999-999-9999
</div>
<div className={style.footerNav}>
</div>
<div className={style.footerNav}></div>
</div>
);
};
export default DesktopFooter;
In my CSS I want to style both on the class and id:
.footerNav {
width: 50%;
display: flex;
}
#phoneNumber {
font-size: 2rem;
}
However my component is not recognizing the id styling I try to apply.
Could anyone point me in the right direction.
Ya, After you update a question, I found the issue, you are try to load style for id as normal Dom, but its will not work since you are not include css file as is, you are import style file to style param...
so, what you need to do is replace id="PhoneNumber" to this
<div className={styles["footerNav"]} id={styles["phoneNumber"]}>
999-999-9999
</div>
Check the demo url
Full Code:
import React from "react";
import style from "./MyComponent.module.css";
export default function App() {
return (
<div className={style.container}>
<div className={style["footerNav"]} id={style["phoneNumber"]}>
999-999-9999
</div>
<div className={style.footerNav}></div>
<div className={style.footerNav}></div>
</div>
);
}
I would suggest having it as either a class or an id. Classes usually have a higher priority, meaning that the id will be ignored.
This is what it should look like:
.phoneNumber {
font-size: 2rem;
width: 50%;
display: flex;
}
<div class = "phoneNumber">999-999-9999</div>
However, if you would like to get around this, I would use another element within the div, such as:
#myID{
font-size: 2rem;
}
.phoneNumber {
width: 50%;
display: flex;
}
<div class = "phoneNumber">
<p id="myID"> 999-999-999 </p>
</div>
Ok I found a solution, or at least a work around. The problem seems to be that the file was a css module and not just a css file.
I changed the name of the css file from DesktopFooter.module.css to DesktopFooter.css
and I changed my import statement from
import style from "./DesktopFooter.module.css" to import "./DesktopFooter.css
I've currently got a modal set up, where once it's pressed, it opens a component of mine up. However, this component has a fixed css (which I want to keep), but when the modal is pressed, I want that CSS to change. Is this possible ? I've looked at a few things but can't seem to find something that works. What I've got currently can be seen below, and it doesn't override the original css.
const [currentModal, openModal] = useState(null);
const [appState, changeChange] = useState({
activeObject: null,
objects: [{id: 1}]
in the return
<div className="modal">
{currentModal === 'modal1' ?
<MyComponent>
</MyComponent>
: null}
<div class="class1">
{appState.objects.map((elements,index) => (
<button key={index} classnName="box active"
onClick={() => openModal('modal1')}>
<Modal1></Modal1>
</button>
))}
</div>
The CSS
.box{
width:200px;
height:200px;
margin:10px;
transition: background-color 0.1s ease-in-out;
}
.inactive{
background-color:pink;
}
.active {
background-color: blue;
width:200px;
height:200px;
margin:10px;
}
Use styled components: https://styled-components.com/docs/basics#adapting-based-on-props
Just pass a prop to the styled component wrapping your model. this prop will indicate if the modal has been pressed or not.
You'll have to change the css according to the prop value.