diff --git a/src/components/EditorControls.js b/src/components/EditorControls.js index 77b0866..4072515 100644 --- a/src/components/EditorControls.js +++ b/src/components/EditorControls.js @@ -4,6 +4,7 @@ import { gzip } from 'pako'; import history from 'history/browser'; import copy from 'copy-to-clipboard'; +import { MenuButton, Button } from './Menu'; import { languageIds } from '../highlighting'; import themes from '../style/themes'; @@ -91,62 +92,6 @@ const Section = styled.div` align-items: center; `; -const Button = styled.div` - cursor: pointer; - height: 100%; - display: flex; - align-items: center; - padding: 0 .25em; - color: inherit; - text-decoration: none; - - :hover { - background: ${props => props.theme.highlight}; - } -`; - -const Menu = styled.ul` - position: absolute; - top: 2em; - margin: 0; - padding: 0; - list-style: none; - background-color: ${props => props.theme.highlight}; - - > li { - padding: .15em .5em; - } - - > li:hover { - background-color: ${props => props.theme.secondary}; - } -`; - -const MenuButton = ({ label, ids, value, setValue }) => { - const [open, setOpen] = useState(false); - - function toggleOpen() { - setOpen(!open); - } - - function select(e, id) { - e.stopPropagation(); - setOpen(false); - setValue(id); - } - - return ( - - ) -} - function langaugeToContentType(language) { if (language === 'json') { return 'application/json'; diff --git a/src/components/Menu.js b/src/components/Menu.js new file mode 100644 index 0000000..e3223f5 --- /dev/null +++ b/src/components/Menu.js @@ -0,0 +1,68 @@ +import { useState, useEffect } from 'react'; +import styled from 'styled-components'; + +export const Button = styled.div` + cursor: pointer; + height: 100%; + display: flex; + align-items: center; + padding: 0 .25em; + color: inherit; + text-decoration: none; + + :hover { + background: ${props => props.theme.highlight}; + } +`; + +const Menu = styled.ul` + position: absolute; + top: 2em; + margin: 0; + padding: 0; + list-style: none; + background-color: ${props => props.theme.highlight}; + + > li { + padding: .15em .5em; + } + + > li:hover { + background-color: ${props => props.theme.secondary}; + } +`; + +export const MenuButton = ({ label, ids, value, setValue }) => { + const [open, setOpen] = useState(false); + + useEffect(() => { + if (!open) { + return; + } + + const listener = (e) => setOpen(false); + window.addEventListener('click', listener); + return () => window.removeEventListener('click', listener); + }, [open, setOpen]); + + function toggleOpen() { + setOpen(!open); + } + + function select(e, id) { + e.stopPropagation(); + setOpen(false); + setValue(id); + } + + return ( + + ) +} \ No newline at end of file diff --git a/src/highlighting.js b/src/highlighting.js index 60faca8..6f96ebc 100644 --- a/src/highlighting.js +++ b/src/highlighting.js @@ -36,7 +36,6 @@ export const languageIds = [ 'markup', 'css', 'clike', - 'javascript', 'bash', 'diff', 'docker',