Don't let flex to wrap label text of toggle - css

I have the following code (with "#fluentui/react": "^8.33.0",)
import React from 'react';
import { Toggle } from '#fluentui/react/lib/Toggle';
class TestIt extends React.Component {
render() {
return (
<div style={{ display: 'flex', flexWrap: 'wrap' }}>
<Toggle onText="Width Limit Auto" offText="Width Limit Manual"/>
</div>
)
}
}
export default TestIt;
It has the following result where the label text is wrapped. If I remove display: 'flex', flexWrap: 'wrap', the label text is not wrapped.
I don't want to wrap the label text; I don't understand why display: 'flex', flexWrap: 'wrap' (for some reason / other code, I need to keep it) has an effect on that.
For instance, there is no problem with codepen: https://codepen.io/SoftTimur/pen/JjJaRvv.
Could anyone help?

I think the main problem is width of parent element. First you have unnecessary wrapper over Toggle Component because it covers only one element inside flex:
Here you are few Codepen examples.
Note:
Codepen has dynamic width and that's the reason why text wrap doesn't exist. But it shows up when you have overflow or fixed width. Put at your local example some CSS for body and html or at parent element:
body, html {
width: 100%;
height: 100%;
}

Related

How to rewrite a modal CSS without affecting other modals in the project?

I have a loading component that I am importing on many pages in my project in that component I am using a modal, a modal that I have rewritten some of his CSS styles in a CSS file that I imported in the component
the problem that I have is that this CSS change of the modal effects other modals in my project
how can I let this CSS change only related to my component modal without affecting others
my loading component
import React, {Component, PropTypes} from 'react';
import {Modal} from "react-bootstrap";
import ReactLoading from 'react-loading';
import './loading.css'
export default class Loading extends Component {
constructor(props) {
super(props);
this.state = {
};
}
render() {
const {show} = this.props;
return(
<div >
<Modal show={show} className="loadingModal" keyboard={false}>
<Modal.Body className="testtest">
<ReactLoading id="modale" type={"bubbles"} color={"#2f96a1"} height={120} width={120} />
</Modal.Body>
</Modal>
</div>
)
}
}
my loading css:
.modal-backdrop.in {
opacity: 0;
filter: alpha(opacity=00);
}
.modal.in .modal-dialog {
display: flex;
justify-content: center;
margin-top: 9%;
}
#modale{
display: flex;
justifyContent: 'center';
margin: auto;
}
.testtest{
flex: 1;
alignItems: 'center';
justifyContent: 'center'
}
If you're not using CSS modules or such, I think the best way would be to respect the cascade and always assume that the css you import will spill. To do so I would encourage you to use BEM model. With it, it could be rewritten as:
<div>
<Modal show={show} className="loading-modal modal--test" keyboard={false}>
<Modal.Body className="modal--test__body">
<ReactLoading id="modale" class="modal--test__content" type={"bubbles"} color={"#2f96a1"} height={120} width={120} />
</Modal.Body>
</Modal>
</div>
And for css:
.modal--test .modal-backdrop.in {
opacity: 0;
filter: alpha(opacity=00);
}
.modal--test .modal.in .modal-dialog {
display: flex;
justify-content: center;
margin-top: 9%;
}
/* don't use ids for css */
.modal--test__content{
display: flex;
justify-content: 'center';
margin: auto;
}
.modal--test__body{
flex: 1;
alignItems: 'center';
justifyContent: 'center'
}
Naturally, this doesn't follow BEM model exactly as it would require passing classes to the internal elements. Alternatively, for scoping, you can use CSS modules.
You have multiple options:
assign a unique ID for the Modal and change your CSS selectors to target elements with this ID only (recommended)
use the Modal.container property to attach the modal to your Loading component rather than to the document body (discouraged if you do some tricky CSS styling, especially use absolute or fixed positioning of elements - this may mess up the Modal positioning logic)
use the Modal.bsPrefix property to change the class names of the modal elements. Probably OK to do, even though the documentation states that this should be used only if absolutely necessary
You should use CSS-module
According to the official repo CSS Modules are “CSS files in which all class names and animation names are scoped locally by default”.

How to stretch div to 100% width inside material ui TableBody

I'm trying to display a CircularProgress centered inside material UI TableBody. The below is what I have tried, but it's not centered as I expected. (Please note that only relevant code has been posted)
const styles = (theme) => ({
progress: {
width: "100%",
display: "flex",
justifyContent: "center",
minHeight: "10vh",
alignItems: "center"
}
})
<TableBody>
<div className={classes.progress}>
<CircularProgress />
</div>
</TableBody>
It looks like this, but I want it to be centered in the TableBody. Currently, it only takes the width of the first column.
Here your div extends 100% to the parent which is TableBody you can make table body 100% or give position: 'absolute' to div

Is wrapping components in order to place them within their parent's layout a correct approach?

I'm torn between two design choices. I'll let the code do the talking...
Approaches
First approach
Navbar component
const NavbarSurface = styled.div`
height: 100px;
`;
function Navbar() {
return (
<NavbarSurface>
<NavbarLogo />
</NavbarSurface>
)
}
export default Navbar;
NavbarLogo component
const Logo = styled.div`
width: 250px;
height: 100%;
min-height: 50px;
display: flex;
`
function NavbarLogo() {
return (
<Logo>
<Typography variant={"body2"} color={"textPrimary"} align={"center"} style={{
margin: "auto"
}}>
LOGO
</Typography>
</Logo>
)
}
export default NavbarLogo;
Second approach
Navbar component
const NavbarSurface = styled.div`
height: 100px;
`;
const StyledNavbarLogo = styled(NavbarLogo)`
height: 100%;
`
function Navbar() {
return (
<NavbarSurface>
<StyledNavbarLogo />
</NavbarSurface>
)
}
export default Navbar;
NavbarLogo component
const Logo = styled.div`
width: 250px;
min-height: 50px;
display: flex;
`
function NavbarLogo({className}) {
return (
<Logo className={className}>
<Typography variant={"body2"} color={"textPrimary"} align={"center"} style={{
margin: "auto"
}}>
LOGO
</Typography>
</Logo>
)
}
export default NavbarLogo;
Difference
The key difference between the two approaches is that on the second one, the child component (NavbarLogo) is released from the responsibility of being placed within the layout the parent component (Navbar) has defined. The child component is wrapped within the parent (StyledNavbarLogo) and the css associated with the parent (height: 100%;) is located within the parent itself.
My thoughts
I believe the second approach to be the better of the two as it is splitting responsibility between the components according to their functionality and leaves room for changes inside the Logo component without it affecting its placement relative to the parent container. At the same time, the second approach introduces an extra div which could be considered adding overhead so I'm in a dilemma which way is best.
What are your thoughts?
Both of your approaches are almost identical, with the exception of the fact that you are extending styles of NavbarLogo in the second case.
Also it doesn't create an additional div, instead just add another className to the NavbarLogo component which your forward to the Logo component
You can use any of the two approaches based on your use case but the second approach gives your more flexibility to provide default styles as well as override it if required from the parent. This is specially useful if you are using the component at multiple places.
However if your component only is used at one place and is not a generic component, the first approach works well and should be preferred to not unnecessarily complicate things

Material UI Paper element not covering 100% of viewport height

I have a Material UI <Paper> component that serves as a background and exists in my main React component- it's nested inside a <ThemeProvider>, which is nested a <div>, which is then nested in the <body>. I've applied the viewport: 100vh attribute to make it take the full height of the screen. It does take up the full height, but only prior to rendering another <Paper> component on the right hand side. Then the bottom of the paper no longer extends to the bottom of the screen:
Beginning of App render method:
return (
<ThemeProvider theme={theme}>
<Paper style={{ height: '100vh', boxShadow: 'none' }}>
<Container fluid id='app'>
.......
)
I tried applying the viewport: 100vh attribute to both the <div> that encloses the App component in index.js and the <body> element in index.html. There wasn't any difference. It may be worth mentioning that I'm using react-bootstrap Containers/Rows/Cols for my grid system at the moment (haven't switched that part to Material UI yet), but they're all nested inside the Paper, so I wouldn't expect they would be causing the problem. I also tried removing any css applied to the <Container> but it didn't help.
I'm also using a muiTheme for the <ThemeProvider> (obviously):
export default function createTheme(isDarkModeEnabled) {
return createMuiTheme({
palette: {
type: isDarkModeEnabled ? 'dark' : 'light',
primary: {
main: '#6DD3CE',
dark: '#639FAB'
},
secondary: {
main: '#52CBC5'
}
},
typography: {
fontFamily: [ 'montserratlight', 'Times New Roman' ].join(','),
body2: {
fontFamily: [ 'montserratmedium', 'Times New Roman' ].join(',')
},
h3: {
fontSize: '1.75rem'
},
button: {
fontFamily: [ 'montserratmedium', 'Times New Roman' ].join(',')
}
}
})
}
Update and Solution
I did redo my layout using flexbox instead of react-bootstrap, and ultimately fixed the problem by using min-height: 100vh instead of height: 100vh for my container so it had room to expand.
It appears that your <Container> has a bit of padding that is causing the content to go beyond its height:
This could also be due to your <textarea /> not having an assigned height. This is a particular issue when you are in a medium-sized screen.
If you are already planning to do away with Bootstrap's layout system, consider flex box and styled containers:
import React from "react";
import styled from "styled-components";
export default function App() {
return (
<Container>
<StyledHeader>Hello StackOverflow</StyledHeader>
<StyledCol>
<p>This is column 1.</p>
</StyledCol>
<StyledCol>
<p>This is another column.</p>
</StyledCol>
</Container>
);
}
const Container = styled.div`
height: 700px;
width: 100%;
display: flex;
flex-flow: row wrap;
justify-content: space-between;
`;
const StyledHeader = styled.h1`
height: 30%;
width: 100%;
margin: 0;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
`;
const StyledCol = styled.div`
height: 70%;
width: 50%;
& p {
text-align: center;
}
`;
CodeSandbox Example: https://codesandbox.io/s/stack-63802170-flexbox-example-909ol
This will ultimately give you more control over the layout of the page.

Full height in React using flex

I can't find an answer for this, even though there are many flex themed questions.
I created a simple React app (npx create-react-app).
I then deleted App.css and set the contents of index.css to:
html, body, #root {
height: 100%;
margin: 0;
}
Next, in App.js I'm trying to have a full screen element using flex:
import React from 'react';
function App() {
return (
<div style={{ backgroundColor: 'blue', display: "flex", flex: 1 }}>
<div style={{ display: "flex", flexDirection: "column", flex: 1 }} />
</div>
);
}
But it just won't work. Why is the flex element not stretching?
The flex-grow works when there are other elements in the flex. It makes the element to grow x times in comparison to others.
wrt the solution you have implemented: inner div doesn't require display: flex and direction too. It will be inherited from the parent div.
To make the div take the full height. add height: 100vh to the parent div.
Let me know if this will solve the problem.
Thank you.
To achieve height of window you need to mention height: 100vh for parent tag like
class- .parent{height: 100vh}
object style - style={{height: '100vh'}}
import React from 'react'
function App() {
return (
<div style={{ backgroundColor: 'blue', display: "flex", height:'100vh' }}>
<div style={{ display: "flex", flexDirection: "column", flex: 1 }} >Hello world</div>
</div>
);
}

Resources