Search code examples
htmlcsstailwind-cssdaisyui

How to make Daisy UI Drawer component work with Swap component?


I'm trying to do a drawer open with animated button using Daisy UI.

The goal is to open the drawer component by simply clicking a swap button component, however either one of them works! the swap animation works but no drawer open or the drawer open works without the swap component animation!...

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sample Ui</title>

    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/full.min.css" rel="stylesheet" type="text/css" />
    <script src="https://cdn.tailwindcss.com"></script>

</head>
<body>

    <label class="btn btn-circle swap swap-rotate z-10" for="my-drawer"> <!-- my confusing here --> 
        <!-- this hidden checkbox controls the state -->
        <input type="checkbox" />        
        <!-- hamburger icon -->
        <svg class="swap-off fill-current" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 512 512"><path d="M64,384H448V341.33H64Zm0-106.67H448V234.67H64ZM64,128v42.67H448V128Z"/></svg>        
        <!-- close icon -->
        <svg class="swap-on fill-current" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 512 512"><polygon points="400 145.49 366.51 112 256 222.51 145.49 112 112 145.49 222.51 256 112 366.51 145.49 400 256 289.49 366.51 400 400 366.51 289.49 256 400 145.49"/></svg>        
      </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">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   mt-10">
            <!-- Sidebar content here -->
            <li><a>Sidebar Item 1</a></li>
            <li><a>Sidebar Item 2</a></li>            
          </ul>
        </div>
      </div>   
    
</body>
</html>

I tried to move the for="my-drawer" from the drawer component to the swap component I found that the drawer open but no swap animation, if I return it back then the swap animation works but not the drawer open.

How can I do both together please? (No JQ)


Solution

  • You can emulate the functionality of the .swap-on/.swap-off by applying the CSS that would apply with the checked inner <input>, but instead using the drawer checkbox:

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Sample Ui</title>
    
      <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/full.min.css" rel="stylesheet" type="text/css" />
      <script src="https://cdn.tailwindcss.com"></script>
    </head>
    
    <body>
      <div class="drawer">
        <input id="my-drawer" type="checkbox" class="drawer-toggle" />
    
        <label class="btn btn-circle swap swap-rotate z-10" for="my-drawer">
          <!-- hamburger icon -->
          <svg class="swap-off fill-current [:checked~*_&]:!-rotate-45 [:checked~*_&]:!opacity-0" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 512 512"><path d="M64,384H448V341.33H64Zm0-106.67H448V234.67H64ZM64,128v42.67H448V128Z"/></svg>        
          <!-- close icon -->
          <svg class="swap-on fill-current [:checked~*_&]:!rotate-0 [:checked~*_&]:!opacity-100" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 512 512"><polygon points="400 145.49 366.51 112 256 222.51 145.49 112 112 145.49 222.51 256 112 366.51 145.49 400 256 289.49 366.51 400 400 366.51 289.49 256 400 145.49"/></svg>        
        </label>
        
        <div class="drawer-content">
          <!-- Page content here -->
          <!--label for="my-drawer" class="btn btn-primary drawer-button">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   mt-10">
            <!-- Sidebar content here -->
            <li><a>Sidebar Item 1</a></li>
            <li><a>Sidebar Item 2</a></li>
          </ul>
        </div>
      </div>
    
    </body>
    
    </html>