diff options
author | Marcin Aman <maman@virtuslab.com> | 2020-05-28 16:17:13 +0200 |
---|---|---|
committer | Paweł Marks <Kordyjan@users.noreply.github.com> | 2020-06-04 11:25:34 +0200 |
commit | b614604effda51ca7c76c8901be78ced62b642b2 (patch) | |
tree | 971fc02905cf3e0c75ef4fb00d93ce72b43936cb /plugins/base/frontend/src/main/components/app/index.tsx | |
parent | 4065a65fe3294e0ddf54f5756380f7dc1aa032b2 (diff) | |
download | dokka-b614604effda51ca7c76c8901be78ced62b642b2.tar.gz dokka-b614604effda51ca7c76c8901be78ced62b642b2.tar.bz2 dokka-b614604effda51ca7c76c8901be78ced62b642b2.zip |
Update TS migration to current dev, move to a common package, rename to frontend
Diffstat (limited to 'plugins/base/frontend/src/main/components/app/index.tsx')
-rw-r--r-- | plugins/base/frontend/src/main/components/app/index.tsx | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/plugins/base/frontend/src/main/components/app/index.tsx b/plugins/base/frontend/src/main/components/app/index.tsx new file mode 100644 index 00000000..1a51e699 --- /dev/null +++ b/plugins/base/frontend/src/main/components/app/index.tsx @@ -0,0 +1,50 @@ +import React, {useEffect, useRef, useState} from 'react'; +import {WithFuzzySearchFilter} from '../search/search'; +import './index.scss'; + +function useComponentVisible(initialIsVisible: boolean) { + const [isComponentVisible, setIsComponentVisible] = useState(initialIsVisible); + const ref = useRef(null); + + const handleHideDropdown = (event: KeyboardEvent) => { + if (event.key === "Escape") { + setIsComponentVisible(false); + } + }; + + const handleClickOutside = (event : MouseEvent)=> { + // @ts-ignore + if (ref.current && !ref.current.contains(event.target)) { + setIsComponentVisible(false); + } + }; + + useEffect(() => { + document.addEventListener("keydown", handleHideDropdown, false); + document.addEventListener("click", handleClickOutside, false); + return () => { + document.removeEventListener("keydown", handleHideDropdown, false); + document.removeEventListener("click", handleClickOutside, false); + }; + }); + + return { ref, isComponentVisible, setIsComponentVisible }; +} + +const App: React.FC = () => { + const { + ref, + isComponentVisible, + setIsComponentVisible + } = useComponentVisible(false); + + return <div ref={ref} className="search-content"> + {isComponentVisible && (<WithFuzzySearchFilter/>)} + {!isComponentVisible && ( + <span onClick={() => setIsComponentVisible(true)}> + <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20"><path d="M19.64 18.36l-6.24-6.24a7.52 7.52 0 1 0-1.28 1.28l6.24 6.24zM7.5 13.4a5.9 5.9 0 1 1 5.9-5.9 5.91 5.91 0 0 1-5.9 5.9z"/></svg> + </span>)} + </div> +} + +export default App |