Element in SwipeableDrawer of Material UI is repositioned on mobile devices while scrolling

Utilizing Material UI's SwipeableDrawer component, I have successfully created a bottom drawer with a fixed action button container. The container is positioned using position: fixed; bottom: 0;. While this setup works effectively on desktop browsers, I have encountered an issue when viewing the drawer on mobile devices.

The problem arises when the content inside the SwipeableDrawer is long enough to require scrolling. If I scroll to the bottom and then attempt to scroll further, the fixed element (the action button container) shifts position and no longer remains aligned to the bottom.

<SwipeableDrawer
anchor={anchor}
open={state[anchor]}
onClose={toggleDrawer(anchor, false)}
onOpen={toggleDrawer(anchor, true)}
>
  {/* long content */}
  <Box
    sx={{
      backgroundColor: "#fff",
      position: "fixed",
      bottom: "0",
      left: "0",
      display: "flex",
      width: "100%",
      "> *": {
        flex: "1",
      },
    }}
  >
    <Button onClick={toggleDrawer(anchor, false)}>Cancel</Button>
    <Button onClick={toggleDrawer(anchor, false)}>Submit</Button>
  </Box>
</SwipeableDrawer>

You can view example code here and a demo video here

I have attempted using position: sticky as an alternative, but it does not align with my other requirements. As a result, I am not considering utilizing sticky positioning.

If you have any insights or solutions to this issue, your input would be greatly appreciated. Thank you!

Answer №1

When dealing with a drawer element that is already positioned as fixed, adding another element with a fixed position inside it can lead to issues. The solution is to move the button container outside of the drawer, adjust its z-index to be higher than 1200 (the default z-index of the drawer), and hide it when the drawer is not open. Here's how you can achieve this:

<div>
  {(['left', 'right', 'top', 'bottom'] as const).map((anchor) => (
    <React.Fragment key={anchor}>
      <Button onClick={toggleDrawer(anchor, true)}>{anchor}</Button>
      <SwipeableDrawer
        anchor={anchor}
        open={state[anchor]}
        onClose={toggleDrawer(anchor, false)}
        onOpen={toggleDrawer(anchor, true)}
      >
        {list(anchor)}
      </SwipeableDrawer>
      <Box
        sx={{
          backgroundColor: '#fff',
          position: 'fixed',
          bottom: '0',
          left: '0',
          display: state[anchor] ? 'flex' : 'none',
          width: '100%',
          zIndex: 1201,
          '> *': {
            flex: '1',
          },
        }}
      >
        <Button onClick={toggleDrawer(anchor, false)}>Cancel</Button>
        <Button onClick={toggleDrawer(anchor, false)}>Submit</Button>
      </Box>
    </React.Fragment>
  ))}
</div>

If you want to see this solution in action, check out this StackBlitz for a live demonstration based on the updated version of your sample code.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to update an image dynamically within CSS using the :before pseudo-element?

I am attempting to dynamically change the background image, but I am having trouble accessing the wrapper before "wrapper:before{}" from jQuery $('.wrapper:before) .wrapper:before { content: ""; position: fixed; left: 0; right: 0; z-index: -1; displa ...

Remove the dd/mm/yyyy format from Material-UI DateRangePicker

Is there a way to eliminate the dd/mm/yyyy from this control completely, including the space it occupies? https://i.sstatic.net/VI5eT.png ...

I can't seem to figure out why this isn't functioning properly

Upon examining the script, you'll notice the interval() function at the very bottom. The issue arises from bc-(AEfficiency*100)/5; monz+((AEfficiency*100)/5)((AFluencyAProduct)/100); For some reason, "bc" and "monz" remain unchanged. Why is that so? T ...

Ref cannot be assigned to function components. Trying to reference a ref in a function component will not work

Having trouble passing a ref in the MenuItem component while using react-beautiful-dnd. I tried creating a HOC with React.forwardRef but it didn't work. Any help in fixing this issue would be greatly appreciated. Error: https://i.stack.imgur.com/bLIN ...

"Divs of the same type automatically adjust size to fit within the

I am currently exploring the possibility of creating a versatile component that can be customized based on the needs of its parent element, whether through bootstrap or traditional flexbox / CSS grid. I want to determine the level of reusability this compo ...

Incorporate text into the URL of the image

Got a URL of an image like this: https://lipsum.mobi/catalog/product/SE0229E/YG/AAA/4/1/SE0229E-YG-AAA-4.jpg', and looking to add 240x240 within the URL. Current Url: https://lipsum.mobi/catalog/product/SE0229E/YG/AAA/4/1/SE0229E-YG-AAA-4.jpg Desire ...

how to customize nested styles in material-ui

I am facing an issue with a basic Material-UI component <Chip avatar={ <Avatar> <TruckIcon color='primary' /> </Avatar> } label={name} color='primary' /> ...

Errors have popped up unexpectedly in every test file after importing the store into a particular file

Using Jest and Enzyme to test a React application has been successful, but encountering failures when importing redux store in a utility file. The majority of tests fail with the following error: FAIL app/containers/Login/LoginContainer.test.js ● Te ...

React Material UI and DayJs are not syncing up properly when displaying dates

In my React application, I am using MUI Cards to display a list of objects. One of the fields in these objects is of type date in postgres. When I retrieve a sample card object from the database, this is the value that appears in the browser console: { ...

What is the best way to position an image beside a jquery dialog box?

Apologies for not finding a suitable topic to discuss. Is it possible to place an image next to a jQuery dialog, similar to the following example? If so, how can this be achieved? ...

Attempting to showcase a pair of unordered lists side by side using the power of flexbox

I am having trouble getting this to display on a single line, no matter what I try. While there may be simpler ways to achieve this without using flexbox, I am eager to learn how to do it using flexbox properties. My desired outcome is to have the first u ...

Tips for overcoming a script error within the body of a Next.js project

I encountered an error in my _document.js file when trying to add a script to the body of my document. Here is the specific error message that was returned: https://i.stack.imgur.com/QG5zb.png ./pages/_document.js Error: x Expected '}', got &a ...

What steps should be taken to address the RLS policy problem when using `.insert()` in Supabase?

I'm currently working on developing a habit tracking application using Supabase PostgreSQL. I've set up RLS in the habit table, but for some reason, I can't seem to resolve this issue. When I click the 'create habit' button, it sen ...

Python Flask Server: Receiving Error 415 for Unsupported Media Type while attempting to utilize jsonify method

I'm facing an issue with my Flask server where it seems to be correctly: Receiving a JSON object (dictionary of parameters) from my React web page. Generating a response based on those parameters. Using jsonify on that response. However, the Flask s ...

Grid sidebar using tailwindcss

I just started using Tailwind CSS, but I'm having trouble understanding how the grid system works! I want my layout to look like this: https://i.sstatic.net/hlHmy.png Here is what I tried, but it's not working as expected: <div class=" ...

Concealing a block from top to bottom

I currently have a filter that is hidden when clicked, with a transition effect that moves it from the bottom to the top. I have recorded my screen to demonstrate this behavior: . However, I now need the filter to hide from top to bottom instead, essenti ...

underscore.js provides _.where utility for filtering arrays of strings and objects

I am looking to verify whether a specific string is present in a collection of objects. The variable this.props.value contains the string "apple, peach" and this.state.list consists of a list of objects with key-value pairs. My goal is to determine if "ap ...

The content of the text does not align. Alert in React 16

Currently, I am working on developing a ReactJs application with server-side rendering. Here are my entry points for both the client and server: client.jsx const store = createStore(window.__INITIAL_STATE__); hydrate( <Provider store={store}> ...

The combination of DOMDocument and DOMXPath is a powerful duo

I am currently working on converting CSS to inline using DOMDocument and DOMXPath. However, I'm encountering an issue where the HTML I provide is not being recognized as valid XML. Is there a way for me to configure these functions to ignore unknown ...

What is the best way to hide the jQuery modal I created?

Hello everyone! Currently, I am working on coding a simple JS modal that can be opened and closed smoothly. The issue I am facing is related to adding a click function to (.black-overlay) in order to fade out everything and close the modal. <div class ...