Vertically align a button and a dropdown - css

I would like to make a row, where there is a title on the left and several buttons and a dropdown on the right.
I use Flexbox to arrange their horizontal placement.
Now, the dropdown (e.g., Cat) is not vertically-aligned with the buttons. Notice that if we remove the icons (.e.g, iconProps={{ iconName: "Back" }}), they will be aligned.
I tried to put style={{ minWidth: "auto", marginTop: "-10px" }} or style={{ minWidth: "auto", paddingTop: "-10px" }} for the dropdown, but it did not work.
Could anyone help?
https://codesandbox.io/s/peaceful-mopsa-2qr5qu?file=/example.tsx
import { makeStyles, shorthands } from "#fluentui/react-components";
import {
Dropdown,
Option,
OptionGroup,
DropdownProps
} from "#fluentui/react-components/unstable";
import { CommandBarButton } from "#fluentui/react/lib/Button";
import * as React from "react";
import { initializeIcons } from "#fluentui/react/lib/Icons";
initializeIcons(/* optional base url */);
const useStyles = makeStyles({
dropdown: {
// removes default border around the dropdown
...shorthands.borderWidth(0),
// removes the blue bottom border when the dropdown is open
"::after": {
borderBottomWidth: 0
}
}
});
export const Grouped = (props: Partial<DropdownProps>) => {
const land = ["Cat", "Dog", "Ferret", "Hamster"];
const styles = useStyles();
return (
<div style={{ display: "flex", justifyContent: "space-between" }}>
<div style={{ flexBasis: "auto" }}>
<span style={{ lineHeight: "2.7rem" }}>Title</span>
</div>
<div style={{ flexBasis: "auto" }}>
<CommandBarButton
styles={{ root: { height: 44 } }}
iconProps={{ iconName: "Back" }}
ariaLabel="Back"
text="Button 1"
disabled={false}
checked={false}
/>
<CommandBarButton
styles={{ root: { height: 44 } }}
iconProps={{ iconName: "Up" }}
text="Button 2"
disabled={false}
checked={false}
/>
<Dropdown
className={styles.dropdown}
style={{ minWidth: "auto" }}
defaultSelectedOptions={["Cat"]}
{...props}
>
<OptionGroup label="Land">
{land.map((option) => (
<Option key={option} disabled={option === "Ferret"}>
{option}
</Option>
))}
</OptionGroup>
</Dropdown>
</div>
</div>
);
};

There is a div wrapping the buttons and the dropdown. Applying flex to that div and then to the dropdown too will give you the output that you want.
// Flex here
<div style={{ display: "flex" }}>
<CommandBarButton
styles={{ root: { height: 44 } }}
iconProps={{ iconName: "Back" }}
ariaLabel="Back"
text="Button 1"
disabled={false}
checked={false}
/>
<CommandBarButton
styles={{ root: { height: 44 } }}
iconProps={{ iconName: "Up" }}
text="Button 2"
disabled={false}
checked={false}
/>
<Dropdown
className={styles.dropdown}
// Flex here
style={{
minWidth: "auto",
display: "flex",
}}
defaultSelectedOptions={["Cat"]}
{...props}
>
<OptionGroup label="Land">
{land.map((option) => (
<Option key={option} disabled={option === "Ferret"}>
{option}
</Option>
))}
</OptionGroup>
</Dropdown>
</div>;
CodeSandbox : https://codesandbox.io/s/elegant-yalow-4uheom?file=/example.tsx:1554-1570

Related

Reduce the left padding of a dropdown button

I would like to make a row, where there is a title on the left and several buttons and a dropdown on the right.
Now, I would like to make the distance between Button 2 and the dropdown (e.g., Cat) smaller.
I guess we need to overwrite the left padding of the button inside the dropdown. I tried to put marginLeft: "-10px" and paddingLeft: "-10px" in the style of FluentProvider or Dropdown, but it did not work.
Could anyone help?
CodeSandbox: https://codesandbox.io/s/charming-ritchie-40tyj1?file=/example.tsx
import {
FluentProvider,
webLightTheme,
makeStyles,
shorthands
} from "#fluentui/react-components";
import {
Dropdown,
Option,
OptionGroup,
DropdownProps
} from "#fluentui/react-components/unstable";
import { CommandBarButton } from "#fluentui/react/lib/Button";
import * as React from "react";
import { initializeIcons } from "#fluentui/react/lib/Icons";
initializeIcons(/* optional base url */);
const useStyles = makeStyles({
dropdown: {
// removes default border around the dropdown
...shorthands.borderWidth(0),
// removes the blue bottom border when the dropdown is open
"::after": {
borderBottomWidth: 0
}
}
});
export const Grouped = (props: Partial<DropdownProps>) => {
const land = ["Cat", "Dog", "Ferret", "Hamster"];
const styles = useStyles();
return (
<div style={{ display: "flex", justifyContent: "space-between" }}>
<div style={{ flexBasis: "auto" }}>
<span style={{ lineHeight: "2.7rem" }}>Title</span>
</div>
<div style={{ display: "flex" }}>
<CommandBarButton
styles={{ root: { height: 44 } }}
iconProps={{ iconName: "Back" }}
ariaLabel="Back"
text="Button 1"
disabled={false}
checked={false}
/>
<CommandBarButton
styles={{ root: { height: 44 } }}
iconProps={{ iconName: "Up" }}
text="Button 2"
disabled={false}
checked={false}
/>
<FluentProvider style={{ display: "flex" }} theme={webLightTheme}>
<Dropdown
className={styles.dropdown}
style={{
minWidth: "auto",
display: "flex"
}}
defaultSelectedOptions={["Cat"]}
{...props}
>
<OptionGroup label="Land">
{land.map((option) => (
<Option key={option} disabled={option === "Ferret"}>
{option}
</Option>
))}
</OptionGroup>
</Dropdown>
</FluentProvider>
</div>
</div>
);
};
There might be many approaches, perhaps try specify styles for Dropdown button in makeStyles and pass it to the button. The value in below example can be further adjusted to suit the use case.
Forked demo with modification: codesandbox
const useStyles = makeStyles({
dropdown: {
// removes default border around the dropdown
...shorthands.borderWidth(0),
// removes the blue bottom border when the dropdown is open
"::after": {
borderBottomWidth: 0
}
},
// 👇 styles for the dropdown button
button: {
paddingLeft: "0px"
}
});
Pass the styles to the button of Dropdown:
<Dropdown
className={styles.dropdown}
style={{
minWidth: "auto",
display: "flex",
}}
defaultSelectedOptions={["Cat"]}
// 👇 pass styles to the button
button={{ className: styles.button }}
{...props}
>
...
You cant assign negative values to the padding. Try the same idea but on Button2, but with negative margin-right: -10px You then might need to move the whole navbar 10px to the right, since it wont stay aligned as it is now. You can fix it by doing the same on navbar content.
You might also try overriding padding with padding-left: 0 !important

How do I change borderColor in native base Select Component?

I'm trying to change the borderColor on this Select Component from Native base.
Here is an image of the default color and the focused color:
The border is black by default. I already tried with borderColor="" and none/unset but it isn't changing the color.
How can I change the default and active border color?
The code is here..
useState,
} from 'react';
import {
Box,
Select,
CheckIcon,
} from 'native-base';
function Dropdown({
options = [],
placeholder,
backgroundColor,
className,
onChange = () => {},
}) {
const [value, setValue] = useState('');
return (
<Box>
<Box width="3/4" maxWidth="300">
<Select
className={className}
onChange={onChange}
minWidth="200"
selectedValue={value}
accessibilityLabel="Choose Service"
placeholder={placeholder}
backgroundColor={backgroundColor}
_selectedItem={{
bg: 'teal.600',
endIcon: <CheckIcon size="5" />,
}}
_focus={{
bg: 'white',
}}
marginTop={1}
onValueChange={(itemValue) => setValue(itemValue)}
>
{options.map((option) => (
<Select.Item
label={option.label}
value={option.value}
key={option.value}
style={{ display: 'flex', flexDirection: 'column', padding: 5 }}
/>
))}
</Select>
</Box>
</Box>
);
}```
You can use borderColor to specify border color and borderWidth to specify border width. To specify border color for focused inside _focus.
const Example = () => {
let [service, setService] = React.useState('');
return (
<Center>
<Box w="3/4" maxW="300">
<Select
_focus={{ borderColor: 'yellow.500' }}
borderColor="red.500"
selectedValue={service}
minWidth="200"
accessibilityLabel="Choose Service"
placeholder="Choose Service"
_selectedItem={{
bg: 'teal.600',
endIcon: <CheckIcon size="5" />,
}}
mt={1}
onValueChange={(itemValue) => setService(itemValue)}
>
<Select.Item label="UX Research" value="ux" />
<Select.Item label="Web Development" value="web" />
<Select.Item label="Cross Platform Development" value="cross" />
<Select.Item label="UI Designing" value="ui" />
<Select.Item label="Backend Development" value="backend" />
</Select>
</Box>
</Center>
);
};

Material Ui TextField how to change color of border

I haven't been able to find a way to change the color of the TextField border to something other than the grey color it starts off with. I want to change it to black.
Here is my UpdatePage.js file (simplified) :
const useStyles = makeStyles(() => ({
updatePage: {
display: 'flex',
justifyContent: 'center',
marginTop: '100px'
},
updateMovementDiv: {
background: '#00BFFF',
fontFamily: 'PT Sans Caption',
fontSize: '18px',
borderRadius: '10px',
padding: '20px',
marginTop: '50px',
},
textDiv: {
background: '#ffffff',
padding: '8px',
borderRadius: '10px',
},
}));
const UpdatePage = () => {
const classes = useStyles();
return (
<div>
<Header title="Update Movement" />
<div className={classes.updatePage}>
<div className={classes.updateMovementDiv}>
<form onSubmit={handleSubmit} >
<div className={classes.textDiv}>
<TextField
name="movementName"
variant="outlined"
label="Movement Name"
style={{ width:200 }}
value={move.movementName}
onChange={(e) => setMoveData({ ...moveData, movementName: e.target.value })}
/>
<TextField
name="movementWeight"
variant="outlined"
label="New One Rep Max"
style={{ width:200 }}
InputProps={{endAdornment: <InputAdornment position="end">lb</InputAdornment>}}
onChange={(e) => setMoveData({ ...moveData, movementWeight: e.target.value })}
/>
</div>
<div className={classes.buttonDiv}>
<Button className={classes.updateButton} variant="contained" type="submit" fullWidth endIcon={<LoopIcon />} >Update</Button>
</div>
</form>
</div>
</div>
</div>
);
};
export default UpdatePage;
I have tried adding a classname to my TextField tags and changing the color from there.
I have also tried adding the input property to the class like so:
textField: {
input: {
color: 'white'
}
and then adding:
InputProps={{
className: classes.input,
}}
None of these have worked, the TextField border stays grey. Any help would be appreciated.
I guess you are writing just 'color' property and the property for border color is 'borderColor', change it to borderColor from color and then check it.

Align Image and Text in Horizontal Direction in React

I'm trying to achieve like this in the picture below. Right now, my image is on the top while the text is below. I wanted to achieve like the text is just on the right side of the image.
Pls check this codesandbox link CLICK HERE
CODE
const drawer = (
<div>
<h2 className={classes.headerTitle}>Login</h2>
<Divider />
<div className={classes.headerIcon}>
<AccountCircleIcon fontSize="large" />
</div>
<h5 className={classes.headerName}>Bake</h5>
<p className={classes.headerRole}>User</p>
<Divider />
</div>
);
Adding display: "flex" to children doesn't really do much. What I did was I added a wrapper class around you icon, name and role, with the display: "flex", flexDirection:"row" justifyContent: "center" and alignItems:"center" properties. What the wrapper then does is that it puts all the divs "under" it in a row, like:
<div className="classes.wrapper">
<div>This one is to the left</div>
<div>This one is to the right</div>
</div>
However, if I for example put another 2 divs under the one to the right, they will stack on top of each other, because the flexDirection property is set to row for all children under the wrapper, but not for their children.
<div className="classes.wrapper">
<div>This one is to the left</div>
<div>
<div>This one will be to the right on top</div>
<div>This one will be to the right under</div>
</div>
</div>
I also removed some other stuff, but here's the code:
import React from "react";
import { makeStyles } from "#material-ui/styles";
import Divider from "#material-ui/core/Divider";
import Drawer from "#material-ui/core/Drawer";
import Hidden from "#material-ui/core/Hidden";
import AccountCircleIcon from "#material-ui/icons/AccountCircle";
import "./styles.css";
const useStyles = makeStyles(theme => ({
wrapper: {
display: "flex",
flexDirection: "row",
justifyContent: "center",
alignItems: "center",
margin: "0.5rem"
},
innerWrapper: {
display: "flex",
flexDirection: "column",
alignItems: "baseline",
marginLeft: "0.5rem"
},
headerTitle: {
fontSize: "1.3rem",
cursor: "pointer"
},
headerName: {
margin: "0",
fontStyle: "bold",
fontSize: "1rem"
},
headerRole: {
margin: "0",
fontSize: "0.7rem"
},
iconButtons: {
marginLeft: "auto"
}
}));
export default function LoginForm() {
const classes = useStyles();
const drawer = (
<>
<h2 className={classes.headerTitle}>Login</h2>
<Divider />
<div className={classes.wrapper}>
{" "}
<div className={classes.headerIcon}>
{" "}
<AccountCircleIcon fontSize="large" />
</div>
<div className={classes.innerWrapper}>
<h5 className={classes.headerName}>Bake</h5>
<p className={classes.headerRole}>User</p>
</div>
<Divider />
</div>
</>
);
return (
<nav className={classes.drawer}>
<Hidden lgUp implementation="css">
<Drawer
variant="temporary"
anchor={"left"}
classes={{
paper: classes.drawerPaper
}}
ModalProps={{
keepMounted: true
}}
>
{drawer}
</Drawer>
</Hidden>
<Hidden implementation="css">
<Drawer
classes={{
paper: classes.drawerPaper
}}
variant="permanent"
open
>
{drawer}
</Drawer>
</Hidden>
</nav>
);
}
For more information on how to use flexbox in CSS, check out this guide.
Here is how I have done it. I have adjusted the text on the right side of the icon.
You can do further styling:
import React from "react";
import { makeStyles } from "#material-ui/styles";
import Divider from "#material-ui/core/Divider";
import Drawer from "#material-ui/core/Drawer";
import Hidden from "#material-ui/core/Hidden";
import AccountCircleIcon from "#material-ui/icons/AccountCircle";
import "./styles.css";
const headerStyles = {
display: "flex",
justifyContent: "center"
};
const useStyles = makeStyles(theme => ({
root: {
display: "flex"
},
headerTitle: {
...headerStyles,
fontSize: "1.3rem",
cursor: "pointer"
},
headerIcon: {
...headerStyles,
marginTop: "1rem"
},
headerName: {
...headerStyles,
marginTop: "0.2rem"
},
headerRole: {
...headerStyles,
marginTop: "-0.8rem",
marginBottom: "1rem"
},
iconButtons: {
marginLeft: "auto"
},
userName: {
display: "flex",
flexDirection: "row"
}
}));
export default function LoginForm() {
const classes = useStyles();
const drawer = (
<div>
<h2 className={classes.headerTitle}>Login</h2>
<Divider />
<div className={classes.userName}>
<div className={classes.headerIcon}>
<AccountCircleIcon fontSize="large" />
</div>
<div>
<h5 className={classes.headerName}>Bake</h5>
<p className={classes.headerRole}>User</p>
</div>
</div>
<Divider />
</div>
);
return (
<nav className={classes.drawer}>
<Hidden lgUp implementation="css">
<Drawer
variant="temporary"
anchor={"left"}
classes={{
paper: classes.drawerPaper
}}
ModalProps={{
keepMounted: true
}}
>
{drawer}
</Drawer>
</Hidden>
<Hidden implementation="css">
<Drawer
classes={{
paper: classes.drawerPaper
}}
variant="permanent"
open
>
{drawer}
</Drawer>
</Hidden>
</nav>
);
}

Button sit at bottom of page centered

I would like a scroll to the top button to sit at the bottom, much like a footer. Current Behavior: I have an array I'm filtering through and displaying different lengths of data. When there is only one item in a certain category the button will move all the way to the top of the page under the item. Wanted Behavior: I would like the button to stay at the bottom and not move, but not sticky. my button styling is as follows:
import React, { useState, useContext } from "react"
// components
import SelectStatus from "../components/SelectStatus"
import RepoCard from "../components/RepoCard"
// Context
import { GithubContext } from "../context/GithubContext"
// Material UI Stuff
import TextField from "#material-ui/core/TextField"
import CardContent from "#material-ui/core/CardContent"
import Button from "#material-ui/core/Button"
import Card from "#material-ui/core/Card"
import Grid from "#material-ui/core/Grid"
import { makeStyles } from "#material-ui/core/styles"
import Container from "#material-ui/core/Container"
// context
const useStyles = makeStyles((theme) => ({
cardGrid: {
paddingTop: theme.spacing(8),
paddingBottom: theme.spacing(8),
},
card: {
display: "flex",
marginBottom: 10,
minHeight: 90,
},
form: {
display: "flex",
alignItems: "center",
width: "100%",
},
content: {
display: "flex",
alignItems: "center",
width: "100%",
justifyContent: "space-between",
},
jobField: {
margin: 0,
padding: 0,
},
grid: {
padding: 0,
},
dashboardContainer: {
marginTop: 70,
padding: 10,
},
loading: {
textAlign: "center",
},
}))
const INITIAL_STATE = {
language: "All",
search: "",
}
const Profile = () => {
const [formData, setFormData] = useState(INITIAL_STATE)
const [updated, setUpdated] = useState(false)
const [created, setCreated] = useState(false)
const { data } = useContext(GithubContext)
const handleUpdated = () => {
setUpdated(!updated)
data &&
data.sort((a, b) => {
if (updated) return a.updated_at > b.updated_at ? -1 : 1
return a.updated_at > b.updated_at ? 1 : -1
})
}
const handleCreated = () => {
setCreated(!created)
data &&
data.sort((a, b) => {
if (created) return a.created_at > b.created_at ? -1 : 1
return a.created_at > b.created_at ? 1 : -1
})
}
const handleInputChange = (field) => (e) => {
setFormData({ ...formData, [field]: e.target.value })
}
const classes = useStyles()
return (
<>
<div style={{ marginTop: 85, marginBottom: 85 }}>
<Container className={classes.dashboardContainer}>
<Card className={classes.card} style={{ width: "100%" }}>
<CardContent className={classes.content}>
<div className={classes.form}>
<Grid
container
spacing={2}
alignItems='center'
justify='space-between'
>
<Grid item sm={4} xs={12} className={classes.grid}>
<SelectStatus
language={formData.language}
handleInputChange={handleInputChange}
/>
</Grid>
<Grid item sm={4} xs={12} className={classes.grid}>
<TextField
className={classes.jobField}
margin='normal'
fullWidth
id='search'
name='search'
label='Search by Title'
placeholder='Search by Title'
onChange={handleInputChange("search")}
value={formData.search}
/>
</Grid>
<Grid item sm={2} xs={12} className={classes.grid}>
<Button
fullWidth
variant='contained'
color='primary'
onClick={handleUpdated}
>
Updated {updated ? "(oldest)" : "(newest)"}
</Button>
</Grid>
<Grid item sm={2} xs={12} className={classes.grid}>
<Button
fullWidth
variant='contained'
color='primary'
onClick={handleCreated}
>
Created {created ? "(oldest)" : "(newest)"}
</Button>
</Grid>
</Grid>
</div>
</CardContent>
</Card>
</Container>
{!data ? (
<h1 className={classes.loading}>Initializing Repos...</h1>
) : (
<Container style={{ padding: 10 }}>
{!data ? (
<div style={{ placeItems: "center" }}>Loading...</div>
) : (
<Grid container alignItems='center' spacing={4}>
{data &&
data
.filter((data) => {
if (formData.language === "All") return true
return data.language === formData.language
})
.filter((data) => {
if (formData.search === "") return true
return (data.name + data.language)
.toLowerCase()
.includes(formData.search.toLowerCase())
})
.map((user) => <RepoCard key={user.id} user={user} />)}
</Grid>
)}
</Container>
)}
<Button
variant='contained'
color='primary'
disableElevation
style={{
borderRadius: 0,
display: "block",
marginLeft: "auto",
marginRight: "auto",
position: "relative",
marginTop: "80px"
}}
>
Back to Top
</Button>
</div>
</>
)
}
export default Profile
You can use flex and space-around feature. like below:
<div class="container">
<div>
your items
</div>
<Button
variant='contained'
color='primary'
disableElevation
style={{
borderRadius: 0,
elevation: "disabled",
display: "block",
marginLeft: "auto",
marginRight: "auto",
marginTop: "80px",
}}
>
Back to Top
</Button>
</div>
Style:
.container{
display: flex;
flex-direction:column;
justify-content: space-between;
}
Using this you split your content to two part.
First part which will be your array items will be shown at the top.
The second part which is your button will be shown at the bottom.
This is because your making a space between first part and second part by using space-around of justify-content.
without sticky or fixed the only thing i can think of is using position: absolute; and bottom: 0; or placing them in a container with some combination of the same, then positioning it as needed. same as Joeri in the comments, would need a bit more context to help :) good luck!

Resources