Left alignment of Material-UI dialog buttons
Asked Answered
U

7

10

I have three buttons in a Dialog like so:

enter image description here

The JSX is

        <DialogActions classes={{ root: this.props.classes.dialogActionsRoot }} >
          <Button classes={{ root: this.props.classes.buttonRoot }} size="small" onClick={() => this.props.handleDialogClose()} >
            Clear
          </Button>
          <Button classes={{ root: this.props.classes.buttonRoot }} size="small" onClick={() => this.props.handleDialogClose()} >
            Cancel
          </Button>
          <Button classes={{ root: this.props.classes.buttonRoot }} size="small" onClick={() => this.props.handleDialogClose(this.state.selectedValue)} >
            Select
          </Button>
        </DialogActions>

The 'buttonRoot' style simply determines the button coloring. How to I left align the 'Clear' button so it sits on the left? It seems the buttons are each in a div with a MuiDialogActions-action class.

Unpromising answered 22/2, 2018 at 4:48 Comment(0)
R
23

Just use divider element with flex: 1 0 0 CSS style:

<DialogActions>
  <Button>
    Left
  </Button>
  <Button>
    Buttons
  </Button>
  <div style={{flex: '1 0 0'}} />
  <Button>
    Right
  </Button>
  <Button>
    Buttons
  </Button>
</DialogActions>
Rosetta answered 29/5, 2019 at 4:25 Comment(2)
For anyone curious, flex: 1 0 0 essentially means setting flexGrow: 1, which makes the div grow as much as it can.Assignment
This is the best and the cleanest solution. Works without affecting default spacing between buttons in DialogActions component if you have one button on the left and several on the right.Expiration
P
12

No need for complicated answers:


Just set the DialogActions component style to {justifyContent: "space-between"}

<DialogActions style={{ justifyContent: "space-between" }}>
  <Button>
    Left
  </Button>
  <Button>
    Right
  </Button>
</DialogActions>
Pursuit answered 21/12, 2021 at 8:0 Comment(0)
H
5

You can do it with Flexbox:

DialogActions {
  display: flex; /* displays flex-items (children) inline */
}

DialogActions > Button:first-child {
  margin-right: auto;
}
<DialogActions classes={{ root: this.props.classes.dialogActionsRoot }} >
  <Button classes={{ root: this.props.classes.buttonRoot }} size="small" onClick={() => this.props.handleDialogClose()}>
    Clear
  </Button>
  <Button classes={{ root: this.props.classes.buttonRoot }} size="small" onClick={() => this.props.handleDialogClose()}>
    Cancel
  </Button>
  <Button classes={{ root: this.props.classes.buttonRoot }} size="small" onClick={() => this.props.handleDialogClose(this.state.selectedValue)}>
    Select
  </Button>
</DialogActions>

Note: See it in full screen.

Hysterical answered 22/2, 2018 at 7:5 Comment(1)
I ended up with a derivative from this solution. I pick the last button on the left and apply the style={{ marginRight: "auto" }} to that one.Quadratics
M
1

Edit: This does not answer the question. It will align all the buttons to the left.

According to the DialogActions API the classes you can override are root and spacing.

After taking a look at the implemantation of the component you can see in root class the rule justifyContent: 'flex-end'.

I simply apllied justifyContent: 'flex-start' with another class:

<DialogActions classes={{ root: classes.leftAlignDialogActions }}>
   ....
</DialogActions>

Make sure to create the class leftAlignDialogActions with makeStyles

const useStyles = makeStyles(theme => ({
leftAlignDialogActions: {
    justifyContent: 'flex-start'
  }
}))
Myriagram answered 16/12, 2019 at 14:2 Comment(1)
That would align them all on the left, he's asking for just the first one.Quadratics
M
1

I resolved a similar problem with the following addition inside my theme: (using MUI)

 components: {
MuiDialogActions: {
  styleOverrides: {
    root: {
      display: "flex",
      flexWrap: "wrap",
      gap: "8px",
      justifyContent: "start",
      "&>.MuiButtonBase-root": {
        marginLeft: 0,
      },
    },
  },
},
Matless answered 16/7, 2024 at 8:49 Comment(0)
H
0

I would also disableSpacing and then add the flex attributes and MUI styles overrides I need via the sx prop. In my case I had to modify the spacing (gap) from 8px to 10px.

<DialogActions disableSpacing sx={{gap: '10px'}}>
  <Button>Cancel</Button>
  <Button>Submit</Button>
</DialogActions>

Maybe in your case another option is to wrap the 2nd and the 3rd buttons in a Box component and on the container to add justifyContent: space-between. In this way in the flex container you will have 2 flex items which will be pushed to the left inner edge and right inner edge of the container. The left item will be the first button and the right item will be the Box which has the 2nd and 3rd buttons as children.

Humpy answered 4/1, 2023 at 15:41 Comment(0)
B
-4

You can use first child selector because first button is clear button in tag DialogActions

DialogActions button:first-child {
  float:left;
}

OR

You can try. You need to adjust left and bottom property accordingly.

DialogActions{
  position:relative;
  width:100%;
}
DialogActions button:first-child {
  position:absolute;
  left:10px;
  bottom:10px;
}

Hope it helps you.

Bullington answered 22/2, 2018 at 6:26 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.