Search code examples
cssreactjsmaterial-uilabeloverlay

Issue with Label Overlapping and Obscuring in Text Field


When I click on or start typing in the text field, the label "Type your New Task here" shifts upwards and becomes obscured by what seems like a white overlay. How can I remove this behavior and ensure the label remains fully visible?

Before focusing on it:: Normal Behaviour

When focusing on it: enter image description here

This is the code I used. I used material UI for styling this.

 <DialogTitle>Add New Task</DialogTitle>
    <DialogContent>
      <TextField
        autoFocus
        label={isEmpty ? "Task can't be empty" : "Type your New Task here"}
        fullWidth
        variant="outlined"
        value={todoName}
        inputRef={todoInput}
        onChange={(e) => {
          setTodoName(e.target.value.toLowerCase());
          setIsEmpty(false);
        }}
        InputProps={{
          style: { color: isEmpty ? warningColor : "initial" },
        }}
        InputLabelProps={{
          style: { color: isEmpty ? warningColor : "initial" },
        }}
        error={isEmpty}
      />
    </DialogContent>
    <DialogActions>
      <Button onClick={handleClose} color="primary">
        Cancel
      </Button>
      <Button
        onClick={addTodo}
        color="primary"
        disabled={isEmpty || isLoading}
      >
        Add
      </Button>
    </DialogActions>
  </Dialog>

Any help would be greatly appreciated. Thank you.


Solution

  • When you place a DialogTitle next to DialogContent, it removes the top padding from DialogContent:

    enter image description here

    To fix this, you can override it by adding extra top padding to DialogContent:

    <DialogContent style={{ paddingTop: 16 }}>
    

    Edit dialog-content-no-top-padding