Overview - svelte:body for body events
What is it?
In Svelte, is a special element that lets you listen to events happening on the entire HTML body element. This means you can catch clicks, key presses, or other events anywhere on the page, not just inside your component. It helps you react to user actions globally without adding event listeners manually to the body.
Why it matters
Without , you would have to manually add and remove event listeners on the body element, which can be error-prone and messy. This feature makes it easy and safe to handle global events in a clean, declarative way. It improves user experience by letting your app respond to clicks or keys anywhere, like closing a menu when clicking outside it.
Where it fits
Before learning , you should understand basic Svelte components and event handling inside components. After mastering it, you can explore more advanced global event handling patterns, custom actions, and Svelte's lifecycle for managing resources.