Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:heyjul3s/artifak into chore/bund…
Browse files Browse the repository at this point in the history
…le-size-reduction
  • Loading branch information
heyjul3s committed Mar 29, 2021
2 parents f51da90 + 8c75088 commit 1f91531
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/useclickaway/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# `@artifak/useclickaway`

> TODO: description
## Usage

```
const useclickaway = require('@artifak/useclickaway');
// TODO: DEMONSTRATE API
```
37 changes: 37 additions & 0 deletions packages/useclickaway/__tests__/useclickaway.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import { useClickAway } from '../src';

describe('@artifak/useclickaway', () => {
const onClickAway = jest.fn();

const Dummy = () => {
const ref: React.RefObject<HTMLDivElement> = React.createRef();

useClickAway(ref, onClickAway);

const onClick = () => {
console.log('Clicked');
};

return (
<div>
Wrapper
<div ref={ref} onClick={onClick}>
Click Me
</div>
</div>
);
};

it('calls the clickAway callback when click event occurs on non-target ref', () => {
render(<Dummy />);

const Wrapper = screen.getByText('Wrapper');

Wrapper.focus();
Wrapper.click();

expect(onClickAway).toHaveBeenCalled();
});
});
42 changes: 42 additions & 0 deletions packages/useclickaway/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "@artifak/useclickaway",
"version": "1.0.0",
"description": "A hook to handle click events when not occurring on targeted element.",
"keywords": [],
"author": "Julian Low",
"license": "MIT",
"sideEffects": false,
"main": "dist/useclickaway.cjs.js",
"module": "dist/useclickaway.esm.js",
"src": "src/index.ts",
"types": "dist/src/index.d.ts",
"directories": {
"src": "src",
"test": "__tests__"
},
"files": [
"dist"
],
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/heyjul3s/artifak.git"
},
"scripts": {
"build": "rimraf dist && builder",
"test": "echo \"Error: run tests from root\" && exit 1"
},
"bugs": {
"url": "https://github.com/heyjul3s/artifak/issues"
},
"homepage": "https://github.com/heyjul3s/artifak#readme",
"devDependencies": {
"@artifak/bundler": "^1.1.4"
},
"peerDependencies": {
"react": ">=17.0.1",
"react-dom": ">=17.0.1"
}
}
18 changes: 18 additions & 0 deletions packages/useclickaway/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useEffect, RefObject } from 'react';

export function useClickAway(
ref: RefObject<HTMLElement | null>,
onClickAway: (e: Event) => void
) {
const handleClick = (e: Event) => {
ref.current && !ref.current.contains(e.target as Node) && onClickAway(e);
};

useEffect(() => {
document.addEventListener('click', handleClick);

return function unmountUseClickAway() {
document.removeEventListener('click', handleClick);
};
}, [ref]);
}
9 changes: 9 additions & 0 deletions packages/useclickaway/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "."
},
"references": [],
"include": ["src"]
}

0 comments on commit 1f91531

Please sign in to comment.