The New Way to Build with AI! Introducing ReUI — the developer platform for agentic UI with shadcn/ui
Browse ReUI Pro

KTDrawer missing target element


How can I get the element that opened the drawer? For KTModal, I use KTModal.getRelatedTarget(), but for drawers the target is always null.


KTDrawer.on("show", async () => {
console.log(KTDrawer.getRelatedTarget());
});


Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (3)


Hi,

Let us know if still have the issues.

Thanks


The issue is in the KTDrawer implementation. While the _relatedTarget property and getRelatedTarget() method exist, the public toggle() method doesn't accept a relatedTarget parameter, and the handleToggle() static method doesn't pass the target element.

Here are your options:

Option 1: Manual Target Setting (Immediate Fix)

<pre> // Instead of using data-kt-drawer-toggle attribute const button = document.querySelector('[data-kt-drawer-toggle="#myDrawer"]'); const drawer = KTDrawer.getInstance(document.querySelector('#myDrawer')); button.addEventListener('click', (event) => { drawer.show(event.target); // Pass the target element }); KTDrawer.on("show", async () => { console.log(KTDrawer.getRelatedTarget()); // Now returns the button element }); </pre>

Option 2: Direct Event Handling

<pre> // Get the target element directly from the event document.addEventListener('click', (event) => { if (event.target.matches('[data-kt-drawer-toggle]')) { const drawer = KTDrawer.getInstance(document.querySelector(event.target.getAttribute('data-kt-drawer-toggle'))); if (drawer) { drawer.show(event.target); } } }); </pre>

Option 3: Use show() Instead of toggle()

<pre> // If you need to manually control the drawer const drawer = KTDrawer.getInstance(document.querySelector('#myDrawer')); const button = document.querySelector('#myButton'); button.addEventListener('click', () => { if (drawer.isOpen()) { drawer.hide(); } else { drawer.show(button); // Pass the button as related target } }); </pre>

Thank you. Will the problem be fixed?


Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(
buy metronic keenthemes mega bundle

ReUI

Open-source React components, blocks and templates built on shadcn/ui and Tailwind CSS by Keenthemes.