3

I have a Menu panel next to my sider. I am trying to add a drawer type animation however I can't seem position open/close within the pink column

const album = ["Album1", "Album2", "Album3"];
export const Menu = () => {
  const [open, setOpen] = useState(false);
  return (
    <div className="flex flex-1 flex-col p-3">
      <h2 className="text-lg font-medium text-gray-900">Album</h2>
      <div className="flex-1">
        <div className="flex flex-1 flex-col space-y-3 py-3">
          {album.map((name) => (
            <button
              key={name}
              id={name}
              className={`flex space-x-2 items-center`}
              onClick={() => setOpen(true)}
            >
              <span>{name}</span>
            </button>
          ))}
        </div>
      </div>
      <aside
        onClick={() => setOpen(false)} //temporary
        className={`transform top-0 left-0 w-72 bg-blue-400 fixed h-full overflow-auto ease-in-out transition-all duration-1000 z-30 ${
          open ? "translate-x-14" : "-translate-x-full"
        }`}
      >
        hello
      </aside>
    </div>
  );
};

enter image description here

1 Answer 1

1

Javascript. Just add a eventListener to the 'button' which will handle the actions. In the eventListener, remove or add a css class. For example, by default the position is '-100px right' with css set position to '300px right', also add a transition 'all 300ms ease'. Now you element start with no css, and when user press button add the class.

<div class="text-3xl font-bold underline transition-all duration-300" id="must-change">
 Lorem Ipsu
</div>

<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold p-3 rounded" id="press-me">
  Button
</button>

<script>
const btn = document.querySelector("#press-me")
const mustChange =  document.querySelector("#must-change")
btn.addEventListener("click", () => {
  mustChange.classList.toggle("font-bold")
})
</script>

I set 'transition all' but you can use a specific transition if you want.

Sign up to request clarification or add additional context in comments.

2 Comments

I have updated my answer with a GIF of current behaviour. I am trying to open drawer within the pink column only. Do you know what I am missing?
Fixed it, forgot z-index on the sidebar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.