项目作者: MargaretKrutikova

项目描述 :
Custom hook to determine click outside in reason-react
高级语言: OCaml
项目地址: git://github.com/MargaretKrutikova/use-click-outside-re.git
创建时间: 2019-04-28T22:35:52Z
项目社区:https://github.com/MargaretKrutikova/use-click-outside-re

开源协议:MIT License

下载


Use click outside hook in reason-react

A simple implementation of detecting a click outside of an element encapsulated in a custom hook useClickOutside.
It uses react hooks useRef and useEffect and bs-webapi that provides bindings to the DOM API.

Example usage

The hook returns a ref that needs to be attached to an element, and takes in a function of type Dom.mouseEvent => unit that will be called if a click is detected outside of that element.

Basic usage:

  1. open ClickOutside;
  2. [@react.component]
  3. let make = () => {
  4. let handleClickOutside = _ => {
  5. Js.log("Click outside detected");
  6. };
  7. let divRef = useClickOutside(handleClickOutside);
  8. <div ref={ReactDOMRe.Ref.domRef(divRef)} ></div>;
  9. };

The repo contains a simple dropdown component to show the usage of the hook. Styling of the dropdown is implemented with the amazing bs-css.

Run Project

  1. npm install
  2. npm start
  3. # in another tab
  4. npm run webpack

After you see the webpack compilation succeed (the npm run webpack step), open up build/index.html (no server needed!). Then modify whichever .re file in src and refresh the page to see the changes.

Run Project with Server

To run with the webpack development server run npm run server and view in the browser at http://localhost:8000. Running in this environment provides hot reloading and support for routing; just edit and save the file and the browser will automatically refresh.