Skip to main content
Elastic UI
Elastic UI
Getting startedComponentsPatternsContentData visualization
EUI ChangelogGitHubFigma
  • Setup
  • Theming
  • Accessibility
  • Testing
  • Utilities
    • Accessibility
    • Auto sizer
    • Color palettes
    • Copy
    • CSS utility classes
    • Delay
    • Error boundary
    • Focus trap
    • Highlight and mark
    • HTML ID generator
    • I18n
    • Inner text
    • Mutation observer
    • Outside click detector
    • Overlay mask
    • Portal
    • Pretty duration
    • Provider
    • Resize observer
    • Scroll
    • Text diff
    • Text truncation
    • Window events
  • EUI
  • Utilities
  • Window events

Window events

Basic example: closing a modal on escape

Use an EuiWindowEvent to safely and declaratively manage adding and auto-removing event listeners to the

window. This is preferable to setting up your own window event listeners because it will remove old listeners when your component unmounts, preventing you from accidentally leaving them around forever.

This modal example registers a listener on the keydown event and listens for ESC key presses, which closes the open modal.

Loading...

Avoiding event conflicts

Be careful with global listeners

Since window event listeners are global, they can conflict with other event listeners if you aren't careful.

The safest and best way to avoid these conflicts is to use event.stopPropagation() at the lowest, most specific level where you are responding to a DOM event. This will prevent the event from bubbling up to the window, and the EuiWindowEvent listener will never be triggered, avoiding the conflict.

Loading...

Tracking mouse position

For some DOM events, you have to listen on the window. One example of this is tracking mouse position. Below, when you click the toggle switch, your mouse position is tracked. When you toggle off, tracking stops.

If you were manually attaching window listeners, you might forget to remove the listener and be silently responding to mouse events in the background for the life of your app. The EuiWindowEvent component manages that unmount/unregister process for you.

Loading...

Props

EuiWindowEvent

๐˜‚๐˜‚
โœ„๐˜—โœ„๐˜—
Prop
โ†ฆ
Description and type
โ†ฆ
Default value
โ†ต
Prop
event#
โ†ฆ
Description and type
Type: keyof WindowEventMap
โ†ฆ
Default value
Required
โ†ต
Prop
handler#
โ†ฆ
Description and type
Type: (this: Window, ev: WindowEventMap[E]) => any
โ†ฆ
Default value
Required
โ†ต
๐˜‚๐˜‚
Edit this page

Previous
Text truncation
  • Basic example: closing a modal on escape
  • Avoiding event conflicts
  • Tracking mouse position
  • Props
EUI is dual-licensed under Elastic License 2.0 and Server Side Public License, v 1 | Crafted with โค by Elastic