Skip to content

Commit

Permalink
fix: dont prevent internal clicks, fixes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed Apr 10, 2019
1 parent ad2c6cc commit 229eaad
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
4 changes: 3 additions & 1 deletion example/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default class App extends Component <{}, AppState> {
<GHCorner openInNewTab href={repoUrl}/>
<button>outside</button>
outside

<button onClick={()=>alert('ok')}>test outside event</button>
<button onClick={toggle} ref={this.toggleRef}>toggle drop</button>
<button onClick={toggle}>toggle drop 2</button>
{on && <div style={{backgroundColor: '#EEE'}}>
Expand All @@ -54,6 +54,8 @@ export default class App extends Component <{}, AppState> {
shards={[this.toggleRef, this.scrollRef]}
>
Holala!!
<button onClick={()=>alert('ok')}>test inside event</button>
<a href="http://github.com">link</a>
<button onClick={toggle}>close</button>
<ScrollBox>innerbox</ScrollBox>
</FocusOn>
Expand Down
7 changes: 4 additions & 3 deletions src/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ const extractRef = (ref: React.RefObject<any> | HTMLElement): HTMLElement => (

export class ReactFocusOn extends Component<ReactFocusOnProps> {
private _undo?: () => void;
private lastEventTarget?: EventTarget;

private lockProps = {
onClick: (e: React.MouseEvent) => e.preventDefault(),
onClick: (e: React.MouseEvent) => this.lastEventTarget = e.target
};

private onActivation = (node: HTMLElement) => {
Expand All @@ -47,7 +48,7 @@ export class ReactFocusOn extends Component<ReactFocusOnProps> {
document.addEventListener('click', this.onClick);
};

private onDeactivation = (node: HTMLElement) => {
private onDeactivation = () => {
this._undo!();
const {onDeactivation} = this.props;
if (onDeactivation) {
Expand All @@ -69,7 +70,7 @@ export class ReactFocusOn extends Component<ReactFocusOnProps> {

private onClick = (event: MouseEvent) => {
const {shards = [], onClickOutside} = this.props;
if (event.defaultPrevented) {
if (event.defaultPrevented || event.target === this.lastEventTarget) {
return;
}
if (
Expand Down

0 comments on commit 229eaad

Please sign in to comment.