From 885bf34ac6d9b9a5974cab35d9dd5a224b0ccc4c Mon Sep 17 00:00:00 2001 From: Marcin Aman Date: Tue, 19 May 2020 10:23:18 +0200 Subject: Tabs for sections v1 --- .../src/main/js/search/app-root.js | 52 ++++++++++++++++++---- core/search-component/src/main/js/search/app.css | 2 +- core/search-component/src/main/js/search/app.js | 2 +- 3 files changed, 45 insertions(+), 11 deletions(-) (limited to 'core/search-component') diff --git a/core/search-component/src/main/js/search/app-root.js b/core/search-component/src/main/js/search/app-root.js index 25a374a5..5d650581 100644 --- a/core/search-component/src/main/js/search/app-root.js +++ b/core/search-component/src/main/js/search/app-root.js @@ -1,13 +1,47 @@ -import React, {Component} from 'react'; +import React, {useRef, useState, useEffect} from 'react'; import {WithFuzzySearchFilter} from './search'; import './app.css'; -export default class AppRoot extends Component { - render() { - return ( -
- -
- ); - } +function useComponentVisible(initialIsVisible) { + const [isComponentVisible, setIsComponentVisible] = useState(initialIsVisible); + const ref = useRef(null); + + const handleHideDropdown = (event) => { + if (event.key === "Escape") { + setIsComponentVisible(false); + } + }; + + const handleClickOutside = event => { + if (ref.current && !ref.current.contains(event.target)) { + setIsComponentVisible(false); + } + }; + + useEffect(() => { + document.addEventListener("keydown", handleHideDropdown, true); + document.addEventListener("click", handleClickOutside, true); + return () => { + document.removeEventListener("keydown", handleHideDropdown, true); + document.removeEventListener("click", handleClickOutside, true); + }; + }); + + return { ref, isComponentVisible, setIsComponentVisible }; +} + +export const AppRoot = () => { + const { + ref, + isComponentVisible, + setIsComponentVisible + } = useComponentVisible(false); + + return
+ {isComponentVisible && ()} + {!isComponentVisible && ( + setIsComponentVisible(true)}> + + )} +
} \ No newline at end of file diff --git a/core/search-component/src/main/js/search/app.css b/core/search-component/src/main/js/search/app.css index 933237e0..c51d3fe1 100644 --- a/core/search-component/src/main/js/search/app.css +++ b/core/search-component/src/main/js/search/app.css @@ -17,5 +17,5 @@ html, } .search-content { - margin: calc(var(--ring-unit) * 4); + padding: 24px 41px; } diff --git a/core/search-component/src/main/js/search/app.js b/core/search-component/src/main/js/search/app.js index 64091607..bceffa48 100644 --- a/core/search-component/src/main/js/search/app.js +++ b/core/search-component/src/main/js/search/app.js @@ -2,7 +2,7 @@ import React from 'react'; import {render} from 'react-dom'; import RedBox from 'redbox-react'; -import AppRoot from './app-root'; +import {AppRoot} from './app-root'; import './app.css'; const appEl = document.getElementById('searchBar'); -- cgit