Tips for avoiding a noticeable sliding drawer when changing the direction of an HTML element

I am encountering an issue with the Daisy UI drawer component on my page. When I attempt to change the page direction using a JavaScript function, the drawer animates visibly from left to right or right to left, which is not the desired behavior. I have tried hiding it by adding the hidden class, but then I struggled to find a way to make it visible again. Additionally, using z-index did not help in bringing it back.

I experimented with

<div class="drawer-side invisible peer-checked:visible">
, which partially fixed the problem, but the drawer did not smoothly slide back in after shifting directions. It simply disappeared immediately.

Could someone please advise me on how to prevent this abnormal sliding behavior of the drawer while changing the HTML direction? I want the drawer to smoothly slide in and out without any visibility issues during direction changes.

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cda9aca4beb4b8a48df9e3fbe3fc">[email protected]</a>/dist/full.min.css" rel="stylesheet" type="text/css" />
  <script src="https://cdn.tailwindcss.com"></script>
</head>

<body>
  <label id="lng" class="btn btn-primary drawer-button m-1" onclick="switchDir()">Switch Html Dir</label>

  <div class="drawer">
    <input id="my-drawer" type="checkbox" class="drawer-toggle" />
    <div class="drawer-content">
      <!-- Page content here -->
      <label for="my-drawer" class="btn btn-primary drawer-button m-1">Open drawer</label>
    </div>
    
    <div class="drawer-side">
      <label for="my-drawer" aria-label="close sidebar" class="drawer-overlay"></label>
      <ul class="menu p-4 w-80 min-h-full bg-base-200 text-base-content">
        <!-- Sidebar content here -->
        <li><a>Sidebar Item 1</a></li>
        <li><a>Sidebar Item 2</a></li>
      </ul>
    </div>
  </div>

  <script>
    // Function to switch the HTML direction
    function switchDir() {
      let page = document.getElementsByTagName('html')[0];
      if (page.dir == 'rtl') {
        page.dir = 'ltr';
      } else {
        page.dir = 'rtl';
      }
    };
  </script>
</body>

Answer №1

Prior to altering the dir, ensure that all elements with the class .drawer-side are hidden:

const drawers = document.querySelectorAll('.drawer-side');
drawers.forEach(drawer => {
  drawer.style.display = 'none';
});

Implement the change in dir, then allow one frame render before reverting the style back to its original state, preventing CSS transitions:

requestAnimationFrame(() => {
  drawers.forEach(drawer => {
    drawer.style.display = '';
  });
});

// Function for toggling HTML direction
function toggleDirection() {
  let htmlElement = document.getElementsByTagName('html')[0];
  
  const drawers = document.querySelectorAll('.drawer-side');
  drawers.forEach(drawer => {
    drawer.style.display = 'none';
  });

  if (htmlElement.dir == 'rtl') {
    htmlElement.dir = 'ltr';
  } else {
    htmlElement.dir = 'rtl';
  }
  
  requestAnimationFrame(() => {
    drawers.forEach(drawer => {
      drawer.style.display = '';
    });
  });
};
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="88ece9e1fbf1fde1c8bca6beA6B9">[email protected]</a>/dist/full.min.css" rel="stylesheet" type="text/css" />
  <script src="https://cdn.tailwindcss.com"></script>
</head>

<body>
  <label id="lng" class="btn btn-primary drawer-button m-1" onclick="toggleDirection()">Toggle Html Dir</label>

  <div class="drawer">
    <input id="my-drawer" type="checkbox" class="drawer-toggle" />
    <div class="drawer-content">
      <!-- Content of page goes here -->
      <label for="my-drawer" class="btn btn-primary drawer-button m-1">Open drawer</label>
    </div>
    
    <div class="drawer-side">
      <label for="my-drawer" aria-label="close sidebar" class="drawer-overlay"></label>
      <ul class="menu p-4 w-80 min-h-full bg-base-200 text-base-content">
        <!-- Sidebar contents go here -->
        <li><a>Sidebar Item 1</a></li>
        <li><a>Sidebar Item 2</a></li>
      </ul>
    </div>
  </div>
</body>

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

Issue where the link in the first column of a horizontal scrolling table is unclick

In my attempt to create a fixed first column horizontal scrolling table, I encountered an issue where hyperlinks in the first column become unclickable once scrolled to the right. This seems to be because the fixed column is not clickable by default, and o ...

What is the formula to determine the sizes of grandchildren div containers?

Hey everyone, I'm having some trouble with my jQuery code and I can't seem to figure out what's going on. Here's the scenario: I'm creating a few divs (based on entries from MySQL) and I'd like to show you the end result: ...

Combining Various Template Variables into a Single Variable in Django

I am facing a challenge in assigning multiple values from a dictionary to a variable within a Django template. For example: fruit.supplier = tesco fruit.color = blue {% with test=fruit.supplier_fruit.color %} {{ test }} {% endwith %} The expected ...

Toggle the disable state of a button depending on a specific condition

I have a scenario where I am fetching data from a server, and I need to apply a filter to a specific column. The requirement is that if the "bought" column (boolean) has a value of true, then the edit button should be disabled; otherwise, it should be enab ...

Tips for personalizing a jQuery Mobile popup

Is there a way to customize the appearance of a dialog box using CSS? I've been struggling with my attempts so far... <div data-role="dialog" id="confirm-clear" class="dialog-custom" > <div data-role="content" > <p>Some ...

PHP - What is the reason for the output being a multi-dimensional array?

Hey there, I'm currently working with this code and for some reason, the variable records2 is returning a multi-dimensional array. Can anyone help me figure out why this is happening? I actually need it to be a simple, single dimension array. functi ...

What is the best way to access bootstrap col and row styles without needing to import the entire framework?

I am currently in the process of developing a react app using styled-components and I am looking to design a component that mimics the functionality of the col col-x classes from bootstrap. Additionally, I also want to create another component for the row ...

Using JQuery to Load a CSHTML File into a Modal in C#

I am attempting to have the content of one specific page loaded into a modal on a different page when a button is clicked. Unfortunately, I am encountering an issue where not only the desired content from the specified page is loading, but also content fro ...

Tips on discovering a combined query for this specific scenario

In my database, there are two tables: branch and source. The table branch has columns b_id and b_name, while the table source has columns s_name and b_code, with each row containing multiple b_code values separated by commas. I need to retrieve results tha ...

Currently, I am expanding my knowledge of PHP and MySQL by working on a dynamic form that utilizes arrays. As I progress through the project,

One of my recent projects involved creating dynamic rows in a form using code. I managed to save the data in an array and display it using a foreach loop, but I'm facing challenges when trying to insert this data into a database. Here's a glimps ...

What is the fastest way to add an email subscription form to my website without needing to configure a database?

Would it be possible to integrate a sign-up text box on our site for users interested in receiving launch notifications? Any suggestions on the most effective method to implement this feature? EDIT: Our current use of Prefinery has limitations as we cann ...

What is preventing me from accessing the sub-elements of an AngularJS controller?

I know this question has probably been asked countless times before, but I'm struggling to find the right words to explain my specific issue. Essentially, I'm having trouble accessing something like $ctrl.thing.subelement. However, the following ...

Elevate the expanded card above the others on hover instead of displacing them downward

I'm working on a grid of cards where hovering over one card expands the bottom section to reveal more content. The issue is, when it expands, it pushes all other cards down. What I actually want is for it to overlay on top of the card below it. Here ...

What is the best way to ensure that a React app is always displayed in full screen

I am facing an issue while developing an app with React and Material UI. I am trying to display the app in full-page mode, but unable to achieve it successfully. Currently, it appears like this: https://i.sstatic.net/Hg0CA.png Here is my code snippet from ...

I am currently implementing pagination for my data and it is functioning properly. However, I have noticed that when I search for specific data, it always appears on

My issue arises when I attempt to paginate my data and then search for specific information. The problem is that even if I am on page 3, the search results always display on page 1, making it difficult for me to locate the relevant data. What I want is for ...

Transform your Email Template with Inline Styling to mimic a Bootstrap grid, ranging from col-md-6 to col-12 styling

Currently, I am looking to create an email template. Typically, I create my email templates with each content/row separated. However, this time I need to design an email template with a layout that consists of 2 columns on desktop and switches to 1 column ...

The Power of HTML Floating Elements

<nav> <div class="row"> <img src="resources/img/logo-white.png" alt="logo" class="logo"> <ul class="main-nav"> <li><a href="#">Food delivery</a></li> <li ...

Changing the cursor while the onDrag event is active in ReactJs

I want to implement Drag functionality on certain elements in my application, but I am facing an issue where the cursor changes during dragging. How can I change the cursor specifically when onDrag is active? ...

Can the default DOM structure of a Jquery Data Table be customized to fit specific needs?

I have incorporated a Jquery Data Table Plugin that can be found at http://datatables.net/api. This plugin comes with its own search box, which is automatically added to the page when enabled. The search box is nested within its own div structure like this ...

Incorporating a class into ever-changing Bootstrap Table rows

Looking to enhance my table rows with a class, excluding the header. Struggling to find a way to do this. Any ideas? This is the table in question: <table id="table" class="hidden table table-bordered table-striped table-hover"> <thead> ...