-
Notifications
You must be signed in to change notification settings - Fork 841
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uses new WindowEvent component for Flyout "close on ESC" #1127
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
55535bb
Adds new EuiWindowEvent component
jasonrhodes 48608b7
Switches flyout close on ESC to window event
jasonrhodes dbe6c71
Removes old event listener on Flyout
jasonrhodes 6acd453
Adds EuiWindowEvent example to docs
jasonrhodes 300fcb3
Updates changelog for EuiWindowEvent and Flyout changes
jasonrhodes b703e5f
Adds more clarification to WindowEvent example
jasonrhodes a348dbb
Moves WindowEvent to services folder
jasonrhodes 73f0ed0
Updates changelog to group changes correctly
jasonrhodes e4d90d9
Adds more about how to use the EuiWindowEvent in example docs
jasonrhodes f8b6f3e
Rewrites examples for WindowEvent docs
jasonrhodes 870a759
Adds EuiWindowEvent props to docs
jasonrhodes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React from 'react'; | ||
|
||
import { | ||
EuiModal, | ||
EuiModalBody, | ||
EuiModalHeader, | ||
EuiModalHeaderTitle, | ||
EuiOverlayMask | ||
} from '../../../../src/components'; | ||
|
||
import { ModalExample } from './modal_example_container'; | ||
|
||
const BasicModal = ({ onClose }) => ( | ||
<EuiOverlayMask> | ||
<EuiModal | ||
onClose={onClose} | ||
style={{ width: '800px' }} | ||
> | ||
<EuiModalHeader> | ||
<EuiModalHeaderTitle > | ||
Example modal | ||
</EuiModalHeaderTitle> | ||
</EuiModalHeader> | ||
<EuiModalBody> | ||
<p>This modal closes when you press ESC, using a window event listener.</p> | ||
</EuiModalBody> | ||
</EuiModal> | ||
</EuiOverlayMask> | ||
); | ||
|
||
export const BasicWindowEvent = () => <ModalExample modal={BasicModal} />; |
50 changes: 50 additions & 0 deletions
50
src-docs/src/views/window_event/modal_example_container.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React, { Component } from 'react'; | ||
import { | ||
EuiButton, | ||
} from '../../../../src/components'; | ||
|
||
import { | ||
EuiWindowEvent, | ||
} from '../../../../src/services'; | ||
|
||
export class ModalExample extends Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
open: false | ||
}; | ||
|
||
this.open = this.open.bind(this); | ||
this.close = this.close.bind(this); | ||
this.closeOnEscape = this.closeOnEscape.bind(this); | ||
} | ||
|
||
open() { | ||
this.setState({ open: true }); | ||
} | ||
|
||
close() { | ||
if (this.state.open) { | ||
this.setState({ open: false }); | ||
} | ||
} | ||
|
||
closeOnEscape({ key }) { | ||
if (key === 'Escape') { | ||
this.close(); | ||
} | ||
} | ||
|
||
render() { | ||
const { modal: Modal, buttonText = 'Open Modal' } = this.props; | ||
const button = <EuiButton onClick={this.open}>{buttonText}</EuiButton>; | ||
|
||
return ( | ||
<div> | ||
<EuiWindowEvent event="keydown" handler={this.closeOnEscape} /> | ||
{this.state.open ? <Modal onClose={this.close} /> : button} | ||
</div> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React, { Component } from 'react'; | ||
|
||
import { | ||
EuiSwitch, | ||
EuiDescriptionList, | ||
EuiSpacer | ||
} from '../../../../src/components'; | ||
|
||
import { | ||
EuiWindowEvent, | ||
} from '../../../../src/services'; | ||
|
||
export class MousePosition extends Component { | ||
|
||
state = { | ||
tracking: false, | ||
coordinates: {} | ||
}; | ||
|
||
onSwitchChange = () => this.setState((state) => ({ tracking: !state.tracking })); | ||
|
||
onMouseMove = ({ clientX, clientY }) => this.setState({ coordinates: { clientX, clientY } }); | ||
|
||
render() { | ||
const listItems = [ | ||
{ title: 'Position X', description: this.state.coordinates.clientX || '??' }, | ||
{ title: 'Position Y', description: this.state.coordinates.clientY || '??' } | ||
]; | ||
return ( | ||
<div> | ||
<EuiSwitch | ||
label="Track mouse position" | ||
checked={this.state.tracking} | ||
onChange={this.onSwitchChange} | ||
/> | ||
{this.state.tracking ? <EuiWindowEvent event="mousemove" handler={this.onMouseMove} /> : null} | ||
<EuiSpacer size="l" /> | ||
<EuiDescriptionList listItems={listItems} /> | ||
<EuiSpacer size="xxl" /> | ||
</div> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import React from 'react'; | ||
|
||
import { | ||
EuiModal, | ||
EuiModalBody, | ||
EuiModalHeader, | ||
EuiModalHeaderTitle, | ||
EuiOverlayMask, | ||
EuiFieldText, | ||
EuiSpacer | ||
} from '../../../../src/components'; | ||
|
||
import { ModalExample } from './modal_example_container'; | ||
|
||
class ConflictModal extends React.Component { | ||
|
||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
inputValue: '' | ||
}; | ||
} | ||
|
||
updateInputValue = e => this.setState({ inputValue: e.target.value }); | ||
|
||
clearInputValueOnEscape = e => { | ||
if (e.key === 'Escape') { | ||
this.setState({ inputValue: '' }); | ||
e.stopPropagation(); | ||
} | ||
} | ||
|
||
render() { | ||
return ( | ||
<EuiOverlayMask> | ||
<EuiModal | ||
onClose={this.props.onClose} | ||
style={{ width: '800px' }} | ||
> | ||
<EuiModalHeader> | ||
<EuiModalHeaderTitle > | ||
Example modal | ||
</EuiModalHeaderTitle> | ||
</EuiModalHeader> | ||
<EuiModalBody> | ||
<EuiFieldText | ||
value={this.state.inputValue} | ||
onChange={this.updateInputValue} | ||
onKeyDown={this.clearInputValueOnEscape} | ||
/> | ||
<EuiSpacer size="s" /> | ||
<p>While typing in this field, ESC will clear the field.</p> | ||
<EuiSpacer size="l" /> | ||
<p>Otherwise, the event bubbles up to the window and ESC closes the modal.</p> | ||
</EuiModalBody> | ||
</EuiModal> | ||
</EuiOverlayMask> | ||
); | ||
} | ||
} | ||
|
||
export const WindowEventConflict = () => ( | ||
<ModalExample modal={ConflictModal} buttonText="Open Modal with Conflicting Listener" /> | ||
); |
116 changes: 116 additions & 0 deletions
116
src-docs/src/views/window_event/window_event_example.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import React from 'react'; | ||
|
||
import { renderToHtml } from '../../services'; | ||
|
||
import { | ||
GuideSectionTypes, | ||
} from '../../components'; | ||
|
||
import { | ||
EuiWindowEvent | ||
} from '../../../../src/services'; | ||
|
||
import { | ||
EuiCode, | ||
EuiCallOut, | ||
EuiSpacer | ||
} from '../../../../src/components'; | ||
|
||
import { BasicWindowEvent } from './basic_window_event'; | ||
const basicSource = require('!!raw-loader!./basic_window_event'); | ||
const basicHtml = renderToHtml(BasicWindowEvent); | ||
|
||
import { WindowEventConflict } from './window_event_conflict'; | ||
const conflictSource = require('!!raw-loader!./window_event_conflict'); | ||
const conflictHtml = renderToHtml(WindowEventConflict); | ||
|
||
import { MousePosition } from './mouse_position'; | ||
const mousePositionSource = require('!!raw-loader!./mouse_position'); | ||
const mousePositionHtml = renderToHtml(MousePosition); | ||
|
||
export const WindowEventExample = { | ||
title: 'Window Events', | ||
sections: [ | ||
{ | ||
title: 'Basic example: closing a modal on escape', | ||
source: [{ | ||
type: GuideSectionTypes.JS, | ||
code: basicSource, | ||
}, { | ||
type: GuideSectionTypes.HTML, | ||
code: basicHtml, | ||
}], | ||
text: ( | ||
<div> | ||
<p> | ||
Use an <EuiCode>EuiWindowEvent</EuiCode> to safely and declaratively manage adding and auto-removing event listeners | ||
to the <EuiCode>window</EuiCode>. 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. | ||
</p> | ||
<p> | ||
This modal example registers a listener on the <EuiCode>keydown</EuiCode> event and listens for ESC key presses, | ||
which closes the open modal. | ||
</p> | ||
</div> | ||
), | ||
components: { EuiWindowEvent }, | ||
demo: <BasicWindowEvent />, | ||
}, | ||
{ | ||
title: 'Avoiding event conflicts', | ||
source: [{ | ||
type: GuideSectionTypes.JS, | ||
code: conflictSource, | ||
}, { | ||
type: GuideSectionTypes.HTML, | ||
code: conflictHtml, | ||
}], | ||
text: ( | ||
<div> | ||
<EuiCallOut | ||
title="Be careful with global listeners" | ||
color="warning" | ||
iconType="alert" | ||
> | ||
<p> | ||
Since window event listeners are global, they can conflict with other event listeners if you aren't careful. | ||
</p> | ||
</EuiCallOut> | ||
<EuiSpacer /> | ||
<p> | ||
The safest and best way to avoid these conflicts is to use <EuiCode>event.stopPropagation()</EuiCode> 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 <EuiCode>WindowEvent</EuiCode> listener will never be triggered, avoiding the conflict. | ||
</p> | ||
</div> | ||
), | ||
components: { EuiWindowEvent }, | ||
demo: <WindowEventConflict />, | ||
}, | ||
{ | ||
title: 'Tracking mouse position', | ||
source: [{ | ||
type: GuideSectionTypes.JS, | ||
code: mousePositionSource, | ||
}, { | ||
type: GuideSectionTypes.HTML, | ||
code: mousePositionHtml, | ||
}], | ||
text: ( | ||
<div> | ||
<p> | ||
For some DOM events, you have to listen on the window. One example of this is tracking <em>mouse position</em>. Below, | ||
when you click the toggle switch, your mouse position is tracked. When you toggle off, tracking stops. | ||
</p> | ||
<p> | ||
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 <EuiCode>WindowEvent</EuiCode> component | ||
manages that unmount/unregister process for you. | ||
</p> | ||
</div> | ||
), | ||
components: { EuiWindowEvent }, | ||
demo: <MousePosition />, | ||
} | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as EuiWindowEvent } from './window_event'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add
props: { EuiWindowEvent }